Skip to main content

Claude Code and Codex setup commands

Developers often want Claude Code or Codex to go through the gateway every time, without a daily sign-in. The spanwayctl CLI does that wiring for them. spanwayctl configure edits the agents' own configuration files so every session uses the gateway with a long lived virtual key, keeps an undo record, and spanwayctl unconfigure restores the files later without touching anything the developer changed in between. The CLI reference lists every option, this page explains what gets written.

spanwayctl runs from the gateway image through the shell function in the CLI reference, which mounts ~/.claude so the Claude Code commands change the settings on your machine. The Codex commands check the installed Codex first, and the image has no Codex, so from the image set Codex up by hand with the entry under what Codex gets.

A real-world scenario​

A new developer joins and gets a virtual key from the Admin UI. They run one command and Claude Code starts on the gateway's claude-auto model with their key, their model picker fills from the gateway's model list, and the key never has to be pasted into a settings file by hand. Six months later they switch laptops. spanwayctl unconfigure --forget puts the old laptop's settings back the way they were and deletes the saved key.

Before you start​

  • Claude Code, or Codex 0.129.0 or newer, installed on the developer's machine.
  • spanwayctl, from the gateway image through the shell function in the CLI reference.
  • The gateway's address, reachable from the developer's machine, and a virtual key made as in the next section.

Make a key for the agents​

Give each developer their own key, so their spend, errors and limits stay separate and one laptop can be cut off on its own.

  1. In the Admin UI open Virtual Keys (/ui/api-keys) and click + Create New Key.
  2. Under Key Details, pick the developer's team and set Key Name to who and where, for example jane-laptop. Leave Key Type on AI APIs, so the key can call models but cannot change the gateway.
  3. In Models, pick the models the agents use, for example claude-auto, claude-sonnet-5-5 and gpt-6.1-sol. Claude Code also sends background tasks, such as titles and summaries, to a small fast model, so add claude-haiku-5-5 too, or those calls fail with 403 while the main calls work.
  4. Open Optional Settings. Set Max Budget (USD), for example 200, with Reset Budget on monthly, and Requests per minute Limit (RPM), for example 120, so a runaway agent loop slows down and stops at the budget.
  5. Click Create Key and copy the key. It is shown once.

The same key from the API, with the master key or a proxy admin's key:

curl -X POST https://gateway.example.com/key/generate \
-H "Authorization: Bearer $SPANWAY_MASTER_KEY" -H "Content-Type: application/json" \
-d '{"key_alias": "jane-laptop", "key_type": "llm_api", "models": ["claude-auto", "claude-sonnet-5-5", "claude-haiku-5-5", "gpt-6.1-sol"], "max_budget": 200, "budget_duration": "30d", "rpm_limit": 120}'

