Quick fix: the Claude Code "OAuth session expired" error means your saved claude.ai login could not be renewed, so Claude Code signed you out. Type /login at the Claude Code prompt and sign in again. If you hit it in claude -p, a CI job or an Agent SDK script, run claude and /login once in that same environment, or switch the automation to an API key or a token from claude setup-token (steps 3 and 4).
Type "claude code error" into Google and the first suggestion is "failed to authenticate oauth session expired and could not be refreshed". The message usually appears in scripts and the Agent SDK. In an interactive session, the same problem shows up as a different line:
# non-interactive mode (claude -p) and the Agent SDK
Failed to authenticate: OAuth session expired and could not be refreshed
# interactive Claude Code
Login expired · Please run /login
This guide explains why the Claude Code OAuth session expires, then gives the fixes in order, quickest first. Every step comes from Anthropic's official Claude Code error reference or from bug reports on the Claude Code GitHub repository.
Why the Claude Code OAuth session expires
When you sign in with a claude.ai account, Claude Code stores two things: a short-lived access token and a refresh token that it uses to get new access tokens in the background. The error reference says this message appears when the sign-in service rejects the stored refresh token. Claude Code then clears the saved credentials, and with nothing else to fall back on, every request stops. Common reasons:
- The login reached its time limit, or you signed out elsewhere. Recent versions say "reached its time limit" in the message when that is the cause.
- Automation reused an interactive login. A CI job, container or cron script copied credentials from a desktop session and could not renew them.
- An expired
CLAUDE_CODE_OAUTH_TOKEN. A long-lived token set as an environment variable gives "OAuth token has expired" messages when it runs out. - Several processes refreshing at once, such as many terminals or a background session plus your editor.
- A refresh bug. GitHub issues describe the token expiring mid-session without an automatic refresh, which updates address over time.
How to fix "OAuth session expired" in Claude Code
Work through these in order and test with a short prompt after each one.
- Run /login and sign in again. At the Claude Code prompt, type
/loginand complete the browser sign-in. This is the fix the official error reference gives for both "Login expired" and "OAuth token revoked or expired". If the error came fromclaude -por an Agent SDK program, open Claude Code in the same environment (same user, same machine or container), sign in there, then rerun your command:claude # then, at the prompt: /login - Check which credential is active with /status. Run
/status. It shows whether Claude Code is using your claude.ai login, anANTHROPIC_API_KEY, or a token from theCLAUDE_CODE_OAUTH_TOKENenvironment variable. A401 Invalid authentication credentialsmessage is a different problem (a revoked credential, or an organization removed your access), so/statustells you which fix applies. - Renew or unset CLAUDE_CODE_OAUTH_TOKEN. If
/statusshows an Auth token row readingCLAUDE_CODE_OAUTH_TOKEN, that long-lived token has expired or been revoked. Claude Code keeps sending the value you set, so/loginalone does not help. Generate a fresh token and restart with it, or unset the variable and sign in normally:# create a new long-lived token claude setup-token # or stop using the variable (macOS / Linux) unset CLAUDE_CODE_OAUTH_TOKEN claude - Use an API key or a long-lived token for automation. Scripts, CI jobs, cron tasks and background agents cannot complete a browser sign-in. For these, the docs recommend
ANTHROPIC_API_KEYor a long-lived token fromclaude setup-tokeninstead of a saved interactive login. Store either one as a secret in your CI system, never in the repository. - Close other Claude Code windows and retry. If the message says another Claude Code process "is refreshing it", wait a minute and try again: when the other process finishes, your session uses the renewed login. If it keeps returning, close other Claude Code windows, terminals and background processes, then retry. If nothing else is running, run
/login, which does not wait on the refresh lock. - Check your computer's clock. A sign-in token is only valid for a time window, so a wrong system clock can make a fresh login look expired straight away. Turn on automatic date and time in your OS settings, then sign in again. For repeated login prompts across launches, the official error reference points to system clock checks and macOS credential-storage recovery steps in the Claude Code troubleshooting docs.
- Update Claude Code. Run
claude update(or update with the package manager you installed it with) and restart your sessions. Users have reported on GitHub that the login sometimes expires mid-session without refreshing, including in long-running background sessions, and login fixes ship in regular releases. If you use the VS Code extension, update it too: one open report describes the error appearing there even with a valid saved credential.
If nothing works
Run /status and note the credential type and Claude Code version, then check the Claude status page for a login or API incident. If the service is healthy, search the Claude Code GitHub issues for your exact message and add your details to an existing report rather than opening a duplicate. If your account shows "Your account is on hold" or your organization's admin removed access, only the account owner or admin can fix it.
New to the tool? Our Claude Code tutorial for beginners covers installing, signing in and your first tasks. For other developer errors, see npm "could not determine executable to run" and Flutter "error connecting to the service protocol".
Frequently asked questions
What does "OAuth session expired and could not be refreshed" mean?
Claude Code tried to renew your claude.ai login, the sign-in service rejected the stored refresh token, and Claude Code cleared the saved credentials. In interactive mode the same event shows as Login expired · Please run /login.
Why do I see this error in claude -p or the Agent SDK but not in the terminal?
Non-interactive mode and the Agent SDK cannot open a browser to sign you in, so they report the failure with the error code authentication_failed. Sign in once by running claude and /login in the same environment, or use an API key or a token from claude setup-token.
Will I lose my conversation if I run /login?
Signing in again replaces the credential, not your project files. You can keep working in the same session after the login succeeds, or resume an earlier session with claude --resume.
Is "Not logged in · Please run /login" the same error?
It is closely related: Claude Code has no valid saved credential. The fix is also /login, but check /status first if you expected an API key or CLAUDE_CODE_OAUTH_TOKEN to be used.
Sources
- Claude Code Docs: Error reference (Login expired, OAuth token revoked or expired, refresh lock)
- Claude Code Docs: Troubleshooting
- GitHub issue: OAuth session expires mid-session and fails to auto-refresh
- GitHub issue: VS Code extension shows "OAuth session expired" with a valid credential
Last checked November 29, 2026 against the Claude Code error reference.