Claude Code OAuth token expired: fix unattended runs
As of 2026-10-26, in unattended claude -p, cron or CI runs, once the saved login expires and cannot be refreshed, every model request fails with Login expired. For CI and scripts the official docs point to a one-year OAuth token from claude setup-token; you can also authenticate with an API key or a gateway token.
Updated 2026-10-26
One key for Claude, GPT and leading Chinese models, billed per token, up and running in minutes.
- Billed per token — live rates on /models
- Pay by card (Visa / Mastercard / AMEX), Apple Pay, Google Pay or crypto
- Self-service: top up and activate instantly
What to check
Long-lived token lifetime
claude setup-token generates a one-year OAuth token that you set as CLAUDE_CODE_OAUTH_TOKEN; the docs require a Pro, Max, Team, or Enterprise plan.
Expiry error
Once the stored login expires and cannot be refreshed, each model request fails with Login expired until you sign in again.
Renewal warning
Three days before the login expires, Claude Code warns at startup that the login is about to expire and that you should run /login to renew.
Bare mode caveat
The docs say --bare reads neither OAuth credentials nor the system keychain, and does not read CLAUDE_CODE_OAUTH_TOKEN; use ANTHROPIC_API_KEY or apiKeyHelper instead.
Which credential works for unattended runs
As of 2026-10-26, the official authentication docs list these credential sources: a long-lived OAuth token for subscribers (generated with claude setup-token and set as CLAUDE_CODE_OAUTH_TOKEN), a Claude Console API key (set as ANTHROPIC_API_KEY, used whenever present in non-interactive -p mode), a gateway token (set as ANTHROPIC_AUTH_TOKEN and sent as a Bearer token), and apiKeyHelper for dynamic credentials. When a login expires, sign in again with /login or switch to a long-lived credential.
What the official pages say
The official authentication docs say a renewal warning appears 3 days before the login expires. Once the stored login expires and cannot be refreshed, each model request fails with Login expired · Please run /login. The Login row in /status shows Expired — log in again (Claude Code v2.1.210 or later). A long-lived token is generated with claude setup-token and requires a Pro, Max, Team, or Enterprise plan.
Timeline
A community report (Zenn, published 2026-10-23): Windows Task Scheduler ran claude -p at 3 a.m.; after the login expired, the process exited about 20 seconds after start with the error OAuth session expired and could not be refreshed and exit code 1.
The community article (published this day) says nobody was there to see the sign-in prompt during the unattended run; the author noticed on the morning of 2026-10-18, signed in again, and reran the script by hand. The article says the only option after expiry is to sign in manually.
The authentication, headless, and CLI reference pages on code.claude.com and the QCode environment page were checked; the credential and error wording follows those pages as of 2026-10-26.
Confirmed vs. unconfirmed
Confirmed (verbatim on official pages)
The docs say claude setup-token generates a one-year OAuth token and requires a Pro, Max, Team, or Enterprise plan; CLAUDE_CODE_OAUTH_TOKEN is for CI and scripts; ANTHROPIC_API_KEY is used whenever present in -p mode; ANTHROPIC_AUTH_TOKEN is for gateways; --bare reads neither OAuth credentials, the system keychain, nor CLAUDE_CODE_OAUTH_TOKEN; the Expired row in /status requires v2.1.210 or later.
Unconfirmed and not published
A community report (Zenn, published 2026-10-23): an unattended run on 2026-10-18 exited about 20 seconds after start because the OAuth session had expired and could not be refreshed. This is a user report, not an official error list. The official docs say an agent view or Remote Control background session stops making progress once the credential expires; for CI and scripts they describe the long-lived token from claude setup-token.
Subscription login vs. API credentials
Subscription login and long-lived token
claude setup-token generates a one-year OAuth token tied to a Claude subscription; it suits CI and scripts. Bare mode does not read this variable.
API key and gateway token
ANTHROPIC_API_KEY comes from the Claude Console and is used whenever present in -p mode; ANTHROPIC_AUTH_TOKEN is for a gateway or proxy and is sent as a Bearer token.
Troubleshooting steps
① Check the error and status: a request fails with Login expired, or the Login row in /status shows Expired, which means the saved login has expired. ② If you can interact, run /login in a session (or claude auth login in a shell) to sign in again; on an unattended machine, use claude setup-token, which opens the same browser authorization flow as /login and prints the token to the terminal without saving it; copy it into CLAUDE_CODE_OAUTH_TOKEN. ③ If the script uses --bare, set ANTHROPIC_API_KEY (created in the Claude Console) or apiKeyHelper instead; --bare does not read OAuth credentials. ④ If you connect through a gateway, set ANTHROPIC_AUTH_TOKEN; it takes precedence over ANTHROPIC_API_KEY. ⑤ Put the credential in the cron/CI environment or secret store and rerun once to confirm exit code 0.
Run unattended jobs with a QCode key
The QCode docs use ANTHROPIC_BASE_URL and ANTHROPIC_AUTH_TOKEN: set ANTHROPIC_BASE_URL to https://api.qcode.cc/api and ANTHROPIC_AUTH_TOKEN to your QCode key (starting with cr_); the docs say all three access domains serve identical capabilities and the same key works across them. The docs put the key in ANTHROPIC_AUTH_TOKEN rather than ANTHROPIC_API_KEY, and Claude Code sends it as a Bearer token; model calls are billed per token, with per-model rates on /models. Personal Plans are valid for 30 days per order. Not ready for a monthly plan? Pay-as-you-go Packs start at $4 for a one-time quota you use within 7 days; if you already have a Personal Plan, add a booster pack when you need more quota.
Frequently asked questions
What error appears when the Claude Code OAuth token expires?
The official docs give Login expired · Please run /login; a renewal warning also appears 3 days before the login expires.
Which credentials can an unattended claude -p run use?
A one-year CLAUDE_CODE_OAUTH_TOKEN generated by claude setup-token (requires a subscription), an ANTHROPIC_API_KEY from the Claude Console, or a gateway ANTHROPIC_AUTH_TOKEN; apiKeyHelper is also available.
How long is a claude setup-token token valid?
The docs call it a one-year OAuth token; it requires a Pro, Max, Team, or Enterprise plan and can only make model requests.
Why does --bare mode still fail authentication?
The docs say --bare reads neither OAuth credentials, the system keychain, nor CLAUDE_CODE_OAUTH_TOKEN; use ANTHROPIC_API_KEY or apiKeyHelper in that mode.
What is the fastest recovery after the login expires?
In an interactive environment, run /login to sign in again; on an unattended machine, switch to a long-lived token or an API key and confirm the current credential in the Login row of /status.
Does a QCode key use subscription login?
The QCode docs set it up with the ANTHROPIC_BASE_URL and ANTHROPIC_AUTH_TOKEN environment variables, using a QCode key starting with cr_, not a subscription /login. In the official credential order, ANTHROPIC_AUTH_TOKEN ranks ahead of the subscription /login.
Sources
The authentication, headless, and CLI reference docs on code.claude.com (fetched 2026-10-26); a Zenn community article (published 2026-10-23, user report); the QCode environment and cloud-servers pages and qcode.cc/pricing and /pay-as-you-go (fetched 2026-10-26); qcode.cc/models.
Switch unattended runs to an API key
Want the agent to keep running on a server? Put the server overseas, install Claude Code or Codex on it, and call models with your QCode key; the AI service and the cloud server share one balance. See qcode.cc/en/cloud; the cloud server interface is currently available in Chinese and English only.
Related reading
Claude Code Pay-as-you-go Packs: No Subscription, 7 Days
Buy quota once and use it within 7 days, with no subscription; extra packs add to the same key.
Claude Code Without a Claude Subscription (Oct 2026)
The login and access paths for Claude Code when you do not buy a Claude subscription.
Claude Code on a Cloud Server (Oct 2026): Setup & Access
Install Claude Code on a QCode cloud server and call models with your QCode key.
Crypto payment guide
Nine coins, 56 networks, zero platform fee for AI coding API credits.
Claude Code details on this page were checked on 2026-10-26 against the authentication, headless, and CLI reference docs on code.claude.com; QCode access and billing details were checked on 2026-10-26 against docs.qcode.cc and qcode.cc/pricing; model availability and rates follow /models. Official changes are not announced separately.