Expand description
Read-only, normalized quota/usage snapshots for the four OAuth providers the multi-account broker manages: Claude (Anthropic), ChatGPT/Codex, Kimi Code and Grok (xAI).
Design rules (goal plan G3):
- Read-only. Every adapter is a single pinned
GET. Nothing here can redeem a banked reset, purchase credits, or generate inference. - Caller supplies the resolved token.
fetch_usagetakes the provider and the already-resolved access token for exactly that(provider, account)so the credential broker invokes it behind its boundary. This module never readsauth.json, never refreshes, never sees a refresh token. - Pinned destinations. URLs are constants. The only override is a
loopback-only test seam (
UsageFetchOptions::endpoint_override) that is rejected for any non-loopback host, so a bearer token can never be coaxed toward an attacker-chosen destination. - No redirects, bounded time and body.
UsageClientis built with aredirect::Policy::none(); 3xx is an error. Bodies are streamed into a capped buffer. - Sanitized errors.
UsageErrornever contains the token, the URL query, or a byte of an upstream body. Parse notes use fixed vocabulary — never raw upstream key names or values. - Unknown is not capacity.
used_percentis a tagged enum (UsedPercent); missing, null, non-numeric and out-of-range values are explicitlyUnknown { reason }. Reset timestamps are provider-authoritative (RFC 3339 or unix seconds → epoch ms) orNone— never inferred fromnow + window. - Dynamic windows. Codex
primary_windowcan be weekly andsecondary_windowcan benull; durations come fromlimit_window_seconds, never from a hard-coded “primary = 5h”.
Schema provenance (no network was used):
- Codex field set verified against the string table of the locally installed
official
codexbinary (RateLimitWindowSnapshot,RateLimitStatusPayload,RateLimitResetCreditsSummary/Details,/wham/usage,/wham/rate-limit-reset-credits). - Kimi field set verified against the official
kimiCLI bundle (managed-usage.ts:usage,limits[].window/detail,boosterWallet). - Anthropic matches the previously shipped
synaps statusreader. - Grok
/v1/billing?format=creditsfollows the QuotaKit-documented shape (config.creditUsagePercent,onDemandUsed.val / onDemandCap.val,config.currentPeriod.end/config.billingPeriodEnd,billingPeriodMinutes). Unverified against a live account; anything else yields an explicitly unknown window.
Structs§
- Account
Usage Entry - Banked
Reset Credit - Banked
Resets - Codex banked weekly-reset inventory (read-only view).
- Credit
Balance - Billing/credit balance. This is money-like inventory, not quota.
- Model
Availability - Per-model availability. Sources, in precedence order:
- Usage
Client - HTTP client with the policy this module requires: no redirects, bounded connect/total timeouts, built-in roots. Construct once and reuse.
- Usage
Error Summary - Secret-free error row.
- Usage
Fetch Options - Options for
fetch_usage. Production callers useDefault. - Usage
Report synaps status --json/ broker listing payload.- Usage
Snapshot - Normalized, secret-free usage observation for one
(provider, account). - Usage
Window - One quota window as reported by the provider.
Enums§
- Account
Usage Outcome - One account’s outcome inside a
UsageReport: partial failures never discard the accounts that succeeded. - Availability
- Three-state model availability derived from model-scoped windows.
- Reset
Kind - What kind of clock a
reset_atdescribes. - Usage
Error - Usage failures.
Display/Serializenever include a token, a URL query, or any upstream body bytes. - Used
Percent - Fraction of a window consumed. Never a bare number: the parser must decide
Valid(finite,0.0..=100.0) orUnknownwith a fixed-vocabulary reason. - Window
Scope - What a window applies to.
Constants§
- ANTHROPIC_
USAGE_ URL - Anthropic OAuth usage summary (same endpoint the broker’s typed op uses).
- CODEX_
RESET_ CREDITS_ URL - ChatGPT/Codex banked-reset inventory (
GETonly — the sibling/consumeendpoint redeems a reset and is deliberately NOT reachable from here). - CODEX_
USAGE_ URL - ChatGPT/Codex rate-limit usage (
GET, bearer +chatgpt-account-id). - GROK_
BILLING_ URL - Grok Build billing (credits view).
- KIMI_
USAGE_ URL - Kimi Code managed usage.
- OVERRUN_
PERCENT_ MAX - Largest reported percentage still read as “window overrun” (exhausted)
rather than garbage. Above 10x the value is more likely a unit error
(basis points, a raw counter) than a real overrun, so it stays unknown;
both fail closed. Shared with
quota_policy::WindowLimit::verdict. - SCHEMA_
VERSION - Snapshot JSON schema version (bump on incompatible field changes).
- USAGE_
CONNECT_ TIMEOUT - TCP/TLS connect budget.
- USAGE_
MAX_ BODY_ BYTES - Hard cap on a buffered usage body. Real payloads are a few KiB.
- USAGE_
REQUEST_ TIMEOUT - Total time budget for one usage request.
Functions§
- duration_
label 18000→5h,604800→7d,90→90s.- fetch_
usage - Read-only usage fetch for one
(provider, account)using the caller’s already-resolved access token. See the module docs for the guarantees. - parse_
anthropic_ usage - Parse
GET /api/oauth/usage. Every object value carryingutilizationorresets_atis a window;nullwindows are skipped with a note. - parse_
codex_ reset_ credits - Parse
GET /wham/rate-limit-reset-credits(inventory detail). - parse_
codex_ usage - Parse
GET /wham/usage.expected_account_idis the id derived from the bearer token; a differing responseaccount_idfails closed. - parse_
grok_ billing - Parse
GET /v1/billing?format=credits. - parse_
kimi_ usage - Parse
GET /coding/v1/usages(official CLIparseManagedUsagePayload). - supported_
usage_ providers - Providers with a usage adapter, in display order.
- supports_
usage - Whether a usage adapter exists for the provider. Copilot and Gemini expose no quota endpoint we can read, so they are honestly unsupported.