Skip to content

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.

fix(litellm): add async_post_call_success_hook to HeadroomCallback (#1322)

Lucas Santos3107994aed5f
diff --git a/headroom/integrations/litellm_callback.py b/headroom/integrations/litellm_callback.py
index 680805434..d5d54cacb 100644
--- a/headroom/integrations/litellm_callback.py
+++ b/headroom/integrations/litellm_callback.py
@@ -33,6 +33,10 @@ except ImportError: # litellm not installed — fall back to plain object
class HeadroomCallback(_CustomLogger):
"""LiteLLM callback that compresses messages before each API call.
+ Implements the LiteLLM callback hooks looked up by name:
+ ``async_pre_call_hook``, ``async_post_call_success_hook``,
+ ``async_success_handler`` and ``async_failure_handler``.
+
Inherits from litellm.integrations.custom_logger.CustomLogger so that
any hook LiteLLM adds in future versions (e.g. async_post_call_success_hook
added in 1.89.x) has a no-op default and won't raise AttributeError (#1114).
@@ -192,6 +196,15 @@ class HeadroomCallback(_CustomLogger):
result: dict[str, Any] = resp.json()
return result
+ async def async_post_call_success_hook(
+ self,
+ data: dict[str, Any],
+ user_api_key_dict: Any,
+ response: Any,
+ ) -> Any:
+ """Called by the LiteLLM proxy after a successful call. Returns response unchanged."""
+ return response
+
async def async_success_handler(
self, kwargs: dict, response: Any, start_time: Any, end_time: Any
) -> None:
diff --git a/tests/test_integrations/test_litellm_callback.py b/tests/test_integrations/test_litellm_callback.py
new file mode 100644
index 000000000..50f698316
--- /dev/null
+++ b/tests/test_integrations/test_litellm_callback.py
@@ -0,0 +1,54 @@
+"""Tests for headroom.integrations.litellm_callback."""
+
+from __future__ import annotations
+
+import importlib
+import inspect
+from pathlib import Path
+
+import pytest
+
+
+def _import_callback() -> type:
+ # Import the module directly to avoid triggering headroom/integrations/__init__.py
+ # which pulls in langchain and the native .so extension.
+ module_path = (
+ Path(__file__).resolve().parents[2] / "headroom" / "integrations" / "litellm_callback.py"
+ )
+ spec = importlib.util.spec_from_file_location(
+ "headroom.integrations.litellm_callback",
+ module_path,
+ )
+ assert spec is not None and spec.loader is not None
+ mod = importlib.util.module_from_spec(spec)
+ spec.loader.exec_module(mod) # type: ignore[union-attr]
+ return mod.HeadroomCallback # type: ignore[attr-defined]
+
+
+HeadroomCallback = _import_callback()
+
+
+class TestHeadroomCallbackPostCallSuccessHook:
+ """async_post_call_success_hook must exist and return response unchanged."""
+
+ def test_method_exists(self) -> None:
+ cb = HeadroomCallback()
+ assert hasattr(cb, "async_post_call_success_hook"), (
+ "HeadroomCallback must define async_post_call_success_hook "
+ "for LiteLLM proxy compatibility"
+ )
+
+ def test_method_is_coroutine(self) -> None:
+ cb = HeadroomCallback()
+ assert inspect.iscoroutinefunction(cb.async_post_call_success_hook)
+
+ @pytest.mark.asyncio
+ async def test_returns_response_unchanged(self) -> None:
+ cb = HeadroomCallback()
+ sentinel = object()
+ result = await cb.async_post_call_success_hook(
+ data={},
+ user_api_key_dict=None,
+ response=sentinel,
+ )
+ assert result is sentinel