Automatically switch between Claude Code and Codex accounts — always use the one with the most quota remaining.
If you use multiple Claude Code or Codex subscriptions (personal, work, team…), you've probably hit the 5-hour rate limit mid-session and waited for it to reset. auto-switch eliminates that wait by transparently routing each claude / codex invocation to the account with the most headroom.
| Feature | auto-switch | Manual switching | CCS (proxy) |
|---|---|---|---|
| Auto-selects least-used account | ✅ | ❌ | |
| Zero-overhead process replacement | ✅ | ✅ | ❌ proxy layer |
| No daemon / background process | ✅ | ✅ | ❌ |
| Built-in usage monitor | ✅ | ❌ | ❌ |
| Token auto-sync | ✅ | ❌ | ❌ |
| Single binary, no runtime deps | ✅ | ✅ | ❌ Node.js |
auto-switch claude → fetch usage for all accounts (parallel, cached 5 min)
→ score = 5h_util × 0.7 + 7d_util × 0.3
→ pick lowest score
→ write credentials (Keychain + file)
→ syscall.Exec("claude", args...) ← becomes claude, zero wrapper
The final step uses syscall.Exec to replace the current process with claude. There is no wrapper process — signals, stdin, stdout, and TTY all behave exactly as if you ran claude directly.
git clone https://github.com/zhangweiii/auto-switch
cd auto-switch
go build -o auto-switch .
cp auto-switch ~/.local/bin/ # or any directory in $PATHbrew tap zhangweiii/tap
brew install zhangweiii/tap/auto-switchTo update:
brew update && brew upgrade zhangweiii/tap/auto-switchMigrating from the old cask:
brew uninstall --cask zhangweiii/tap/auto-switch
brew install zhangweiii/tap/auto-switchYour saved accounts and cache live under ~/.config/auto-switch and system credentials storage, so migrating from the cask to the formula does not remove them.
Make sure you are already logged in to the target CLI, then run:
auto-switch login
# or skip the prompt with a flag:
auto-switch login --alias personal
auto-switch login --provider codex --alias personalLog out from that CLI, then log in with your second account. Then save it:
auto-switch login --alias work
auto-switch login --provider codex --alias workauto-switch claude
auto-switch codexauto-switch checks the usage of every saved account and launches claude / codex as the one with the most quota left.
All arguments are forwarded verbatim to the target CLI:
auto-switch claude --continue
auto-switch claude -p "explain this file"
auto-switch claude --model claude-opus-4-6
auto-switch --account work codex
auto-switch codex exec "review this repo"auto-switch --account work claude
auto-switch --account work codexThe best way to use auto-switch is to replace your claude command entirely. Add one line to your shell config:
zsh (~/.zshrc):
alias claude='auto-switch claude'bash (~/.bashrc or ~/.bash_profile):
alias claude='auto-switch claude'fish (~/.config/fish/config.fish):
alias claude 'auto-switch claude'Reload your shell to activate it:
source ~/.zshrc # or ~/.bashrc for bashFrom now on just type claude as always — auto-switch silently picks the best account and gets out of the way:
claude # auto-selects least-used account
claude --continue # args pass through unchanged
claude -p "review PR" # non-interactive mode works too| Command | Description |
|---|---|
auto-switch login [--provider <name>] [--alias <name>] |
Save the currently logged-in Claude Code or Codex account |
auto-switch claude [args...] |
Switch to least-used account and launch claude |
auto-switch codex [args...] |
Switch to least-used account and launch codex |
auto-switch list |
Show all accounts with live usage bars |
auto-switch status |
Detailed usage view with reset countdowns |
auto-switch remove <alias> |
Delete a saved account |
Provider-aware management:
auto-switch login --provider codex --alias workauto-switch list --provider codexauto-switch status --provider codexauto-switch remove --provider codex work
Claude Code accounts (2)
Alias Email 5h window 7d window
───────────────────────────────────────────────────────────────────────────────────────────
* personal user1@example.com ████████░░ 67% ↺1h23m ███░░░░░ 30% ↺5d12h
work user2@example.com ░░░░░░░░░░ 5% ↺3h10m █░░░░░░░ 10% ↺5d12h
* active account refreshed at 14:32:05
Claude Code usage (2026-03-07 14:32:05)
────────────────────────────────────────────────────────────
personal (user1@example.com) [active]
5h window: ████████████████░░░░ 67.0% resets in 1h23m
7d window: ██████░░░░░░░░░░░░░░ 30.0% resets in 5d12h
work (user2@example.com)
5h window: █░░░░░░░░░░░░░░░░░░░ 5.0% resets in 3h10m
7d window: ██░░░░░░░░░░░░░░░░░░ 10.0% resets in 5d12h
| Path | Description |
|---|---|
~/.config/auto-switch/accounts.json |
Account metadata (mode 0600) |
~/.config/auto-switch/usage-cache.json |
Usage cache (5-min TTL) |
macOS Keychain (Claude Code-credentials) |
OAuth tokens |
~/.claude/.credentials.json |
OAuth token fallback (Linux) |
Cache behaviour:
- Successful responses cached for 5 minutes
- Errors never cached — next call retries immediately
Claude Code silently rotates its OAuth token from time to time. auto-switch keeps this in sync in two ways:
- On every invocation, it compares the currently active Claude Code token in Keychain / credentials file with the stored value and updates
accounts.jsonif they differ. - On
auto-switch claudeandauto-switch list, it proactively refreshes any saved Claude account whose token expires within the next hour using that account'srefresh_token. Tokens with plenty of remaining validity are left untouched to avoid racing with Claude Code's own background refresh rotation.
You never need to re-run login just because a token was rotated.
For Codex, auto-switch stores each account in its own isolated CODEX_HOME under ~/.config/auto-switch/codex/<alias>. Usage is inferred from the latest rate_limits data emitted into that account's local session logs, which lets auto-switch codex pick the least-used account without overwriting your main ~/.codex.
- Phase 1 — Claude Code multi-account switching
- Phase 2 — OpenAI Codex support
- Shell completion (zsh, bash, fish)
- Homebrew formula (via zhangweiii/homebrew-tap)
MIT