Files
ai-kit/test
CatoandClaude Opus 5 b7f741048b feat(meter): read the limit that only the refusal admits to (#56)
Headers do not describe every limit a vendor enforces.

Groq publishes its per-minute token window and its per-day REQUEST count as
headers, and meters a third limit — tokens per DAY — that appears in no header
at all. It is stated once, in prose, inside the body of the 429 enforcing it:

  on tokens per day (TPD): Limit 200000, Used 199773, Requested 571.

Observed on production 2026-09-13, and the surrounding numbers are the point:
every header on that same response looked healthy — 2,672 of 8,000 tokens left
this minute, 999 of 1,000 requests left today — while the account was locked out
of the model for the rest of the day. A dashboard fed only by headers reported a
working provider throughout an outage.

That is exactly the failure the top of this file exists to prevent ("reports a
full tank during an outage"), arriving through a door it left open. The module
refused to poll a usage endpoint because the endpoint lies; it then trusted
headers to be complete, and they are not.

readingFromRefusalBody() takes the body and returns the counter the vendor named
there — scope, window, limit, and remaining computed from the stated Used.

STILL NEVER INVENTS ONE

Returns null unless the body states numbers. OpenRouter's daily refusal names no
counter, so it yields nothing and the caller falls back to readingFromRefusal(),
which records the one fact any 429 carries: spent, now. Inventing a reading here
would be the same bug with the sign flipped — an invented outage.

Two details that cost a measurement each: the window comes from the vendor's own
sentence, so unlike a header name it is not overridden by the provider profile
(TPD read as a minute understates it 1440x); and remaining is clamped at zero,
because a vendor counting the rejected request against the total would otherwise
render a negative tank.

4 new tests, proved by mutation: breaking the arithmetic fails 3 of them.
261 tests pass, verify green.


Claude-Session: https://claude.ai/code/session_01Nm2REqtdsVsvjukBU2maCz

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-13 06:43:56 -07:00
..