OAuth Pro subscription breaks when ANTHROPIC_AUTH_TOKEN/ANTHROPIC_BASE_URL set in env — users workaround via OpenRouter
Problem
Claude Code with Pro subscription OAuth fails with 401 Invalid bearer token when environment variables \ANTHROPIC_AUTH_TOKEN\ or \ANTHROPIC_BASE_URL\ are set in the active shell session — even after a successful \/login\.
Root cause
The internal \m$()\ / \x$()\ function in \cli.js\ returns \false\ (disabling OAuth mode) if \ANTHROPIC_AUTH_TOKEN\ is present in the environment. Claude Code silently falls out of OAuth mode and tries to use a non-existent API key → 401.
Additionally, if a project's \.env\ file contains \ANTHROPIC_API_KEY\ (common when a bot/app shares the same server), Claude Code reads it automatically and creates an auth conflict with the OAuth credentials.
Impact
This is especially common on Linux VPS/servers where:
- A Python/Node bot uses \
ANTHROPIC_API_KEY\or \ANTHROPIC_AUTH_TOKEN\in the same environment - The developer also runs Claude Code on the same machine with Pro OAuth subscription
The workaround users are discovering: Route Claude model calls through OpenRouter instead of Anthropic directly — this is a direct revenue loss for Anthropic.
Reproduction
- Have a valid Claude Pro OAuth session (\
~/.claude/.credentials.json\with \user:inference\scope) - Set \
ANTHROPIC_AUTH_TOKEN=anything\in the shell - Run \
claude\→ 401 on every request despite valid credentials
Fix
\\\bash\
unset ANTHROPIC_BASE_URL ANTHROPIC_AUTH_TOKEN
claude
\\
Suggested improvements
- Better error message: When 401 occurs with OAuth credentials, check if \
ANTHROPIC_AUTH_TOKEN\/ \ANTHROPIC_API_KEY\env vars are set and explicitly warn the user - OAuth mode should take precedence over stale env vars when valid credentials file exists and user explicitly logged in via \
/login\ - Warn on startup if conflicting env vars are detected alongside OAuth credentials
Environment
- Claude Code v2.1.73
- Ubuntu 24.04 LTS
- Pro subscription via claude.ai OAuth
This issue has 5 comments on GitHub. Read the full discussion on GitHub ↗