Use Claude Code, Codex, VSCode, Pi, and OpenCode (and 6 other harnesses) for free (1.3B+ free tokens) from your terminal, app, IDE, or phone, and now from the browser with native browser sessions (multi-harness + multi-model) like OpenClaw (voice supported + ToS friendly)
Python
Fix Claude auto-mode classifier thinking policy (#865)
Ali Khokhard9e42457bc46
diff --git a/ARCHITECTURE.md b/ARCHITECTURE.mdindex bf15601f..f4a8bf64 100644--- a/ARCHITECTURE.md+++ b/ARCHITECTURE.md@@ -427,6 +427,12 @@ common low-value client requests before they reach a provider:The service runs these only after model routing and after local server-toolhandling. Each optimization is controlled by settings flags.+Claude Code auto-mode safety-classifier requests are a message-only routing+policy, not a short-circuit response. After routing, the pipeline detects the+narrow classifier prompt shape and forces thinking off before provider execution+so Claude Code receives a parser-readable `<block>yes</block>` or+`<block>no</block>` verdict.+Local `web_search` and `web_fetch` handling lives under[api/web_tools/](api/web_tools/). When `ENABLE_WEB_SERVER_TOOLS` is true, theservice can stream local Anthropic server-tool responses without sending thediff --git a/api/detection.py b/api/detection.pyindex 7977c1bb..42238880 100644--- a/api/detection.py+++ b/api/detection.py@@ -1,7 +1,7 @@"""Request detection utilities for API optimizations.-Detects quota checks, title generation, prefix detection, suggestion mode,-and filepath extraction requests to enable fast-path responses.+Detects quota checks, title generation, prefix detection, safety classifier,+suggestion mode, and filepath extraction requests to enable targeted handling."""from core.anthropic import extract_text_from_content@@ -71,6 +71,22 @@ def is_prefix_detection_request(request_data: MessagesRequest) -> tuple[bool, streturn False, ""+def is_safety_classifier_request(request_data: MessagesRequest) -> bool:+ """Return whether this is Claude Code's auto-mode safety classifier prompt."""+ if request_data.tools:+ return False++ system_text = (+ extract_text_from_content(request_data.system) if request_data.system else ""+ )+ messages_text = "".join(+ extract_text_from_content(message.content) for message in request_data.messages+ )+ combined = f"{system_text}\n{messages_text}"+ has_verdict_instruction = "yes</block>" in combined or "no</block>" in combined+ return "<transcript>" in combined and has_verdict_instruction++def is_suggestion_mode_request(request_data: MessagesRequest) -> bool:"""Check if this is a suggestion mode request.diff --git a/api/request_pipeline.py b/api/request_pipeline.pyindex 600449db..7f56d931 100644--- a/api/request_pipeline.py+++ b/api/request_pipeline.py@@ -5,6 +5,7 @@ from __future__ import annotationsimport tracebackimport uuidfrom collections.abc import AsyncIterator, Callable+from dataclasses import replacefrom typing import Anyfrom fastapi import HTTPException@@ -20,6 +21,7 @@ from core.trace import api_messages_request_snapshot, trace_event, traced_async_from providers.base import BaseProviderfrom providers.exceptions import InvalidRequestError, ProviderError+from .detection import is_safety_classifier_requestfrom .model_router import ModelRouter, RoutedMessagesRequestfrom .models.anthropic import MessagesRequest, TokenCountRequestfrom .models.openai_responses import OpenAIResponsesRequest@@ -126,6 +128,7 @@ class ApiRequestPipeline:try:_require_non_empty_messages(request_data.messages)routed = self._model_router.resolve_messages_request(request_data)+ routed = self._apply_message_routing_policies(routed)self._reject_unsupported_server_tools(routed)intercepted = self._run_message_intercepts(routed)@@ -272,6 +275,26 @@ class ApiRequestPipeline:if tool_err is not None:raise InvalidRequestError(tool_err)+ def _apply_message_routing_policies(+ self, routed: RoutedMessagesRequest+ ) -> RoutedMessagesRequest:+ if not is_safety_classifier_request(routed.request):+ return routed+ changed = routed.resolved.thinking_enabled+ trace_event(+ stage="routing",+ event="api.optimization.safety_classifier_no_thinking",+ source="api",+ model=routed.request.model,+ changed=changed,+ )+ if not changed:+ return routed+ return RoutedMessagesRequest(+ request=routed.request,+ resolved=replace(routed.resolved, thinking_enabled=False),+ )+def _run_message_intercepts(self, routed: RoutedMessagesRequest) -> object | None:for intercept in self._message_intercepts:result = intercept(routed)diff --git a/providers/base.py b/providers/base.pyindex 8f918ad1..caacce21 100644--- a/providers/base.py+++ b/providers/base.py@@ -54,16 +54,21 @@ class BaseProvider(ABC):if isinstance(thinking, dict)else getattr(thinking, "type", None))+ if isinstance(thinking, dict):+ enabled = thinking.get("enabled")+ enabled_supplied = "enabled" in thinking+ else:+ enabled = getattr(thinking, "enabled", None)+ fields_set = getattr(thinking, "model_fields_set", None)+ enabled_supplied = (+ "enabled" in fields_set+ if isinstance(fields_set, set | frozenset)+ else enabled is not None+ )+ if enabled_supplied and enabled is not None:+ request_enabled = bool(enabled)if thinking_type == "disabled":request_enabled = False-- enabled = (- thinking.get("enabled")- if isinstance(thinking, dict)- else getattr(thinking, "enabled", None)- )- if enabled is not None:- request_enabled = bool(enabled)return config_enabled and request_enableddef preflight_stream(diff --git a/providers/transports/anthropic_messages/transport.py b/providers/transports/anthropic_messages/transport.pyindex 14d81154..b1ebb1a7 100644--- a/providers/transports/anthropic_messages/transport.py+++ b/providers/transports/anthropic_messages/transport.py@@ -119,6 +119,15 @@ class AnthropicMessagesTransport(BaseProvider):) -> dict:"""Build a native Anthropic request body."""thinking_enabled = self._is_thinking_enabled(request, thinking_enabled)+ return self._build_request_body_with_resolved_thinking(+ request,+ thinking_enabled=thinking_enabled,+ )++ def _build_request_body_with_resolved_thinking(+ self, request: Any, *, thinking_enabled: bool+ ) -> dict:+ """Build a native Anthropic request body after thinking is resolved."""return build_base_native_anthropic_request_body(request,default_max_tokens=ANTHROPIC_DEFAULT_MAX_OUTPUT_TOKENS,diff --git a/providers/wafer/client.py b/providers/wafer/client.pyindex 6fe74088..9b875c87 100644--- a/providers/wafer/client.py+++ b/providers/wafer/client.py@@ -23,9 +23,19 @@ class WaferProvider(AnthropicMessagesTransport):self, request: Any, thinking_enabled: bool | None = None) -> dict:"""Build native body; Wafer rejects omitted thinking as ``reasoning_effort=none``."""- body = super()._build_request_body(request, thinking_enabled=thinking_enabled)+ effective_thinking_enabled = self._is_thinking_enabled(+ request, thinking_enabled+ )+ body = self._build_request_body_with_resolved_thinking(+ request,+ thinking_enabled=effective_thinking_enabled,+ )if "thinking" not in body:- body["thinking"] = {"type": "enabled"}+ body["thinking"] = (+ {"type": "enabled"}+ if effective_thinking_enabled+ else {"type": "disabled"}+ )return bodydef _request_headers(self) -> dict[str, str]:diff --git a/pyproject.toml b/pyproject.tomlindex a851af47..1a5dd661 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -4,7 +4,7 @@ build-backend = "hatchling.build"[project]name = "free-claude-code"-version = "2.3.11"+version = "2.3.12"description = "Middleware between Claude Code CLI (Anthropic API) and NVIDIA NIM"readme = "README.md"requires-python = ">=3.14.0"diff --git a/tests/api/test_detection.py b/tests/api/test_detection.pyindex a28564a0..0d28fa11 100644--- a/tests/api/test_detection.py+++ b/tests/api/test_detection.py@@ -5,6 +5,7 @@ from unittest.mock import patchfrom api.detection import (is_filepath_extraction_request,is_prefix_detection_request,+ is_safety_classifier_request,)from api.models.anthropic import Message, MessagesRequest@@ -52,6 +53,47 @@ class TestIsPrefixDetectionRequest:assert cmd == ""+class TestIsSafetyClassifierRequest:+ _SYSTEM = (+ "You are a security monitor. Respond with <block>yes</block> "+ "or <block>no</block>."+ )+ _USER = (+ "<transcript>\nUser: review the repo\n"+ "WebFetch https://example.com: fetch\n</transcript>\n<block> immediately."+ )++ def test_classifier_request_detected(self):+ req = _make_request(self._USER, system=self._SYSTEM)+ assert is_safety_classifier_request(req) is True++ def test_markers_split_across_system_and_user(self):+ req = _make_request(+ "<transcript>\nWebFetch x\n</transcript>", system=self._SYSTEM+ )+ assert is_safety_classifier_request(req) is True++ def test_request_with_tools_is_not_classifier(self):+ req = _make_request(self._USER, system=self._SYSTEM, tools=[{"name": "search"}])+ assert is_safety_classifier_request(req) is False++ def test_missing_transcript_marker(self):+ req = _make_request("<block> immediately", system=self._SYSTEM)+ assert is_safety_classifier_request(req) is False++ def test_missing_verdict_instruction(self):+ req = _make_request(+ "<transcript>\nWebFetch x\n</transcript>", system="just chatting"+ )+ assert is_safety_classifier_request(req) is False++ def test_xml_content_without_verdict_instruction(self):+ req = _make_request(+ "Explain this format: <transcript> ... </transcript> and a <block> tag."+ )+ assert is_safety_classifier_request(req) is False++class TestIsFilepathExtractionRequest:def test_output_marker_minus_one_returns_false(self):"""Output: not found after Command: returns False."""diff --git a/tests/api/test_request_pipeline.py b/tests/api/test_request_pipeline.pyindex 119263c6..95834786 100644--- a/tests/api/test_request_pipeline.py+++ b/tests/api/test_request_pipeline.py@@ -13,6 +13,14 @@ from api.request_pipeline import ApiRequestPipelinefrom config.settings import Settingsfrom providers.base import BaseProvider, ProviderConfig+_CLASSIFIER_SYSTEM = (+ "You are a security monitor. Respond with <block>yes</block> or <block>no</block>."+)+_CLASSIFIER_USER = (+ "<transcript>\nUser: review the repo\nWebFetch https://example.com: fetch\n"+ "</transcript>\n<block> immediately."+)+class FakeProvider(BaseProvider):def __init__(self) -> None:@@ -62,6 +70,14 @@ async def _streaming_body_text(response: StreamingResponse) -> str:return "".join(parts)+def _trace_events(trace_mock: MagicMock, event: str) -> list[dict[str, Any]]:+ return [+ dict(call.kwargs)+ for call in trace_mock.call_args_list+ if call.kwargs.get("event") == event+ ]++@pytest.mark.asyncioasync def test_pipeline_provider_execution_passes_routed_request_and_stream_metadata():provider = FakeProvider()@@ -84,6 +100,102 @@ async def test_pipeline_provider_execution_passes_routed_request_and_stream_metaassert len(provider.preflight_calls) == 1+async def test_pipeline_forces_no_thinking_for_safety_classifier_messages():+ provider = FakeProvider()+ pipeline = ApiRequestPipeline(Settings(), provider_getter=lambda _: provider)+ request = MessagesRequest(+ model="nvidia_nim/test-model",+ max_tokens=100,+ system=_CLASSIFIER_SYSTEM,+ messages=[Message(role="user", content=_CLASSIFIER_USER)],+ )++ with patch("api.request_pipeline.trace_event") as trace_mock:+ response = pipeline.create_message(request)+ assert isinstance(response, StreamingResponse)+ await _streaming_body_text(response)++ assert provider.preflight_calls[0][1] is False+ assert provider.stream_kwargs[0]["thinking_enabled"] is False+ assert provider.requests[0].model == "test-model"+ assert provider.requests[0].system == _CLASSIFIER_SYSTEM+ assert _trace_events(+ trace_mock, "api.optimization.safety_classifier_no_thinking"+ ) == [+ {+ "stage": "routing",+ "event": "api.optimization.safety_classifier_no_thinking",+ "source": "api",+ "model": "test-model",+ "changed": True,+ }+ ]+++async def test_pipeline_preserves_thinking_for_non_classifier_messages():+ provider = FakeProvider()+ pipeline = ApiRequestPipeline(Settings(), provider_getter=lambda _: provider)+ request = MessagesRequest(+ model="nvidia_nim/test-model",+ max_tokens=100,+ system="Explain XML formats.",+ messages=[+ Message(+ role="user",+ content=(+ "Explain <transcript>...</transcript> and a <block> tag "+ "without making a verdict."+ ),+ )+ ],+ )++ with patch("api.request_pipeline.trace_event") as trace_mock:+ response = pipeline.create_message(request)+ assert isinstance(response, StreamingResponse)+ await _streaming_body_text(response)++ assert provider.preflight_calls[0][1] is True+ assert provider.stream_kwargs[0]["thinking_enabled"] is True+ assert (+ _trace_events(trace_mock, "api.optimization.safety_classifier_no_thinking")+ == []+ )+++async def test_pipeline_keeps_existing_no_thinking_for_classifier_messages():+ provider = FakeProvider()+ pipeline = ApiRequestPipeline(Settings(), provider_getter=lambda _: provider)+ request = MessagesRequest(+ model="claude-3-freecc-no-thinking/nvidia_nim/test-model",+ max_tokens=100,+ system=_CLASSIFIER_SYSTEM,+ messages=[Message(role="user", content=_CLASSIFIER_USER)],+ )++ with patch("api.request_pipeline.trace_event") as trace_mock:+ response = pipeline.create_message(request)+ assert isinstance(response, StreamingResponse)+ await _streaming_body_text(response)++ assert provider.preflight_calls[0][1] is False+ assert provider.stream_kwargs[0]["thinking_enabled"] is False+ assert _trace_events(+ trace_mock, "api.optimization.safety_classifier_no_thinking"+ ) == [+ {+ "stage": "routing",+ "event": "api.optimization.safety_classifier_no_thinking",+ "source": "api",+ "model": "test-model",+ "changed": False,+ }+ ]++def test_pipeline_message_optimization_intercepts_before_provider_execution():provider_getter = MagicMock()pipeline = ApiRequestPipeline(Settings(), provider_getter=provider_getter)@@ -120,3 +232,28 @@ async def test_pipeline_responses_bypass_message_only_optimizations():body = await _streaming_body_text(response)assert "response.completed" in bodyassert provider.requests[0].messages[0].content == "quota check"+++async def test_pipeline_responses_do_not_apply_safety_classifier_policy():+ provider = FakeProvider()+ pipeline = ApiRequestPipeline(Settings(), provider_getter=lambda _: provider)++ with patch("api.request_pipeline.trace_event") as trace_mock:+ response = await pipeline.create_response(+ request_data=OpenAIResponsesRequest(+ model="nvidia_nim/test-model",+ input=_CLASSIFIER_USER,+ instructions=_CLASSIFIER_SYSTEM,+ )+ )++ assert isinstance(response, StreamingResponse)+ await _streaming_body_text(response)++ assert provider.preflight_calls[0][1] is True+ assert provider.stream_kwargs[0]["thinking_enabled"] is True+ assert (+ _trace_events(trace_mock, "api.optimization.safety_classifier_no_thinking")+ == []+ )diff --git a/tests/providers/test_wafer.py b/tests/providers/test_wafer.pyindex 59ea60bd..a59a5be1 100644--- a/tests/providers/test_wafer.py+++ b/tests/providers/test_wafer.py@@ -1,6 +1,7 @@"""Tests for Wafer native Anthropic Messages provider."""from contextlib import asynccontextmanager+from typing import Anyfrom unittest.mock import AsyncMock, MagicMock, patchimport httpx@@ -43,6 +44,18 @@ class FakeResponse:response.raise_for_status()+class CountingWaferProvider(WaferProvider):+ def __init__(self, config: ProviderConfig):+ super().__init__(config)+ self.thinking_checks = 0++ def _is_thinking_enabled(+ self, request: Any, thinking_enabled: bool | None = None+ ) -> bool:+ self.thinking_checks += 1+ return super()._is_thinking_enabled(request, thinking_enabled)++@pytest.fixturedef wafer_config():return ProviderConfig(@@ -145,7 +158,22 @@ def test_build_request_body_drops_reasoning_effort_none(wafer_provider):assert body["thinking"] == {"type": "enabled"}-def test_build_request_body_keeps_upstream_thinking_enabled_when_client_disables_it(+def test_build_request_body_honors_effective_no_thinking(+ wafer_provider,+):+ request = MessagesRequest.model_validate(+ {+ "model": "DeepSeek-V4-Pro",+ "messages": [{"role": "user", "content": "Explore the codebase."}],+ }+ )++ body = wafer_provider._build_request_body(request, thinking_enabled=False)++ assert body["thinking"] == {"type": "disabled"}+++def test_build_request_body_preserves_request_disabled_thinking(wafer_provider,):request = MessagesRequest.model_validate(@@ -156,9 +184,51 @@ def test_build_request_body_keeps_upstream_thinking_enabled_when_client_disables})+ body = wafer_provider._build_request_body(request, thinking_enabled=True)++ assert body["thinking"] == {"type": "disabled"}+++def test_build_request_body_resolves_thinking_once(wafer_config):+ provider = CountingWaferProvider(wafer_config)+ request = MessagesRequest.model_validate(+ {+ "model": "DeepSeek-V4-Pro",+ "messages": [{"role": "user", "content": "Explore the codebase."}],+ }+ )++ body = provider._build_request_body(request, thinking_enabled=False)++ assert body["thinking"] == {"type": "disabled"}+ assert provider.thinking_checks == 1+++def test_build_request_body_classifier_no_thinking_uses_disabled_payload(+ wafer_provider,+):+ request = MessagesRequest.model_validate(+ {+ "model": "DeepSeek-V4-Pro",+ "system": (+ "You are a security monitor. Respond with <block>yes</block> "+ "or <block>no</block>."+ ),+ "messages": [+ {+ "role": "user",+ "content": (+ "<transcript>\nWebFetch https://example.com\n"+ "</transcript>\n<block> immediately."+ ),+ }+ ],+ }+ )+body = wafer_provider._build_request_body(request, thinking_enabled=False)- assert body["thinking"] == {"type": "enabled"}+ assert body["thinking"] == {"type": "disabled"}@pytest.mark.asynciodiff --git a/uv.lock b/uv.lockindex 350e287f..f3482916 100644--- a/uv.lock+++ b/uv.lock@@ -561,7 +561,7 @@ wheels = [[[package]]name = "free-claude-code"-version = "2.3.11"+version = "2.3.12"source = { editable = "." }dependencies = [{ name = "aiohttp" },