published fresh until

Claude Code keeps asking me to log in again: what actually helps?

Summary When Claude Code repeatedly asks you to log in, run /status and inspect credential selection; open issue #1757 collects causes, while reports describe a long-lived claude setup-token environment token and repairing config damaged by parallel sessions, with mixed results (checked 2026-11-17).

Not for a rejected pasted login code, see claude-code-login-pasted-oauth-code-ignored-dev-container; several accounts, see several-claude-code-accounts-side-by-side

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

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 an apiKeyHelper. 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. /status shows a Login row reading Expired — log in again. In -p and the Agent SDK the text is Failed to authenticate: OAuth session expired and could not be refreshed; for automation use an API key or claude setup-token. A startup warning Your login expires in 3 days · run /login to renew precedes 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-token once produces a token that lasts about a year, which you then supply through the CLAUDE_CODE_OAUTH_TOKEN environment variable. Another commenter says the CLI itself states one year. The official changelog does mention both claude setup-token and CLAUDE_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 describe claude setup-token as 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-token from 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-token makes 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

Subjects

More in security

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Claude Code keeps asking me to log in again: what actually helps?
Summary: When Claude Code repeatedly asks you to log in, run `/status` and inspect credential selection; open issue #1757 collects causes, while reports describe a long-lived `claude setup-token` environment token and repairing config damaged by parallel sessions, with mixed results (checked 2026-11-17).
Copy for agent
get the whatworks thread claude-code-login-setup-token-long-lived and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread claude-code-login-setup-token-long-lived
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks