fix: floor responses max_output_tokens to 16 for tiny budgets (#63)

Zen's Responses endpoint rejects max_output_tokens below 16, and clients
such as Claude Code send tiny budgets for background tasks. The 400 was
retried across every anonymous exit before reaching the client. Raise
budgets in (0, 16) to 16 on both the bridged and the same-protocol
Responses paths; nil and larger budgets pass through unchanged.

Co-authored-by: ahoo <ahoo@hroze.org>
This commit is contained in:
Jason Xuandahoo committed 2026-10-15 07:52:13 +08:00
1 parent 9a5b4e23e5
commit 7a6c1f9386
1 file changed
+21 -1
+21 -1
View File
@@ -19,6 +19,11 @@ func PrepareRequest(from, to Protocol, input map[string]any, upstreamURL string)
if err != nil {
return nil, err
}
if to == Responses {
// The bridge already floors converted bodies; a same-protocol body is
// a clone of the client's and needs the same guard.
jsonutil.Put(output, "max_output_tokens", floorResponsesMaxTokens(output["max_output_tokens"]))
}
normalizeToolReasoningHistory(to, jsonutil.StringAt(output, "model"), upstreamURL, output)
return output, nil
}
@@ -916,11 +921,26 @@ func responsesReasoning(value any) any {
}
}
// minResponsesMaxOutputTokens is the smallest max_output_tokens the Zen
// Responses endpoint accepts; anything lower is rejected with a 400.
const minResponsesMaxOutputTokens = 16
// floorResponsesMaxTokens keeps tiny downstream budgets (background titles,
// probes) from failing Zen responses validation. Larger budgets pass through
// untouched. Decoded JSON numbers are always float64, so no other numeric type
// can reach this point.
func floorResponsesMaxTokens(value any) any {
if n, ok := value.(float64); ok && n > 0 && n < minResponsesMaxOutputTokens {
return float64(minResponsesMaxOutputTokens)
}
return value
}
func encodeResponsesRequest(request bridgeRequest) map[string]any {
output := map[string]any{"model": request.Model, "stream": request.Stream}
jsonutil.Put(output, "temperature", request.Temperature)
jsonutil.Put(output, "top_p", request.TopP)
jsonutil.Put(output, "max_output_tokens", request.MaxTokens)
jsonutil.Put(output, "max_output_tokens", floorResponsesMaxTokens(request.MaxTokens))
jsonutil.Put(output, "stop", request.Stop)
jsonutil.Put(output, "metadata", request.Metadata)
if len(request.System) > 0 {