In short:
- Claude Code sends every request to
ANTHROPIC_BASE_URLplus/v1/messages, with the key fromANTHROPIC_AUTH_TOKEN(orANTHROPIC_API_KEY). Change those and the same program talks to a different server; nothing else about it changes. - You gain prepaid, per-token billing, one balance for several tools and a separate key per project or machine. You lose the features tied to a claude.ai login.
- What a session costs depends mostly on which model each of Claude Code’s slots uses and how much of the prompt is read from the cache.
- When something breaks, test the key outside Claude Code first. That splits a key or network problem from a Claude Code configuration problem in one step.
Pointing Claude Code at a custom API base URL changes only where its requests go and which key pays for them: the program stays the same, billing becomes per-token against a gateway balance, and features tied to a claude.ai login stop working. This post explains how the setup works and what it changes. For the settings themselves, in order, with copy-ready values, use the Claude Code setup guide in our docs.
How Claude Code decides where to send requests
Claude Code is a client for the Anthropic Messages API. Each turn of a conversation, and each tool result it sends back to the model, is one POST to /v1/messages on whatever host ANTHROPIC_BASE_URL names. If the variable is unset, that host is Anthropic’s. Set it to a gateway that speaks the same API, such as https://api.oneaiapikey.com, and the requests go there instead. The value is an origin without /v1, because Claude Code appends the path itself; a base URL ending in /v1 produces /v1/v1/messages and a 404.
The credential travels in one of two headers, depending on the variable:
| Variable | Header sent | What happens at startup |
|---|---|---|
ANTHROPIC_AUTH_TOKEN |
Authorization: Bearer ... |
Used straight away, ahead of a saved claude.ai login. |
ANTHROPIC_API_KEY |
x-api-key: ... |
In interactive mode, Claude Code asks once whether to use the key. |
Our gateway reads either header on /v1/messages. ANTHROPIC_AUTH_TOKEN avoids the approval prompt, which is why the setup guide uses it. Setting both makes Claude Code warn about two credential sources.
Claude Code reads these variables from the shell it starts in and from the env block of its settings files. The user file, ~/.claude/settings.json, applies everywhere and wins over the shell. A project’s .claude/settings.json is meant to be committed, so a key never belongs there; .claude/settings.local.json is the per-project file that stays out of git. Inside a session, /status shows the base URL and credential source actually in use, which settles any doubt about which file won.
What do you gain behind a gateway?
Behind a gateway you gain a spending cap at the balance, one balance across tools, a key per project or machine, and no upstream credentials on developer machines.
- Spending stops at the balance. With a prepaid balance, requests fail when it runs out instead of running up a bill. That matters most when an agent works unattended in a loop.
- One balance for several tools. The same account drives Claude Code, OpenAI-format tools such as Codex CLI, and your own scripts.
- A key per project or machine. Each key has its own usage log and can have its own spend limit, so you can see which project spent what and revoke one laptop’s key without touching the others.
- Upstream credentials stay off developer machines. A team hands out gateway keys; nobody holds the provider account’s key.
What do you give up behind a gateway?
You give up features tied to a claude.ai login, a subscription’s flat monthly price and non-Claude models.
- Features tied to a claude.ai identity, such as Remote Control and voice dictation, are unavailable while a gateway credential is active. Claude Code’s docs also say Remote Control is turned off when
ANTHROPIC_BASE_URLpoints at a non-Anthropic host. - A subscription’s flat monthly price. Per-token billing is cheaper for light use and more expensive for heavy daily use, so estimate before you switch (see estimating your monthly LLM API cost).
- Non-Claude models. Anthropic does not support running Claude Code on other model families through any gateway, and our Anthropic route serves Claude models only. For GPT or Grok in an agent, use a tool built for the OpenAI format; the setup guides cover several.
- One AI API Key is an independent service, not affiliated with or endorsed by Anthropic.
Where does the money go in a session?
A session’s cost depends mostly on which model each of Claude Code’s slots uses and how much of the prompt is read from the cache. Claude Code does not use one model for everything. It has three slots, selected with /model opus, /model sonnet and /model haiku, and it uses the Haiku slot on its own for background work such as naming sessions. Sub-agents use the model set in CLAUDE_CODE_SUBAGENT_MODEL, or the main model if that is unset. The three ANTHROPIC_DEFAULT_*_MODEL variables pin each slot to an exact model id, which is the only way to know what each request costs: on our models page, Claude Opus 5.5, Claude Sonnet 5 and Claude Haiku 4.5 each have their own rate.
Three things decide most of a session’s cost:
- The main model. Everything you type, and every file the agent reads into the conversation, is input to that model on every following turn.
- The sub-agent model. Sub-agents do much of the searching and reading in long tasks. A cheaper tier there often costs little in quality; our post on choosing a model for coding agents goes through which tier fits where.
- The cache. Claude Code marks the stable start of its prompt (the system prompt, tools and earlier turns) for prompt caching. Because it uses the Messages API, the gateway passes that through, and input read from the cache is billed at the model’s lower cached-input rate. Long sessions with many short turns benefit most. The prompt caching docs list the rates.
The usage page in the console lists every request with its model, tokens and cost, so after a day of work you can see which of the three dominated.
Which layer failed?
Most problems sit in one of three layers, the key or account, the configuration or the address, and each has its own signs:
- The key or the account. A
401means the key itself was rejected: a typo, a stray space, or a revoked or expired key. A403means the key works but is not allowed this request: its model list leaves out that model, or the key’s limit or the balance is used up. A plaincurlto/v1/messageswith the same key shows the same error, which is why the setup guide has you run one before opening Claude Code. The errors reference maps each code. - The configuration. Claude Code opening its login screen, or
/statusshowing a claude.ai login method instead of an auth token, means the variables did not reach the process. Usually they were set in a different shell, or only in a project file. - The address.
ENOTFOUND, connection refused or a 404 on every request point at the base URL: a typo, a trailing/v1, or a network that blocks the host.
A 400 naming a field the server does not accept is a fourth, rarer case: a new Claude Code release sending a beta field before every upstream supports it. Setting CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS=1 turns those fields off.
How do you switch back to a normal login?
Remove the variables from ~/.claude/settings.json and from your shell profile, open a new terminal and run /status. A saved claude.ai login is kept while a gateway credential is set; it is simply not used until the variable is gone.
What should you read next?
Start with the setup guide, then the key security and API format posts below.
- The Claude Code setup guide: every setting in order, a
curltest, recommended model ids and fixes for common errors. - API key security for AI apps, because a key in
settings.jsonis still a secret on disk. - OpenAI-compatible vs Anthropic-compatible APIs, for why Claude Code uses one format and most other tools the other.