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.

style: fix linting and formatting issues

Devdeep Sarkara0c54c7f2238
diff --git a/headroom/perf/analyzer.py b/headroom/perf/analyzer.py
index 17a26f89c..0a29b227f 100644
--- a/headroom/perf/analyzer.py
+++ b/headroom/perf/analyzer.py
@@ -18,7 +18,6 @@ from datetime import datetime, timedelta
from headroom import paths as _paths
from headroom.pricing.litellm_pricing import resolve_litellm_model
-
log = logging.getLogger(__name__)
LOG_DIR = _paths.log_dir()
diff --git a/headroom/pricing/litellm_pricing.py b/headroom/pricing/litellm_pricing.py
index 602a2607c..1f09fd77c 100644
--- a/headroom/pricing/litellm_pricing.py
+++ b/headroom/pricing/litellm_pricing.py
@@ -44,6 +44,8 @@ _MODEL_ALIASES: dict[str, str] = {
}
_resolved_model_cache: dict[str, str] = {}
+
+
def resolve_litellm_model(model: str) -> str:
"""Resolve model name to one LiteLLM recognizes, adding provider prefix if needed.
Results are cached per model name to avoid blocking the event loop
@@ -54,6 +56,8 @@ def resolve_litellm_model(model: str) -> str:
resolved = _resolve_litellm_model_uncached(model)
_resolved_model_cache[model] = resolved
return resolved
+
+
def _resolve_litellm_model_uncached(model: str) -> str:
"""Uncached resolution — called once per unique model name."""
if not LITELLM_AVAILABLE:
@@ -82,7 +86,7 @@ def _resolve_litellm_model_uncached(model: str) -> str:
except Exception:
break
return model
-
+
@dataclass
class LiteLLMModelPricing:
diff --git a/headroom/proxy/cost.py b/headroom/proxy/cost.py
index d62bff144..766e018e8 100644
--- a/headroom/proxy/cost.py
+++ b/headroom/proxy/cost.py
@@ -16,7 +16,6 @@ from typing import TYPE_CHECKING, Any
from headroom.proxy.modes import PROXY_MODE_CACHE
-
if TYPE_CHECKING:
from headroom.proxy.prometheus_metrics import PrometheusMetrics
@@ -567,7 +566,6 @@ class CostTracker:
self._api_cache_write_1h_by_model.clear()
self._api_uncached_by_model.clear()
-
def estimate_cost(
self,
model: str,
@@ -595,6 +593,7 @@ class CostTracker:
try:
from headroom.pricing.litellm_pricing import resolve_litellm_model
+
resolved_model = resolve_litellm_model(model)
# litellm.cost_per_token handles all token types natively:
@@ -704,6 +703,7 @@ class CostTracker:
return None
try:
from headroom.pricing.litellm_pricing import resolve_litellm_model
+
resolved = resolve_litellm_model(model)
info = litellm.model_cost.get(resolved, {})
cost_per_token = info.get("input_cost_per_token")
@@ -722,6 +722,7 @@ class CostTracker:
return None
try:
from headroom.pricing.litellm_pricing import resolve_litellm_model
+
resolved = resolve_litellm_model(model)
info = litellm.model_cost.get(resolved, {})
uncached = info.get("input_cost_per_token")
diff --git a/tests/test_cost_tracker_counterfactual.py b/tests/test_cost_tracker_counterfactual.py
index 8164c1192..4dde0f678 100644
--- a/tests/test_cost_tracker_counterfactual.py
+++ b/tests/test_cost_tracker_counterfactual.py
@@ -37,6 +37,7 @@ def test_savings_at_list_price():
# Savings should be 100k tokens * list input price (NOT affected by cache mix)
import litellm
+
from headroom.pricing.litellm_pricing import resolve_litellm_model
resolved = resolve_litellm_model(model)
diff --git a/tests/test_proxy_streaming_resilience.py b/tests/test_proxy_streaming_resilience.py
index 62df2eeff..332d1c8d9 100644
--- a/tests/test_proxy_streaming_resilience.py
+++ b/tests/test_proxy_streaming_resilience.py
@@ -33,7 +33,8 @@ class TestModelResolutionCaching:
import headroom.pricing.litellm_pricing as lp
with patch(
- "headroom.pricing.litellm_pricing._resolve_litellm_model_uncached", return_value="anthropic/claude-opus-4-6"
+ "headroom.pricing.litellm_pricing._resolve_litellm_model_uncached",
+ return_value="anthropic/claude-opus-4-6",
) as mock_uncached:
# First call — should invoke uncached resolution
result1 = lp.resolve_litellm_model("claude-opus-4-6")
@@ -165,7 +166,8 @@ class TestModelResolutionCaching:
import headroom.pricing.litellm_pricing as lp
with patch(
- "headroom.pricing.litellm_pricing._resolve_litellm_model_uncached", return_value="resolved/model-a"
+ "headroom.pricing.litellm_pricing._resolve_litellm_model_uncached",
+ return_value="resolved/model-a",
) as mock_uncached:
# Resolve
result1 = lp.resolve_litellm_model("model-a")
@@ -417,12 +419,12 @@ class TestConcurrentSessionSafety:
return f"resolved/{model}"
with patch(
- "headroom.pricing.litellm_pricing._resolve_litellm_model_uncached", side_effect=slow_uncached
+ "headroom.pricing.litellm_pricing._resolve_litellm_model_uncached",
+ side_effect=slow_uncached,
):
# Launch 50 concurrent resolution tasks for the same model
tasks = [
- asyncio.to_thread(lp.resolve_litellm_model, "claude-opus-4-6")
- for _ in range(50)
+ asyncio.to_thread(lp.resolve_litellm_model, "claude-opus-4-6") for _ in range(50)
]
results = await asyncio.gather(*tasks)
@@ -503,8 +505,8 @@ class TestConcurrentSessionSafety:
@pytest.mark.asyncio
async def test_estimate_cost_concurrent_with_caching(self):
"""Multiple concurrent estimate_cost calls should not block each other."""
- from headroom.proxy.server import CostTracker
import headroom.pricing.litellm_pricing as lp
+ from headroom.proxy.server import CostTracker
tracker = CostTracker()