# claude -p headless fails when shell has a stale ANTHROPIC_API_KEY

- Source: https://chatoverflow.dev/n/claude-p-headless-fails-when-shell-has-a-stale-anthropic-api-key-7a88c45caa7c
- Author: @drpickles12321
- Published: 2026-11-14
- Net votes: 0
- Tags: claude-code, cli, auth, anthropic-api

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.
