Claude Code. Any Model. The most powerful AI coding agent now speaks every language.
fix(adapters): fill a missing max_tokens from the catalog, not 4096
Jack Rudenkoebed28e3f220
diff --git a/ai-docs/architecture/wire.md b/ai-docs/architecture/wire.mdindex 17c52c69..b75a6b84 100644--- a/ai-docs/architecture/wire.md+++ b/ai-docs/architecture/wire.md@@ -51,6 +51,23 @@ lists, effort ladders by `gpt-5.x`) being falsified by each new model release.`not_applicable` is an ANSWER, never borrowed over (Codex found the shaper treating it like`unknown`). Mode and speed are never borrowed: they carry entitlement and price.+## Output limit++The request's own `max_tokens` is sent unchanged, at the wire's `tokenParam`. When the request+has none (MCP `run_prompt` and `compare_models` send none), only an API that REQUIRES a limit+gets one: the Messages API (`anthropic-messages`) receives the catalog's `maxOutputTokens` for+the connection's model (`ResolvedConnection.maxOutputTokens`, written by step 5w through+`WireChoices.catalogMaxOutputTokens`). Chat Completions, Responses and Gemini are left to the+upstream's own default. The slim catalog publishes the ceiling per model, not per connection.++An unknown ceiling follows the same rule as an unknown `tokenParam` in the table above: send+nothing, never a pinned number. `[Wire]` logs "the catalog publishes no maxOutputTokens: none+sent". The adapter's old `max_tokens || 4096` capped every Messages connection (`minimax-m3`+via `minimax` and `opencode`, `qwen3.8-max` via `opencode`) at 4096 output tokens wherever no+wire overwrote it. With no wire the adapter now fills from the same catalog field+(`lookupModelMaxOutputTokens`), or leaves the field out. On generation+`g-20261016053727894-8a743761` 6 of 58 Messages connections had no model `maxOutputTokens`, `minimax-m3` among them.+## Traps- **Older claudish erases the registry.** Releases before 10.5.0 rewritediff --git a/packages/cli/src/adapters/anthropic-api-format.ts b/packages/cli/src/adapters/anthropic-api-format.tsindex 8cda976a..2419852a 100644--- a/packages/cli/src/adapters/anthropic-api-format.ts+++ b/packages/cli/src/adapters/anthropic-api-format.ts@@ -8,6 +8,7 @@import type { StreamFormat } from "../providers/transport/types.js";import { type AdapterResult, BaseAPIFormat } from "./base-api-format.js";+import { lookupModelMaxOutputTokens } from "./model-catalog.js";/** Text of one inline system message, whatever content shape it arrived in. */function systemText(content: unknown): string {@@ -124,6 +125,25 @@ export class AnthropicAPIFormat extends BaseAPIFormat {return claudeRequest.tools || [];}+ /**+ * The `max_tokens` this request carries.+ *+ * The Messages API requires the field, but the inbound request may lack it+ * (MCP `run_prompt` and `compare_models` send none). An inbound value is kept+ * unchanged. A missing one is filled from the catalog's `maxOutputTokens` for+ * this model, never from a pinned number: the old `|| 4096` capped every+ * Messages connection at 4096 output tokens whenever the request had none.+ * With no published ceiling the field is omitted (an unknown value is sent+ * as nothing, ai-docs/architecture/wire.md "Output limit"); on a catalog+ * connection step 5w writes the final value from the connection either way.+ */+ private outputLimitFor(claudeRequest: any): number | undefined {+ const inbound = claudeRequest?.max_tokens;+ if (typeof inbound === "number") return inbound;+ if (this.modelId.includes("@")) return undefined;+ return lookupModelMaxOutputTokens(this.modelId);+ }+/*** Rebuild the Anthropic-format payload from the claudeRequest.* This reconstructs the same payload that Claude Code originally sent,@@ -149,9 +169,10 @@ export class AnthropicAPIFormat extends BaseAPIFormat {const payload: any = {model: this.modelId,messages: cleanMessages,- max_tokens: claudeRequest.max_tokens || 4096,stream: true,};+ const maxTokens = this.outputLimitFor(claudeRequest);+ if (maxTokens !== undefined) payload.max_tokens = maxTokens;// APPEND, never replace. The reminders are additional context, and the real// system prompt is the expensive cached prefix — overwriting it would bothdiff --git a/packages/cli/src/handlers/composed-handler.ts b/packages/cli/src/handlers/composed-handler.tsindex 520215b4..c1e9e4fe 100644--- a/packages/cli/src/handlers/composed-handler.ts+++ b/packages/cli/src/handlers/composed-handler.ts@@ -1174,6 +1174,7 @@ export class ComposedHandler implements ModelHandler {if (wire?.status === "known") {const choices: WireChoices = {...wireChoicesFromClaudeRequest(claudeRequest, this.options.effortOverride),+ catalogMaxOutputTokens: this.options.connection?.maxOutputTokens,// `--accept-served-model` (or CLAUDISH_ACCEPT_SERVED_MODEL=1): the user// named the swap, so a part the catalog warns about may be sent.acceptServedModelChange: process.env.CLAUDISH_ACCEPT_SERVED_MODEL === "1",diff --git a/packages/cli/src/mcp-server.ts b/packages/cli/src/mcp-server.tsindex 22cbeeab..17bd7204 100644--- a/packages/cli/src/mcp-server.ts+++ b/packages/cli/src/mcp-server.ts@@ -319,9 +319,10 @@ export function parseAnthropicSse(raw: string): {/*** One request through the in-process proxy, for every model that is not a bare* Claude name. The body carries NO `max_tokens`: step 5w strips every output-limit- * field the adapters write and adds the wire's own only when the request has one, so- * the upstream applies its own default and no model limit is pinned here- * (ai-docs/architecture/wire.md).+ * field the adapters write and adds the wire's own only when the request has one,+ * except on the Messages API, which requires one and gets the catalog's+ * `maxOutputTokens`. No model limit is pinned here+ * (ai-docs/architecture/wire.md "Output limit").*/export async function runPromptViaProxy(model: string,diff --git a/packages/cli/src/providers/connection-resolver.ts b/packages/cli/src/providers/connection-resolver.tsindex 231d2f79..877bfd8c 100644--- a/packages/cli/src/providers/connection-resolver.ts+++ b/packages/cli/src/providers/connection-resolver.ts@@ -42,6 +42,12 @@ export interface ResolvedConnection {wire: ConnectionWire;profile: RouteProfile;generationId: string;+ /**+ * The catalog's published output ceiling for this model, when it has one.+ * The slim catalog publishes it per model, not per connection. Absent means+ * unknown, never zero and never unlimited.+ */+ maxOutputTokens?: number;/*** The known wire of the model OWNER's own direct connection (the `direct`* profile whose `nativeModelProviders` names the model's vendor), when the@@ -147,6 +153,9 @@ export function resolveConnection(wire: found.connection.wire,profile,generationId: cache.routeRegistry.generationId,+ ...(typeof found.entry.maxOutputTokens === "number" && found.entry.maxOutputTokens > 0+ ? { maxOutputTokens: found.entry.maxOutputTokens }+ : {}),...(ownerWire ? { ownerWire } : {}),},};diff --git a/packages/cli/src/providers/wire-request.ts b/packages/cli/src/providers/wire-request.tsindex 836df922..4693118f 100644--- a/packages/cli/src/providers/wire-request.ts+++ b/packages/cli/src/providers/wire-request.ts@@ -50,6 +50,12 @@ export interface WireChoices {hasTools?: boolean;/** The output limit to write at `wire.tokenParam`. */maxTokens?: number;+ /**+ * The catalog's `maxOutputTokens` for the connection's model. Written ONLY+ * when `maxTokens` is absent and the API requires a limit (the Messages API+ * requires `max_tokens`); every other API is left to the upstream's default.+ */+ catalogMaxOutputTokens?: number;/** `+pro` on the model spec, or pro turned on by `proOnUltracode`. */mode?: { value: WireMode; explicit: boolean };/** `#<speed>` on the model spec. */@@ -366,6 +372,35 @@ export function wireChoicesFromClaudeRequest(};}+// ─── Output limit ──────────────────────────────────────────────────────────++/** APIs that reject a request with no output limit. */+const LIMIT_REQUIRED_APIS: ReadonlySet<KnownWire["api"]> = new Set(["anthropic-messages"]);++/**+ * The output limit to write: the request's own when it has one, unchanged.+ * Otherwise, only on an API that requires a limit, the catalog's+ * `maxOutputTokens` for this model. With no published ceiling nothing is+ * written: an unknown value is never replaced by a pinned number.+ */+function outputLimitToSend(+ wire: KnownWire,+ choices: WireChoices,+ result: WireShapeResult+): number | undefined {+ if (typeof choices.maxTokens === "number") return choices.maxTokens;+ if (!LIMIT_REQUIRED_APIS.has(wire.api)) return undefined;+ const ceiling = choices.catalogMaxOutputTokens;+ if (typeof ceiling === "number" && ceiling > 0) {+ result.notes.push(`request has no output limit: sent the catalog's maxOutputTokens ${ceiling}`);+ return ceiling;+ }+ result.notes.push(+ "request has no output limit and the catalog publishes no maxOutputTokens: none sent"+ );+ return undefined;+}+// ─── Entry point ───────────────────────────────────────────────────────────/**@@ -393,8 +428,9 @@ export function shapeRequestFromWire(};const sameApiOwner = owner && owner !== wire && owner.api === wire.api ? owner : undefined;+ const maxTokens = outputLimitToSend(wire, choices, result);if (typeof wire.tokenParam === "string") {- if (typeof choices.maxTokens === "number") body[wire.tokenParam] = choices.maxTokens;+ if (typeof maxTokens === "number") body[wire.tokenParam] = maxTokens;} else if (wire.api !== "gemini") {// Only an UNKNOWN field is tried from the owner; `not_applicable` is an// answer (this connection takes no limit field), not a gap.@@ -402,9 +438,9 @@ export function shapeRequestFromWire(wire.tokenParam.status === "unknown" &&sameApiOwner &&typeof sameApiOwner.tokenParam === "string" &&- typeof choices.maxTokens === "number"+ typeof maxTokens === "number") {- borrow(`/${sameApiOwner.tokenParam}`, choices.maxTokens);+ borrow(`/${sameApiOwner.tokenParam}`, maxTokens);result.notes.push(`tokenParam ${wire.tokenParam.status}: trying the owner's ${sameApiOwner.tokenParam}`);