Files
magpie/internal/gateway/user_output_test.go
T
yetone b9c589ebb1 gateway: the user's own Max output lowers a plain provider's max_tokens, not only a group's (YaCo on Discord)
Claude Code asks claude-opus-5-5 for max_tokens 128000. A relay that
holds back max_tokens x price from a key's quota answered 403 "token
quota is not enough ... need $2.59" for every turn (128000 x $20/M plus
~8K input x $4/M). Setting the provider's Max output didn't help: the
gateway lowered max_tokens with withMaxOutput only inside a group
(outputLimit), so a request routed straight to a provider went out as
the agent asked.

userOutput finds the Max output the user set for the provider's model
(provider > model or the provider's "*", Entry.OutputSet) and lowers a
larger request to it on any route. Only the user's own value does this
outside a group: a vendor's catalog figure may be stale. Group routing
is unchanged.

Run:
- TestUserMaxOutputLowersAPlainProvidersRequest (new): 128000 -> 32000
  with Max output 32k on the model, 8000 kept, a sibling model with no
  Max output kept at 128000. Red without the gateway.go change ("went
  as 128000").
- go test ./internal/gateway -count=1 (whole package, -timeout 45m):
  ok, 1200s, before rebasing onto 4e9335c96; after the rebase the
  Output/MaxTokens tests again: ok.
- gofmt -l; go vet darwin, GOOS=linux -tags nogui, GOOS=windows: clean.
- A real Claude Code 2.1.29x against a fake server with the reporter's
  env: it sends max_tokens 128000 for claude-opus-5-5 with or without
  magpie. Not tried against the reporter's relay.
2026-10-14 21:11:42 +08:00

48 lines
1.8 KiB
Go

package gateway
import (
"net/http/httptest"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// YaCo on Discord: Claude Code asks claude-opus-5-5 for its full 128000
// (its own registry's max_output_tokens) and magpie writes no
// CLAUDE_CODE_MAX_OUTPUT_TOKENS for a claude-* model, so a key whose quota
// is held per request as max_tokens times the price was turned away on
// every turn. The Max output the user sets on the provider's model lowers
// such a request outside a group too; with none set the agent's own goes
// as it is, and a request under the user's limit isn't raised.
func TestUserMaxOutputLowersAPlainProvidersRequest(t *testing.T) {
fresh(t)
up := &adaptiveVendor{}
srv := httptest.NewServer(up)
t.Cleanup(srv.Close)
if err := provider.Save(provider.Provider{ID: "anth", Name: "Anth", Key: "k", Anthropic: srv.URL,
Models: []string{"claude-opus-5-5", "claude-sonnet-5-5"}}); err != nil {
t.Fatal(err)
}
if err := provider.SetModelOutput("anth/claude-opus-5-5", 32000); err != nil {
t.Fatal(err)
}
ask := func(model string, max int) float64 {
t.Helper()
body := `{"model":"anth/` + model + `","max_tokens":` + itoa(max) + `,"thinking":{"type":"adaptive","display":"omitted"},"output_config":{"effort":"medium"},"stream":false,"messages":[{"role":"user","content":"hi"}]}`
if code, out := post(t, "/v1/messages", body); code != 200 {
t.Fatalf("status %d: %s", code, out)
}
n, _ := up.last()["max_tokens"].(float64)
return n
}
if n := ask("claude-opus-5-5", 128000); n != 32000 {
t.Errorf("Claude Code's 128000 went as %v, want the user's 32000", n)
}
if n := ask("claude-opus-5-5", 8000); n != 8000 {
t.Errorf("8000 went as %v, want as asked", n)
}
if n := ask("claude-sonnet-5-5", 128000); n != 128000 {
t.Errorf("a model with no Max output set went as %v, want the agent's 128000", n)
}
}