Pricing
Claude Code "Invalid API Key · Please Run /login": How to Fix It
Claude Code says 'Invalid API key · Please run /login'? The usual causes (stray ANTHROPIC_API_KEY, expired key, wrong login) and the documented fixes.
9 min read
Causes and fixes checked Oct 31, 2026 in Claude Code’s error reference, authentication docs and VS Code docs. The error string is quoted from Claude Code GitHub issue #5167 (verified Oct 31, 2026). Every source is listed at the bottom. No affiliate links on this page (how we work).
The exact error: Invalid API key · Please run /login
If Claude Code greets you with this:
Invalid API key · Please run /login
you’re in good company. That’s the exact title of GitHub issue #5167 (verified Oct 31, 2026), opened on Claude Code 1.0.68, and it’s still open. Current builds, per Anthropic’s error reference, word it slightly differently when the bad key comes from your environment:
Invalid API key · Fix external API key
The one-line answer (as of Oct 31, 2026): Claude Code sent a key the API rejected, and very often it’s a key you forgot you had. Anthropic’s docs say this error means “The ANTHROPIC_API_KEY environment variable or apiKeyHelper script returned a key the API rejected, or Claude Code blocked a key from ANTHROPIC_API_KEY before sending it.”
Your Pro or Max subscription is probably fine. Claude Code just isn’t using it. It’s Jason Bourne opening the safe-deposit box: six passports, and the one the border guard is checking isn’t the one you meant to show.
Why it happens
Three causes cover almost every report, and all three are in Anthropic’s own docs.
1. A stray ANTHROPIC_API_KEY is overriding your subscription login. This is the big one. Claude Code picks one credential when several exist, and an API key ranks above your /login subscription. From the authentication docs: “If you have an active Claude subscription but also have ANTHROPIC_API_KEY set in your environment, Claude Code uses the API key once you approve it. This can cause authentication failures if the key belongs to a disabled or expired organization.” The error reference adds the sneaky part: tools like direnv, dotenv shell plugins and IDE terminals “can load a stale key from a .env file in your project without you setting it explicitly.”
2. The key itself is dead. Typo, revoked in the Console, or expired. Anthropic’s API docs define a 401 authentication_error as “There’s an issue with your API key (for example, it’s malformed, revoked, or expired…)”. If the key belongs to a disabled Console organization, newer builds say so directly: Your ANTHROPIC_API_KEY belongs to a disabled organization · Unset the environment variable to use your subscription instead.
3. The wrong login is winning. Claude Code checks credentials in a fixed order (authentication docs, verified Oct 31, 2026):
| Rank | Credential | Set by |
|---|---|---|
| 1 | Cloud provider | CLAUDE_CODE_USE_* |
| 2 | Bearer token | ANTHROPIC_AUTH_TOKEN |
| 3 | API key | ANTHROPIC_API_KEY |
| 4 | Key script | apiKeyHelper |
| 5 | Long-lived token | CLAUDE_CODE_OAUTH_TOKEN |
| 6 | Profile | ANTHROPIC_PROFILE |
| 7 | Subscription | /login |
Source: Claude Code authentication, verified Oct 31, 2026.
Your Pro or Max login sits at the bottom (an ant profile only outranks it when you name it in ANTHROPIC_PROFILE). Anything above it wins, including a key you exported in your shell profile months ago and never thought about again. If CLAUDE_CODE_USE_BEDROCK, CLAUDE_CODE_USE_VERTEX or CLAUDE_CODE_USE_FOUNDRY is set, Claude Code goes to your cloud provider first, so a leftover variable from a work setup can route you there too.
There’s also a messier edge case. Issue #98838 (opened Oct 1, 2026 UTC, still open, verified Oct 31, 2026) reports that with an apiKeyHelper and a custom ANTHROPIC_BASE_URL, a stored credential still gets sent instead of the helper’s key, the gateway returns 401, and “Please run /login” just stores the same credential again. The reporter’s way out was claude /logout (or deleting the saved credential). That’s a user report, not an Anthropic fix, but if you run a gateway and /login keeps looping, it’s worth reading.
Fix it: the documented steps
In order, per Anthropic’s error reference and authentication docs (verified Oct 31, 2026):
- See which credential is active. Run
/statusinside Claude Code. When a login and an API key are both configured,/statusmarks the one that isn’t in use. - Find the stray key. In the same shell, run
env | grep ANTHROPIC(PowerShell:Get-ChildItem Env:ANTHROPIC*). Check your shell profile and any.envfile in the project. - Want your subscription? Drop the key. Run
unset ANTHROPIC_API_KEY, remove it from your shell profile, relaunchclaude, then/loginif it asks. Check/statusagain. - Want the API key? Make it a good one. Check for typos and confirm the key hasn’t been revoked in the Console. If the message continues past
Fix external API keywith something like “it contains a line break at character 41”, the key never left your machine: Claude Code found a character HTTP headers can’t carry. Anthropic says the usual cause is a key pasted from a document or chat, so re-enter it cleanly. - Using
apiKeyHelper? Run the script on its own and confirm it prints a valid key. - Approved the key by accident? In interactive mode Claude Code asks once whether to use
ANTHROPIC_API_KEYand remembers the answer. Change it with the “Use custom API key” toggle in/config(it only shows while the variable is set). In non-interactive mode (-p), the key is always used when present. - Still stuck? Type
/logout, then log in again. Logging out also resets first-launch setup, so the nextclaudewalks you through login fresh.
If the error is actually Not logged in · Please run /login, that’s the sibling: no valid credential at all. Run /login. If it keeps coming back, Anthropic’s troubleshooting guide says to check that your system clock is accurate, and on macOS, if you’re asked to unlock your Keychain, to run claude doctor and check its report for a keychain warning.
Fixing it in VS Code
The VS Code extension reads the same ANTHROPIC_API_KEY, ANTHROPIC_AUTH_TOKEN and apiKeyHelper as the terminal (authentication docs), so a stray key breaks both. What’s different, per Anthropic’s VS Code docs (verified Oct 31, 2026):
- No API key needed. Any paid Claude plan or a Console account works through the extension’s Sign in screen.
- Sign-in screen didn’t reappear? If you see
Not logged in · Please run /login, the extension should reopen sign-in automatically. If it doesn’t, run Developer: Reload Window from the Command Palette. - Key set in your shell but VS Code doesn’t see it? VS Code “may not have inherited your shell environment.” Launch it from a terminal with
code ., or sign in with your Claude account instead.
The reverse also holds: if VS Code inherited a dead key from your shell, unset it, then relaunch VS Code from that cleaned-up shell.
Subscription login vs API key: what each bills
This is the part that costs money, so it’s worth thirty seconds.
| Auth | Billed to | Good for |
|---|---|---|
| /login (Pro, Max) | Your plan | Daily coding |
| Console API key | Per-token usage | Scripts, CI |
| Bedrock/Vertex | Your cloud bill | Company cloud |
Sources: authentication, verified Oct 31, 2026; Pro plan, verified Oct 25, 2026.
Claude Pro is “$20 per month (US)” and “does not include API usage through the Claude Console” (verified Oct 25, 2026). Max is $100 (5x) or $200 (20x) a month (verified Oct 24, 2026). So if a stray key has been quietly winning, Claude Code has been billing your Console account per token while your subscription sat unused. That’s the honest frustration here: the most expensive credential outranks the one most people are paying for, and the only warning is a one-time prompt you may have clicked through months ago.
For the full cost picture, see Claude Code pricing and Claude API pricing. Deciding between plans? Claude Pro vs Max.
Anthropic documents the precedence order clearly, and
/statusdoes mark the unused credential. But a subscription login ranking below a stale environment variable, with an error that says “Please run /login” when logging in doesn’t fix it, is how issue #5167 has stayed open since August 2025.
Not an auth problem?
If the message mentions 529 or Overloaded, your key is fine and Anthropic’s servers are busy: see Claude Code 529 overloaded. If it mentions a session or weekly limit, that’s your plan’s allowance: see Claude Code usage limits. And if you’re comparing tools while this one sulks, AI coding tools compared has dated prices.
Here’s what I’d do
Run /status first, every time. If it shows an API key and you pay for Pro or Max, unset ANTHROPIC_API_KEY, delete it from your shell profile and .env, relaunch and /login. If you meant to use a key, check it in the Console and retype it. If you run a gateway with apiKeyHelper and /login loops, /logout and read issue #98838 before burning an afternoon.
Getting Unable to connect to API (ECONNRESET) rather than a key error? The Claude Code ECONNRESET fix walks through status, proxy and VPN checks.
If Claude Code connects fine but won’t write files, the “File has been unexpectedly modified” fix covers watchers, formatters and stale reads.
If Claude Code instead says your login expired, the Claude Code OAuth session expired fix covers re-authenticating and headless sessions.
Getting API Error: 400 instead of a key error? The Claude Code 400 fix covers each variant.
Key looks fine but the API still returns 403? Check Claude Code API Error 403 for access-grant and “Request not allowed” fixes.
Invalid-key symptoms often surface as HTTP 401. See Claude Code API Error 401.
FAQ
Why does Claude Code say Invalid API key · Please run /login?
The API rejected the key Claude Code sent, usually because an ANTHROPIC_API_KEY in your environment is overriding your subscription login or the key is revoked, expired or mistyped.
How do I fix Claude Code invalid API key in VS Code? Unset the stray ANTHROPIC_API_KEY, relaunch VS Code from a clean terminal with code ., and sign in with your Claude account; use Developer: Reload Window if the sign-in screen doesn’t appear.
Does an API key override my Claude Pro login? Yes, Anthropic’s docs say Claude Code uses ANTHROPIC_API_KEY instead of your subscription once you approve it, and always in non-interactive -p mode.
What is a 401 invalid API key in Claude Code? A 401 authentication_error means the API key is malformed, revoked or expired, per Anthropic’s API error docs.
What does Invalid API key · Fix external API key mean?
It’s the current wording for a bad key from ANTHROPIC_API_KEY or an apiKeyHelper script; check the key in the Console or unset it and run /login.
How do I check which login Claude Code is using? Run /status inside Claude Code; it shows the active credential and marks the one that isn’t in use.
Sources
- Claude Code GitHub issue #5167 (“Invalid API key · Please run /login”, v1.0.68): https://github.com/anthropics/claude-code/issues/5167 (verified 2026-10-31)
- Claude Code GitHub issue #98838 (stored credentials override apiKeyHelper, 401 loop, user report): https://github.com/anthropics/claude-code/issues/98838 (verified 2026-10-31)
- Claude Code error reference (Invalid API key, Not logged in, disabled organization, header-value errors): https://code.claude.com/docs/en/errors (verified 2026-10-31)
- Claude Code authentication (precedence order, unset ANTHROPIC_API_KEY, /status, /logout, /config toggle): https://code.claude.com/docs/en/authentication (verified 2026-10-31)
- Claude Code in VS Code (sign-in, Reload Window, shell environment): https://code.claude.com/docs/en/vs-code (verified 2026-10-31)
- Claude Code install troubleshooting (not logged in or token expired, system clock, Keychain): https://code.claude.com/docs/en/troubleshoot-install (verified 2026-10-31)
- Anthropic API errors (401 authentication_error): https://docs.claude.com/en/api/errors (verified 2026-10-31)
- What is the Pro plan (price, Console API usage not included): https://support.claude.com/en/articles/8325606-what-is-the-pro-plan (verified 2026-10-25)
- What is the Max plan (Max 5x and 20x prices): https://support.claude.com/en/articles/11049741-what-is-the-max-plan (verified 2026-10-24)