Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio: honor Claude Code's thinking-off setting on /v1/messages (#17557)

oobabooga53eca64ead57
diff --git a/studio/backend/models/inference.py b/studio/backend/models/inference.py
index c096e05326b..9f16aca1211 100644
--- a/studio/backend/models/inference.py
+++ b/studio/backend/models/inference.py
@@ -4560,6 +4560,7 @@ class AnthropicMessagesRequest(BaseModel):
)
model_config = {"extra": "allow"}
_message_ordinals: Optional[list[int]] = PrivateAttr(default = None)
+ _omitted_thinking_is_off: bool = PrivateAttr(default = False)
@model_validator(mode = "after")
def _effort_from_output_config(self) -> "AnthropicMessagesRequest":
@@ -4579,7 +4580,8 @@ class AnthropicMessagesRequest(BaseModel):
return self.enable_thinking
if self.thinking is not None:
return self.thinking.type != "disabled"
- return None
+ # Omitted means the model default, as on Anthropic's API.
+ return False if self._omitted_thinking_is_off else None
@model_validator(mode = "before")
@classmethod
diff --git a/studio/backend/routes/inference.py b/studio/backend/routes/inference.py
index 8bb447137d7..5b6d347eea7 100644
--- a/studio/backend/routes/inference.py
+++ b/studio/backend/routes/inference.py
@@ -4983,6 +4983,18 @@ def _reject_confirm_gate_without_channel(
)
+def _mark_claude_code_thinking(payload, request) -> None:
+ """Claude Code omits `thinking` when thinking is off on a model id it does not know."""
+ headers = getattr(request, "headers", None)
+ if headers is None:
+ return
+ betas = (beta.strip() for beta in headers.get("anthropic-beta", "").split(","))
+ if headers.get("user-agent", "").startswith("claude-cli/") or any(
+ beta.startswith("claude-code-") for beta in betas
+ ):
+ payload._omitted_thinking_is_off = True
+
+
def _anthropic_reasoning_args(payload) -> dict:
"""Forwards /v1/messages thinking and x-unsloth reasoning fields; else the load-time default sticks."""
# x-unsloth controls outrank `thinking`; consult reasoning_effort before the native block.
@@ -43742,6 +43754,7 @@ async def anthropic_count_tokens(
tokenizer, or a resident MLX model's through the chat count route, and returns
``{"input_tokens": int}`` only. Unlike /messages, max_tokens is NOT required here.
"""
+ _mark_claude_code_thinking(payload, request)
if _connection_model_ref(payload.model) is not None:
raise HTTPException(
status_code = 400,
@@ -44402,6 +44415,7 @@ async def anthropic_messages(
responses in Anthropic Messages API format (streaming SSE or non-streaming
JSON).
"""
+ _mark_claude_code_thinking(payload, request)
_admit_tool_access(payload)
connection = _connection_model_ref(payload.model)
if connection is not None:
diff --git a/studio/backend/tests/test_anthropic_messages.py b/studio/backend/tests/test_anthropic_messages.py
index 63acaf1344a..5ad66d0a2ba 100644
--- a/studio/backend/tests/test_anthropic_messages.py
+++ b/studio/backend/tests/test_anthropic_messages.py
@@ -6281,6 +6281,100 @@ def test_x_unsloth_effort_still_outranks_thinking_when_sent_explicitly():
assert args["reasoning_effort"] == "high"
+_CLAUDE_CODE_UA = {"user-agent": "claude-cli/2.1.322 (external, sdk-cli)"}
+_CLAUDE_CODE_BETA = {"anthropic-beta": "interleaved-thinking-2025-05-14, claude-code-20250219"}
+
+
+class _HeaderRequest:
+ state = SimpleNamespace()
+ url = SimpleNamespace(path = "/v1/messages")
+ method = "POST"
+
+ def __init__(self, headers):
+ self.headers = headers
+
+ async def is_disconnected(self):
+ return False
+
+
+ "headers, expected",
+ [
+ (_CLAUDE_CODE_UA, True),
+ (_CLAUDE_CODE_BETA, True),
+ ({"user-agent": "Anthropic/Python 0.70.0"}, False),
+ ({"anthropic-beta": "interleaved-thinking-2025-05-14"}, False),
+ ({}, False),
+ ],
+)
+def test_only_claude_code_marks_an_omitted_thinking_as_off(headers, expected):
+ from routes.inference import _mark_claude_code_thinking
+
+ payload = _basic_payload(output_config = {"effort": "high"})
+ _mark_claude_code_thinking(payload, _HeaderRequest(headers))
+ assert payload.resolved_enable_thinking() is (False if expected else None)
+
+
+ "fields, expected_enable, expected_effort",
+ [
+ ({}, False, None),
+ ({"thinking": None}, False, None),
+ ({"thinking": {"type": "adaptive", "display": "omitted"}}, True, None),
+ ({"thinking": {"type": "disabled"}}, False, None),
+ ({"enable_thinking": True}, True, None),
+ ({"reasoning_effort": "high"}, True, "high"),
+ ],
+)
+def test_claude_code_omitted_thinking_is_off_below_explicit_controls(
+ fields, expected_enable, expected_effort
+):
+ from routes.inference import _anthropic_reasoning_args, _mark_claude_code_thinking
+
+ payload = _basic_payload(**fields)
+ _mark_claude_code_thinking(payload, _HeaderRequest(_CLAUDE_CODE_UA))
+ args = _anthropic_reasoning_args(payload)
+ assert args["enable_thinking"] is expected_enable
+ assert args["reasoning_effort"] == expected_effort
+
+
[email protected]("headers, expected", [(_CLAUDE_CODE_UA, False), ({}, None)])
+def test_claude_code_thinking_off_reaches_generation_and_counting(monkeypatch, headers, expected):
+ from routes.inference import anthropic_count_tokens, anthropic_messages
+
+ counted: list = []
+ backend = _mock_backend(
+ monkeypatch,
+ _request_reasoning_kwargs = lambda enable, effort, preserve: (
+ None if enable is None else {"enable_thinking": enable}
+ ),
+ )
+ backend.count_chat_tokens = lambda *args, chat_template_kwargs = None, **kwargs: (
+ counted.append(chat_template_kwargs) or 2
+ )
+
+ _drive(
+ anthropic_messages(
+ _basic_payload(output_config = {"effort": "high"}),
+ request = _HeaderRequest(headers),
+ current_subject = "t",
+ )
+ )
+ _drive(
+ anthropic_count_tokens(
+ _basic_payload(output_config = {"effort": "high"}),
+ request = _HeaderRequest(headers),
+ current_subject = "t",
+ )
+ )
+
+ [(_path, kwargs)] = backend.calls
+ assert kwargs["enable_thinking"] is expected
+ assert kwargs["reasoning_effort"] is None
+ template_kwargs = None if expected is None else {"enable_thinking": False}
+ assert counted and all(kwargs == template_kwargs for kwargs in counted)
+
+
def test_the_anthropic_paths_promote_a_replayed_mcp_envelope():
"""Replayed MCP image envelopes must be promoted, or the model reads base64 as text."""
import inspect