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.jsonOne 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 /login | Long-lived token | |
|---|---|---|
| Lifetime | Up to 4 weeks | Up to 1 year |
| Remote control | Yes | No |
| Connectors from Claude.ai | Yes | No |
Creating a token
claude setup-tokenCopy the token.
Set up Claude Code to use the long-lived token
Edit
~/.claude/settings.jsonand put aCLAUDE_CODE_OAUTH_TOKENinside"env":{ "env": { "CLAUDE_CODE_OAUTH_TOKEN": "<paste your token>" } }Update
~/.claude.jsonand set"hasCompletedOnboarding"totrue:jq '.hasCompletedOnboarding = true' ~/.claude.json > /tmp/claude.json.tmp && mv /tmp/claude.json.tmp ~/.claude.jsonWithout it, Claude Code will prompt you to log in via a subscription. (See issue #8938 for details.)