Conversation
Chat sent no max_output_tokens, so Unity Gateway applied its default of 8,192 output tokens to Claude, and longer replies stopped early with an unknown reason. Send the output limit the gateway lists for the model, when it lists one. Models without a listed limit still get no cap, since some reject one above their own limit. Co-authored-by: Isaac <no-reply@databricks.com>
| const listed = this.gateway.models.value; | ||
| const maxOutputTokens = | ||
| listed.status === "ready" | ||
| ? listed.value.find((m) => m.id === model.id)?.limits?.output |
There was a problem hiding this comment.
[blocking] This now asks for the model's whole listed output limit, and toInformation gives Copilot a prompt budget of exactly limits.input - limits.output. Prompt plus cap only fits the window if the prompt really is within that budget, and Copilot measures it with provideTokenCount: chars/4, plus a flat 1,000 per image. Code, JSON tool output and non-Latin text take more tokens than that. Since Claude 3.7, Anthropic rejects a request whose input plus max_tokens exceeds the context window ("input length and max_tokens exceed context limit"). It doesn't shrink the cap. Example: a Claude model listed at 200k/64k gets a 136k budget. A code-heavy agent session that the estimate puts at 136k may really be ~170k tokens. 170k + 64k > 200k, so every turn fails with HTTP 400 ("choose another model"). Before this PR the gateway's 8,192 default fit easily (170k + 8k), and nothing here retries without the cap. Has a near-full Claude conversation been tried through Unity Gateway with the cap? If the gateway does reject it, leave headroom: cap at min(listed output, some margin below it), shrink maxInputTokens by a margin, or retry once without max_output_tokens on that specific 400.
There was a problem hiding this comment.
Thanks, this is real for one model. Anthropic documents that from Claude 4.5 on, a prompt plus max_tokens over the window is accepted, and generation stops at the window with model_context_window_exceeded. Earlier models get a validation error. Through Unity Gateway:
- Claude Sonnet 4.5 (200K window) accepted 162,634 input tokens with a 64,000 cap and completed.
- Claude Opus 4.1 returned 400, "input length and
max_tokensexceed context limit: 178970 + 32000 > 204658", and accepted the same prompt without a cap.
Opus 4.1 is the only listed model older than 4.5. 0407720 now fits the cap to the window: the listed output limit, less the prompt counted at three characters a token, plus 1,000 for each image and the tools' JSON. When that leaves 8,192 or less, no cap is sent, so a near-full prompt falls back to the gateway's default as before. I couldn't check GPT with a prompt that large; the fit applies to it either way.
| const listed = this.gateway.models.value; | ||
| const maxOutputTokens = | ||
| listed.status === "ready" | ||
| ? listed.value.find((m) => m.id === model.id)?.limits?.output |
There was a problem hiding this comment.
[follow-up] The limits come from /ai-gateway/anthropic/v1/models, which lists every model the gateway serves on that API, not only Claude (UnityGatewayModelLimitsClient.ts). The cap goes to the unified Responses route. The old extension sent no cap because some models reject one above their own limit (#2247 names Llama 4 Maverick at 8,192 and Qwen3 Next at 10,000). The manual check covered Claude and GPT. Has a listed Llama or Qwen model been tried with max_output_tokens at its listed limit? If the two routes disagree, every turn on that model now fails with a 400 where it used to work. This can be done in a follow-up PR.
There was a problem hiding this comment.
I checked the listing on two workspaces. Only Claude and GPT 5.6+ models have usable limits there. Llama and Qwen aren't listed, and GLM and Kimi are listed at 0/0, which the limits client skips. So no other model gets a cap today.
Some models, such as Claude Opus 4.1, refuse a request whose prompt and max_output_tokens together exceed the context window. Chat's prompt budget counts four characters a token, which runs low for code, so a near-full prompt plus the full listed cap could fail every turn. Cap the output at the listed limit less the prompt, counted at three characters a token, and send no cap when that leaves no more than the gateway's default. Co-authored-by: Isaac <no-reply@databricks.com>
| for (const tool of tools) { | ||
| chars += JSON.stringify(tool).length; | ||
| } | ||
| const room = limits.input - Math.ceil(chars / 3) - images * IMAGE_TOKENS; |
There was a problem hiding this comment.
[blocking] The room has no margin under the 3-chars-a-token estimate, and code often runs denser on Claude. E.g. 450k chars of code at 2.5 chars/token is 180k real tokens, estimated at 150k, so Opus 4.1 (200k/32k) gets a 32k cap: 212k > window, a 400 on every turn, where the 8,192 default fit (188k). Retry once without max_output_tokens on that context-limit 400, or leave a real margin (e.g. count at 2 chars a token).
There was a problem hiding this comment.
I measured this on Opus 4.1's tokenizer, which is the one that matters here: newer Claude models accept a prompt plus cap over the window and stop at it. These are characters per token for 30,000-character samples, from usage.input_tokens through Unity Gateway:
| Content | Chars/token |
|---|---|
| English prose | 4.54 |
| Pretty JSON | 3.83 |
| Compact JSON | 3.68 |
| TypeScript | 3.48 |
| Python | 3.47 |
| JSON schema | 3.38 |
git log --stat |
3.24 |
grep -rn output |
3.17 |
yarn.lock |
2.07 |
| Minified JS | 1.80 |
Code runs at about 3.5 there, not 2.5; the denser figures fit the newer tokenizers, whose models don't refuse the overflow. With the count at three characters and no cap at 8,192 or less, Opus 4.1 fails only when a near-full prompt averages below about 2.9 characters a token. That takes more than about a quarter of its characters being lockfile- or minified-like. I'd rather keep three than halve the caps of the 4.5 models with 200K windows, which accept the overflow anyway. A conversation full of lockfiles or minified bundles on Opus 4.1 is the case this leaves open.
Changes
Claude replies in Chat stopped at 8,192 output tokens. Chat sent no
max_output_tokens, so Unity Gateway applied its default, and the reply ended with "Unity Gateway stopped the reply early: unknown reason".Chat now sends
max_output_tokensfrom the output limit that the gateway lists for the model, when it lists one. The model listing already reads these limits from/ai-gateway/anthropic/v1/models. Models without a listed limit still get no cap, because some reject a cap above their own limit.The cap leaves room in the context window for the prompt, because some models, such as Claude Opus 4.1, refuse a request whose prompt and cap together exceed the window. Chat's prompt budget counts four characters a token, which runs low for code, so the room is counted at three characters a token, plus 1,000 for each image and the tools' JSON. When that leaves no more than the gateway's default of 8,192, no cap is sent.
Known gap: a request sent while the models are listed again after a new connection, or after a failed listing, gets no cap, as before.
Tests
New unit tests:
Each test fails when its behaviour is removed.
yarn checkandyarn testpass.Manually, against a workspace:
max_output_tokensat their listed limits.This pull request and its description were written by Isaac.