Skip to content
AI Dev Toolkit.
Esc
  • AI Token CounterCount tokens for GPT, Claude, Gemini, DeepSeek, Qwen and more.Tool
  • LLM API Cost CalculatorEstimate per-request, daily and monthly API costs.Tool
  • AI Model ComparisonCompare prices, context windows and features across models.Tool
  • AI Model Pricing PagesSpecs, real costs and cheaper alternatives for popular models.Tool
  • Context Window CheckerSee whether your text fits each model's context window.Tool
  • Subscription vs API CalculatorFind out whether a chat plan or the API is cheaper for you.Tool
  • GPU / VRAM CalculatorCheck how much VRAM a local model needs and which GPUs fit.Tool
  • Claude Code Error DatabaseExact Claude Code error messages with tested fixes.Tool

Claude Code · Sign-in

Invalid API key · Fix external API key

The key from ANTHROPIC_API_KEY or your apiKeyHelper was rejected by the API, or blocked before sending because it’s malformed.

Message

The exact error

Claude Code
Invalid API key · Fix external API key

Also appears as:

When the key contains a character headers can’t carry
Invalid API key · Fix external API key · Invalid X-Api-Key header value from ANTHROPIC_API_KEY: it contains a line break at character 41 (120 characters on 2 lines).

Meaning

What it means

“External” means the key came from outside Claude Code’s own login: the ANTHROPIC_API_KEY variable or an apiKeyHelper script. Either the API rejected it (wrong, revoked or from another account) or Claude Code stopped it first because it contains a character an HTTP header can’t hold, such as a line break from a bad copy-paste.

Causes

Common causes

  • A typo, a truncated paste, or a key that was revoked in the Console
  • A stale key loaded from a .env file by direnv, a shell plugin or your IDE’s terminal
  • A line break, space or quote character inside the variable
  • An apiKeyHelper script printing something other than the key

Fix

How to fix it

  1. Find out where the key comes from

    Run /status in Claude Code to see which credential it is actually using. Look for .env files in your project if you didn’t set the variable yourself.

    macOS / Linux
    env | grep ANTHROPIC
    Windows PowerShell
    Get-ChildItem Env:ANTHROPIC*
  2. Check the key in the Console

    Confirm it exists and isn’t revoked at platform.claude.com/settings/keys. Create a new one if in doubt and paste it without surrounding quotes or line breaks.

  3. Or use your subscription instead

    Unset ANTHROPIC_API_KEY and run /login.

  4. Test an apiKeyHelper script

    Run the script by hand: it must print only the key on standard output.

Related

Search all Claude Code errors