Quota tracker

From sonichi/sutando · View interactive version

Quota tracker is an AI agent skill from the GitHub repository sonichi/sutando. Track Claude Code quota usage via Anthropic API rate limit headers. Shows 5h and 7d utilization, reset times, and quota status. Works with both subscription and API key auth.

How to use this skill

Skill file

Quota Tracker

Monitor your Claude Code quota in real time by intercepting Anthropic API rate limit headers.

When to Use

How It Works

A credential proxy sits between Claude Code and the Anthropic API. It reads anthropic-ratelimit-unified-* headers from every API response and writes quota state to a JSON file.

Without the proxy, the status line recorder writes the same file from Claude Code's own reports (next section). Both sources write one record; the newest write wins.

Without the proxy: the status line recorder

On by default. At every Claude session launch (the core and each pool worker), the skill's on_session_config hook (scripts/install-statusline.py) writes two entries into that session's config-dir settings.json:

If the config dir already had a status line, the installer saves it beside that settings file as quota-statusline-passthrough.json and the recorder pipes the same input to it and prints its output unchanged; otherwise the recorder prints a short line (model, 5h and 7d percentages). Turning the recorder off puts the saved status line back.

Config (manifest.json config; env overrides):

With source: "statusline", the readers (src/quota_availability.py, health-check's credential-proxy and quota-telemetry rows) treat a missing proxy as expected, not as a failure. quota-telemetry warns when the record's last_checked is more than 10 minutes (the gate's freshness window) older than the core's last recorded work.

What it gives up compared with the proxy:

Sessions call Anthropic directly

This is the default after upgrading, for new and existing installs alike, and there is no fallback to the proxy. A session that worked through the proxy may have no login of its own, because the proxy injected the credential. If so, you are asked to sign in: health-check's claude-sign-in row fails, naming the config dir, and your DM gets one message a day until it is signed in. The message carries the exact command to run in a terminal on this computer, for example:

CLAUDE_CONFIG_DIR='<config dir>' claude auth login

Follow the link it prints and sign in; the next session launch picks the login up. The check (src/claude_signed_in.py) runs claude auth status --json with the environment the direct session gets and counts only exit 0 with "loggedIn": true; a probe that times out (8 seconds) or a missing claude is unknown and sends no message. Config dirs are probed four at a time, and the daily message markers are removed after 7 days. It never changes routing and never reads a credential.

With the recorder on, Sutando's Claude sessions (the core and every pool worker) no longer go through the credential proxy. The core and worker launchers (src/agent/claude/cli/session-launch.sh) and src/startup.sh drop an ANTHROPIC_BASE_URL that points at the local proxy (http://localhost:7846, 127.0.0.1 or [::1]), in the launcher and in the session's own environment, and no longer set it. A base URL the owner set to anything else is left alone.

The plist stays in ~/Library/LaunchAgents/. The job is per user, so the intent is recorded per user, in ~/Library/Application Support/Sutando/credential-proxy-retire.json: retiring before the bootout, retired once the port stops listening. Nothing is signalled, by PID or by name. Retirement and the opt-out route hook share one per-user lock beside that file (credential-proxy-retire.lock). The route hook holds it from starting the proxy through its port wait, and never proceeds without it. It waits up to 25 seconds, which covers a retirement running in a config hook that is killed after 10. Once it holds the lock, it reads the job state again, so a retirement that just finished is undone. If it still can't take the lock, or can't open the lock file, it starts nothing and claims nothing: the launcher's own port check decides, and a session with no listener runs unrouted with a warning. Retirement re-checks every condition while holding the lock, and keeps the proxy for that launch if it can't take the lock within 3 seconds. If the port is still served after the bootout, the retirement stays unconfirmed and is logged. The next run reconciles a retiring intent: a job loaded again means the bootout didn't take (kept); an unloaded job whose port is quiet is confirmed retired. When the proxy is kept, the agent keeps running and the reason, naming the client still using it, goes to the launch log and state/quota-proxy-retire.json. python3 scripts/proxy_retire.py --dry-run shows the decision without acting. Health-check does not fail when the proxy is down and the quota record comes from the status line. While sessions go direct, src/startup.sh neither installs nor starts the proxy; it leaves a loaded job alone, since retiring it is this step's call. With the recorder on and nothing still using the proxy, a running proxy that is out of date or reports an exhausted credential is shown as ok, unused, with that state as information.

The quota in the s.space agent sidebar

On by default. The gateway bridge sends the core's quota to s.space as health.quota on its existing heartbeat (POST /v1/heartbeat, capability quota_status.v1), next to health.recovery, so the agent sidebar can show it. The block is built by src/quota_status.py from a record of the core's own login, by the rule the recovery state and the limit hold use (quota_availability.record_for_account): a status line record only for the core's current login, a proxy record only when its acct_ label is that login's (an unlabeled proxy record only while the core's account is unreadable). Another login's record sends no block. It carries only:

{"v": 1,
 "five_hour": {"used_pct": 42.0, "resets_at": 1795190400},
 "seven_day": {"used_pct": 7.0, "resets_at": 1795600000},
 "limit_state": "allowed",
 "last_checked": 1795176000}

and, when limited:

{"v": 1,
 "five_hour": {"used_pct": 100.0, "resets_at": 1795190400},
 "seven_day": {"used_pct": 7.0, "resets_at": null},
 "limit_state": "rejected",
 "resumes_at": 1795190400,
 "last_checked": 1795176000}

Times are epoch seconds. limit_state is allowed, overage or rejected. resumes_at is omitted when the state is allowed; otherwise it is present, and null when no reset time is known. A window the record does not name is left out. A window's resets_at and the block's last_checked are null when the record does not carry them. used_pct is not clamped: in overage it can exceed 100, and a consumer may clamp it for display. No account digest, other login's numbers, headers, rejection ledger or model is sent. The full heartbeat examples are contracts/quota_status.v1/heartbeat.json (allowed) and heartbeat-limited.json (limited).

python3 src/quota_status.py show prints the block as it would be sent now.

Quick Check

# Read current quota state
cat quota-state.json

Output includes:

limit_state is allowed, overage or rejected. overage means a limit window is rejected but anthropic-ratelimit-unified-overage-status is allowed/allowed_warning and no 429 has landed since: the core is serving on extra usage, so available stays true. Clients read this field rather than re-deriving it from the headers; the rule is classify_limit_state in src/quota_availability.py.

recent_rejections is a bounded ledger (newest 20) of upstream responses the proxy forwarded with a 4xx/5xx other than 401 — {ts, status, path, snippet, model, user_agent, peer_port, account} — the proxy serves every seat on the host, so the client fields are what make a rejection attributable; the probe counts only entries for this core's own model (SUTANDO_CORE_MODEL or core-runtime.json) and counts every client when that is unknown. account (on a rejection and on the record) is the acct_ label of the credential the request was sent with, present only when the proxy took it from its config dir's own keychain item; a rejection counts toward the limit state only on a record of the same account (record_rejections / boundRejections), a model-less request keeps last_request only from a record of the same account, and the recovery state and limit hold act on the record only when it is labeled with the core's signed-in account (record_for_account; while that account is unknown, only an unlabeled record, and the resulting hold is marked account_unread so it does not bind an account read later). It survives the per-response header rewrite, and health-check.py's core-request-rejections probe warns on one inside 15 minutes and fails on five inside an hour, so a credits/overage rejection that leaves every unified-status header allowed still reaches the owner.

Setup

1. Start the credential proxy

npx tsx "$SKILL_DIR/scripts/credential-proxy.ts"

This starts on port 7846 and reads OAuth credentials from macOS keychain.

2. Route Claude Code through the proxy

ANTHROPIC_BASE_URL=http://localhost:7846 claude ...

Or add to your voice agent's launchd plist:

<key>ANTHROPIC_BASE_URL</key>
<string>http://localhost:7846</string>

3. Read quota state

python3 "$SKILL_DIR/scripts/read-quota.py"           # human readable
python3 "$SKILL_DIR/scripts/read-quota.py" --json     # machine readable
python3 "$SKILL_DIR/scripts/read-quota.py" --gate     # exit 1 if exhausted, not routed, OR stale

Requirements

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.