Fix Claude Code "API Error: 401 OAuth token has expired"
Claude Code 401 OAuth token has expired means the saved login failed to refresh. Run /login, then check for a stale CLAUDE_CODE_OAUTH_TOKEN variable.
Please run /login · API Error: 401 OAuth token has expired ...The access token of your saved login expired and the automatic refresh failed; in most cases /login fixes it. If the same message comes back soon after you sign in again, check whether a stale token is sitting in the CLAUDE_CODE_OAUTH_TOKEN environment variable.
Three similar messages need different fixes, so match the wording first.
| Message | State | Fix |
|---|---|---|
401 OAuth token has expired | The API rejected an expired token | Steps [1] to [4] below |
OAuth token revoked · Please run /login | You signed out everywhere, or an admin removed access | /login; if that fails, ask the admin |
Login expired · Please run /login | The refresh token was rejected too, so Claude Code cleared the saved login; no request is sent | Only /login helps |
Could not refresh your login because another Claude Code process is refreshing it ... | Another Claude Code process on the same machine holds the refresh lock; not a rejection | Retry in a minute → close other windows → then /login |
In claude -p and the Agent SDK the same state reads Failed to authenticate. API Error: 401 OAuth token has expired ..., with the structured error code authentication_failed.
[1] Run /login in an interactive session. Once the browser authorization completes, the new token is saved and the same session keeps working.
[2] Run /status to see which credential is active. If the Auth token row reads CLAUDE_CODE_OAUTH_TOKEN, the session is using the token from the environment variable, not your saved login. In that case Claude Code keeps sending the value you set even after a 401, rather than switching to the stored login.
To find where the variable is set, count how many times the name appears in each file. This prints counts, not the value.
$ grep -sc CLAUDE_CODE_OAUTH_TOKEN ~/.bashrc ~/.zshrc ~/.profile ~/.claude/settings.jsonIf the variable lives in a shell profile or in the env block of a settings file, /login switches only the current session; every new session reads the variable again. Pick one:
- To use your subscription login, unset the variable (
unset CLAUDE_CODE_OAUTH_TOKEN, or in PowerShellRemove-Item Env:CLAUDE_CODE_OAUTH_TOKEN), remove the line from the profile or settings file, then run/login. - For CI or scripts where browser login is impossible, generate a fresh token with
claude setup-tokenand replace the variable's value. The token lasts one year and is not saved anywhere, so put the printed value straight into your secret store.
$ claude setup-token[3] If a -p command or Agent SDK program uses the saved login, open claude in the same environment (same user, same container), complete /login, then rerun the command. For automation that cannot sign in, switch to the setup-token token from [2] or to ANTHROPIC_API_KEY. An ANTHROPIC_API_KEY is a Console API key billed by usage, so check that difference before moving work that ran on a subscription.
[4] If every launch asks you to log in again, check the system clock. Token validation depends on correct timestamps, so a skewed clock makes this repeat.
$ date -uIf the UTC time printed is several minutes off the real time, fix the operating system's time sync first. On macOS, also see whether claude doctor reports a warning starting with macOS Keychain is not writable. When the Keychain rejects writes, the login is saved to the plaintext file ~/.claude/.credentials.json; the official docs walk through unlocking the Keychain and then running /logout followed by /login.
The official repository has reports of logins expiring several times a day (#36911) and of having to sign in after every restart (#34306); both are closed. The docs say a bug where two sessions refreshed with the same token on wake from sleep, revoking the saved login, was fixed in v2.1.211, so on an older version run claude update first.
For other authentication messages such as Not logged in and Invalid API key, see Fix common Claude Code errors by message.