diff --git a/serve/frontend.py b/serve/frontend.py index edcddb79..84310811 100644 --- a/serve/frontend.py +++ b/serve/frontend.py @@ -219,10 +219,11 @@ def anthropic_to_messages(req: dict) -> tuple[list[dict], list[dict] | None, dic kwargs.update(effort_kwargs(effort)) elif isinstance(thinking, dict) and thinking.get("budget_tokens"): kwargs.update(budget_effort(thinking["budget_tokens"])) - elif thinking is None: + elif thinking is None and not req.get("reasoning_budget_tokens"): # Anthropic's thinking is opt-in. Claude Code's helper calls (a session title, a topic check) ask for none # and allow a few dozen tokens, which the model otherwise spent thinking and answered with no text at all. - # A config's reasoning_effort still applies: Service.with_shared sets output_config before this runs. + # A config's reasoning_effort still applies: Service.with_shared sets output_config before this runs. A + # request that gives its own reasoning_budget_tokens (#123) asks for thinking, so it thinks as before. kwargs["enable_thinking"] = False return _late_system_to_user(messages), tools, kwargs diff --git a/serve/test_server.py b/serve/test_server.py index e5d31184..123e0780 100644 --- a/serve/test_server.py +++ b/serve/test_server.py @@ -65,6 +65,17 @@ class MaxTokens(unittest.TestCase): u = b.get("usage", {}) return s, b, u.get("input_tokens"), u.get("output_tokens") + def test_anthropic_thinks_only_when_asked(self): + # #278: Anthropic's thinking is opt-in; "thinking", an effort or a reasoning_budget_tokens (#123) asks for it + from serve.frontend import anthropic_to_messages + msgs = [{"role": "user", "content": "u"}] + kw = lambda **r: anthropic_to_messages({"messages": msgs, **r})[2] # noqa: E731 + self.assertEqual(kw(), {"enable_thinking": False}) + self.assertEqual(kw(thinking={"type": "disabled"}), {"enable_thinking": False}) + self.assertNotIn("enable_thinking", kw(thinking={"type": "enabled", "budget_tokens": 2048})) + self.assertNotIn("enable_thinking", kw(output_config={"effort": "high"})) + self.assertNotIn("enable_thinking", kw(reasoning_budget_tokens=30)) + def test_count_tokens_is_the_prompt_messages_reads(self): # /v1/messages/count_tokens renders and tokenizes the same prompt /v1/messages would read, without running it msgs = [{"role": "user", "content": "how many tokens is this?"}]