fix(ai): read long-context thresholds from models.dev tiers

Merged
PA
19d ago
+68-4

Fixes #30144

Problem

The catalog sync prices long context from models.dev's context_over_200k and gives every model a 200K threshold (LONG_CONTEXT_THRESHOLD_TOKENS). models.dev deprecated that field on 2026-08-23 (anomalyco/models.dev#5335). The real threshold now comes from cost.tiers[].tier.size, and it isn't always 200K:

  • 13 OpenAI models (gpt-6-luna, gpt-6-sol, gpt-6.1-sol, gpt-5.6, gpt-5.5, gpt-5.4 and others) switch to long-context rates above 272K. computeCostBreakdown switches them at 200K, so a request of 200K–272K is debited at double the input rate.
  • Claude Haiku 5.5 switches above 100K, to 5× its base rates. models.dev publishes that tier only in tiers, so the catalog has no longContextCost for it, and requests over 100K are debited at base rates.

Debited by computeCostBreakdown today, against the provider's price (90% of input cache-read, 1K output):

ModelInputProvider priceDebited today
gpt-6-luna240K$0.0051$0.0099
gpt-6.1-sol240K$0.0796$0.1542
claude-haiku-5-5150K$0.0168$0.0034

Fix

  • extractCost takes the lowest context tier from cost.tiers and uses its size as thresholdTokens.
  • It falls back to context_over_200k at 200K when a model has no tiers.
  • ModelsDevModel gains the tiers field.

Built against today's models.dev payload, the only catalog changes are:

  • the 13 OpenAI thresholds (200K → 272K);
  • Haiku 5.5 gaining its 100K tier;
  • gpt-6.1-terra getting its 272K tier.

Rates and the Google and xAI entries are unchanged. ai-models.json and ai-providers.json aren't regenerated here; the next daily sync rewrites them. The regenerated catalog can be added to this PR if preferred.

Test plan

  • build-catalog.spec.ts: two new cases, a 272K tier quoted in both fields and a 100K tier quoted only in tiers. Both fail on main (200000 instead of 272000; longContextCost undefined) and pass with the fix.
  • All suites under scripts/ai-catalog-sync and metadata-modules/ai pass, except build-front-component-shared-dependencies, which needs a local twenty-ui build.
  • oxlint and oxfmt are clean on the changed files. tsgo reports no errors in them.

Written with Claude Code (Claude Opus 5.5).