claude-code - 💡(How to fix) Fix Silent ANTHROPIC_API_KEY precedence shadows Max subscription auth [1 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
anthropics/claude-code#53728Fetched 2026-04-28 06:48:38
View on GitHub
Comments
1
Participants
2
Timeline
4
Reactions
0
Timeline (top)
labeled ×3commented ×1

Root Cause

The boundary between "scripts use API tier" and "interactive use is covered by Max" is reasonable and industry-standard. But the silent precedence in Claude Code converts users into pay-as-you-go customers without informing them. This produces revenue from users' confusion rather than their stated intent.

Fix Action

Workaround

alias claude='env -u ANTHROPIC_API_KEY claude'

Adding this to ~/.zshrc causes interactive sessions to ignore the env var and use OAuth/Max instead. Scripts that explicitly read os.getenv("ANTHROPIC_API_KEY") continue to work because the env var is still set in the shell, just stripped from the claude invocation.

Code Example

alias claude='env -u ANTHROPIC_API_KEY claude'
RAW_BUFFERClick to expand / collapse

What

When both ANTHROPIC_API_KEY (env var) and a Claude Pro/Max OAuth login are present on the same machine, Claude Code silently uses the API key. There is no warning at session start, no notice in claude /status until the user explicitly checks, and no one-time prompt asking which auth method to use.

Impact

Users who set ANTHROPIC_API_KEY for a single project's scripts (e.g. a cron job, a CI step, a tool that needs programmatic access) end up unknowingly running all subsequent interactive Claude Code sessions against the API tier instead of their Max subscription. They're paying twice for usage their subscription already covers.

In my own case, I burned ~$20 of API credit across two interactive coding sessions that should have been covered by my Max plan. The fix (an alias claude='env -u ANTHROPIC_API_KEY claude') is one line, but only if you know to look for it. Most users won't until they see the bill.

Why this matters

The boundary between "scripts use API tier" and "interactive use is covered by Max" is reasonable and industry-standard. But the silent precedence in Claude Code converts users into pay-as-you-go customers without informing them. This produces revenue from users' confusion rather than their stated intent.

Suggested fix (any one of these would resolve it)

  1. First-run prompt: when Claude Code detects both ANTHROPIC_API_KEY AND an active OAuth login, prompt once: "We detected both an API key and a logged-in subscription. Which billing source should interactive sessions use? (API key / Subscription / Ask each session)". Persist the choice in ~/.claude/config.json.
  2. Status banner: show a single line at session start, e.g. Auth: API key (ANTHROPIC_API_KEY env). Run /login to switch to your logged-in subscription. Make it dismissable but visible by default.
  3. Documentation: add a clear note in the Claude Code billing/auth docs explaining precedence and the env -u workaround. Currently this is buried.

Reproduction

  1. export ANTHROPIC_API_KEY=sk-... in shell profile
  2. claude /login to a Max account
  3. claude /status — confirms both, but doesn't say which is active for billing
  4. Run an interactive session — billed to API, not Max

Workaround

alias claude='env -u ANTHROPIC_API_KEY claude'

Adding this to ~/.zshrc causes interactive sessions to ignore the env var and use OAuth/Max instead. Scripts that explicitly read os.getenv("ANTHROPIC_API_KEY") continue to work because the env var is still set in the shell, just stripped from the claude invocation.

Why a one-time prompt rather than always-prefer-Max

I'm not asking for "always prefer subscription" because there are legit reasons to keep the current behaviour (e.g. an org-issued API key with shared billing should override personal Max). The ask is just make the choice visible and explicit, not silent.

extent analysis

TL;DR

To resolve the issue, consider adding a one-time prompt or status banner to inform users when both ANTHROPIC_API_KEY and an active OAuth login are detected, allowing them to choose the billing source for interactive sessions.

Guidance

  • Implement a first-run prompt that detects both ANTHROPIC_API_KEY and an active OAuth login, and asks the user to choose the billing source for interactive sessions.
  • Add a status banner at session start to indicate which billing source is being used, with an option to switch to the other source.
  • Update the documentation to clearly explain the precedence of ANTHROPIC_API_KEY and the env -u workaround.
  • Consider using the provided workaround alias claude='env -u ANTHROPIC_API_KEY claude' to ignore the ANTHROPIC_API_KEY env var for interactive sessions.

Example

alias claude='env -u ANTHROPIC_API_KEY claude'

This alias can be added to ~/.zshrc to ignore the ANTHROPIC_API_KEY env var for interactive sessions.

Notes

The issue highlights the need for explicit user choice when both ANTHROPIC_API_KEY and an active OAuth login are present. The suggested fixes aim to make this choice visible and explicit, rather than relying on silent precedence.

Recommendation

Apply the workaround by adding the alias claude='env -u ANTHROPIC_API_KEY claude' to ~/.zshrc, as it provides a simple and effective solution to ignore the ANTHROPIC_API_KEY env var for interactive sessions.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

claude-code - 💡(How to fix) Fix Silent ANTHROPIC_API_KEY precedence shadows Max subscription auth [1 comments, 2 participants]