How to Fix Claude Code "API Error: 401" When /login Fails¶
For / Key Points
Audience: Developers locked out of Claude Code by persistent API Error: 401 that even /login cannot fix
Key Points:
- A stale or expired cached login can cause
/loginitself to fail with 401; this failure mode is documented in Anthropic's official issue tracker1 - If
/loginis already stuck, clearing cached credentials and signing in again is the most reliable recovery path12 - API key authentication is available as a fallback
What the Error Looks Like¶
You launch Claude Code one morning and the terminal greets you with a red error message.
API Error: 401 {"type":"error","error":{"type":"authentication_error",
"message":"Invalid authentication credentials"}}
Please run /login
You run /login as instructed -- another 401. You try /logout -- 401 again. Everything returns 401. This is a common symptom of stale cached credentials or an expired OAuth login state.
The error comes in several variations:
Invalid authentication credentialsInvalid API keyOAuth token has expired. Please obtain a new token or refresh your existing token.
The wording differs, but the recovery path is usually the same: clear the stuck local auth state, then sign in again.
Why This Happens¶
Claude Code supports multiple authentication types, but the common locked-out case in the official issue tracker is an expired OAuth login. When the cached login state becomes invalid, API requests can start returning 401.
The problem is that authentication commands like /login and /logout can fail in the same broken state1. When that happens, the normal recovery path is blocked inside the CLI itself.
At that point, the practical recovery path is to bypass the stuck session and clear the cached credentials directly.
The Fix¶
Clear the cached credentials manually, then sign in again using the supported flow. When /login itself is stuck, this is the most reliable recovery path.
Step 1: Delete Auth Files¶
Anthropic's IAM docs explicitly document where Claude Code stores credentials.2 If CLI-based recovery does not work, clear the cached state directly from that location.
Linux / WSL2 / Windows:
rm -f ~/.claude/.credentials.json
macOS:
On macOS, credentials are stored in the Keychain. Run the following in your terminal.
security delete-generic-password -s "claude-code" 2>/dev/null
rm -f ~/.claude/.credentials.json
Step 2: Sign In Again¶
With the cached auth state cleared, start Claude Code again and use the documented sign-in flow.
claude
If Claude Code does not prompt automatically, run /login inside the session. Anthropic's official docs list /login as the built-in account sign-in / account-switch command.3
Step 3: Verify¶
Launch Claude Code and run the following inside the interactive session.
/status
If you see your account info (auth method, subscription type), the recovery is complete.
If It Still Does Not Work¶
Three additional approaches when clearing cached credentials does not resolve the issue.
Update Claude Code¶
Older versions may have auth-flow bugs or stale credential handling behavior.
npm install -g @anthropic-ai/claude-code@latest
claude --version
Use an API Key Directly (Bypass OAuth)¶
You can bypass OAuth entirely by providing an API key.
- Go to Anthropic Console and create an API key
- Set the environment variable
export ANTHROPIC_API_KEY="sk-ant-api03-xxxxx"
This bypasses Claude.ai login entirely for terminal CLI sessions. Anthropic's IAM docs also note that terminal sessions can use API-key-based auth paths, while desktop and remote sessions stay OAuth-only.2
Check for Service Outages¶
What looks like an auth error may actually be an Anthropic infrastructure incident.
During outages, authentication endpoints are also affected. In that case, the only option is to wait.
Preventing Recurrence¶
These practices reduce recovery time the next time auth gets stuck.
- Remember the auth file path (
~/.claude/.credentials.json). You can delete it instantly next time - Keep a backup API key ready. Lets you switch immediately during OAuth failures
- Update Claude Code regularly. Auth flow improvements ship frequently
Summary¶
The Claude Code 401 loop is a documented failure mode where cached login state becomes invalid and even /login can stop working. Anthropic's docs confirm where credentials are stored, and the official GitHub issue confirms that clearing cached auth state is a real-world workaround when the normal recovery path is blocked.12
If /login is already returning 401, clear the cached credentials, then start claude and sign in again. Keep an API key fallback ready for terminal workflows where Claude.ai login is unavailable or undesirable.
Related Articles¶
- Claude Code Complete Guide
- Claude Code Reference Guide
- Fix Claude Code "command not found / not recognized"
- Claude Code Windows Native Installation Guide
- Claude Code Auto Permission Guide
- Claude Code x Codex CLI Review Loop Automation
Claude Code issue #33811 —
/loginと/logoutを含めて 401 で詰む事例。 ↩↩↩↩Authentication - Claude Code Docs — 認証情報の保存先とサポートされる認証方式。 ↩↩↩↩
Quickstart - Claude Code Docs —
claude起動時のログイン導線と/loginによるアカウント切替。 ↩