CLI silently uses API key instead of Max plan when ANTHROPIC_API_KEY is in environment
Resolved 💬 3 comments Opened Apr 2, 2026 by dioliveira07 Closed Apr 6, 2026
Bug description
When ANTHROPIC_API_KEY is present in the shell environment, Claude Code CLI silently switches from the authenticated Max plan (OAuth) to direct API billing — with no warning or confirmation.
This caused unexpected API charges on an Opus key that was intended only for backend scripts, not for the CLI.
Steps to reproduce
- Authenticate Claude Code via
claude login(Max plan / OAuth) - In the same shell, export an API key:
export ANTHROPIC_API_KEY=sk-ant-... - Open a new Claude Code session
- The session silently uses the API key (and its billing) instead of the Max plan
Expected behavior
Claude Code should either:
- Warn the user when it detects both an OAuth session AND an
ANTHROPIC_API_KEYin the environment, letting them choose which to use - Prioritize the Max plan login over the environment variable, since the user explicitly authenticated via
claude login - At minimum, show a visible indicator in the UI that the session is using API billing rather than the Max plan
Actual behavior
- No warning is shown
- The CLI silently picks up
ANTHROPIC_API_KEYand uses it for all requests - The user only discovers the issue after checking API usage/billing
Workaround
Add unset ANTHROPIC_API_KEY to ~/.bashrc to prevent the CLI from picking it up. Keep project-specific keys in separate env vars (e.g. ANTHROPIC_API_KEY_PRODUCTION).
Environment
- Claude Code CLI (latest)
- Linux (Ubuntu)
- Max plan subscriber
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