ANTHROPIC_AUTH_TOKEN vs ANTHROPIC_API_KEY in Claude Code
Claude Code has two variables that can hold an API key: ANTHROPIC_API_KEY and ANTHROPIC_AUTH_TOKEN. They send different request headers, so set the one your provider accepts. Tested with Claude Code 2.1.312 on 30 October 2026.
The RoamAI teamPublished October 30, 2026
They send different headers
- ANTHROPIC_API_KEY sends x-api-key: YOUR_KEY. This is the header the Anthropic API uses.
- ANTHROPIC_AUTH_TOKEN sends Authorization: Bearer YOUR_KEY. Many third-party services and gateways use this one.
If both are set, Claude Code sends both headers. Which one the server reads, and which wins when they differ, depends on the service. The safe choice is to set only the one your provider documents and remove the other.
A rejected key shows a different error
When the server answers 401, the message depends on the variable:
- With ANTHROPIC_API_KEY: Invalid API key · Fix external API key
- With ANTHROPIC_AUTH_TOKEN: Not logged in · Please run /login
The second one reads like you need to log in to claude.ai, but the token was rejected. With a custom base URL, check the token first. All the common errors are in Claude Code API errors.
The confirmation prompt
The first time you start interactive claude with ANTHROPIC_API_KEY set, it shows Detected a custom API key in your environment and asks whether to use it. If you answer no, the key is not used. ANTHROPIC_AUTH_TOKEN does not trigger this prompt. Non-interactive runs such as claude -p do not prompt for either.
settings.json wins over the shell
Both variables can go in the env block of ~/.claude/settings.json. In our test, a value in settings.json overrode the same variable exported in the shell, and the same is true for ANTHROPIC_BASE_URL.
So when you change the key or the URL, check settings.json first. A project can also set env in .claude/settings.json or .claude/settings.local.json inside its folder.
Keep the values in one place. For one setup per computer, use settings.json. To switch often, remove the env block from settings.json and use shell variables.
See which one is in use
Type /status inside claude. Anthropic base URL is where requests go; Auth token and API key show where each variable comes from. A line marked not in use exists but is not being used.
Which one to use
- An Anthropic API key (it starts with sk-ant-): use ANTHROPIC_API_KEY.
- A third-party service: follow its documentation. If it mentions Authorization or Bearer, use ANTHROPIC_AUTH_TOKEN.
- Neither works: send one request with each header using curl and use whichever succeeds. The curl command is in Claude Code API errors.
One key per person for Claude Code
RoamAI is a Claude API for teams: create an API key in the console and use it in Claude Code, Claude Desktop or an SDK. VIP adds lower usage prices, dedicated servers and a named contact.
Get started