Steps​

  1. Get a key for the developer, as in make a key for the agents.

  2. Run the command for the agent. Without an agent name it opens a wizard that lets you select Claude Code, Codex or both and choose a gateway model for each:

    spanwayctl --base-url https://gateway.example.com configure claude --api-key sk-... --model claude-auto
    spanwayctl configure

    Expected result, for Claude Code:

    Configured Claude Code: /app/.claude/settings.json now routes through https://gateway.example.com.
    Credential: your virtual key, stored in the file as ANTHROPIC_AUTH_TOKEN.
    Starting model: claude-auto (the /model picker's default row, the model Claude Code starts and resumes on); switch any time with /model.
    /model will list 3 of the proxy's 5 models: Claude Code shows only ids containing 'claude' or 'anthropic', and this proxy does not list the rest under such names.
    Start `claude` from any terminal. Undo with `spanwayctl unconfigure claude`.

    The fourth line tells you which gateway models Claude Code's /model picker will show. Claude Code lists only model ids that contain claude or anthropic, so the gateway lists a model whose name has neither, such as gpt-coder, under a claude-router- id with its real display name, and it still shows in the picker. Only wildcard routes, names with *, stay out of it, and they are what makes the line read fewer than all models. When every gateway model shows, the line reads /model will list all <n> of the proxy's models. instead. If the settings file is a symlink, a note on stderr says where the key now lives.

  3. Start the agent as usual. The first prompt appears on the Logs page under the key's alias. The same check from a terminal, with the same key, confirms the gateway accepts it:

    curl https://gateway.example.com/v1/models -H "Authorization: Bearer sk-..."

Before changing anything the command validates the key against GET /v1/models and reads the models the key can access. A later spanwayctl configure reuses the saved setup for each agent, and spanwayctl reconfigure opens the wizard again with the saved values filled in.

Expected result​

The command prints the lines shown in step 2 and exits 0. The next claude or codex session talks to the gateway with the key, its /model picker lists the gateway's models, and every prompt appears on Logs under the key alias with its model, tokens and cost.

What Claude Code gets​

spanwayctl configure claude patches ~/.claude/settings.json, or the file under CLAUDE_CONFIG_DIR. It writes env.ANTHROPIC_BASE_URL with the gateway URL and env.ANTHROPIC_AUTH_TOKEN with the key, sets env.ENABLE_TOOL_SEARCH and env.CLAUDE_CODE_ENABLE_GATEWAY_MODEL_DISCOVERY when they are missing so the model picker fills from the gateway's /v1/models, and removes a stray env.ANTHROPIC_API_KEY, stale env.ANTHROPIC_AUTH_TOKEN or apiKeyHelper that would fight the token. A static key and an apiKeyHelper are two different setups. CLI sign-in describes the helper, where Claude Code runs spanwayctl auth print-token to get a short lived token that renews itself. Running configure claude or reconfigure deletes a helper you added by hand, so pick one setup per machine. --model pins the model Claude Code starts on as the top level model and env.ANTHROPIC_MODEL, which also keeps a resumed session on that model, and --default-model removes the pin. Everything else in the file is preserved, the file is written atomically with owner only permissions, and a symlinked settings file is written through to its target. A key is required, the command refuses to use a short lived spanwayctl login credential because keeping it fresh would mean Claude Code launching the CLI on every credential refresh.

What Codex gets​

spanwayctl configure codex edits ~/.codex/config.toml, or the file under CODEX_HOME. It adds a spanway entry under model_providers named "Spanway proxy" with base_url set to the gateway's /v1, wire_api: responses, WebSockets off, and the key in a static Authorization header, then sets model_provider = "spanway" and model to the chosen model. A default profile selection is removed so it cannot override the gateway settings, with its contents preserved and restored on undo. Other providers, hooks, MCP servers and comments in the file are kept. Codex 0.129.0 or newer is required, checked with codex --version before anything is changed, because older releases let repository settings redirect requests that carry the saved key.

From the gateway image configure codex stops at that version check, because the image has no Codex. Add the same entry to ~/.codex/config.toml yourself, with Codex 0.129.0 or newer. The Codex page has a variant that reads the key from an environment variable with env_key instead of storing it in the file, and fills the /model picker from the gateway's model catalog:

model_provider = "spanway"
model = "gpt-6.1-sol"

[model_providers.spanway]
name = "Spanway proxy"
base_url = "https://gateway.example.com/v1"
wire_api = "responses"
supports_websockets = false
requires_openai_auth = false
http_headers = { Authorization = "Bearer sk-..." }

Undo​

spanwayctl unconfigure claude
spanwayctl unconfigure codex --forget
spanwayctl unconfigure --forget

Each configure records a receipt of the values it wrote, next to the settings file for Codex and under ~/.spanway for Claude Code. unconfigure restores only values that still hold what configure wrote, so edits made since are preserved, and the provider URL and credential are restored together. --forget also deletes the saved setup and its key. Expected result:

Restored in /Users/jane/.claude/settings.json: env.ANTHROPIC_BASE_URL, env.ANTHROPIC_AUTH_TOKEN, model.
Forgot saved claude setup, including its saved key.

The agent starts without the gateway on its next run.

Errors​

MessageMeaningFix
Key validation against /v1/models failsThe key is wrong, expired, blocked or the gateway URL is not reachable from the laptop.Check the key on Virtual Keys and the --base-url.
The command refuses a spanwayctl login credentialClaude Code needs a long lived key, not the short lived login token.Pass --api-key with a virtual key.
Codex 0.129.0 or newer (stable) must be installed before saving a gateway key.Codex is missing or older than 0.129.0, and older Codex releases let repository settings redirect requests that carry the saved key.Upgrade Codex and run the command again.
No claude undo receipt at ...; nothing to undo. Agent settings were not changed.Configure was never run on this machine, or the receipt was deleted.Remove the gateway connection from the settings file by hand.
Nothing in ... was still ours to restore.Every value configure wrote was changed since, so undo left the file alone.Nothing to do, the file already holds your own values.
`spanwayctl up` is currently managing <path> (backup at <backup>) and will restore it when it stops. Run `spanwayctl down` first, then retry.A local gateway started with spanwayctl up, or spanwayctl autoroute start, holds a backup of the settings file and would overwrite the change when it stops.Run the stop command the message names, then configure again.
'<model>' is not served by <url> for this key. /v1/models lists: ...The model given with --model is not on the key, or not registered on the gateway.Pick one of the listed models, or ask an admin to add the model to the key.
Missing option '--model'. First-time Codex setup needs a starting model or First-time Codex setup needs --model. Run spanwayctl configure for the model pickerCodex has no saved setup yet and no model was given.Pass --model, or run spanwayctl configure in a terminal to pick one.
--model and --default-model cannot be used togetherOne flag pins a model and the other removes the pin.Pass only one of them.
The gateway changed. Pass --api-key for the new gateway; the saved key was not used--base-url names another gateway than the saved setup, and the saved key belongs to the old one.Pass --api-key with a key from the new gateway.
`spanwayctl configure` asks questions, so it needs a terminal. ...The command ran without a terminal, for example in a script or docker run without -it, and needs to ask for values.Run it with -it, or pass claude or codex with --api-key and --model as the message shows.
The agent still uses another providerFor Codex a profile selection or another model_provider was added after configure. For Claude Code an apiKeyHelper or ANTHROPIC_API_KEY was added.Run spanwayctl reconfigure, which removes the conflicting values again.

Logs​

spanwayctl configure, reconfigure and unconfigure run on the developer's machine and write no gateway log lines of their own. What they print is shown in expected result and errors. The key check they make is one GET /v1/models request, which shows up in the gateway's access log, and every prompt the configured agent sends appears on Logs under the key alias.

Troubleshooting​

  1. Run the /v1/models curl from step 3 with the same key. If it fails, the problem is the key or the address, not the agent, see the client error reference.
  2. Run spanwayctl configure claude or spanwayctl configure codex again and read the lines it prints. The credential and starting model lines say what the file now holds.
  3. Open the settings file the first line names and check that no apiKeyHelper, ANTHROPIC_API_KEY, Codex --profile file or other model_provider overrides what configure wrote.
  4. Turn on the agent's own debug output, claude --debug writes to ~/.claude/debug/<session-id>.txt and RUST_LOG=debug codex to ~/.codex/log/codex-tui.log, and compare the address and header it sends with the one configure wrote.
  5. Look for the request on Logs. No row means the request never reached the gateway, see connect any client.