Help us improve
Share bugs, ideas, or general feedback.
Auto-switch sub-agent models based on real-time usage percentage
npx claudepluginhub jung-wan-kim/usage-gateAutomatically switches sub-agent models from opus to sonnet based on real-time Anthropic Usage API percentages. Includes a persistent status line showing gate status, thresholds, and fallback models.
Share bugs, ideas, or general feedback.
A Claude Code plugin that automatically switches sub-agent models from opus to sonnet/haiku when your Anthropic API usage approaches rate limits. Fully configurable via interactive setup command.
You submit a prompt
↓
usage-tracker.js fetches real-time usage from Anthropic API
→ Caches result (60s TTL)
→ Shows usage warning if approaching threshold
↓
Claude wants to spawn a sub-agent (Task tool)
↓
model-gate.js checks cached usage
→ Usage < threshold → ALLOW (opus OK)
→ Usage >= threshold → AUTO-SWITCH model via updatedInput
↓
Task executes with fallback model (sonnet/haiku) — no retry needed
claude plugin add /path/to/usage-gate
Or from GitHub:
claude plugin add github:jung-wankim/usage-gate
Use the interactive setup command:
/usage-gate:setup
This will guide you through:
All settings are saved to ~/.claude/settings.json automatically.
You can also manually set thresholds via environment variables in your Claude Code settings:
// ~/.claude/settings.json
{
"env": {
"CLAUDE_USAGE_GATE_ENABLED": "true",
"CLAUDE_OPUS_LIMIT_5H": "90",
"CLAUDE_FALLBACK_MODEL_5H": "sonnet",
"CLAUDE_OPUS_LIMIT_7D": "90",
"CLAUDE_FALLBACK_MODEL_7D": "sonnet",
"CLAUDE_USAGE_CACHE_TTL": "60"
}
}
| Variable | Default | Description |
|---|---|---|
CLAUDE_USAGE_GATE_ENABLED | true | Enable/disable the usage gate |
CLAUDE_OPUS_LIMIT_5H | 90 | 5-hour usage threshold (%) |
CLAUDE_FALLBACK_MODEL_5H | sonnet | Model to use when 5H limit exceeded (sonnet or haiku) |
CLAUDE_OPUS_LIMIT_7D | 90 | 7-day usage threshold (%) |
CLAUDE_FALLBACK_MODEL_7D | sonnet | Model to use when 7D limit exceeded (sonnet or haiku) |
CLAUDE_USAGE_CACHE_TTL | 60 | API cache TTL (seconds) |
Conservative (Recommended - Early switch, maximize cost savings):
Balanced:
Aggressive (Use opus as much as possible):
Very Aggressive (Almost no limits):
usage-gate includes a persistent status line that displays gate status under the Claude Code prompt.
// ~/.claude/settings.json
{
"statusLine": {
"type": "command",
"command": "node ~/.claude/plugins/cache/usage-gate/usage-gate/1.0.0/statusline/index.js"
}
}
Run alongside another statusline plugin by setting USAGE_GATE_CHAIN_CMD:
// ~/.claude/settings.json
{
"env": {
"USAGE_GATE_CHAIN_CMD": "node ~/.claude/plugins/cache/claude-dashboard/claude-dashboard/1.3.0/dist/index.js"
},
"statusLine": {
"type": "command",
"command": "node ~/.claude/plugins/cache/usage-gate/usage-gate/1.0.0/statusline/index.js"
}
}
Output example (chain mode):
🤖 Opus │ ░░░░░ │ 30% │ 60K/200K │ $1.25 │ 5h: 26% │ 7d: 80% ← claude-dashboard
⏱ 3분 │ 🔥 2/min ← claude-dashboard
Gate ACTIVE │ 5h 25%/70% │ 7d 80%/70% →S ← usage-gate
| Display | Meaning |
|---|---|
Gate standby (green) | Gate enabled, usage below all thresholds |
Gate ACTIVE (orange/red) | Usage exceeded a threshold, auto-switching active |
Gate OFF | Gate disabled via settings |
→S | Fallback to Sonnet |
→H | Fallback to Haiku |
/usage-gate:setup - Interactive configuration - Set thresholds, fallback models, and enable/disable the gateNo additional dependencies needed. Uses only Node.js built-in modules.
The plugin retrieves your OAuth token automatically:
ANTHROPIC_ACCESS_TOKEN (all platforms)Claude Code-credentials (automatic)~/.claude/credentials.json or ~/.config/claude/credentials.json (Linux)When usage exceeds the threshold, the hook automatically injects the fallback model into the Task parameters using updatedInput. No blocking, no retry — the model is switched transparently in a single call.