Skip to content

zhangweiii/auto-switch

Repository files navigation

auto-switch

Automatically switch between Claude Code and Codex accounts — always use the one with the most quota remaining.

Go License Platform

中文文档


Why auto-switch?

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 ⚠️ reactive only
Zero-overhead process replacement ❌ proxy layer
No daemon / background process
Built-in usage monitor
Token auto-sync
Single binary, no runtime deps ❌ Node.js

How it works

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.


Installation

Build from source

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 $PATH

Homebrew (macOS / Linux)

brew tap zhangweiii/tap
brew install zhangweiii/tap/auto-switch

To update:

brew update && brew upgrade zhangweiii/tap/auto-switch

Migrating from the old cask:

brew uninstall --cask zhangweiii/tap/auto-switch
brew install zhangweiii/tap/auto-switch

Your 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.


Quick start

1. Save your first account

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 personal

2. Add a second account

Log 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 work

3. Let auto-switch decide

auto-switch claude
auto-switch codex

auto-switch checks the usage of every saved account and launches claude / codex as the one with the most quota left.

4. Pass arguments through

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"

5. Force a specific account

auto-switch --account work claude
auto-switch --account work codex

Seamless experience — alias claude

The 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 bash

From 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

Commands

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 work
  • auto-switch list --provider codex
  • auto-switch status --provider codex
  • auto-switch remove --provider codex work

auto-switch list

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

auto-switch status

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

Configuration

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

Token auto-sync

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.json if they differ.
  • On auto-switch claude and auto-switch list, it proactively refreshes any saved Claude account whose token expires within the next hour using that account's refresh_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.


Roadmap

  • Phase 1 — Claude Code multi-account switching
  • Phase 2 — OpenAI Codex support
  • Shell completion (zsh, bash, fish)
  • Homebrew formula (via zhangweiii/homebrew-tap)

License

MIT

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors