oc-auth-switcher
v0.2.1
Published
OpenCode auth plugin for multi-account Anthropic Claude Max rotation with automatic failover.
Maintainers
Readme
oc-auth-switcher
OpenCode auth plugin for multi-account Anthropic Claude Max rotation with automatic failover. Captures rate-limit headers in real-time and switches accounts before you hit usage limits.
How It Works
This plugin registers as the Anthropic auth provider for OpenCode. It uses @ex-machina/opencode-anthropic-auth as a library for request/response transformation (billing header, system prompt sanitization, tool name prefixing) and adds:
- Multi-account rotation — maintains a pool of OAuth accounts and selects the best one on each request
- Real-time metric capture — reads Anthropic's rate-limit response headers on every API call (no manual pinging needed)
- Automatic failover — when any utilization metric exceeds the threshold (default 95%), the next request automatically uses a different account
Setup
1. Add the plugin to your opencode.json
{
"plugin": ["oc-auth-switcher@latest"]
}This is the only auth plugin you need for Anthropic. Do not also list @ex-machina/opencode-anthropic-auth — it is included as a dependency.
2. Add accounts
# Add your primary account (first account = always preferred)
oc-auth-switcher add primary
# Add fallback accounts
oc-auth-switcher add fallback-1
oc-auth-switcher add fallback-2Each add command runs an OAuth flow — you'll be given a URL to open in your browser and prompted to paste the callback.
3. Use OpenCode normally
Metrics update automatically on every API request. When the primary account exceeds the utilization threshold, the plugin switches to the next available fallback. When the primary recovers, it switches back.
CLI Commands
oc-auth-switcher <command> [options]| Command | Description |
|---------|-------------|
| add [name] | Add a new account via OAuth |
| reauth <name> | Re-authenticate an existing account |
| usage [--watch] | Show utilization dashboard with progress bars |
| config [options] | View/modify thresholds and check interval |
| switch <name> | Manually switch to a specific account |
| status | Show current active account and rotation state |
| remove <name> | Remove an account from the pool |
Configuration
# Set uniform threshold (default: 95%)
oc-auth-switcher config --threshold 0.95
# Set per-metric thresholds (5h, 7d, 7d-sonnet)
oc-auth-switcher config --thresholds 90,80,70
# Set primary recovery check interval (default: 60 min)
oc-auth-switcher config --interval 30
# Reset to defaults
oc-auth-switcher config --resetData Files
| File | Location | Purpose |
|------|----------|---------|
| Accounts | ~/.config/opencode/auth-switcher-accounts.json | OAuth token pool |
| State | ~/.config/opencode/auth-switcher-state.json | Usage metrics, active account, config |
Both files use atomic writes with .bak fallback for crash safety.
Rotation Algorithm
- The first account in the pool is always "primary" (preferred)
- When the primary exceeds any utilization threshold, switch to the first fallback under threshold
- If all fallbacks are also over threshold, pick the one with the lowest utilization score
- When on a fallback, periodically check if the primary has recovered (configurable interval)
- Auth failures trigger a 1-hour cooldown per account
- Token refresh is handled automatically with retry and fallback to other accounts
Running the CLI
When installed via npm (as part of the OpenCode plugin):
npx oc-auth-switcher <command>From the project directory (development):
bun dist/cli.js <command>Building from Source
bun install
bun run buildPublishing to npm
npm login
npm publishThe prepublishOnly script automatically runs bun run build before publishing.
Versioning
# Patch (bug fixes)
npm version patch && npm publish && git push --follow-tags
# Minor (new features)
npm version minor && npm publish && git push --follow-tags
# Major (breaking changes)
npm version major && npm publish && git push --follow-tagsUpdating the ex-machina dependency
bun update @ex-machina/opencode-anthropic-auth
bun run build
# Test, then publishLicense
MIT
