GandClaude Opus 5 07c492ce27 fix: four of the nine default pins were already retired (#4)
* fix: four of the nine default pins were already retired

Checked `freeChain()` against the live catalogues on 2026-08-25. Four of its
nine ids no longer exist:

    groq       llama-3.3-70b-versatile        GONE
    groq       llama-3.1-8b-instant           GONE   <- whole vendor dead
    openrouter openai/gpt-oss-20b:free        GONE   <- was FIRST in the list
    openrouter nvidia/nemotron-3-nano-30b...  GONE

The chain exists because a single pinned free model is a scheduled outage. That
reasoning has a hole this package fell into: the chain is ITSELF a list of pins,
so it rots too — and a chain whose first vendor is entirely dead is a slower
version of the failure it was built to prevent. Every caller paid two Groq 404s
and then a third on the preferred OpenRouter fallback before reaching a model
that could answer.

Consumers inherited it. FleetCrown used the same Groq id for direct, unchained
calls and was silently broken for eight days: its nightly frontier digest fell
back to a canned headline, and activity digests, calendar extraction, proposal
extraction and hosted-runner analysis all failed one at a time, quietly enough
that nothing looked broken.

Replacements probed, not guessed — the package's own rule. `openai/gpt-oss-120b`
and `openai/gpt-oss-20b` each returned a correct native tool_call with correct
arguments. The two retired OpenRouter ids are removed; the other five were
confirmed present in the 419-model catalogue.

── catalog: the check that can actually run on a timer ──────────────────────

New module, exported as `checkCatalog` / `hasRot` / `deadProviders` /
`catalogReport`. One GET /models per provider and ZERO tokens, which is the
entire point: a tool-call probe costs real money and therefore can never be
scheduled, so the only existing defence was a command someone had to remember
to type. Existence is cheap enough to check nightly. `npm run check:catalog`
runs it against this package's own defaults and exits 1 on confirmed rot.

It lives here rather than in each app because the check is the same everywhere,
and the first app to need it had already written its own, slightly differently.

Three states, not two. `live: null` means the catalogue could not be READ — no
key, network failure, non-200, or a body that parses but lists nothing. That is
not an empty catalogue. Reporting it as rot invents an outage; reporting it as
a pass hides one. `hasRot` counts only CONFIRMED absences, so an offline or
keyless run cannot fail a pipeline for something it never saw.

That distinction earned itself immediately: Node's global fetch is intermittently
broken on the laptop this was written on, and the checker correctly reported
7 UNCHECKED rather than "every model retired" — which would have sent me
"fixing" ids that were live.

`deadProviders` is separate from `hasRot` on purpose: a chain missing some
models is degraded, a chain missing an entire vendor is back to being a single
point of failure, and those deserve different words.

── modelCost is provider-dependent, and was silently wrong about it ─────────

The existing `paidModelsIn` guard caught the new Groq pins and called them PAID.
It was not a false alarm to suppress — it was the guard finding a real hole in
its own rule.

`modelCost` decides from the id: a slashed id without `:free` is paid. That is
right for OpenRouter, where `openai/gpt-oss-20b` genuinely bills. It is wrong
for Groq, where the same string is just that vendor's name for a model whose
cost is the account's tier. The rule was only ever safe because direct vendors
used bare ids (`llama-3.1-8b-instant` → "unknown"); Groq now ships
vendor-prefixed ids, so the shape stopped identifying the vendor.

So `Provider` gains `routed?: boolean` (true for OpenRouter), and `modelCostAt`
judges an id AT its provider. At a direct vendor the verdict is "unknown" —
never "free", which would reopen the hole this module was written to close.
`modelCost(id)` is unchanged for existing callers, with a docstring saying what
it assumes.

A false "paid" here is not harmless: it pressures someone into "fixing" a
working free model, and a guard that cries wolf gets disabled — taking the three
real cases it does catch with it. Pinned by a test that the routed vendor's
`anthropic/claude-sonnet-5` and `google/gemini-2.0-flash-001` are still flagged.

verify: 44/44 pass. Live catalogue check: all 7 pins present, no rot. Mutation:
restoring the retired pins turns it red (exit 1, deadProviders ["groq"]).

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* chore: 0.2.1 — retired pins replaced, catalog check added

Consumers pin this package by git TAG (FleetCrown:
github:maonakamoto/ai-ration#v0.2.0), so the retired-model fix only reaches
them once a new tag exists and their pin moves. Bumping here keeps
package.json and the tag in agreement — the previous release left an installed
copy reporting 0.1.0 under a #v0.2.0 pin, which makes 'which code is actually
running' unanswerable from the tree.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
2026-08-25 15:11:04 +02:00

ai-ration

Keep an LLM app on free tiers, and share what is free fairly.

Three small, pure modules. No HTTP client, no SDK, no framework — every app already has its own calling conventions, and replacing those is a rewrite rather than an adoption. This package supplies the decisions; you keep the fetch.

npm install github:maonakamoto/ai-ration#v0.1.0

Why

Free LLM tiers fail in three specific ways, and each one has been mistaken for an application bug at least once:

Failure What it looks like What actually fixes it
A pinned free model is retired "the assistant is broken" a chain, not a pin
The vendor's daily budget is gone a 429 that retrying never clears a different vendor
One eager user spends the day's tokens by 10am everyone else meets a wall rationing

The third is the one that costs you users, because the person who hits the wall is usually the one trying the product for the first time.

chain — a fallback list across vendors

import { freeChain, usableChain, chainFrom } from 'ai-ration';

const providers = freeChain('MYAPP');            // groq → openrouter, free models
const links = usableChain(providers, process.env); // drops vendors with no key
for (const { provider, model } of chainFrom(process.env.MYAPP_MODEL, links)) {
  // POST `${provider.baseUrl}/chat/completions` with `model`
  // on failure, continue — that is the whole point
}

Both properties are load-bearing. Falling back 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 in that "fallback" is already dead. Only a different vendor has a different meter.

Probe before you pin. Of nine free models probed live for the default chain, five answered only via a text tool protocol, not native tool_calls. A native-only client would have silently lost most of the chain. The shipped list is evidence from one day, not a constant — free catalogues rot, so re-probe.

limits — the three kinds of 429

They share a status code, a type, and a code. Only the body tells them apart, and they need opposite responses:

import { classifyRateLimit, rateLimitMessage } from 'ai-ration';

classifyRateLimit(body); // 'capacity' | 'size' | 'daily'
  • capacity — the per-minute window is spent. Wait, or step down.
  • size — one request exceeds the whole window. Waiting never helps, and stepping down makes it strictly worse (the cheaper model has a smaller ceiling). Only a smaller prompt helps.
  • daily — the day's budget is gone, org-wide. Every response that works for capacity is actively harmful: a step-down draws on the same exhausted budget, and a 25-second wait is nothing against a reset measured in tens of minutes.

rateLimitMessage(body) returns a clause you embed in your own framing. It says whether waiting can help and when, because "try again shortly" on an exhausted day invites exactly the retry that is guaranteed to fail for the next hour.

An unrecognised body degrades to capacity — the safe guess, since its response is harmless when wrong.

fair-share — divide a fixed daily pool

Pure policy: no database, no clock, no provider. You own "what has this user spent today"; it owns "may they spend more".

import { fairShare, utcDayElapsed } from 'ai-ration';

const decision = fairShare({
  dayCapacityTokens: dayCapacityTokens(providers, process.env),
  activeUsers,        // distinct users who drew TODAY, including this one
  userSpentTokens,
  costTokens: ESTIMATED_TURN,
  dayElapsed: utcDayElapsed(new Date()),
});
// decision.reason: 'ok' | 'paced' | 'share-spent' | 'no-capacity'

Two ideas, both needed:

  1. Share = capacity ÷ users active today. Recomputed per request, so a new user is counted the moment they arrive. Counting dormant accounts would ration a quiet day down to nothing.
  2. Pacing — a share is a whole-day allowance, and the day is consumed in order. Without pacing, three users legitimately spend their full shares by 09:00 and the fourth finds nothing left. So the allowance unlocks gradually.

Plus a one-turn floor: the allowance never sits below the cost of a single turn, so nobody's first question of the morning is refused. Without it, pure pacing tells a first-time user to come back in three hours — indistinguishable from broken.

retryAfterSeconds is present only for paced, the one refusal a wait actually fixes. Telling someone whose share is spent to try again in 20 minutes is the same lie as "try again shortly" on an exhausted daily quota.

No clawback: a user who spent under a larger share when they were alone is not punished when a second user appears — their allowance simply stops growing.

Estimating a turn

Whatever you pass as costTokens must err high. Under-estimating admits turns the pool cannot cover, draining the day while the gate still believes there is room. One measured turn on a free model — a single tool call — cost ~16k tokens; an estimate of 10k had been documented as "deliberately on the high side" and was 37% below it. Measure, then replace the estimate with the mean.

Development

npm run verify   # build + test

MIT.

Languages
TypeScript 58.9%
JavaScript 41.1%