From da27f2f0d7416dce23871add4f70cf76a3b73565 Mon Sep 17 00:00:00 2001 From: yetone Date: Fri, 25 Sep 2026 16:32:40 +0800 Subject: [PATCH] =?UTF-8?q?gateway:=20a=20Claude=20Code=20request=20that?= =?UTF-8?q?=20doesn't=20ask=20for=20thinking=20goes=20out=20with=20thinkin?= =?UTF-8?q?g=20off=20=E2=80=94=20Anthropic's=20API=20assumes=20that,=20but?= =?UTF-8?q?=20DeepSeek's=20Anthropic=20endpoint=20thinks=20by=20default,?= =?UTF-8?q?=20so=20the=20session-title=20requests=20Claude=20Code=20sends?= =?UTF-8?q?=20without=20it=20spent=20~100=20tokens=20thinking=20before=20a?= =?UTF-8?q?=2010-token=20title;=20and=20output=5Fconfig's=20effort=20becom?= =?UTF-8?q?es=20reasoning=5Feffort=20on=20an=20OpenAI-style=20upstream=20o?= =?UTF-8?q?nly=20when=20thinking=20was=20asked=20for?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/gateway/anthropic.go | 25 +++++++++++++++++---- internal/gateway/gateway.go | 5 ++++- internal/gateway/gateway_test.go | 37 ++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 5 deletions(-) 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)