Claude Code API key setup means running Claude Code on an Anthropic API key instead of signing in with a claude.ai account. You set the ANTHROPIC_API_KEY variable, and Claude Code sends that key with each request. Anthropic’s Claude Code authentication docs, read 2026-11-19, describe the steps below.
Set up the key
- Get a key. Create one in the Claude Console at platform.claude.com. The key belongs to your Console organization.
- Export it. Set
ANTHROPIC_API_KEYin your shell, for example in your shell profile. Keep the key out of shared scripts. - Start Claude Code. Run
claudein your project folder. In interactive mode it asks once whether to use the key. Your answer is remembered. - Check which login is active. Run
/status. It marks the login in use and the one that is not.
For the “use this key” choice later, open /config and use the “Use custom API key” toggle. The toggle only shows while ANTHROPIC_API_KEY is set. In non-interactive mode (claude -p), the key is always used when it is present, with no prompt.
If you want Claude Code to skip the browser login the first time, set the key before the first run and approve it when asked. The install guide shows the first-run steps.
Which login wins
Claude Code uses the first credential it finds in this order:
- Cloud provider variables (Bedrock, Vertex or Foundry), when one of them is set.
ANTHROPIC_AUTH_TOKEN, sent as a bearer token. Used for LLM gateways.ANTHROPIC_API_KEY, sent as theX-Api-Keyheader.- The
apiKeyHelperscript, for keys that rotate, such as short-lived tokens from a vault. CLAUDE_CODE_OAUTH_TOKEN, a one-year token fromclaude setup-token, meant for CI.- Anthropic profile credentials, when you name one.
- Your
/loginsubscription. This is the default for Pro, Max, Ultra, Team and Enterprise users.
The trap: if you have a subscription and also an old ANTHROPIC_API_KEY in your shell, Claude Code uses the key once you approve it. If that key belongs to a disabled or expired organization, requests fail. Run unset ANTHROPIC_API_KEY to fall back to the subscription.
API key vs subscription
Subscription login (/login) | API key (ANTHROPIC_API_KEY) | |
|---|---|---|
| Who pays | Your Pro, Max, Ultra, Team or Enterprise plan | Your Console organization, per token |
| Set up | Browser sign-in | Environment variable plus one approval |
| Cloud sessions | Used | Not used: cloud sessions always use the subscription, and the key does not override it |
| Rotating or scripted keys | Not for this | Use apiKeyHelper |
The docs say that a long-lived token from claude setup-token also uses your subscription, not an API key. It works for CI, but it can only make model requests. It does not enable Remote Control or claude.ai connectors.
Where the key is kept
- macOS: the encrypted macOS Keychain. If the Keychain is locked, as it can be in an SSH session, Claude Code writes the login to
~/.claude/.credentials.jsoninstead. - Linux and Windows:
~/.claude/.credentials.json, with file mode0600on Linux. - Scripts:
apiKeyHelperin settings runs a shell script that prints the key. If it takes more than 10 seconds, Claude Code shows a warning. If it fails, requests fail after three tries.
To remove a key that /login saved, run /logout. The docs say nothing about the shell variable after that, so check your shell profile and run unset ANTHROPIC_API_KEY if it is still set.
Keys in Console roles
Team admins decide who can make keys. In the Claude Console, the Claude Code role can only create Claude Code API keys. The Developer role can create any kind of key. The docs say this, read 2026-11-19.
Memory and API keys
The API key only decides how Claude Code is billed and signed in. It does not give Claude Code a memory. Claude Code keeps notes in CLAUDE.md files and its auto memory, and a store that carries facts between sessions is a separate tool. Claude Code memory covers the options.
One option is Hindsight. Its Coding Agents package installs Claude Code memory with npx @vectorize-io/hindsight-coding-agents install claude-code. It covers 22 coding CLIs, and capture is automatic, with one memory bank per repo (Hindsight coding agents docs, read 2026-11-12). Hindsight’s benchmark score on long histories is a reason to look at it: 64.1 on BEAM at 10M tokens, against 48.6 for Mem0’s Platform. Both figures come from Mem0’s own comparison page, so they are vendor-run, not independent.
The limits matter for your key bill. Hindsight’s writes call an LLM to extract facts, so every saved session adds API usage on top of Claude Code’s own calls. The retired per-agent Claude Code plugin is not updated any more; the Coding Agents package replaces it. Its benchmark figures do not show what the memory costs you on your own sessions, so measure that yourself.
Sources: Claude Code authentication, read 2026-11-19; Hindsight README; Hindsight coding agents docs, read 2026-11-12; Mem0 vs Hindsight.