Skip to content
Nilay Kabariya

Entry No.035·Agents·Updated ·5 min read

Fix: Claude Code "Invalid API key · Fix external API key"

Invalid API key · Fix external API key, Please run /login and 401 in Claude Code: every auth message reproduced on 2.1.314, and the Windows quoting trap.

What changed on Nov 9, 2026: Re-ran the cases that need no account on Claude Code 2.1.321 (Linux). Same messages as on 2.1.316.

by Nilay#claude-code#anthropic#windowsAGENTS

The error, verbatim

Jump to the fix ↓
Invalid API key · Fix external API key

# the other messages for the same problem:
Not logged in · Please run /login
Failed to authenticate. API Error: 401 API key is invalid.
Failed to authenticate. API Error: 401 OAuth access token has expired. Re-authenticate to continue.
✓ Reproduced on Windows 11 Pro

The fix that worked

Sign in again, then see which credential Claude Code is using:

claude auth login
claude auth status
Not it? Every cause, tested ↓

Tested on

Claude Code
2.1.138; re-tested on 2.1.292 and 2.1.314 (October 31), 2.1.316 (November 2, Linux), 2.1.321 (November 10, Linux)
OS
Windows 11 Pro 26200
Shells
cmd.exe, PowerShell 5.1, Git Bash
Keys
fake keys only, in a separate config folder

✓ Last re-run on GitHub Actions · see the run · all repros

Claude Code can sign in two ways: your Claude account (/login), or an API key from the ANTHROPIC_API_KEY environment variable. Every message above means one of those two is missing, malformed or dead. The message tells you which, if you know how to read it.

Which message do you have?

Message What it means Go to
Invalid API key · Fix external API key The key in ANTHROPIC_API_KEY isn’t shaped like a key, or (on current versions) Anthropic rejected it 1, then 2
Nothing for minutes, then 401 API key is invalid Older versions (2.1.138): the key looks fine, but Anthropic rejects it 2
401 OAuth access token has expired Your /login session ran out 3
Not logged in · Please run /login No login and no key at all 3

1. “Invalid API key · Fix external API key”: quotes in the key

This one appears instantly, before any request is made. Claude Code checks the key’s format first, and the usual reason it fails on Windows is quotation marks that became part of the key.

In cmd.exe, quotes around the value are kept as characters:

C:\> set ANTHROPIC_API_KEY="sk-ant-api03-..."
C:\> claude -p hi
Invalid API key · Fix external API key

Put the quotes around the whole assignment instead, or none at all:

set "ANTHROPIC_API_KEY=sk-ant-api03-..."

Single quotes do the same damage ('sk-ant-...' failed identically). PowerShell’s $env:ANTHROPIC_API_KEY = "sk-ant-..." is fine: there the quotes are syntax, not part of the value.

2. A frozen terminal, then “401 API key is invalid”

A key with the right shape but the wrong value (revoked, deleted, a typo in the middle) doesn’t fail fast. I ran claude -p with one:

C:\> claude -p "say hi"
                                    ← nothing for 191 seconds
Failed to authenticate. API Error: 401 API key is invalid.

The debug log shows why. Claude Code retries the rejected request up to 11 times, backing off longer each time, and prints nothing while it does:

[ERROR] API error (attempt 1/11): 401 {"type":"error","error":{"type":"authentication_error","message":"API key is invalid."}}
[ERROR] API error (attempt 2/11): 401 ...
[ERROR] API error (attempt 3/11): 401 ...

So if Claude Code seems to hang on the first message, don’t assume a network problem. Run it once with a debug file and look:

claude -p "hi" --debug-file debug.txt

Then search debug.txt for 401. If it’s there, the key is the problem: create a new one in the Anthropic Console and replace the old value everywhere it’s set (section 4).

3. “Please run /login” or an expired OAuth token

With no key and no login, the message is immediate:

Not logged in · Please run /login

An expired login is louder. I hit this one for real: the standalone CLI on the test machine hadn’t been used in a while, the token refresh failed, and every request came back with:

