Skip to content

feat: Multi-Account OAuth Support with Auto-Relogin #11830

Closed as not planned
Closed as not planned

Description

@mguttmann

Summary

Add support for multiple OAuth accounts per provider with automatic credential rotation and browser-based session management for expired tokens.

Motivation

Currently, OpenCode only supports a single OAuth account per provider. When that account hits rate limits, work stops. Users who have multiple accounts (e.g. personal + work) need to manually switch between them.

This feature enables:

  • Multiple accounts per provider — add as many OAuth accounts as you want
  • Automatic failover — when one account hits rate limits or auth failures, seamlessly rotate to the next
  • Browser-based auto-relogin — automatically refresh expired tokens using Puppeteer sessions
  • Per-request credential tracking — know which account is being used for each session

Changes

Backend (packages/opencode)

  • Multi-account store: Track multiple OAuth records per provider in auth/index.ts
  • Credential rotation (auth/rotating-fetch.ts): Automatically failover to next account on rate limits or auth failures
  • Browser sessions (auth/browser.ts): Puppeteer-based auto-relogin for expired tokens
  • Credential manager (auth/credential-manager.ts): Event-driven failover notifications
  • Auth context (auth/context.ts): AsyncLocalStorage for per-request credential tracking
  • Config: oauth rotation settings (cooldowns, retries, max attempts)
  • CLI: Browser session management commands
  • Server routes: Account CRUD, usage, browser session endpoints

Frontend (packages/app)

  • Provider settings: Manage accounts, set active, view usage
  • Session context tab: Show which account is active per session

SDK

  • Regenerated with all new multi-account endpoints

Related

Split from #9971 (closed) for cleaner review.

