gateway: a turn that names no reply limit is sent the model's own as max_tokens on Anthropic's API, not a flat 16384 (#1792, tensornull)

Anthropic's Messages API needs max_tokens. A request that names none
(Codex's Responses turns, a Chat client's) was always sent 16384, so a
long thinking and tool turn on a Claude-shaped relay hit the limit, and
Codex showed "Incomplete response returned, reason: max_output_tokens".

Now such a request is sent askedOutput(p, model). That is the plain
Served entry's PublishedOutput(), the same number the agents are told:
the user's Max output first, else the vendor's list or models.dev,
within the window. 16384 (defaultMaxTokens) is used only when nobody
knows the limit. A limit the client names still goes as it is. The
thinking budget is still fitted under max_tokens (anthropicBody),
whatever the limit is.

Both places that build an Anthropic body are covered:
forwardTranslated, for every translated route, and serveZed for a Zed
subscription's Claude models.

The provider editor's Max output hint now says this number is also
sent as max_tokens (en, zh, zh-TW, ja, de). docs/reference.md and
docs/subsystems/gateway-routing.md no longer say the gateway never uses
the limit.

Run:
- TestAnthropicMaxTokensIsTheModels covers: the user's 65536 (Responses
  plain and streamed, and Chat), models.dev's 64000, a 500000 limit
  held to the 100000 window, an unknown model at 16384, the client's
  own 1000, and thinking under 8192.
- TestZedClaudeMaxTokensIsTheModels expects 40000 when the client names
  no limit and 1000 when it does.
- Both tests fail on the old code: 16384 where the model's number was
  expected.
- go test ./internal/gateway/ passed (1270s, under load).
- provider-max-output.test.cjs passes 28/28 in chromium and webkit:
  5 languages at 900 and 420px. The hint names max_tokens and
  Anthropic, is translated, and doesn't overflow.
- gofmt -l is clean. go vet passes on darwin, on linux with -tags
  nogui, and on windows.

Not tried against a real Anthropic-compatible relay with a real Codex
session.
This commit is contained in:
yetone
2026-10-12 22:44:26 +08:00
parent 556c01c803
commit 50eeaae6ce
10 changed files with 231 additions and 8 deletions
+7 -2
View File
@@ -1001,8 +1001,13 @@ numbers are kept, and are what its models take once it is serving again.
A window is a number agents are shown **and a routing input**: at 95% of the
window a request held on a routing-group member moves to one that takes more,
so overstating a window makes that move happen too late. A reply limit is
advertised in `/models` and is what a group advertises the smallest of; the
gateway does not itself cap a reply by it.
advertised in `/models` and is what a group advertises the smallest of. The
gateway doesn't lower a limit an agent sends by it. A request that names none
and goes to Anthropic's Messages API, which needs one (Codex's Responses turns,
a Chat client's), is sent it as `max_tokens`, within the window as agents are
told it. Only a model whose limit nobody knows goes at 16384. A reply that
runs into it ends with `stop_reason: max_tokens`, which a Responses client
reads as `incomplete_details.reason: "max_output_tokens"` (#1792).
Saving a window or a reply limit writes that number into the model lists magpie
keeps in the agents' own files — Pi's `contextWindow` and `maxTokens`, OpenCode's