Files
ai-kit/test/limits.test.js
T
Mao NakamotoandClaude Opus 5 71b665e5b5 feat: ai-ration — free-tier chain, 429 classification, fair-share rationing
Three pure modules extracted from FleetCrown, where each was written after the
matching failure had already happened in production:

  chain      — a fallback list ACROSS VENDORS. A single pinned free model is a
               scheduled outage, and stepping down to a smaller model at the
               SAME vendor buys nothing: it draws on the same org-wide daily
               budget, so when the day runs dry every link is already dead.

  limits     — the three kinds of 429 share a status, a `type` and a `code`, and
               need OPPOSITE responses. Only the body separates them. Treating
               `size` as `capacity` broke a tool loop on every single turn;
               treating `daily` as `capacity` is the same mistake one level
               deeper.

  fair-share — divide a fixed daily pool across users active TODAY, paced through
               the day, with a one-turn floor so nobody's first question is
               refused. The goal is not throughput; it is that every active user
               gets a usable amount every day.

No HTTP client on purpose. Every consumer already has its own calling
conventions, retries and logging; replacing those is a rewrite rather than an
adoption. This package supplies the decisions and leaves the fetch alone.

Two things fixed in the extraction rather than carried over:

- `humanizeWait`'s singular hour was unreachable. With the boundary at 90
  minutes, anything reaching the hours branch divided to at least 1.5, which
  rounds to 2 — so "about 1 hour" could never print and an hour-long wait was
  announced as two. Boundary moved to 60 and the case is now tested.
- The fair-share header quoted a 10k turn cost. A measured turn was ~16k, so the
  worked example understated the squeeze by nearly half.

Tests import the package BY NAME rather than reaching into dist/, so a broken
`exports` map or a `files` list that forgets to ship dist/ fails here instead of
at the first consumer's install.

31 tests, all green via `npm run verify`.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-16 11:49:17 +02:00

87 lines
4.1 KiB
JavaScript

/**
* The three kinds of 429 need OPPOSITE responses, so a misclassification is not
* a cosmetic error — it is the difference between recovering and making things
* worse. These are the real response bodies, not paraphrases.
*/
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { classifyRateLimit, retryAfterSeconds, humanizeWait, rateLimitMessage } from 'ai-ration';
const CAPACITY =
'Rate limit reached for model `llama-3.3-70b-versatile` on tokens per minute (TPM): Limit 12000, Used 11800, Requested 400. Please try again in 3.6s.';
const SIZE =
'Request too large for model `llama-3.1-8b-instant` on tokens per minute (TPM): Limit 6000, Requested 15041, please reduce your message size and try again.';
const DAILY =
'Rate limit reached for model `llama-3.3-70b-versatile` on tokens per day (TPD): Limit 100000, Used 99331, Requested 4589. Please try again in 56m26.88s.';
test('the three kinds are told apart by BODY, not status', () => {
assert.equal(classifyRateLimit(CAPACITY), 'capacity');
assert.equal(classifyRateLimit(SIZE), 'size');
assert.equal(classifyRateLimit(DAILY), 'daily');
});
test('DAILY wins over CAPACITY — it matches both wordings', () => {
// The daily body opens with the same "Rate limit reached" phrase as capacity,
// so a classifier that tests capacity first silently swallows every daily cap
// and then "helpfully" retries against an empty budget for the rest of the day.
assert.match(DAILY, /Rate limit reached/);
assert.equal(classifyRateLimit(DAILY), 'daily');
});
test('requests-per-day is treated as daily too', () => {
assert.equal(classifyRateLimit('Rate limit reached on requests per day (RPD): Limit 50'), 'daily');
});
test('an unrecognisable body degrades to CAPACITY, the safe guess', () => {
// Guessing "size" would shed context that was never the problem; guessing
// "daily" would abandon a turn that might well have succeeded.
assert.equal(classifyRateLimit('some vendor phrasing nobody has seen'), 'capacity');
assert.equal(classifyRateLimit(''), 'capacity');
});
test('the wait the provider named is parsed, in both shapes it emits', () => {
assert.equal(retryAfterSeconds(CAPACITY), 4); // 3.6s → ceil
assert.equal(retryAfterSeconds(DAILY), 3387); // 56m26.88s
assert.equal(retryAfterSeconds(SIZE), null, 'no stated wait must be null, not zero');
});
test('humanizeWait stays readable across the range', () => {
assert.equal(humanizeWait(4), '4s');
assert.equal(humanizeWait(3387), '57 minutes');
assert.equal(humanizeWait(7200), 'about 2 hours');
assert.equal(humanizeWait(null), null);
assert.equal(humanizeWait(0), null, 'a zero wait is no wait');
assert.equal(humanizeWait(-5), null);
});
test('the singular hour is REACHABLE — a dead branch is a lie about the output', () => {
// With the boundary at 90 minutes this was impossible: anything reaching the
// hours branch divided to >= 1.5, which rounds to 2. So "about 1 hour" could
// never print, and an hour-long wait was announced as two.
assert.equal(humanizeWait(3600), 'about 1 hour');
assert.equal(humanizeWait(4800), 'about 1 hour');
});
test('the message tells the user whether waiting can possibly help', () => {
// This is the whole point: "try again shortly" on an exhausted DAY invites
// exactly the retry that is guaranteed to fail for the next hour.
assert.match(rateLimitMessage(DAILY), /daily model quota is used up/);
assert.match(rateLimitMessage(DAILY), /resets in 57 minutes/);
assert.match(rateLimitMessage(CAPACITY), /rate-limited/);
assert.match(rateLimitMessage(CAPACITY), /retry in 4s/);
const size = rateLimitMessage(SIZE);
assert.match(size, /more context than the model allows/);
assert.doesNotMatch(size, /try again/i, 'retrying is not the fix for an oversized request');
});
test('the message is a CLAUSE the caller can embed', () => {
for (const body of [CAPACITY, SIZE, DAILY]) {
const msg = rateLimitMessage(body);
assert.doesNotMatch(msg, /^[A-Z]/, 'a leading capital reads wrong mid-sentence');
assert.doesNotMatch(msg, /\.$/, 'a trailing period double-punctuates the caller');
}
});