Activity

  1. github-actions commented on Feb 2, 2026

    @github-actions
    Contributor

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

    Feel free to ignore if this addresses your specific case differently or if there are additional improvements needed beyond the closed issues.

  2. mguttmann commented on Feb 2, 2026

    @mguttmann
    Author

    Thanks for flagging these!

    This issue is the clean successor to #9971 (which I closed myself to split into focused PRs). Here's how this relates to the duplicates found:

    Issue Status Comparison
    #9971 Closed by me Was a combined PR (Multi-Account + YOLO + Auto-Relogin). Split into this issue + #11831 for cleaner review
    #8591 Open Similar scope — credential rotation. This implementation goes further with Puppeteer-based auto-relogin, per-request credential tracking via AsyncLocalStorage, and a full Settings UI for account management
    #8145 Open (feature request) This is the implementation for that request. Covers round-robin load balancing via rotating-fetch.ts with configurable cooldowns and automatic failover

    The PR implementing this is #11832 — all CI checks passing ✅

  3. mguttmann commented on Feb 2, 2026

    @mguttmann
    Author
    Image
  4. mguttmann commented on Feb 2, 2026

    @mguttmann
    Author
    Image
  5. aydgn commented on Feb 4, 2026

    @aydgn

    Here is a plugin with a similar purpose: https://github.com/NoeFabris/opencode-antigravity-auth/
    It automatically switches accounts when a rate limit is reached.

  6. mguttmann commented on Feb 4, 2026

    @mguttmann
    Author

    Thanks for sharing @aydgn! That's an interesting plugin, but it solves a different problem.

    opencode-antigravity-auth is specifically for authenticating against Google's Antigravity IDE to access Gemini and Claude models through Google's quota. It's a third-party auth plugin for one specific provider.

    This issue/PR is about native multi-account support for OpenCode's built-in OAuth providers — Copilot, Codex, and any other OAuth-based provider. It adds:

    • Multiple accounts per provider with automatic rotation on rate limits
    • Puppeteer-based auto-relogin when tokens expire (no manual re-auth)
    • A Settings UI for managing accounts (add/remove, set active, view status)
    • Per-request credential tracking via AsyncLocalStorage
    • Configurable cooldowns, retries, and failover logic

    So it's more of a core infrastructure feature for OpenCode itself rather than a provider-specific plugin. Both can happily coexist though — you could use the Antigravity plugin for Google models and this PR's multi-account support for Copilot/Codex at the same time.

  7. abien commented on Feb 4, 2026

    @abien

    There might be a strategic consideration here: some providers may prefer that their subscriptions aren't used via third-party tools like OpenCode. Integrating this into the core might be perceived as conflicting with provider interests (e.g., Anthropic). Would it be better to implement this as a plugin? This gives users the choice while keeping the core engine provider-neutral.

  8. mguttmann commented on Feb 4, 2026

    @mguttmann
    Author

    Valid concern @abien, but I think it's worth noting: OpenCode already has OAuth integration for Claude, Copilot and Codex built into its core. This isn't something new — it's been there from the start, and it's what enables users to connect their existing subscriptions.

    This PR doesn't introduce OAuth support — it extends what's already built-in. It adds the ability to manage multiple accounts per provider and auto-relogin when tokens expire. If there were a philosophical problem with using provider subscriptions via OpenCode, that ship has already sailed — the entire auth infrastructure is a core feature.

    As for the "provider interests" angle — the OAuth tokens get revoked or blocked from time to time, and new workarounds are found. That's been the cycle. If the stance were that this conflicts with provider interests, the existing single-account OAuth would need to be removed too. My PR just makes the existing experience more robust by adding failover and rotation.

    That said, I do agree plugins are great for provider-specific things (like the Antigravity plugin @aydgn linked). But multi-account management for OpenCode's own built-in auth providers feels like core infrastructure to me.

  9. aydgn commented on Feb 4, 2026

    @aydgn

    I completely agree @mguttmann. Having it automatically switch to the next account would be ideal just like in the plugin I shared earlier. That would really provide the best experience.

  10. blurskye commented on Feb 18, 2026

    @blurskye

    when will this be merged :\

  11. cavanaug commented on Feb 22, 2026

    @cavanaug

    I dont know about auto switching based on usage limits. But I definitely would like a way to switch between work copilot and personal copilot. Perhaps via an environment variable OPENCODE_COPILOT_USER.

    Alternatively we could have something similar to NVIM_APPNAME where there would be a completely seperate config and state tracking. This would allow to keep the current single auth strategy and handle things with configuration/state isolation. Note: You cant do this with OPENCODE_CONFIG_DIR because that only isolates the config, it still keeps all state information in a shared location which would disallow separate auth strategy.

  12. young5lee commented on Mar 4, 2026

    @young5lee

    Is there any particular reason why it hasn’t been merged?

  13. cavanaug commented on Mar 4, 2026

    @cavanaug

    I would really like to see #8963 merged. It would be of great help and is a nice foundational building block.

  14. Heelc commented on Mar 12, 2026

    @Heelc

    I need it

  15. kqvanity commented on Mar 13, 2026

    @kqvanity

    Would great help as many plugins have to add the duplicate code for multiaccounting

  16. saeedvaziry commented on Mar 19, 2026

    @saeedvaziry
    Contributor

    made a little script while waiting for the PR

    https://github.com/saeedvaziry/oc-profile

  17. vferreirati commented on Apr 20, 2026

    @vferreirati

    Hopefully this gets merged soon 👍

  18. hildebrandt commented on Apr 20, 2026

    @hildebrandt

    Please add this feature.

  19. HicaroD commented on Apr 25, 2026

    @HicaroD

    This feature would be awesome for allowing parallel workflows running on different accounts.
    Excited for it.

  20. assigned and unassigned on Apr 26, 2026
  21. NaturalDevCR commented on May 5, 2026

    @NaturalDevCR

    I'm exactly in that position, I do have my personal account, and a work account... this would be the perfect fit.

  22. antongulin commented on May 5, 2026

    @antongulin

    They might be afraid that users will misuse the tools provided by OpenAI and other companies, not directly, but through their usage. So, it’s better to use workarounds to achieve your goals on your own risk.

  23. doomsday616 commented on Jun 7, 2026

    @doomsday616

    Adding a concrete real-world case + a source-level observation that might help scope this.

    My scenario: several GitHub Copilot Business accounts with identical model/quota. I burn one down, then rotate to the next. Because Copilot has no multi-account switch, the only way to rotate is to overwrite the stored credential (edit auth.json, which is migrated into account.json on load). This is exactly the manual switching this issue wants to eliminate.

    Source-level note — the v2 storage already has the foundation, the Copilot plugin just doesn't use it:

    account.json (version: 2, in ~/.local/share/opencode) is already a multi-account structure:

    {
      "version": 2,
      "accounts": {
        "<id1>": { "serviceID": "github-copilot", "description": "default", "credential": { ... } }
        // a second github-copilot account could live here
      },
      "active": { "github-copilot": "<id1>" }
    }

    Account.forService(serviceID) already returns an array, and create / activate / active per serviceID already exist (packages/core/src/auth.ts). The opencode.ai Console account even has full login / logout / switch CLI commands wired to it.

    What's missing for Copilot specifically: the Copilot auth plugin's authorize flow (packages/opencode/src/plugin/github-copilot/copilot.ts) returns a single credential that lands on the one fixed github-copilot key, so re-running /connect overwrites rather than adding a second account, and there's no switch exposed for it. So a meaningful chunk of this feature for OAuth providers like Copilot may be "wire the existing v2 multi-account API + a picker/switch into the provider auth flow" rather than building storage from scratch.

    A concrete breakage that rotation causes today (worth designing for): rotating the Copilot credential mid-session breaks github-copilot/gpt-5.5 with 401 input item ID does not belong to this connection, because gpt-5.5's stateful Responses API itemId handles are bound to the credential that created them, and the session still references the old ones. Filed separately as #31236. If automatic rotation/failover is implemented here, it should also invalidate (or retry-without) the stale Responses API itemId references on credential change, otherwise auto-rotation would silently break in-flight gpt-5.5 sessions.

  24. Hronom commented on Sep 25, 2026

    @Hronom

    Please add this, I have personal and work account and I wanna have easy switch between them. There is no misuse for this case

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