anthropic-multi-auth
v0.1.1
Published
Multi-account Anthropic auth plugin for OpenCode with quota-aware load balancing
Maintainers
Readme
anthropic-multi-auth
Multi-account Anthropic auth plugin for OpenCode with quota-aware load balancing.
Features
- Multi-account support: Add multiple Anthropic accounts (Claude Pro/Max)
- Quota-aware load balancing: Automatically selects the account with the lowest quota utilization at session start
- Sticky sessions: Stays on the same account throughout a session for consistency
- Automatic failover: On 429 rate limit errors, automatically switches to the next available account
- Token refresh: Automatically refreshes OAuth tokens when expired
- Account management: List and remove accounts via interactive CLI
- Usage inspection: View per-account Anthropic quota utilization from the login menu
- First-time migration: Automatically imports accounts from OpenAuth if available
Installation
Add the plugin to your OpenCode configuration in opencode.json:
{
"plugins": ["anthropic-multi-auth"]
}Setup
Adding Accounts
Run the OpenCode auth flow to add accounts:
opencode auth loginWhen prompted, select "Claude Pro/Max" to authorize a new account. After successful authorization, you'll be asked if you want to add another account. Repeat this process to add multiple Anthropic accounts.
Managing Accounts
To view and manage your configured accounts:
opencode auth loginSelect "Manage Accounts" to see your accounts and remove ones you no longer need.
Viewing Account Usage
To view current utilization for all configured accounts:
opencode auth loginSelect "View Account Usage" to print each account's status and usage utilization.
How It Works
Account Selection
When a session starts, the plugin:
- Loads your saved accounts from
~/.config/opencode/anthropic-accounts.json - Checks the quota usage API for each account
- Selects the account with the lowest utilization
- Marks it as the active account for the session
Session Stickiness
Once an account is selected at session start, it remains active throughout the session. This ensures consistent behavior within a single session.
Rate Limit Handling
If an API request returns a 429 (Too Many Requests) response:
- The plugin waits the specified retry period
- Automatically switches to the next available account
- Retries the request with the new account
- If all accounts are rate-limited, returns an error with the shortest wait time
Token Refresh
When tokens expire (checked via the expires timestamp):
- The plugin automatically refreshes the OAuth token
- Saves the new credentials to storage
- Syncs with OpenAuth to maintain consistency
Account Storage
Accounts are stored at:
~/.config/opencode/anthropic-accounts.jsonEach account includes:
access: OAuth access tokenrefresh: OAuth refresh tokenexpires: Token expiration timestamp (milliseconds)addedAt: When the account was addedenabled: Whether this account is activeemail: (optional) User email from OAuthlabel: (optional) Friendly name for the account
Comparison with Builtin Auth
This plugin extends the OpenCode builtin opencode-anthropic-auth with:
- Multi-account support (builtin only supports one account)
- Quota-aware account selection (builtin doesn't consider quota)
- Automatic failover on rate limits (builtin doesn't switch accounts)
If no accounts are configured, the plugin falls back to the builtin auth plugin, so the plugin is fully backward compatible.
Troubleshooting
No accounts configured
Symptom: Plugin doesn't seem to be working.
Solution: Run opencode auth login and select "Claude Pro/Max" to add at least one account.
All accounts rate-limited
Symptom: Error: "All accounts rate-limited. Shortest wait: Xs."
Solution: Wait the specified time before trying again. If this happens frequently, consider adding more accounts.
Token refresh failures
Symptom: Error during session with 401/403 status codes.
Solution:
- Verify your refresh tokens haven't been revoked
- Run
opencode auth login→ "Manage Accounts" to remove and re-add affected accounts - Make sure your internet connection is stable
Plugin not loading
Symptom: Plugin doesn't appear to be active.
Solution:
- Check
opencode.jsonhas the correct plugin name:"anthropic-multi-auth" - Run
bun install(ornpm install) to ensure dependencies are installed - Check OpenCode version is compatible (requires
@opencode-ai/plugin@^0.4.45)
License
MIT
