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.
96 lines
3.1 KiB
Go
96 lines
3.1 KiB
Go
package gateway
|
|
|
|
import (
|
|
"encoding/json"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// outputLimit is the most a reply of a provider's model may hold, where
|
|
// known: its catalog entry's (the user's setting, else the vendor's list
|
|
// or models.dev).
|
|
func outputLimit(p provider.Provider, model string) int {
|
|
for _, e := range provider.Served() {
|
|
if e.Group == "" && e.Provider.ID == p.ID && (e.Model == model || e.ID == model) && e.Output > 0 {
|
|
return e.Output
|
|
}
|
|
}
|
|
return 0
|
|
}
|
|
|
|
// askedOutput is the reply limit a request that names none is sent on
|
|
// Anthropic's Messages API, which needs one: the model's own as the agents
|
|
// are told it (the user's Max output, else the vendor's list or
|
|
// models.dev, within the window), or 0 when none is known. A Codex turn
|
|
// names none, and was sent a flat 16384, which a long thinking and tool
|
|
// turn ran out of, Codex showing "Incomplete response returned, reason:
|
|
// max_output_tokens" (#1792, tensornull).
|
|
func askedOutput(p provider.Provider, model string) int {
|
|
for _, e := range provider.Served() {
|
|
if e.Group == "" && e.Provider.ID == p.ID && (e.Model == model || e.ID == model) {
|
|
return e.PublishedOutput()
|
|
}
|
|
}
|
|
return 0
|
|
}
|
|
|
|
// defaultMaxTokens is what a request naming no reply limit is sent on
|
|
// Anthropic's API for a model whose limit isn't known.
|
|
const defaultMaxTokens = 16384
|
|
|
|
// withMaxOutput is body, in proto's words, asking for a reply of at most
|
|
// limit tokens: a request that asks for more is lowered to limit, and
|
|
// Anthropic's thinking budget, which must be under it, with it. A request
|
|
// that asks for limit or less, or names no limit, goes as it is.
|
|
func withMaxOutput(proto provider.Protocol, body []byte, limit int) []byte {
|
|
var v struct {
|
|
MaxTokens int `json:"max_tokens"`
|
|
MaxCompletionTokens int `json:"max_completion_tokens"`
|
|
MaxOutputTokens int `json:"max_output_tokens"`
|
|
Thinking *struct {
|
|
Type string `json:"type"`
|
|
Budget int `json:"budget_tokens"`
|
|
} `json:"thinking"`
|
|
GenerationConfig map[string]any `json:"generationConfig"`
|
|
}
|
|
if limit <= 0 || json.Unmarshal(body, &v) != nil {
|
|
return body
|
|
}
|
|
fields := map[string]any{}
|
|
switch proto {
|
|
case provider.Chat:
|
|
if v.MaxCompletionTokens > limit {
|
|
fields["max_completion_tokens"] = limit
|
|
}
|
|
if v.MaxTokens > limit {
|
|
fields["max_tokens"] = limit
|
|
}
|
|
case provider.Responses:
|
|
if v.MaxOutputTokens > limit {
|
|
fields["max_output_tokens"] = limit
|
|
}
|
|
case provider.Anthropic:
|
|
if v.MaxTokens <= limit {
|
|
return body
|
|
}
|
|
fields["max_tokens"] = limit
|
|
if t := v.Thinking; t != nil && t.Type == "enabled" && t.Budget >= limit {
|
|
if limit-1 >= 1024 {
|
|
fields["thinking"] = map[string]any{"type": "enabled", "budget_tokens": limit - 1}
|
|
} else {
|
|
// no budget fits under it (Anthropic's least is 1024)
|
|
fields["thinking"] = map[string]any{"type": "disabled"}
|
|
}
|
|
}
|
|
case provider.Gemini:
|
|
if n, ok := v.GenerationConfig["maxOutputTokens"].(float64); ok && int(n) > limit {
|
|
v.GenerationConfig["maxOutputTokens"] = limit
|
|
fields["generationConfig"] = v.GenerationConfig
|
|
}
|
|
}
|
|
if len(fields) == 0 {
|
|
return body
|
|
}
|
|
return withFields(body, fields)
|
|
}
|