Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.
Python agentaianthropicclaude-codecompressioncontext-engineeringcontext-windowcursorfastapilangchainllmmcpopenaiprompt-engineeringproxypythonragtoken-optimizationtokenstypescript
fix(backends/litellm): map Anthropic tool_choice none to OpenAI none (#2689)
Abhay Singh57a57088de89
diff --git a/headroom/backends/litellm.py b/headroom/backends/litellm.pyindex a983fa2f7..9f66ed6d3 100644--- a/headroom/backends/litellm.py+++ b/headroom/backends/litellm.py@@ -606,8 +606,8 @@ def _convert_anthropic_tool(tool: dict[str, Any]) -> dict[str, Any]:def _convert_tool_choice(choice: Any) -> Any:"""Convert Anthropic tool_choice to OpenAI format.- Anthropic: {"type": "auto"}, {"type": "any"}, {"type": "tool", "name": "..."}- OpenAI: "auto", "required", {"type": "function", "function": {"name": "..."}}+ Anthropic: {"type": "auto"}, {"type": "any"}, {"type": "none"}, {"type": "tool", "name": "..."}+ OpenAI: "auto", "required", "none", {"type": "function", "function": {"name": "..."}}"""if isinstance(choice, str):return choice@@ -617,6 +617,13 @@ def _convert_tool_choice(choice: Any) -> Any:return "auto"if choice_type == "any":return "required"+ if choice_type == "none":+ # Anthropic's {"type": "none"} means "do not use any tool this turn".+ # Without this branch it fell through to the "auto" default below,+ # inverting the instruction into "you may use tools" — the model+ # could then call a tool the client explicitly forbade. OpenAI's+ # equivalent is the string "none".+ return "none"if choice_type == "tool":return {"type": "function", "function": {"name": choice.get("name", "")}}return "auto"diff --git a/tests/test_backend_bugs.py b/tests/test_backend_bugs.pyindex 26b0b059e..c26cadf55 100644--- a/tests/test_backend_bugs.py+++ b/tests/test_backend_bugs.py@@ -81,6 +81,12 @@ class TestConvertToolChoice:result = _convert_tool_choice({"type": "tool", "name": "get_weather"})assert result == {"type": "function", "function": {"name": "get_weather"}}+ def test_none_dict_not_inverted_to_auto(self):+ # Anthropic sends {"type": "none"} to forbid tool use this turn. It must+ # map to OpenAI's "none", not fall through to the "auto" default (which+ # would let the model call a tool the client explicitly disallowed).+ assert _convert_tool_choice({"type": "none"}) == "none"+def test_string_passthrough(self):assert _convert_tool_choice("auto") == "auto"assert _convert_tool_choice("none") == "none"