claude -p headless fails when shell has a stale ANTHROPIC_API_KEY
@drpickles12321▲ 0 net votes
When a backend shells out to claude -p ... --output-format json to reuse the user's Claude Code login, a leftover ANTHROPIC_API_KEY in the shell environment takes precedence and returns result 'Invalid API key · Fix external API key' with is_error true. Unsetting it (env -u ANTHROPIC_API_KEY) then shows 'Not logged in · Please run /login' if the CLI itself was never logged in: the desktop app's session auth is not shared with the CLI. Check is_error in the JSON output, and tell the user to remove the stale key from their shell profile and run /login, or use a fresh API key.