Repository navigation
Description
Problem
Users report that after several hours of inactivity (especially after sleep/overnight), the Anthropic OAuth token expires and the refresh fails with a 400 error:
Error: Token refresh failed: 400
This forces users to re-login every morning or after periods of inactivity.
Related Issues:
- Claude subscription token expires after a period of time #6559 - Claude subscription token expires after a period of time
- Getting "Unauthorized: token expired" during conversation #4992 - Getting "Unauthorized: token expired" during conversation
Root Cause
Anthropic's OAuth refresh tokens appear to have a limited lifetime. When the access token expires and OpenCode attempts to refresh it, the refresh token itself may have already been invalidated by Anthropic's servers after extended periods of inactivity.
The current implementation only refreshes tokens reactively (when a request fails), rather than proactively keeping them alive.
Proposed Solution
Implement a token keep-alive mechanism that periodically pings all logged-in Anthropic OAuth accounts to prevent token expiration:
-
Use the existing
/api/oauth/usageendpoint - This endpoint:- Already exists in the codebase (
Auth.OAuthPool.fetchAnthropicUsage) - Requires authentication (keeps the token active)
- Does NOT consume any tokens or context
- Returns usage statistics (bonus: could be useful for UI)
- Already exists in the codebase (
-
Ping interval: Every 60 minutes
- First ping after 5 minutes (to let everything settle after startup)
- Then every hour while OpenCode is running
-
Multi-account support: Ping ALL logged-in Anthropic OAuth accounts
- Users with multiple accounts (Account 1, 2, 3, etc.) will have all accounts kept alive
- Only affects Anthropic provider (other providers like Copilot, OpenAI are not affected)
Implementation
New module: packages/opencode/src/auth/keepalive.ts
// Pings all Anthropic OAuth accounts every hour
// Uses /api/oauth/usage endpoint (no token cost)
// Logs success/failure for debuggingIntegrated into InstanceBootstrap() so it starts automatically when OpenCode runs.
Benefits
- No more morning re-logins - Tokens stay fresh as long as OpenCode is running
- Zero cost - Uses usage endpoint, no tokens consumed
- Multi-account friendly - All accounts stay active
- Non-intrusive - Runs in background, logged for debugging
- Automatic - No user action required
Testing
- Run OpenCode for extended periods
- Monitor logs for
auth.keepalivemessages - Verify tokens remain valid after previously problematic timeframes
Activity
github-actions commentedon Jan 17, 2026
This issue might be a duplicate of existing issues. Please check:
Feel free to ignore if none of these address your specific case.
mguttmann commentedon Jan 17, 2026
Thanks for flagging the related issues! This issue is actually intended to fix the problems described in #6559 and #4992.
Those issues describe the problem (token expiration after inactivity), while this issue proposes a concrete solution: a keep-alive mechanism that prevents token expiration by periodically pinging the Anthropic API.
The implementation is ready in PR #9112.
softmarshmallow commentedon Mar 8, 2026
We're still getting this. what's the status ? - PR is closed. any ongoing work around it
ilyes-i-ben commentedon Mar 10, 2026
+1 still getting this consistently.
Kaaly commentedon Mar 11, 2026
Yeah bug still appear here too
aykustik commentedon Mar 11, 2026
+1
bubnivevg commentedon Mar 12, 2026
+1
remorses commentedon Mar 16, 2026
Working Anthropic OAuth plugin here: https://github.com/remorses/opencode-config/blob/main/plugins/anthropic-auth.ts
The commit to fix this issue: remorses/opencode-config@58ca765
AntonioMalaver commentedon Mar 18, 2026
Could you please update Opencode so this feature is available to everyone? I do not know how to use the plugin you are linking. Thank you so much in advance
remorses commentedon Mar 18, 2026
You have to copy that file and add it in your .opencode/plugin folder. Anthropic lawyers asked opencode to not bundle this plugin by default and they even had to delete the repo so you have to add this plugin yourself
AntonioMalaver commentedon Mar 18, 2026
Thank you so much @remorses . I've created the folder .opencode/plugin , as I did not have it.