Unattended runs · 2026-10-26

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-11-13

#Login expired#setup-token#CLAUDE_CODE_OAUTH_TOKEN#ANTHROPIC_API_KEY

What to check

1 year

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.

Login expired

Expiry error

Once the stored login expires and cannot be refreshed, each model request fails with Login expired until you sign in again.

3 days

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

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

  1. 2026-10-18

    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.

  2. 2026-10-23

    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.

  3. 2026-10-26

    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

As of 2026-11-12: if the error you see is “Failed to authenticate: this login reached its time limit and cannot be renewed. Run claude auth login to sign in again, or use claude setup-token and set CLAUDE_CODE_OAUTH_TOKEN for unattended jobs”, the stored claude.ai login has expired and cannot be refreshed, so use the steps below as written (since Claude Code 2.1.318, claude -p names claude auth login and claude setup-token in that error).

  1. 1Check 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.
  2. 2If 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.
  3. 3If the script uses --bare, set ANTHROPIC_API_KEY (created in the Claude Console) or apiKeyHelper instead; --bare does not read OAuth credentials.
  4. 4If you connect through a gateway, set ANTHROPIC_AUTH_TOKEN; it takes precedence over ANTHROPIC_API_KEY.
  5. 5Put 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 does "Failed to authenticate: this login reached its time limit and cannot be renewed" mean, and how do I recover?

As of 2026-11-12, 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. That error means the stored claude.ai login has expired and cannot be refreshed, the same kind of sign-in failure as Login expired; the fix is the two commands it names: claude auth login to sign in again, or claude setup-token to generate a one-year OAuth token and set it as CLAUDE_CODE_OAUTH_TOKEN.

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.

Not sure where to start?

Try it for a week

Small Pack: $4 for $30 of quota, valid 7 days

Use it every day

Starter: $9.00/month with $5 of quota every day — just $0.060 per $1 of quota, vs about $0.113 for the Medium Pack