Auto-rotate API keys when rate-limited. Quota Switch manages multi-provider API key vaults, injects fresh keys into CLI tool configs (Claude Code, Codex, OpenAI), and automatically switches keys when your proxy provider hits your usage cap.
- Key Vault — store keys from any provider (OpenAI, Anthropic, Google, Groq, DeepSeek, etc.)
- Auto-Rotation —
runmonitors known tools for 402/429/rate-limit errors, marks the key, and retries with the next available key - Config Injection — writes the best available key and gateway URL into tool config files (Claude Code, Codex, OpenAI CLI)
- Gateway Probing —
check/syncprobes live rate-limit headers from your proxy - Least-Recently-Used Selection — picks the key that was used longest ago
- Shared Key State — duplicate keys across entries stay in sync (cooldown, error count, enabled/disabled)
- JSON Import — bulk import keys from a
keys.jsonfile; auto-imported on first run - Environment Import — pull keys from
OPENAI_API_KEY,ANTHROPIC_API_KEY, etc.
Requires Python 3.11+ (stdlib only — no pip dependencies).
git clone https://github.com/yourusername/quota-switch.git
cd quota-switch
pip install .
quotaswitch --helpThis installs the quotaswitch command to your PATH via the [project.scripts] entry point.
git clone https://github.com/yourusername/quota-switch.git
cd quota-switch
pip install -e .Use the included quotaswitch.bat launcher (Windows) or add the repo directory to PATH:
# Clone, then add to PATH
set PATH=%PATH%;C:\path\to\quota-switch
quotaswitch --help# Add keys
quotaswitch add openai sk-xxx --gateway https://my-proxy.com/v1
quotaswitch add anthropic sk-ant-xxx
# List available keys
quotaswitch list
# Inject the best key into Claude Code config
quotaswitch inject claude
# Run a command with auto-rotation on rate-limit
quotaswitch run --monitor -- claude
# Check live rate-limit status for all keys
quotaswitch sync| Command | Description |
|---|---|
add |
Add a new API key |
list |
List all keys with status |
status |
Detailed key information |
select |
Print best available key ID or value |
run |
Run command with key injection and auto-rotation |
check |
Probe gateway for live rate-limit data |
sync |
Check all keys (alias for check --all) |
ratelimit |
Manually mark a key as rate-limited |
available / unmark |
Clear cooldown, mark key available |
inject |
Write best key and gateway into tool config |
disable / enable |
Toggle key state |
remove |
Remove key from vault |
config |
View or update vault configuration |
import-env |
Import keys from environment variables |
import |
Import keys from a JSON file |
Monitored tool runs (default for known tools): injects the best key into the tool config, streams output, scans for rate-limit patterns, marks the limited key, injects the next key, and restarts the command.
quotaswitch run -- claude
quotaswitch run -- codexExplicit monitor (--monitor): enables the same output scanner for commands that are not recognized as supported tools.
quotaswitch run --monitor -- codex run 'write tests'
quotaswitch run --monitor -- claudePassthrough (--passthrough): injects the best key into the tool config, then runs your command without output scanning. If the command exits non-zero, rotates to the next key and retries.
quotaswitch run --passthrough -- claudeWrites the best available key into a tool's config file. Supported tools:
| Tool | Key File | URL File |
|---|---|---|
claude |
~/.claude/settings.json (env.ANTHROPIC_API_KEY) |
same file (env.ANTHROPIC_BASE_URL) |
codex |
~/.codex/auth.json (OPENAI_API_KEY) |
~/.codex/config.toml ([model_providers.*].base_url) |
openai |
~/.config/openai/config.json (api_key) |
same file (base_url) |
quotaswitch inject claude
quotaswitch inject codex --provider openaiKeys are stored in ~/.apikey/vault.json. Override with:
export QUOTASWITCH_VAULT=/path/to/vault.json
quotaswitch --vault /path/to/vault.json ...| Setting | Default | Description |
|---|---|---|
cooldown_seconds |
3600 | Cooldown duration when rate-limited |
max_retries |
3 | Auto-retry attempts before giving up |
max_errors |
5 | Auto-disable key after this many errors |
quotaswitch config --cooldown 7200 --max-retries 5Place a keys.json in the project directory. Format:
[
{
"provider": "openai",
"key": "sk-...",
"model": "gpt-4",
"gateway": "https://my-proxy.com/v1"
}
]Auto-imported on first vault creation. Manual import:
quotaswitch import keys.json
quotaswitch import keys.json --update # update existing keys by IDexport OPENAI_API_KEY=sk-...
export ANTHROPIC_API_KEY=sk-ant-...
quotaswitch import-env- Key selection sorts available keys by
last_used(ascending) — LRU policy. - Config injection writes the selected key + gateway into the tool's config file(s), preserving all other config fields.
- Monitor mode pipes output through a rolling scanner that detects 402, 429, 529, and rate-limit error patterns even when terminal UI output is not newline-delimited. On match, the key is marked with a cooldown, the next key is selected + injected, and the wrapped command is restarted so it inherits the new environment.
- Passthrough mode injects on start, then rotates on non-zero exit.
- Error parsing handles multiple formats: proxy reset time messages,
Retry-Afterheaders, and duration patterns.
MIT