What this thread does
Invalid API key · Fix external API key (older builds printed Invalid API key · Please run /login), Not logged in · Please run /login, Login expired · Please run /login: Claude Code rejected, or could not find, the credential it picked. First action: run /status to see which credential is active, then unset ANTHROPIC_API_KEY and /login if an environment key is overriding your subscription; for a daily re-login loop use claude setup-token.
Anthropic's 2.1.318 notes add a stored claude.ai sign-in's expiry to claude auth status (loginExpiresAt) and claude -p JSON (credential_expires_at), plus a stderr warning in claude -p text output starting ten days before that sign-in expires, and fix shared-home jobs losing login when another host signs in again to the same account and organization (checked 2026-11-05).
Evidence: strings and fixes below are from Anthropic's error reference and authentication pages (checked 2026-10-31, untested here); the older string is one closed GitHub report. Not for: a pasted login code that is rejected (see claude-code-login-pasted-oauth-code-ignored-dev-container), a failing key script (see claude-code-apikeyhelper-script-is-failing-stdout), several accounts (see several-claude-code-accounts-side-by-side).
Diagnostic: Invalid API key
Cause: ANTHROPIC_API_KEY or an apiKeyHelper script returned a key the API rejected (typo, revoked, or an organization that is disabled or expired).
1. env | grep ANTHROPIC (PowerShell: Get-ChildItem Env:ANTHROPIC*). direnv, dotenv plugins and IDE terminals can load a stale key from a .env file. 2. unset ANTHROPIC_API_KEY, then /login, then /status. 3. If apiKeyHelper supplies the key, run the script directly and check it prints a valid key.
Why a key beats a fresh login: the authentication page orders credentials as cloud provider variables, ANTHROPIC_AUTH_TOKEN, ANTHROPIC_API_KEY, apiKeyHelper, CLAUDE_CODE_OAUTH_TOKEN, named profiles or federation, /login, then unnamed ant profiles. Interactive mode asks once to approve a key, and the "Use custom API key" toggle in /config appears only while the variable is set; with -p the key is always used. /status marks the credential that is not in use. A CLAUDE_CODE_OAUTH_TOKEN is read again in every new session until you remove it from your shell profile or a settings env block.
Older report: issue 5369 by maxt-debug (Aug 2025, v1.0.71, macOS), titled "Authentication fails immediately after successful /login": status bar Missing API key, /status listed no auth token. A stale bot closed it as not planned with no maintainer cause; the reporter had already tried unsetting the key, so do not assume it is the conflict above.
Diagnostic: Not logged in / Login expired
Not logged in · Please run /login: no valid credential. Run/login; in CI use anapiKeyHelper. Signing in from another window with the same config directory is picked up without a restart (macOS before v2.1.286 needed one).Login expired · Please run /login: the refresh token was rejected and saved credentials were cleared, so each request stops locally./statusshows aLoginrow readingExpired — log in again. In-pand the Agent SDK the text isFailed to authenticate: OAuth session expired and could not be refreshed; for automation use an API key orclaude setup-token. A startup warningYour login expires in 3 days · run /login to renewprecedes expiry.
The problem
Users of Claude Code report being sent back to /login every day or after every reboot, with errors such as "OAuth token has expired" or a 401 that clears only after logging in again. The GitHub issue was opened by scrapebros on 2025-06-07. As of November 2026 it was still open, with no maintainer statement of a single root cause.
This thread is about staying signed in on one account. Running several accounts side by side is a different problem, covered in another WhatWorks thread.
What commenters report
- Long-lived token (a well-upvoted workaround). Several commenters say the normal login token lasts only about 4 to 8 hours in their experience, and one says that running
claude setup-tokenonce produces a token that lasts about a year, which you then supply through theCLAUDE_CODE_OAUTH_TOKENenvironment variable. Another commenter says the CLI itself states one year. The official changelog does mention bothclaude setup-tokenandCLAUDE_CODE_OAUTH_TOKEN(for example a later fix so a transient 401 no longer replaces a long-lived token with a short-lived stored login), so the mechanism exists, and the authentication docs also describeclaude setup-tokenas generating a one-year OAuth token; the docs say it does not save the token, that it needs a Pro, Max, Ultra, Team or Enterprise plan, and that such a token can only make model requests (no Remote Control, no claude.ai connectors). The few-hour lifetime of a normal login is the commenters' observation, not a documented figure. - SSH or headless macOS. One commenter traced their case to the macOS Keychain being locked or unable to show its approval dialog over SSH. Their steps: run
claude setup-tokenfrom a normal desktop terminal, save the token in a file readable only by you (chmod 600), and load it from your shell startup file on the remote machine. Treat the token as a secret: never commit it or paste it into a shared channel. - Corrupted config file. A Windows commenter running three or four parallel sessions found dozens of corrupted backup copies of the user config file (partial writes) and said the re-login loop stopped after closing every session, moving the broken file aside and starting Claude Code once so it recreated a clean one. They also said using one git worktree per session stopped the corruption. This is one person's diagnosis; they cite a changelog entry that already fixed concurrent writes, yet say it still happened afterward.
- Third-party apps. One commenter named a third-party app (ClaudeBar) as the cause.
What did not work for everyone
- One commenter said the env-var token ran but Claude Code claimed no subscription and did not offer the 1M-context model; another saw expiry in a cloud environment.
- Several report the problem returning after reboot, so no fix is confirmed in issue #1757.
Takeaway
If you only need unattended or SSH sessions, try setup-token plus the env var and expect plan features to differ. If logins loop on a desktop with several terminals open, check for a corrupted config file and for background apps that touch the credentials. Keep the token out of repositories.
Sources
- Claude Code authentication docs and error reference, checked 2026-10-31: credential order,
/status, the three messages and their fixes;claude setup-tokenmakes a one-year token that is printed, not saved, needs a Pro, Max, Ultra, Team or Enterprise plan, can only make model requests, and is not read in bare mode; on macOS a locked Keychain falls back to a mode 0600 credentials file. - anthropics/claude-code issue 1757 (scrapebros, 2025-06-07; open, checked 2026-11-17) and issue 5369.
Limits
The commenters' reports (hour-scale login lifetime, SSH Keychain, corrupted config, ClaudeBar) are individual observations, not reproduced here. The docs state no lifetime for a normal login. Issue #1757 checked 2026-11-17.
Started from a post by @scrapebros, packaged by the editor. Original post