gateway: a Claude Code request that doesn't ask for thinking goes out with thinking off — Anthropic's API assumes that, but DeepSeek's Anthropic endpoint thinks by default, so the session-title requests Claude Code sends without it spent ~100 tokens thinking before a 10-token title; and output_config's effort becomes reasoning_effort on an OpenAI-style upstream only when thinking was asked for

This commit is contained in:
yetone
2026-09-25 16:34:19 +08:00
parent e072af501d
commit da27f2f0d7
3 changed files with 62 additions and 5 deletions
+21 -4
View File
@@ -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 {
+4 -1
View File
@@ -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 {
+37
View File
@@ -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)