Skip to content

OAuth token expires after inactivity causing 'Token refresh failed: 400' #9111

Closed

Description

@mguttmann

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:

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:

  1. Use the existing /api/oauth/usage endpoint - 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)
  2. Ping interval: Every 60 minutes

    • First ping after 5 minutes (to let everything settle after startup)
    • Then every hour while OpenCode is running
  3. 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 debugging

Integrated 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.keepalive messages
  • Verify tokens remain valid after previously problematic timeframes

Activity

  1. github-actions commented on Jan 17, 2026

    @github-actions
    Contributor

    This issue might be a duplicate of existing issues. Please check:

    Feel free to ignore if none of these address your specific case.

  2. mguttmann commented on Jan 17, 2026

    @mguttmann
    Author

    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.

  3. softmarshmallow commented on Mar 8, 2026

    @softmarshmallow

    We're still getting this. what's the status ? - PR is closed. any ongoing work around it

  4. ilyes-i-ben commented on Mar 10, 2026

    @ilyes-i-ben

    +1 still getting this consistently.

  5. Kaaly commented on Mar 11, 2026

    @Kaaly

    Yeah bug still appear here too

  6. aykustik commented on Mar 11, 2026

    @aykustik

    +1

  7. bubnivevg commented on Mar 12, 2026

    @bubnivevg

    +1

  8. remorses commented on Mar 16, 2026

    @remorses
    Contributor
  9. AntonioMalaver commented on Mar 18, 2026

    @AntonioMalaver

    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

    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

  10. remorses commented on Mar 18, 2026

    @remorses
    Contributor

    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

  11. AntonioMalaver commented on Mar 18, 2026

    @AntonioMalaver

    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

    Thank you so much @remorses . I've created the folder .opencode/plugin , as I did not have it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions