Skip to content
claudecookie
Back to the converter

Claude Code keeps logging out: fixing “session expired, run /login”

Claude Code stops mid-task with “Your session has expired. Please run /login”, or it silently drops back to a login prompt every few hours. Here is what that message actually means and the fixes that work, in order.

Last updated: 2026-09-14

claude — session fix
claude
✗ Your session has expired. Please run /login to sign in again.
# first, make sure no API key is overriding the subscription:
echo $ANTHROPIC_API_KEY
sk-ant-api03-… ← unset this if you meant to use Pro/Max
/logout
Logged out. Cleared ~/.claude/.credentials.json.
/login
Opening browser to authenticate…
✔ Logged in as you@example.com — Claude Pro

What “session expired” actually means

Claude Code does not use your password on every request. When you run /login it completes a browser sign-in and stores an OAuth token in ~/.claude/.credentials.json (or your OS keychain). That access token is short-lived and is refreshed in the background using a longer-lived refresh token.

“Your session has expired. Please run /login” means the token Claude Code holds is dead and the background refresh was refused. It is not a bug in your code or your prompt — it is an authentication state that has to be re-established.

The fix that works most of the time: /logout then /login

Inside Claude Code, run /login and complete the browser sign-in. If that alone does not stick, run /logout first to discard the stale credentials, then /login again from a clean state. A clean logout-then-login cycle resolves the large majority of cases.

If the browser step opens but never completes, copy the authorization URL into a browser where you are already signed in to claude.ai, approve it, and paste the code back into the terminal.

If it loops or comes back within hours

When /login succeeds but you are logged out again soon after, the refresh itself is failing. Common causes: you changed your Anthropic password, you signed out of claude.ai (or “sign out of all sessions”) elsewhere, an admin or the account revoked sessions, the machine was asleep or offline past the refresh window, or the system clock is wrong — a clock that has drifted makes a valid token look expired.

Fix the underlying cause first: set the clock to sync automatically, and avoid signing out of every session on the web while Claude Code is running. Then do one clean /logout → /login.

The ANTHROPIC_API_KEY trap

The single most common cause of a login that “won’t stick” is an ANTHROPIC_API_KEY environment variable set in your shell profile. Claude Code will prefer the API key over your subscription login, and that pay-as-you-go key can be expired, rate-limited or scoped differently — which surfaces as auth errors even though your subscription is fine.

Check for it with `echo $ANTHROPIC_API_KEY` (macOS/Linux) or `echo %ANTHROPIC_API_KEY%` (Windows). If it is set and you meant to use your Pro/Max subscription, remove it from your shell profile (~/.zshrc, ~/.bashrc, or the Windows environment variables) and restart the terminal, then /login.

Last resort: delete the credentials file

If /logout → /login still loops, the stored credential is corrupt — a partially written ~/.claude/.credentials.json, or a macOS Keychain entry that is locked or not writable. Close Claude Code, delete ~/.claude/.credentials.json, reopen, and run /login to re-authenticate from scratch. On macOS you may also need to remove the “Claude Code” item from Keychain Access.

This file holds live credentials — treat it like a password file, and never paste its contents into a chat or a screenshot.

Check whether your session is actually alive

Before you spend time on fixes, it helps to know whether the underlying claude.ai session is still valid at all — the same account Claude Code signs into. Export your claude.ai session cookie and run it through the checker below: it tells you in seconds whether the session is live, which plan it is on, and how much of your 5-hour and weekly limits are left, so you can tell an expired session apart from a hit usage limit.

Sources

Claude Code login — questions

The refresh token behind /login is good for roughly 30 days of activity, but the access token it mints is short-lived and refreshes in the background. In practice you should only have to run /login again every few weeks — if it is every few hours, something is blocking the refresh (see the ANTHROPIC_API_KEY and clock causes above).

Rarely — reinstalling does not clear ~/.claude/.credentials.json, so a corrupt credential survives. Deleting that file (or the keychain entry) and running /login is the targeted fix; reinstalling is only needed if the binary itself is broken.

No. A session/login error does not consume usage or money. But if an ANTHROPIC_API_KEY is set, Claude Code may be billing that API key per token instead of using your subscription — which is a reason to remove it if you intended to use Pro or Max.

For interactive coding on a Pro/Max plan, use the subscription login (/login) and make sure ANTHROPIC_API_KEY is unset. Use an API key only for automation or CI where you want metered pay-as-you-go billing and no browser step.

A machine asleep or offline past the refresh window lets the token lapse, and a VPN or proxy can make the refresh call fail or look like a new location. Reconnect, make sure the clock is correct, and run /login once from a stable connection.

Is your Claude session still alive?

Paste your claude.ai session cookie and see in seconds whether it is valid, the plan, and your remaining 5-hour and weekly usage.

Check your cookie