Claude Code on one key
Two environment variables point Claude Code at RelaySwitch. Same models, same tool, billed per token to a prepaid balance you top up by card or crypto.
Claude Code speaks Anthropic's Messages format and reads its endpoint from ANTHROPIC_BASE_URL. Set that to https://api.relayswitch.io/v1, put your key in ANTHROPIC_AUTH_TOKEN, and every turn bills to your balance at the rates on the model pages: Claude Sonnet 5.5 is $1.20 in and $3.00 out per million tokens today. The 11 Claude ids that answer the Anthropic format are listed under Picking a model.
01What you need
- Claude Code installed (
claudeon your PATH). - A RelaySwitch key from the console, with a balance loaded.
02Two variables
ANTHROPIC_API_KEY works in place of ANTHROPIC_AUTH_TOKEN. Set one, not both.
export ANTHROPIC_BASE_URL="https://api.relayswitch.io/v1"export ANTHROPIC_AUTH_TOKEN="YOUR_RELAYSWITCH_KEY"claudeANTHROPIC_BASE_URLpoints Claude Code at RelaySwitch. Paste it with the/v1; the gateway accepts the path Claude Code builds from it.ANTHROPIC_AUTH_TOKENsends the key asAuthorization: Bearer.ANTHROPIC_API_KEYsends it asx-api-key. The gateway reads either header. A request that carries two different keys is refused with a400.- The base URL alone is not enough. Without one of the key variables, Claude Code keeps using a saved claude.ai login.
03Make it stick
Add them to ~/.claude/settings.json under env. The model variables take any Claude id from the Models page that answers the Anthropic format.
{ "env": { "ANTHROPIC_BASE_URL": "https://api.relayswitch.io/v1", "ANTHROPIC_AUTH_TOKEN": "YOUR_RELAYSWITCH_KEY", "ANTHROPIC_DEFAULT_OPUS_MODEL": "claude-opus-5-5", "ANTHROPIC_DEFAULT_SONNET_MODEL": "claude-sonnet-5-5", "ANTHROPIC_DEFAULT_HAIKU_MODEL": "claude-haiku-5-5" }}~/.claude/settings.jsonapplies to every project..claude/settings.local.jsonapplies to one project. Keep the key out of a project's.claude/settings.json; that file is meant to be committed and shared.- When a shell export and a settings file set the same variable, the settings file wins.
The VS Code extension reads credentials from its own setting, claudeCode.environmentVariables, in VS Code's user settings:
{ "claudeCode.environmentVariables": [ { "name": "ANTHROPIC_BASE_URL", "value": "https://api.relayswitch.io/v1" }, { "name": "ANTHROPIC_AUTH_TOKEN", "value": "YOUR_RELAYSWITCH_KEY" } ]}04Picking a model
The opus, sonnet and haiku aliases resolve to whatever the three ANTHROPIC_DEFAULT_*_MODEL variables name. Switch in a session with /model, at start with claude --model claude-opus-5-5, or for one session with ANTHROPIC_MODEL.
Use Claude ids only. A GPT, Gemini or DeepSeek id returns a 400 that says the model is served in the openai format, not anthropic; for those models use an OpenAI-compatible tool such as Cursor. The Claude ids on the key that answer the Anthropic format today:
claude-fable-5claude-fable-5.1claude-haiku-4-5claude-haiku-4-5-20251001claude-haiku-5-5claude-opus-4-6claude-opus-4-7claude-opus-5claude-opus-5-5claude-sonnet-5claude-sonnet-5-5
05Check it
List the models your key can call:
curl https://api.relayswitch.io/v1/models -H "x-api-key: $ANTHROPIC_AUTH_TOKEN"Send one small message. -i prints the headers, which include the request id and, on a non-streamed answer, the charge in USD.
curl -i https://api.relayswitch.io/v1/messages \ -H "Authorization: Bearer $ANTHROPIC_AUTH_TOKEN" \ -H "anthropic-version: 2023-06-01" \ -H "content-type: application/json" \ -d '{"model": "claude-haiku-5-5", "max_tokens": 16, "messages": [{"role": "user", "content": "Say ok."}]}'Then run claude. The request shows in the console within seconds. Inside Claude Code, /status shows an Anthropic base URL line with the RelaySwitch address and an Auth token or API key line naming the variable you set. If the base URL line is missing, the variable did not reach the session.
06Caching and billing
Claude Code sends the whole conversation on every turn and marks it for the prompt cache. Tokens read from the cache bill at the cached rate on each model page, a fraction of the input rate. The cache lasts five minutes by default, so a turn after a longer break pays the input rate for the whole context once.
The cost Claude Code shows is its own estimate at Anthropic's list price. What you paid is on the console's Usage page. For one request, look it up by its id; it lands in the log within about a minute:
curl https://api.relayswitch.io/v1/requests/REQUEST_ID \ -H "Authorization: Bearer $ANTHROPIC_AUTH_TOKEN"The answer lists input_tokens, cache_read_tokens, output_tokens and charge_usd. A cache_read_tokens above zero means caching worked.
07Errors and fixes
invalid_api_key(401): the key is wrong or revoked, or it is in a variable Claude Code did not read. Check/status.insufficient_balance(402): top up in the console.monthly_limit_reached(402) means the key hit the monthly spend limit set on it.model_not_found(404): the id is not on the Models page. Check the model variables for typos.- Claude Code asks you to log in although the curl test works: set the key in a shell export or in
~/.claude/settings.json. A project settings file applies only after the first-run setup and the trust prompt. - A startup warning naming two credential sources: a saved login and the variable are both active. Run
/logoutto keep only the key. ANTHROPIC_API_KEYset but ignored: it needs a one-time approval. Turn on Use custom API key under/config, or useANTHROPIC_AUTH_TOKEN.