Failed to authenticate. API Error: 401 OAuth access token has expired. Re-authenticate to continue.

Sign in again. Inside Claude Code type /login; from a terminal:

claude auth login

Then confirm what it’s using:

claude auth status

4. Does an old ANTHROPIC_API_KEY break a working login?

This is the most repeated advice for these errors: “unset ANTHROPIC_API_KEY”. I tested it with a working Claude account login and a dead key in the environment, in print mode (claude -p):

"authMethod": "claude.ai",
"apiKeySource": "ANTHROPIC_API_KEY",

The request succeeded. The login was used and the stale key was ignored, even when the key was the quoted, malformed kind from section 1.

Interactive mode is different. When it starts and finds a key in your environment, it shows “Detected a custom API key in your environment” and asks whether to use it. That prompt can’t be driven from a script, so I didn’t test it, but the logic follows: if you once said yes to a key that has since died, remove the key rather than fight the prompt.

Find where the key is set on Windows. A key saved to your user environment survives every restart:

[Environment]::GetEnvironmentVariable("ANTHROPIC_API_KEY","User")

If that prints an old key, remove it:

[Environment]::SetEnvironmentVariable("ANTHROPIC_API_KEY", $null, "User")

Then close and reopen the terminal, or VS Code. I checked this both ways with a test variable: saving a value didn’t reach the window that was already open, and removing it didn’t either. Each window keeps the copy it started with.

What didn’t work

How this was tested

Claude Code 2.1.138 on Windows 11, run with claude -p and claude auth status. Malformed and invalid keys were fake values, run against a separate config folder (CLAUDE_CONFIG_DIR) so no real login was involved; quoting was tested in cmd.exe batch files, PowerShell and Git Bash. The expired-token case was real, on this machine’s own CLI, and the fix above cleared it. The stale-key-versus-login test used that fresh login plus a fake key in the environment. Retry counts and timings come from --debug-file logs.

November 2, 2026: the cases that don’t need a real account were re-run with Claude Code 2.1.316 on GitHub Actions ubuntu-latest, with an invented key and a throwaway HOME and CLAUDE_CONFIG_DIR. No key gave Not logged in · Please run /login; a quoted key and a well-formed fake key both gave Invalid API key · Fix external API key straight away; and claude auth status with the fake key still said "loggedIn": true. The expired-login and “old key next to a working login” cases need a real account and weren’t re-run. The script and output are public.

November 10, 2026: the same script with Claude Code 2.1.321 on GitHub Actions ubuntu-latest, Node 24.22.2 (run). Every line of output matched the November 2 run on 2.1.316: Not logged in · Please run /login with no key, Invalid API key · Fix external API key at once for the quoted key and for the well-formed fake key, and "loggedIn": true from claude auth status.

— N.K., end of entry No.035

Useful? Pass it on:Post on XFollow @EmotionalMatter

Related entries

  1. No.039

    Fix: Claude Code "Prompt is too long"

    Claude Code fails every message with Prompt is too long, or asks for usage credits. One oversized file did both in my tests, and Claude Code's guard missed it.

    > Prompt is too long

    AGENTS4 min
  2. No.036

    Fix: Claude Code "not compatible with the version of Windows"

    Windows says claude.exe won't run right after an npm install of Claude Code. Your Windows is fine. Reproduced on npm 12, with the reinstall that fixed it.

    > This version of C:\...\claude-code\bin\claude.exe is not compatible with the version of Windows you're running. Check your computer's system information and then contact the software publisher.

    AGENTS5 minWindows
  3. No.058

    Claude Code, Codex, Gemini CLI on Windows: install tested

    All five install and start on Windows 11 with npm 11, in cmd, on Node 24 and Node 26. Two things trip people up. On a default Windows PowerShell setup all five fail with "running scripts is disabled on this system". And with npm 12, Claude Code and OpenCode install with no error but then refuse to start: "not compatible with the version of Windows you're running".

    TESTED6 minWindows

tip: paste the exact error text