Provider quota
One status per connected provider: health, quota, 30-day spend, default model and key health. Quota comes first from the replies OpenProgram already receives; an endpoint is read only on a few events, never on a timer.
A provider is connected when it is enabled or holds a credential: one in OpenProgram's store, its CLI on PATH, the Codex CLI's auth file, or a satisfied cloud chain.
A provider that signs in only through OpenProgram, such as Grok Subscription, counts once its sign-in is stored, so a fresh install lists none: the Providers page shows Connect a model provider and openprogram providers status prints "No connected providers".
| Part | Where |
|---|---|
| Model and store | openprogram/models/usage/quota.py |
| Endpoint readers | openprogram/models/usage/quota_fetchers/, one module per source |
| Status | openprogram/models/usage/provider_status.py, wire types generated to apps/web/lib/settings/provider-status.generated.ts |
| Route | GET /api/providers/status, POST /api/providers/status/refresh, WebSocket frame provider_quota |
| CLI | openprogram providers status [--refresh] [--json]: asks the running Worker for the route's payload (each key's use count and last use live in its memory), and builds it itself when none answers |
One model, four kinds
A QuotaLimit is one limit of one account: key, label, unit (percent, usd, tokens, requests), used, limit, remaining, window, reset time, plus its source (headers, frame, endpoint) and fetched_at. A provider's report has one kind:
| Kind | When | Example |
|---|---|---|
windowed | A percent unit, or a window longer than a minute | Codex 5 h and weekly windows |
balance | A money limit | OpenRouter key limit |
rate_limit | Per-minute requests or tokens | OpenAI and Anthropic API headers |
none | Nothing observed | Mistral |
- Status
- Warning at 80 % used, danger at 95 %, exhausted at 100 % or nothing remaining. A windowed limit warns early only when usage runs ahead of the time elapsed in its window.
- Stale
- A value older than 15 minutes is marked stale; it is still shown, with its age.
- Account
- The stored credential id, or empty for an environment key. A bearer token is matched to its credential by digest; the token itself is never stored.
Captured from replies
| Provider | Read from |
|---|---|
| OpenAI Codex | x-codex-* headers of every answer, a 429 included, every limit family; the codex.rate_limits WebSocket frame |
| OpenAI-compatible APIs | x-ratelimit-* headers (Chat Completions and Responses adapters) |
| Anthropic | anthropic-ratelimit-* headers, only with an API key on api.anthropic.com |
Values stay in memory and reach disk at most every 30 s per account. The old Codex parser's only caller looked for headers Codex does not send; it is replaced by one that follows codex-rs.
Reading an endpoint
| An endpoint is read when | Condition |
|---|---|
| The status page opens | The last read of that account is older than 10 minutes, whatever it answered; read after answering, in the background |
| The user asks for a refresh | — |
| A reply failed | With 402, 429 or a quota message; at most once per account per 10 minutes |
| A key was just added | — |
A refused read (401, 5xx, a body that does not parse) counts as a read: otherwise an endpoint that rejects the key would be asked again on every open.
One read per account at a time; a 429 from the quota endpoint holds that account for its Retry-After, or 5 minutes. Reads use the configured safe HTTP client, pinned to the endpoint's origin, and are logged as probe kind quota.
Each read is recorded per provider and account in usage.db (quota_reads), and the Worker and every CLI run decide from that record:
- kept: when it was sent, its HTTP status, why it failed, the 429 hold, the last read a failed reply asked for;
- so a refused read is not sent again by the next
providers statusor after a Worker restart; - a failed read leaves the earlier values with their own observed time; the status names the failure once below them (
read_error,read_at) until a read answers.
| Source | State | Shape taken from |
|---|---|---|
OpenRouter /key | On; keeps the remaining amount, not a yes/no | OpenRouter docs, Hermes |
| Codex usage | On; the weekly and 5h windows and the credits | Oh My Pi, codex-rs; a real response |
DeepSeek /user/balance | On; the balance in its own currency, spent when the account cannot pay for a call | A real response |
| MiniMax Token Plan, OpenCode Go | On; the plan's windows. A 401 from a plan's endpoint marks nothing on the key | Oh My Pi; a real response |
| Z.ai | Off until verified | Oh My Pi, ZCode |
| GitHub Copilot | Off until verified | Oh My Pi |
Spend
From the usage ledger, per provider over 30 days: calls, cost, list price, tokens by part, cache share (cache reads over prompt tokens) and a daily series of 30 points. A subscription provider reports its spend as Included, with the list price kept beside it. The default model is the last one used, else the most used.
A DeepSeek reply could show more cached tokens than total tokens. The cause: the recorder's fallback wrote total_tokens = input + output, leaving cached tokens out. The recorder now keeps the larger of the reported total and the parts summed, and every aggregate sums the parts, so rows written before the fix read correctly.
Table in usage.db | Holds |
|---|---|
quota_snapshots | The latest value per provider, account and limit |
quota_samples | One sample per 15 minutes, kept 90 days |
quota_reads | The last endpoint read per provider and account |
Health and failure causes
Two records speak about a key: its credential status, written when a call failed (status_at, the credential's updated_at), and the last read of its quota endpoint (read_at). The newer one decides; at the same time a 401 outranks a stored billing stop or throttle.
| Case | Reads as |
|---|---|
| A 401 read newer than the status | invalid_key ("quota endpoint answered 401"), until a read answers or a call with the key succeeds. For a key (not a sign-in) the same status is written on its row, so the provider's list says what its card does |
| Marking the row | Only a verified endpoint, never on a CreditsError body |
A read that answered after a quota stop | Overrules the stop; that read's balance decides |
A refresh token the token endpoint refuses (invalid_grant, already used) | needs_login, read as signed_out with the Sign in again fix; for a stored sign-in and for one delegated to the Codex CLI's file. The delegated one is not asked again until the file holds another login |
| Health | States |
|---|---|
| The best key's state, overridden by a spent limit | ready, rate_limited (with the reset time), no_credit, invalid_key, signed_out, unknown |
| A provider with no key | not_configured |
| A local server that does not answer | not_running |
Each state that needs action carries its fix; see Provider management.
A throttle lasts as long as its cooldown. A key keeps its cooling status until its next success but is chosen again once the cooldown has passed, so health reads such a key as ready, not as rate limited until a time already gone.
The CLI table prints only coming times (until 14:05, resets Nov 19 02:22); a time already passed is left out. It fits the terminal width: a bar gives up cells, down to 5, and then its reset time moves to a line of its own. Output to a pipe without COLUMNS is not wrapped.
A failed reply carries a cause beside its error reason, from the provider error to the turn result, the event (cause) and the stored placeholder (error_cause):
| Cause | From |
|---|---|
quota_exhausted | 402, usage_limit_reached, or a billing body under any status (insufficient_quota, 401 CreditsError "Insufficient balance"); names the spent limit, its reset time, the key (account) and that key's other windows (other_limits) when known |
invalid_key | 401 or a rejected key message |
not_available | Region or plan refusal (403 JSON, model not on the plan) |
unreachable | DNS or connection failure anywhere in the exception chain |
An ordinary 429, a 5xx, a timeout and a 403 web page from a network edge carry no cause; the error card is drawn as before.
Decisions
| Choice | Follows | Why |
|---|---|---|
| Limit record | Oh My Pi UsageLimit | The most complete of the 21: windows, money and request counts in one record |
| Labels, "% left", stale mark | Codex | Subscription users already read its 5h / Weekly rows |
| Time-aware warning | Claude Code | Usage against elapsed window time warns earlier and less often than a fixed line |
| Replies first, no timer | Codex, Claude Code | A timer spends requests while nobody looks; replies already refresh busy providers |
| 429 hold on the quota endpoint | Oh My Pi | Never retries into its own rate limit |
| OpenRouter remaining amount | Hermes | A yes/no hid how close a key was to its limit |
| Unverified readers off | — | Written only from a peer's documented response; switched on after a real check |
Implementation status
- No web UI reads the status route yet; the pages that will are in Provider management.
- No reader for Kimi Coding (its payload is not settled), Moonshot balances, or OpenRouter
/credits(no peer documents their responses). Anthropic, OpenAI, Google, Perplexity and Xiaomi API keys have no balance endpoint; a card shows their 30-day spend. - The Z.ai and GitHub Copilot readers are off: no real response has been seen.
- Not built: a REPL
/status. A top-levelopenprogram statusalias is not possible: that name is the background service's.