Claude Code Auth Conflict Warning and 401 Errors: How to Fix
When Claude Code shows an auth conflict warning, it has found two or more credentials on your machine at the same time, for example a claude.ai login and the ANTHROPIC_API_KEY environment variable. It can only use one of them, chosen by a fixed priority order, and that may not be the one you wanted. The fix: run /status to see which credential is actually in effect, then remove the one you don't plan to use so only one remains.
If you also see "/login says it succeeded, but every prompt returns 401", the usual cause is a leftover ANTHROPIC_AUTH_TOKEN in your environment, which outranks the login session. This article covers what the warning looks like, why the conflict happens, how to diagnose it, and how to fix it. The priorities and behavior described here come from the official Claude Code Authentication page. The GitHub issues cited are all in the anthropics/claude-code repository, and their statuses are as checked in mid-October 2026.
What does the warning look like?
There are three common wordings, taken from issues #50537, #11587 and #67861 respectively:
⚠ Auth conflict: Both a token (claude.ai) and an API key (ANTHROPIC_API_KEY) are set.
This may lead to unexpected behavior.
• Trying to use claude.ai? Unset the ANTHROPIC_API_KEY environment variable,
or claude /logout then say "No" to the API key approval before login.
• Trying to use ANTHROPIC_API_KEY? claude /logout to sign out of claude.ai.
⚠ Auth conflict: Both a token (CLAUDE_CODE_OAUTH_TOKEN) and an API key (apiKeyHelper) are set.
• Trying to use CLAUDE_CODE_OAUTH_TOKEN? Unset the apiKeyHelper setting.
• Trying to use apiKeyHelper? Unset the CLAUDE_CODE_OAUTH_TOKEN environment variable.
Both ANTHROPIC_AUTH_TOKEN and /login managed key set · auth may not work as expected
The names in parentheses are the two sides of the conflict. Note them down and match them against the steps below. The wording can differ slightly between versions, so go by what's on your screen.
Which authentication sources does Claude Code have, and which wins?
The official documentation lists the following priority order. Earlier entries override later ones:
| Priority | Source | How it's sent | Typical use |
|---|---|---|---|
| 1 | Cloud provider credentials (CLAUDE_CODE_USE_BEDROCK / _VERTEX / _FOUNDRY set) | The cloud provider's own auth | Enterprises using a cloud-hosted model |
| 2 | ANTHROPIC_AUTH_TOKEN | Authorization: Bearer header | Going through an LLM gateway or proxy |
| 3 | ANTHROPIC_API_KEY | X-Api-Key header | Direct Anthropic API access with a key created in the Console |
| 4 | Output of an apiKeyHelper script | Used as an API key | Fetching short-lived credentials from a secrets vault |
| 5 | CLAUDE_CODE_OAUTH_TOKEN | OAuth | Long-lived token from claude setup-token, meant for CI |
| 6 | Anthropic profile / federated identity credentials | OAuth / OIDC | The ant CLI, Workload Identity Federation |
| 7 | Subscription login saved by /login | OAuth | Default for Pro, Max, Team and Enterprise users |
A few details that are easy to miss, all from the official docs:
- In interactive mode, Claude Code asks you once whether to use
ANTHROPIC_API_KEY, and remembers your answer. To change it later, find the "Use custom API key" toggle in/config. That toggle only appears whenANTHROPIC_API_KEYis set in the environment. In non-interactive-pmode, the variable is always used whenever it exists. - The docs also note that if you have a subscription and
ANTHROPIC_API_KEYis set, once you approve the key it is used instead of the subscription. If the key belongs to a disabled or expired organization, authentication fails. ANTHROPIC_AUTH_TOKENhas no approval prompt. Once it's set, it sits directly ahead of the login session.- Cloud sessions (Claude Code on claude.ai) always use subscription credentials. Setting these two variables in a cloud environment does not override that.
Why does setting AUTH_TOKEN make my login "stop working" and return 401?
Your login session is still there. It just isn't being used. Look at the table: ANTHROPIC_AUTH_TOKEN is priority 2 and /login is priority 7. As long as that variable exists, Claude Code sends it as a Bearer token.
A typical scenario: you once pointed Claude Code at a custom endpoint and put ANTHROPIC_BASE_URL and ANTHROPIC_AUTH_TOKEN in ~/.zshrc or in your Windows user environment variables. Later you wanted to go back to the official subscription, but you only removed ANTHROPIC_BASE_URL, or only re-ran /login. Now requests go to the official API carrying the old gateway's token, and the server naturally returns 401.
GitHub issue #34826 (title: 401 "Invalid bearer token" caused by ANTHROPIC_AUTH_TOKEN environment variable) describes exactly this. The reporter's /login showed success, yet every prompt returned 401, because of an earlier ANTHROPIC_AUTH_TOKEN; removing it fixed things immediately. That issue is closed with the reason "not planned", and the related #33330 is also "not planned". In other words, this isn't treated as a bug but as the normal result of the priority rules, so you need to clean up the variables yourself.
For how 401 differs from 403, 429 and 529, see the AI API error codes cheat sheet. A 401 almost rules out a service outage; the problem is in the credential itself.
Diagnosis: find out which two credentials are conflicting
Step 1: Run /status inside Claude Code
Start Claude Code and type /status. According to the official docs, when both a login session and an API key are configured, /status marks the one that isn't being used; if a profile is in use, it shows a Profile line. First confirm that the credential currently in use is the one you want.
Step 2: Check environment variables in the current terminal
macOS, Linux, WSL:
env | grep -E '^(ANTHROPIC_|CLAUDE_CODE_)' | sed -E 's/=(.{6}).*/=\1.../'
The sed part truncates values so you don't print a full key to the screen or into a screenshot.
Windows PowerShell:
Get-ChildItem Env: | Where-Object Name -Match '^(ANTHROPIC_|CLAUDE_CODE_)' | Select-Object Name
Step 3: Find where the variable is set
Running unset in the current terminal only affects that one session; the variable comes back the next time you open a terminal. Find the source:
grep -nE 'ANTHROPIC_|CLAUDE_CODE_OAUTH_TOKEN' \
~/.bashrc ~/.zshrc ~/.profile ~/.bash_profile ~/.zprofile 2>/dev/null
On Windows, besides the current session, also check user-level and system-level environment variables:
[System.Environment]::GetEnvironmentVariable("ANTHROPIC_AUTH_TOKEN", "User")
[System.Environment]::GetEnvironmentVariable("ANTHROPIC_AUTH_TOKEN", "Machine")
Step 4: Check env and apiKeyHelper in settings.json
Environment variables aren't always set in your shell. They can also live in Claude Code's settings files:
grep -nE 'ANTHROPIC_|apiKeyHelper|CLAUDE_CODE_OAUTH_TOKEN' \
~/.claude/settings.json .claude/settings.json .claude/settings.local.json 2>/dev/null
For what the user, project and local settings layers each control, see the Claude Code settings.json guide. In the #11587 report of CLAUDE_CODE_OAUTH_TOKEN conflicting with apiKeyHelper, the reporter got back to normal by deleting apiKeyHelper from their settings (that issue was closed as a duplicate).
Step 5: Use the table below and keep only one
| What you want to use | Keep | Remove |
|---|---|---|
| claude.ai subscription (Pro / Max / Team / Enterprise) | The /login session | ANTHROPIC_AUTH_TOKEN, ANTHROPIC_API_KEY, apiKeyHelper; and if you previously used a custom endpoint, ANTHROPIC_BASE_URL too |
| Direct Console API key | ANTHROPIC_API_KEY | Sign out of claude.ai with /logout |
| Self-hosted or third-party gateway (Bearer auth) | ANTHROPIC_BASE_URL + ANTHROPIC_AUTH_TOKEN | /logout if needed, see the next section |
| CI and scripts | Either CLAUDE_CODE_OAUTH_TOKEN or ANTHROPIC_API_KEY, not both | The other one, plus apiKeyHelper in settings |
Example cleanup commands:
# Takes effect immediately in the current session
unset ANTHROPIC_AUTH_TOKEN ANTHROPIC_API_KEY ANTHROPIC_BASE_URL
# Then delete the matching export lines from ~/.zshrc etc. and reopen your terminal
# Windows: remove the user-level variable (the approach the reporter gave in #34826), then reopen your terminal
[System.Environment]::SetEnvironmentVariable("ANTHROPIC_AUTH_TOKEN", $null, "User")
After cleaning up, open a new terminal, start Claude Code and run /status again. The warning should be gone.
What should I watch out for with /logout?
The claude /logout suggested in the warning clears the claude.ai login stored on your machine. The official docs also mention that signing out resets the first-run setup state, so the next time you run claude it goes through login and initialization again.
Issue #30781 is about exactly this side effect. The reporter had set ANTHROPIC_API_KEY in just one terminal, ran /logout as prompted, and other sessions that relied on the login session broke too, because login credentials are stored globally (per the official docs: ~/.claude/.credentials.json on Linux, %USERPROFILE%\.claude\.credentials.json on Windows, and the Keychain on macOS). That issue is closed as completed.
So if you only want to use a key temporarily in one session, prefer unset on the variable, or turn off "Use custom API key" in /config, instead of rushing to /logout.
I need to switch between the official subscription and a custom endpoint. How do I avoid conflicts?
There are two approaches, with different levels of cleanliness.
Option 1: Per project, in settings.local.json
Put the custom endpoint variables only in a project's .claude/settings.local.json, not in your shell config:
{
"env": {
"ANTHROPIC_BASE_URL": "https://your-gateway-address",
"ANTHROPIC_AUTH_TOKEN": "your-gateway-key"
}
}
This way the gateway is used only when you start Claude Code in that project directory; other projects still use the login session. settings.local.json is a local-level settings file that is normally not committed to Git, which makes it a reasonable place for keys, but double-check your .gitignore.
Note that the /login credentials are still on your machine in this project, so you may still see the "Both ANTHROPIC_AUTH_TOKEN and /login managed key set" message at startup. That is what issue #67861 reports: the two credentials go to different servers and it works fine in practice, but the warning appears on every launch. The issue was closed as not planned, and there is currently no official switch to suppress it. If you can live with the message, use this approach. For how to fill in the endpoint address and whether to include /v1, see Configuring a custom API endpoint for Claude Code, Codex and OpenCode.
Option 2: Separate config directories with CLAUDE_CONFIG_DIR
The official docs provide the CLAUDE_CONFIG_DIR environment variable: each config directory keeps its own settings, session history, and login session or API key. Here we use it to split "subscription" and "gateway" usage into two directories.
# Add to ~/.zshrc or ~/.bashrc
alias claude-gw='CLAUDE_CONFIG_DIR=~/.claude-gw claude'
Put the gateway variables in the env section of ~/.claude-gw/settings.json, and don't run /login in that directory. Use claude day to day for the subscription, and claude-gw when you need the gateway. Credentials are stored separately on each side and won't overwrite each other.
Either way, follow the terms of service of whatever service you use. This article only covers tidying up local credentials; it does not cover anything related to multiple accounts or regional restrictions.
If it's not an authentication problem, what else should I check?
If /status shows the right credential after cleanup but requests still fail, look at the network and the server side:
- Is there an incident on the provider's side? cdnping's Anthropic API status page syncs with Anthropic's official status page every 15 minutes and probes api.anthropic.com from Cloudflare's edge every 5 minutes. According to the official status page records synced by cdnping, there were 8 incidents on Anthropic's status page between 2026-09-17 and 2026-10-17 (6 major, 1 minor, 1 none). For the detailed timeline, see Is Claude down often lately?
- Are other users seeing it too? Is Claude Code down? combines official status, API probes and one-click user reports, and the report types include "signed out". If many people report being signed out at the same time, the problem is probably not your configuration.
- The network layer: timeouts and connection resets are unrelated to authentication. Work through them with the four-layer checklist for Claude Code connection timeouts and errors. If you've set a proxy and it isn't taking effect, see Claude Code proxy settings not working.
Checklist
- Read the warning and note which two credentials it names in parentheses
- Run
/statusto confirm which one is in use - Check
ANTHROPIC_*andCLAUDE_CODE_OAUTH_TOKENin the current terminal, shell config files, and Windows user and system environment variables - Check
envandapiKeyHelperin all three settings layers - Keep only one; when going back to a subscription, delete
ANTHROPIC_BASE_URLas well - If you only want a key temporarily,
unsetthe variable or use the/configtoggle first, and don't rush to/logout - If you need both long term, separate them with
settings.local.jsonorCLAUDE_CONFIG_DIR
References: the official Claude Code Authentication docs; issues #34826, #33330, #30781, #11587, #67861 and #50537 in the anthropics/claude-code repository (statuses as checked in mid-October 2026; all closed: #30781 as completed, #11587 as duplicate, the rest as not planned).