Using a long-lived Claude Code OAuth token

In Claude Code, when logging in via your Claude.ai subscription, it normally expires automatically after a few weeks, and after that another /login is required. As of writing, the maximum duration seems to be 4 weeks.

How to check when your /login expires

Look for claudeAiOauth.refreshTokenExpiresAt inside ~/.claude/.credentials.json.

jq -r '.claudeAiOauth.refreshTokenExpiresAt / 1000 | floor | strflocaltime("%Y-%m-%d %H:%M:%S %Z")' ~/.claude/.credentials.json

One solution is to generate a long-lived token, which lasts for 1 year. The trade-off is that a long-lived token can't do everything a normal login can:

Normal /loginLong-lived token
LifetimeUp to 4 weeksUp to 1 year
Remote controlYesNo
Connectors from Claude.aiYesNo

Creating a token

claude setup-token

Copy the token.

Set up Claude Code to use the long-lived token

  1. Edit ~/.claude/settings.json and put a CLAUDE_CODE_OAUTH_TOKEN inside "env":

    {
      "env": {
        "CLAUDE_CODE_OAUTH_TOKEN": "<paste your token>"
      }
    }
  2. Update ~/.claude.json and set "hasCompletedOnboarding" to true:

    jq '.hasCompletedOnboarding = true' ~/.claude.json > /tmp/claude.json.tmp && mv /tmp/claude.json.tmp ~/.claude.json

    Without it, Claude Code will prompt you to log in via a subscription. (See issue #8938 for details.)