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
refactor(backends): reuse the LiteLLM tool converters in the any-llm backend (#4076)
Tejas Chopra03538f2f2f55
diff --git a/headroom/backends/anyllm.py b/headroom/backends/anyllm.pyindex 1d7d2e475..d3ea6c063 100644--- a/headroom/backends/anyllm.py+++ b/headroom/backends/anyllm.py@@ -16,6 +16,7 @@ from headroom.proxy.public_errors import client_messagefrom headroom.utils import format_exception_messagefrom .base import Backend, BackendResponse, StreamEvent+from .litellm import _convert_anthropic_tool, _convert_tool_choicelogger = logging.getLogger(__name__)@@ -28,51 +29,6 @@ except ImportError:AnyLLM = None # type: ignore-def _convert_anthropic_tool(tool: dict[str, Any]) -> dict[str, Any]:- """Convert an Anthropic tool definition to the OpenAI function shape.-- any-llm speaks OpenAI, so an Anthropic ``{name, description, input_schema}``- tool must become ``{type: function, function: {name, description,- parameters}}`` before it is forwarded, or the provider ignores/rejects the- tools array and the model never calls a tool. Mirrors the LiteLLM backend's- converter so both OpenAI-compatible backends send the same shape.- """- func: dict[str, Any] = {"name": tool.get("name", "")}- if "description" in tool:- func["description"] = tool["description"]- if "input_schema" in tool:- func["parameters"] = tool["input_schema"]- return {"type": "function", "function": func}---def _convert_tool_choice(choice: Any) -> Any:- """Convert an Anthropic ``tool_choice`` to the OpenAI shape (mirrors LiteLLM).-- Anthropic: ``{"type": "auto"}``, ``{"type": "any"}``, ``{"type": "none"}``,- ``{"type": "tool", "name": ...}``. OpenAI: ``"auto"``, ``"required"``,- ``"none"``, ``{"type": "function", "function": {"name": ...}}``. Passing the- raw Anthropic dict through makes the provider reject or ignore it.- """- if isinstance(choice, str):- return choice- if isinstance(choice, dict):- choice_type = choice.get("type", "auto")- if choice_type == "auto":- 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"--class AnyLLMBackend(Backend):"""Backend using any-llm for multi-provider support."""diff --git a/headroom/backends/litellm.py b/headroom/backends/litellm.pyindex c2c5ea858..e1e13fc60 100644--- a/headroom/backends/litellm.py+++ b/headroom/backends/litellm.py@@ -649,6 +649,8 @@ def _anthropic_usage_from_litellm(litellm_usage: Any) -> dict[str, Any]:def _convert_anthropic_tool(tool: dict[str, Any]) -> dict[str, Any]:"""Convert Anthropic tool format to OpenAI function format.+ Shared with the any-llm backend, so both OpenAI-compatible backends send the same shape.+Anthropic: {"name": "...", "description": "...", "input_schema": {...}}OpenAI: {"type": "function", "function": {"name": "...", "description": "...", "parameters": {...}}}"""@@ -661,7 +663,7 @@ 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.+ """Convert Anthropic tool_choice to OpenAI format (shared with the any-llm backend).Anthropic: {"type": "auto"}, {"type": "any"}, {"type": "none"}, {"type": "tool", "name": "..."}OpenAI: "auto", "required", "none", {"type": "function", "function": {"name": "..."}}