diff --git a/internal/gateway/anthropic.go b/internal/gateway/anthropic.go index eb89ca3d3..18a408c13 100644 --- a/internal/gateway/anthropic.go +++ b/internal/gateway/anthropic.go @@ -123,18 +123,35 @@ func parseAnthropic(body []byte) (*Request, error) { r.Parallel = &f } } + // output_config's effort sets how hard the model thinks only when it + // was asked to think: Claude Code's title requests carry effort but no + // thinking, and reasoning_effort would turn it on upstream if th := a.Thinking; th != nil && (th.Type == "enabled" || th.Type == "adaptive") { r.Thinking = true r.Effort = effortOfBudget(th.BudgetTokens) - } - if oc := a.OutputConfig; oc != nil { - if e := effortOf(oc.Effort); e != "" { - r.Effort = e + if oc := a.OutputConfig; oc != nil { + if e := effortOf(oc.Effort); e != "" { + r.Effort = e + } } } return r, nil } +// thinkingOffUnlessAsked says thinking is off when the request doesn't +// mention it. That is what Anthropic's API assumes, but DeepSeek and other +// vendors' Anthropic endpoints think by default, so Claude Code's requests +// for a session title, sent without thinking, spent their tokens thinking. +func thinkingOffUnlessAsked(body []byte) []byte { + var v struct { + Thinking json.RawMessage `json:"thinking"` + } + if json.Unmarshal(body, &v) != nil || v.Thinking != nil { + return body + } + return withFields(body, map[string]any{"thinking": map[string]any{"type": "disabled"}}) +} + // buildAnthropic renders a request for an Anthropic-style upstream. func buildAnthropic(r *Request, model string) []byte { type msg struct { diff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.go index c7b7669cf..ab9164d42 100644 --- a/internal/gateway/gateway.go +++ b/internal/gateway/gateway.go @@ -599,8 +599,11 @@ func (s *Server) forward(ctx context.Context, p provider.Provider, to provider.P // but not this one. func (s *Server) passthrough(w http.ResponseWriter, r *http.Request, p provider.Provider, proto provider.Protocol, model string, body []byte, u *Usage) (status int, msg string, done bool) { body = rewriteModel(body, model) - if proto == provider.Chat { + switch proto { + case provider.Chat: body = developerAsSystem(body) + case provider.Anthropic: + body = thinkingOffUnlessAsked(body) } res, err := s.forward(r.Context(), p, proto, pathOf(proto), p.Prepare(body), r.Header) if err != nil { diff --git a/internal/gateway/gateway_test.go b/internal/gateway/gateway_test.go index 423958f5d..79ffd04e7 100644 --- a/internal/gateway/gateway_test.go +++ b/internal/gateway/gateway_test.go @@ -460,6 +460,43 @@ func TestChatPassthroughSendsDeveloperAsSystem(t *testing.T) { } } +// Claude Code asks for a session title without thinking; DeepSeek's +// Anthropic endpoint thinks unless told not to +func TestAnthropicPassthroughTurnsThinkingOffUnlessAsked(t *testing.T) { + f := &fake{t: t, ctype: "application/json", reply: `{"id":"msg","type":"message","content":[]}`} + setup(t, provider.Anthropic, f) + for _, c := range []struct{ req, want string }{ + {`{"model":"m1","max_tokens":5,"messages":[],"output_config":{"effort":"high"}}`, `"thinking":{"type":"disabled"}`}, + {`{"model":"m1","max_tokens":5,"messages":[],"thinking":{"type":"adaptive"}}`, `"thinking":{"type":"adaptive"}`}, + {`{"model":"m1","max_tokens":5,"messages":[],"thinking":{"type":"enabled","budget_tokens":2048}}`, `"thinking":{"budget_tokens":2048,"type":"enabled"}`}, + } { + if code, body := post(t, "/v1/messages", c.req); code != 200 { + t.Fatalf("%d %s", code, body) + } + if !bytes.Contains(f.got, []byte(c.want)) { + t.Errorf("%s forwarded as %s", c.req, f.got) + } + } +} + +// effort without thinking asks for no reasoning on a Chat upstream +func TestAnthropicEffortWithoutThinking(t *testing.T) { + f := &fake{t: t, reply: sse(`data: {"id":"c1","choices":[{"delta":{"content":"T"},"finish_reason":"stop"}]}`, `data: [DONE]`)} + setup(t, provider.Chat, f) + if code, body := post(t, "/v1/messages", `{"model":"m1","max_tokens":5,"stream":true,"messages":[{"role":"user","content":"title?"}],"output_config":{"effort":"high"}}`); code != 200 { + t.Fatalf("%d %s", code, body) + } + if bytes.Contains(f.got, []byte("reasoning_effort")) { + t.Errorf("upstream asked to reason: %s", f.got) + } + if code, body := post(t, "/v1/messages", `{"model":"m1","max_tokens":5,"stream":true,"messages":[{"role":"user","content":"hi"}],"thinking":{"type":"adaptive"},"output_config":{"effort":"high"}}`); code != 200 { + t.Fatalf("%d %s", code, body) + } + if !bytes.Contains(f.got, []byte(`"reasoning_effort":"high"`)) { + t.Errorf("adaptive thinking lost its effort: %s", f.got) + } +} + func TestErrorsAndUnknownModel(t *testing.T) { f := &fake{t: t, ctype: "application/json", code: 402, reply: `{"error":{"message":"Insufficient Balance","type":"x"}}`} setup(t, provider.Chat, f)