Files
mz 82ce30ede2 fix(models): take completion limits from the model author
The generator merged every numeric limit with Math.max across all source
records, and ran that merge twice: once per provider/model and again when
duplicate provider records collapse into one canonical model. Resellers
routinely republish their context window as the completion limit, so one
outlier won the maximum: anthropic/claude-opus-5 and claude-sonnet-5 shipped
outputTokens 1000000 (LiteLLM deepinfra/anthropic records with
max_output_tokens 1000000), openai/gpt-6-astra 1050000 (models.dev
llmgateway limit.output) and z-ai/glm-5.3 / glm-5.3-flash 1048576. The router
forwards Math.max(maxTokens, outputTokens) to clients as the model's output
limit (packages/core/src/gateway/model-catalog.ts modelCatalogMaxOutputTokens),
so clients ask for more than the model accepts.

Completion limits now come from the model author. When the owning provider
(first-party aliases such as zai and zhipuai resolve to z-ai) publishes a
positive maxTokens or outputTokens, each of the two keys is the maximum over
the author's own records, and a key the author does not publish is omitted
rather than filled in from a reseller. Otherwise - the entry has no record
from its author, or the author publishes no completion limit - both keys keep
the previous maximum over every record. Context tokens and every other limit
are untouched, as is supports1MContext.

Regenerating from today's upstream sources gives 128000 / 128000 / 128000 /
131072 / 131072 (the client-visible max of maxTokens and outputTokens) for the
five reported ids, lowers 128 completion limits, raises none, leaves every
other limit and capability flag unchanged and never changes a model whose new
value comes from a provider other than its author. packages/core/models.json
is produced by the release workflow and `npm run models:build`, so it is not
part of this change.
2026-10-29 20:57:39 +03:00

157 lines
5.4 KiB
JavaScript

// Pure helpers shared by the model catalog generator: provider canonicalization and
// the limit merge that turns every source record into one catalog `limits` object.
// Kept dependency free so it can be unit tested without running the generator.
const support1MContextThreshold = 1_000_000;
const numericLimitKeys = [
"contextTokens",
"inputTokens",
"maxAudioLengthHours",
"maxAudioPerPrompt",
"maxDocumentChunksPerQuery",
"maxImagesPerPrompt",
"maxPdfSizeMB",
"maxQueryTokens",
"maxTokens",
"maxTokensPerDocumentChunk",
"maxVideoLength",
"maxVideosPerPrompt",
"outputTokens",
"outputVectorSize"
];
// Resellers routinely republish the context window as a completion limit, and these are
// the only limits the router forwards to clients as an explicit max_tokens, so they are
// merged from the model author's records instead of from every record.
const completionLimitKeys = new Set(["maxTokens", "outputTokens"]);
const firstPartyProviderAliases = new Map(Object.entries({
ai21: "ai21",
alibaba: "alibaba",
anthropic: "anthropic",
amazon: "amazon",
bedrock: "amazon",
cohere: "cohere",
deepseek: "deepseek",
"deepseek-ai": "deepseek",
gemini: "google",
google: "google",
"google-vertex": "google",
llama: "meta-llama",
meta: "meta-llama",
"meta-llama": "meta-llama",
"meta-llama3": "meta-llama",
mistral: "mistral",
mistralai: "mistral",
minimax: "minimax",
minimaxai: "minimax",
moonshot: "moonshotai",
moonshotai: "moonshotai",
openai: "openai",
perplexity: "perplexity",
qwen: "alibaba",
xai: "x-ai",
"x-ai": "x-ai",
zai: "z-ai",
"z-ai": "z-ai",
"zai-org": "z-ai",
zhipuai: "z-ai"
}));
export function isRecord(value) {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
export function readNumber(value) {
const parsed = typeof value === "number" ? value : typeof value === "string" ? Number(value) : Number.NaN;
return Number.isFinite(parsed) && parsed >= 0 ? parsed : undefined;
}
export function compactObject(object) {
if (!isRecord(object)) return undefined;
const output = {};
for (const [key, value] of Object.entries(object)) {
if (value === undefined || value === null) continue;
if (Array.isArray(value) && value.length === 0) continue;
if (isRecord(value) && Object.keys(value).length === 0) continue;
output[key] = value;
}
return Object.keys(output).length > 0 ? output : undefined;
}
export function canonicalKnownProvider(value) {
const normalized = normalizeProviderToken(value);
return firstPartyProviderAliases.get(normalized);
}
export function canonicalProviderToken(value) {
const normalized = normalizeProviderToken(value);
return firstPartyProviderAliases.get(normalized) || normalized || undefined;
}
function normalizeProviderToken(value) {
return String(value || "")
.trim()
.replace(/^hf:/i, "")
.replace(/^@/, "")
.replace(/_/g, "-")
.replace(/\s+/g, "-")
.toLowerCase();
}
// Merges the limit records of one catalog entry into the single `limits` object that
// ships in models.json. `ownerProvider` is the canonical provider of the entry (the
// model author), `records` are the source records feeding it.
export function mergeLimits(records, ownerProvider) {
const output = {};
for (const key of numericLimitKeys) {
const values = completionLimitKeys.has(key)
? authoritativeLimitValues(records, ownerProvider, key)
: records.map((record) => readNumber(record?.limits?.[key])).filter((value) => value !== undefined);
if (values.length > 0) output[key] = Math.max(...values);
}
const contextCandidates = [output.contextTokens, output.inputTokens, output.maxTokens]
.filter((value) => Number.isFinite(value));
output.supports1MContext = contextCandidates.some((value) => value >= support1MContextThreshold);
return compactObject(output);
}
// Completion limits are the only limits the router forwards to clients as an explicit
// max_tokens, and it uses the larger of the two (`Math.max(maxTokens, outputTokens)`).
// Resellers routinely republish the context window as a completion limit, so a maximum
// over every source record can hand clients a request size the model author rejects.
//
// The authority decision is made once per entry and covers both completion keys: when
// the owning provider publishes a positive `outputTokens` or `maxTokens`, each key is
// the maximum over the author's own records and a key the author does not publish is
// omitted (a reseller never fills it). Otherwise — the entry has no record from its
// author, or the author publishes no completion limit — both keys keep the previous
// behaviour of merging every record.
function authoritativeLimitValues(records, ownerProvider, key) {
const owner = canonicalProviderToken(ownerProvider);
const authorRecords = owner
? records.filter((record) => canonicalProviderToken(record?.provider) === owner)
: [];
if (publishesCompletionLimit(authorRecords)) {
return authorRecords
.map((record) => readNumber(record?.limits?.[key]))
.filter((value) => value !== undefined && value > 0);
}
return records
.map((record) => readNumber(record?.limits?.[key]))
.filter((value) => value !== undefined);
}
function publishesCompletionLimit(records) {
return records.some((record) => Array.from(completionLimitKeys).some((key) => {
const value = readNumber(record?.limits?.[key]);
return value !== undefined && value > 0;
}));
}