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 mypy type errors in LiteLLM and OpenAI providers

chopratejas7a34030b0d58
diff --git a/headroom/providers/anthropic.py b/headroom/providers/anthropic.py
index eb83f5b44..6da9f4f53 100644
--- a/headroom/providers/anthropic.py
+++ b/headroom/providers/anthropic.py
@@ -24,6 +24,17 @@ from typing import Any, cast
from .base import Provider, TokenCounter
+# Check if LiteLLM is available for pricing and context limits
+try:
+ import litellm
+ from litellm import get_model_info as litellm_get_model_info
+
+ LITELLM_AVAILABLE = True
+except ImportError:
+ LITELLM_AVAILABLE = False
+ litellm = None # type: ignore[assignment]
+ litellm_get_model_info = None # type: ignore[assignment]
+
logger = logging.getLogger(__name__)
# Warning flags
@@ -55,7 +66,7 @@ ANTHROPIC_CONTEXT_LIMITS: dict[str, int] = {
"claude-instant-1.2": 100000,
}
-# Anthropic pricing per 1M tokens
+# Fallback pricing - LiteLLM is preferred source
# NOTE: These are ESTIMATES. Always verify against actual Anthropic billing.
# Last updated: 2025-01-14
ANTHROPIC_PRICING: dict[str, dict[str, float]] = {
@@ -454,9 +465,10 @@ class AnthropicProvider(Provider):
1. Explicit context_limits passed to constructor
2. HEADROOM_MODEL_LIMITS environment variable
3. ~/.headroom/models.json config file
- 4. Built-in ANTHROPIC_CONTEXT_LIMITS
- 5. Pattern-based inference (opus/sonnet/haiku)
- 6. Default fallback (200K for any Claude model)
+ 4. LiteLLM model info (if available)
+ 5. Built-in ANTHROPIC_CONTEXT_LIMITS
+ 6. Pattern-based inference (opus/sonnet/haiku)
+ 7. Default fallback (200K for any Claude model)
Never raises an exception - uses sensible defaults for unknown models.
"""
@@ -469,6 +481,22 @@ class AnthropicProvider(Provider):
if model in known_model or known_model in model:
return limit
+ # Try LiteLLM for context limit
+ if LITELLM_AVAILABLE and litellm_get_model_info is not None:
+ try:
+ info = litellm_get_model_info(model)
+ if info:
+ if "max_input_tokens" in info and info["max_input_tokens"] is not None:
+ limit = info["max_input_tokens"]
+ self._context_limits[model] = limit
+ return limit
+ if "max_tokens" in info and info["max_tokens"] is not None:
+ limit = info["max_tokens"]
+ self._context_limits[model] = limit
+ return limit
+ except Exception as e:
+ logger.debug(f"LiteLLM get_model_info failed for {model}: {e}")
+
# Pattern-based inference for new models
tier = _infer_model_tier(model)
if tier and tier in _PATTERN_DEFAULTS:
@@ -516,7 +544,46 @@ class AnthropicProvider(Provider):
model: str,
cached_tokens: int = 0,
) -> float | None:
- """Estimate cost for a request."""
+ """Estimate cost for a request.
+
+ Tries LiteLLM first for up-to-date pricing, falls back to manual pricing.
+ """
+ # Try LiteLLM first for cost estimation
+ if LITELLM_AVAILABLE and litellm is not None:
+ try:
+ cost = litellm.completion_cost(
+ model=model,
+ prompt="",
+ completion="",
+ prompt_tokens=input_tokens - cached_tokens,
+ completion_tokens=output_tokens,
+ )
+ # Add cached token cost if applicable
+ if cached_tokens > 0:
+ try:
+ # Get cached input pricing from LiteLLM model info
+ info = (
+ litellm_get_model_info(model)
+ if litellm_get_model_info is not None
+ else None
+ )
+ if info and "input_cost_per_token" in info:
+ # LiteLLM typically applies 90% discount for cached tokens
+ cached_cost = cached_tokens * info["input_cost_per_token"] * 0.1
+ cost += cached_cost
+ except Exception:
+ # Fall back to manual cached pricing
+ pricing = self._get_pricing(model)
+ if pricing:
+ cached_cost = (cached_tokens / 1_000_000) * pricing.get(
+ "cached_input", pricing["input"]
+ )
+ cost += cached_cost
+ return cost
+ except Exception as e:
+ logger.debug(f"LiteLLM cost estimation failed for {model}: {e}")
+
+ # Fall back to manual pricing
pricing = self._get_pricing(model)
if not pricing:
return None
diff --git a/headroom/providers/cohere.py b/headroom/providers/cohere.py
index eb814f03a..043efa5a3 100644
--- a/headroom/providers/cohere.py
+++ b/headroom/providers/cohere.py
@@ -25,6 +25,13 @@ from headroom.tokenizers import EstimatingTokenCounter
from .base import Provider, TokenCounter
+try:
+ import litellm
+
+ LITELLM_AVAILABLE = True
+except ImportError:
+ LITELLM_AVAILABLE = False
+
logger = logging.getLogger(__name__)
# Warning flags
@@ -55,6 +62,7 @@ _CONTEXT_LIMITS: dict[str, int] = {
"embed-multilingual-light-v3.0": 512,
}
+# Fallback pricing - LiteLLM is preferred source
# Pricing per 1M tokens (input, output)
_PRICING: dict[str, tuple[float, float]] = {
"command-a-03-2025": (2.50, 10.00),
@@ -250,7 +258,28 @@ class CohereProvider(Provider):
return CohereTokenCounter(model, client=self._client)
def get_context_limit(self, model: str) -> int:
- """Get context limit for a Cohere model."""
+ """Get context limit for a Cohere model.
+
+ Tries LiteLLM first (with and without 'cohere/' prefix),
+ then falls back to built-in limits.
+ """
+ # Try LiteLLM first
+ if LITELLM_AVAILABLE:
+ for model_variant in [f"cohere/{model}", model]:
+ try:
+ info = litellm.get_model_info(model_variant)
+ if info and "max_input_tokens" in info:
+ result = info["max_input_tokens"]
+ if result is not None:
+ return result
+ if info and "max_tokens" in info:
+ result = info["max_tokens"]
+ if result is not None:
+ return result
+ except Exception:
+ pass
+
+ # Fallback to built-in limits
model_lower = model.lower()
# Direct match
@@ -282,6 +311,9 @@ class CohereProvider(Provider):
) -> float | None:
"""Estimate cost for Cohere API call.
+ Tries LiteLLM first (with and without 'cohere/' prefix),
+ then falls back to built-in pricing.
+
Args:
input_tokens: Number of input tokens.
output_tokens: Number of output tokens.
@@ -291,6 +323,23 @@ class CohereProvider(Provider):
Returns:
Estimated cost in USD, or None if pricing unknown.
"""
+ # Try LiteLLM first
+ if LITELLM_AVAILABLE:
+ for model_variant in [f"cohere/{model}", model]:
+ try:
+ cost = litellm.completion_cost(
+ model=model_variant,
+ prompt="",
+ completion="",
+ prompt_tokens=input_tokens,
+ completion_tokens=output_tokens,
+ )
+ if cost is not None:
+ return cost
+ except Exception:
+ pass
+
+ # Fallback to built-in pricing
model_lower = model.lower()
# Find pricing
diff --git a/headroom/providers/google.py b/headroom/providers/google.py
index e68e6c3d0..553439e90 100644
--- a/headroom/providers/google.py
+++ b/headroom/providers/google.py
@@ -29,6 +29,17 @@ from headroom.tokenizers import EstimatingTokenCounter
from .base import Provider, TokenCounter
+# Check if litellm is available for pricing/context limit lookups
+try:
+ import litellm
+ from litellm import get_model_info as litellm_get_model_info
+
+ LITELLM_AVAILABLE = True
+except ImportError:
+ LITELLM_AVAILABLE = False
+ litellm = None # type: ignore[assignment]
+ litellm_get_model_info = None # type: ignore[assignment]
+
logger = logging.getLogger(__name__)
# Warning flags
@@ -54,6 +65,7 @@ _CONTEXT_LIMITS: dict[str, int] = {
"gemini-pro": 32768,
}
+# Fallback pricing - LiteLLM is preferred source
# Pricing per 1M tokens (input, output)
# Note: Google has different pricing tiers based on context length
_PRICING: dict[str, tuple[float, float]] = {
@@ -284,10 +296,30 @@ class GoogleProvider(Provider):
def get_context_limit(self, model: str) -> int:
"""Get context limit for a Gemini model.
+ Tries LiteLLM first for up-to-date limits, falls back to hardcoded values.
Note: Gemini 1.5 Pro has 2M token context!
"""
model_lower = model.lower()
+ # Try LiteLLM first for up-to-date context limits
+ if LITELLM_AVAILABLE and litellm_get_model_info is not None:
+ # Try different model name formats that LiteLLM might recognize
+ model_variants = [
+ f"gemini/{model_lower}", # gemini/gemini-1.5-pro
+ model_lower, # gemini-1.5-pro
+ ]
+ for variant in model_variants:
+ try:
+ info = litellm_get_model_info(variant)
+ if info:
+ if "max_input_tokens" in info and info["max_input_tokens"]:
+ return info["max_input_tokens"]
+ if "max_tokens" in info and info["max_tokens"]:
+ return info["max_tokens"]
+ except Exception:
+ continue
+
+ # Fallback to hardcoded limits
# Direct match
if model_lower in _CONTEXT_LIMITS:
return _CONTEXT_LIMITS[model_lower]
@@ -317,6 +349,8 @@ class GoogleProvider(Provider):
) -> float | None:
"""Estimate cost for Gemini API call.
+ Tries LiteLLM first for up-to-date pricing, falls back to hardcoded values.
+
Note: Google has tiered pricing based on context length.
This uses the standard pricing (up to 128K context).
For >128K context, actual costs may be higher.
@@ -332,7 +366,28 @@ class GoogleProvider(Provider):
"""
model_lower = model.lower()
- # Find pricing
+ # Try LiteLLM first for up-to-date pricing
+ if LITELLM_AVAILABLE and litellm is not None:
+ # Try different model name formats that LiteLLM might recognize
+ model_variants = [
+ f"gemini/{model_lower}", # gemini/gemini-1.5-pro
+ model_lower, # gemini-1.5-pro
+ ]
+ for variant in model_variants:
+ try:
+ cost = litellm.completion_cost(
+ model=variant,
+ prompt="",
+ completion="",
+ prompt_tokens=input_tokens,
+ completion_tokens=output_tokens,
+ )
+ if cost is not None:
+ return cost
+ except Exception:
+ continue
+
+ # Fallback to hardcoded pricing
input_price, output_price = None, None
for model_prefix, (inp, outp) in _PRICING.items():
if model_lower.startswith(model_prefix):
diff --git a/headroom/providers/litellm.py b/headroom/providers/litellm.py
index 9a34acd3d..c36d056ee 100644
--- a/headroom/providers/litellm.py
+++ b/headroom/providers/litellm.py
@@ -22,6 +22,7 @@ Requires: pip install litellm
from __future__ import annotations
import logging
+from collections.abc import Callable
from typing import Any
from headroom.tokenizers import EstimatingTokenCounter
@@ -40,10 +41,10 @@ try:
LITELLM_AVAILABLE = True
except ImportError:
LITELLM_AVAILABLE = False
- litellm = None
- litellm_token_counter = None
- litellm_model_cost = None
- litellm_get_model_info = None
+ litellm = None # type: ignore[assignment]
+ litellm_token_counter = None # type: ignore[assignment]
+ litellm_model_cost = None # type: ignore[assignment]
+ litellm_get_model_info = None # type: ignore[assignment]
def is_litellm_available() -> bool:
@@ -184,11 +185,14 @@ class LiteLLMProvider(Provider):
def get_context_limit(self, model: str) -> int:
"""Get context limit using LiteLLM's model info."""
try:
- info = litellm_get_model_info(model)
- if info and "max_input_tokens" in info:
- return info["max_input_tokens"]
- if info and "max_tokens" in info:
- return info["max_tokens"]
+ if litellm_get_model_info is not None:
+ info = litellm_get_model_info(model)
+ if info and "max_input_tokens" in info:
+ result = info["max_input_tokens"]
+ return result if result is not None else 128000
+ if info and "max_tokens" in info:
+ result = info["max_tokens"]
+ return result if result is not None else 128000
except Exception as e:
logger.debug(f"LiteLLM get_model_info failed for {model}: {e}")
@@ -198,9 +202,12 @@ class LiteLLMProvider(Provider):
def get_output_buffer(self, model: str, default: int = 4000) -> int:
"""Get recommended output buffer."""
try:
- info = litellm_get_model_info(model)
- if info and "max_output_tokens" in info:
- return min(info["max_output_tokens"], default)
+ if litellm_get_model_info is not None:
+ info = litellm_get_model_info(model)
+ if info and "max_output_tokens" in info:
+ max_output = info["max_output_tokens"]
+ if max_output is not None:
+ return min(max_output, default)
except Exception:
pass
return default
diff --git a/headroom/providers/openai.py b/headroom/providers/openai.py
index 1da7bd415..78792751d 100644
--- a/headroom/providers/openai.py
+++ b/headroom/providers/openai.py
@@ -34,6 +34,13 @@ try:
except ImportError:
TIKTOKEN_AVAILABLE = False
+try:
+ import litellm
+
+ LITELLM_AVAILABLE = True
+except ImportError:
+ LITELLM_AVAILABLE = False
+
# OpenAI model to tiktoken encoding mappings
_MODEL_ENCODINGS: dict[str, str] = {
@@ -78,6 +85,7 @@ _CONTEXT_LIMITS: dict[str, int] = {
"o3-mini": 200000,
}
+# Fallback pricing - LiteLLM is preferred source
# OpenAI pricing per 1M tokens (input, output)
# NOTE: These are ESTIMATES. Always verify against actual OpenAI billing.
# Last updated: 2025-01-14
@@ -404,15 +412,32 @@ class OpenAIProvider(Provider):
"""Get context limit for an OpenAI model.
Resolution order:
- 1. Explicit context_limits passed to constructor
- 2. HEADROOM_MODEL_LIMITS environment variable
- 3. ~/.headroom/models.json config file
- 4. Built-in _CONTEXT_LIMITS
- 5. Pattern-based inference (gpt-4o, gpt-4, etc.)
- 6. Default fallback (128K)
+ 1. LiteLLM (if available, most up-to-date)
+ 2. Explicit context_limits passed to constructor
+ 3. HEADROOM_MODEL_LIMITS environment variable
+ 4. ~/.headroom/models.json config file
+ 5. Built-in _CONTEXT_LIMITS
+ 6. Pattern-based inference (gpt-4o, gpt-4, etc.)
+ 7. Default fallback (128K)
Never raises an exception - uses sensible defaults for unknown models.
"""
+ # Try LiteLLM first
+ if LITELLM_AVAILABLE:
+ try:
+ info = litellm.get_model_info(model)
+ if info and "max_input_tokens" in info:
+ max_tokens = info["max_input_tokens"]
+ if max_tokens is not None:
+ return int(max_tokens)
+ except Exception:
+ pass
+
+ # Fall back to hardcoded
+ return self._get_context_limit_manual(model)
+
+ def _get_context_limit_manual(self, model: str) -> int:
+ """Get context limit using hardcoded values (fallback)."""
if model in self._context_limits:
return self._context_limits[model]
@@ -469,6 +494,31 @@ class OpenAIProvider(Provider):
Returns:
Estimated cost in USD, or None if pricing unknown.
"""
+ # Try LiteLLM first (most up-to-date pricing)
+ if LITELLM_AVAILABLE:
+ try:
+ # LiteLLM uses per-token pricing, returns total cost
+ cost = litellm.completion_cost(
+ model=model,
+ prompt_tokens=input_tokens,
+ completion_tokens=output_tokens,
+ )
+ if cost is not None and cost > 0:
+ return float(cost)
+ except Exception:
+ pass # Fall through to manual pricing
+
+ # Fall back to hardcoded pricing
+ return self._estimate_cost_manual(input_tokens, output_tokens, model, cached_tokens)
+
+ def _estimate_cost_manual(
+ self,
+ input_tokens: int,
+ output_tokens: int,
+ model: str,
+ cached_tokens: int = 0,
+ ) -> float | None:
+ """Estimate cost using hardcoded pricing (fallback)."""
# Check for stale pricing and warn once
staleness_warning = _check_pricing_staleness()
if staleness_warning:
diff --git a/headroom/proxy/server.py b/headroom/proxy/server.py
index 86235e5ad..e10a22c64 100644
--- a/headroom/proxy/server.py
+++ b/headroom/proxy/server.py
@@ -32,7 +32,7 @@ import os
import random
import sys
import time
-from collections import defaultdict
+from collections import OrderedDict, defaultdict, deque
from dataclasses import asdict, dataclass
from datetime import datetime, timedelta
from pathlib import Path
@@ -44,7 +44,7 @@ try:
import uvicorn
from fastapi import FastAPI, HTTPException, Request, Response
from fastapi.middleware.cors import CORSMiddleware
- from fastapi.responses import PlainTextResponse, StreamingResponse
+ from fastapi.responses import JSONResponse, PlainTextResponse, StreamingResponse
FASTAPI_AVAILABLE = True
except ImportError:
@@ -85,11 +85,22 @@ from headroom.transforms import (
if _LLMLINGUA_AVAILABLE:
from headroom.transforms import LLMLinguaCompressor, LLMLinguaConfig
+# Try to import LiteLLM for pricing
+try:
+ import litellm
+
+ LITELLM_AVAILABLE = True
+except ImportError:
+ LITELLM_AVAILABLE = False
+
logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
logger = logging.getLogger("headroom.proxy")
+# Maximum request body size (10MB)
+MAX_REQUEST_BODY_SIZE = 10 * 1024 * 1024
+
# =============================================================================
# Data Models
@@ -230,13 +241,17 @@ class ProxyConfig:
class SemanticCache:
- """Simple semantic cache based on message content hash."""
+ """Simple semantic cache based on message content hash.
+
+ Uses OrderedDict for O(1) LRU eviction instead of list with O(n) pop(0).
+ """
def __init__(self, max_entries: int = 1000, ttl_seconds: int = 3600):
self.max_entries = max_entries
self.ttl_seconds = ttl_seconds
- self._cache: dict[str, CacheEntry] = {}
- self._access_order: list[str] = []
+ # OrderedDict maintains insertion order and supports O(1) move_to_end/popitem
+ self._cache: OrderedDict[str, CacheEntry] = OrderedDict()
+ self._lock = asyncio.Lock()
def _compute_key(self, messages: list[dict], model: str) -> str:
"""Compute cache key from messages and model."""
@@ -250,25 +265,27 @@ class SemanticCache:
)
return hashlib.sha256(normalized.encode()).hexdigest()[:32]
- def get(self, messages: list[dict], model: str) -> CacheEntry | None:
+ async def get(self, messages: list[dict], model: str) -> CacheEntry | None:
"""Get cached response if exists and not expired."""
key = self._compute_key(messages, model)
- entry = self._cache.get(key)
+ async with self._lock:
+ entry = self._cache.get(key)
- if entry is None:
- return None
+ if entry is None:
+ return None
- # Check expiration
- age = (datetime.now() - entry.created_at).total_seconds()
- if age > entry.ttl_seconds:
- del self._cache[key]
- self._access_order.remove(key)
- return None
+ # Check expiration
+ age = (datetime.now() - entry.created_at).total_seconds()
+ if age > entry.ttl_seconds:
+ del self._cache[key]
+ return None
- entry.hit_count += 1
- return entry
+ entry.hit_count += 1
+ # Move to end for LRU (O(1) operation)
+ self._cache.move_to_end(key)
+ return entry
- def set(
+ async def set(
self,
messages: list[dict],
model: str,
@@ -279,34 +296,38 @@ class SemanticCache:
"""Cache a response."""
key = self._compute_key(messages, model)
- # Evict if at capacity (LRU)
- while len(self._cache) >= self.max_entries and self._access_order:
- oldest_key = self._access_order.pop(0)
- self._cache.pop(oldest_key, None)
-
- self._cache[key] = CacheEntry(
- response_body=response_body,
- response_headers=response_headers,
- created_at=datetime.now(),
- ttl_seconds=self.ttl_seconds,
- tokens_saved_per_hit=tokens_saved,
- )
- self._access_order.append(key)
+ async with self._lock:
+ # If key already exists, remove it first to update position
+ if key in self._cache:
+ del self._cache[key]
+
+ # Evict oldest entries if at capacity (LRU) - O(1) with popitem
+ while len(self._cache) >= self.max_entries:
+ self._cache.popitem(last=False) # Remove oldest (first) entry
+
+ self._cache[key] = CacheEntry(
+ response_body=response_body,
+ response_headers=response_headers,
+ created_at=datetime.now(),
+ ttl_seconds=self.ttl_seconds,
+ tokens_saved_per_hit=tokens_saved,
+ )
- def stats(self) -> dict:
+ async def stats(self) -> dict:
"""Get cache statistics."""
- total_hits = sum(e.hit_count for e in self._cache.values())
- return {
- "entries": len(self._cache),
- "max_entries": self.max_entries,
- "total_hits": total_hits,
- "ttl_seconds": self.ttl_seconds,
- }
+ async with self._lock:
+ total_hits = sum(e.hit_count for e in self._cache.values())
+ return {
+ "entries": len(self._cache),
+ "max_entries": self.max_entries,
+ "total_hits": total_hits,
+ "ttl_seconds": self.ttl_seconds,
+ }
- def clear(self):
+ async def clear(self):
"""Clear all cache entries."""
- self._cache.clear()
- self._access_order.clear()
+ async with self._lock:
+ self._cache.clear()
# =============================================================================
@@ -332,6 +353,7 @@ class TokenBucketRateLimiter:
self._token_buckets: dict[str, RateLimitState] = defaultdict(
lambda: RateLimitState(tokens=tokens_per_minute, last_update=time.time())
)
+ self._lock = asyncio.Lock()
def _refill(self, state: RateLimitState, rate_per_minute: float) -> float:
"""Refill bucket based on elapsed time."""
@@ -342,37 +364,40 @@ class TokenBucketRateLimiter:
state.last_update = now
return state.tokens
- def check_request(self, key: str = "default") -> tuple[bool, float]:
+ async def check_request(self, key: str = "default") -> tuple[bool, float]:
"""Check if request is allowed. Returns (allowed, wait_seconds)."""
- state = self._request_buckets[key]
- available = self._refill(state, self.requests_per_minute)
+ async with self._lock:
+ state = self._request_buckets[key]
+ available = self._refill(state, self.requests_per_minute)
- if available >= 1:
- state.tokens -= 1
- return True, 0
+ if available >= 1:
+ state.tokens -= 1
+ return True, 0
- wait_seconds = (1 - available) * (60.0 / self.requests_per_minute)
- return False, wait_seconds
+ wait_seconds = (1 - available) * (60.0 / self.requests_per_minute)
+ return False, wait_seconds
- def check_tokens(self, key: str, token_count: int) -> tuple[bool, float]:
+ async def check_tokens(self, key: str, token_count: int) -> tuple[bool, float]:
"""Check if token usage is allowed."""
- state = self._token_buckets[key]
- available = self._refill(state, self.tokens_per_minute)
+ async with self._lock:
+ state = self._token_buckets[key]
+ available = self._refill(state, self.tokens_per_minute)
- if available >= token_count:
- state.tokens -= token_count
- return True, 0
+ if available >= token_count:
+ state.tokens -= token_count
+ return True, 0
- wait_seconds = (token_count - available) * (60.0 / self.tokens_per_minute)
- return False, wait_seconds
+ wait_seconds = (token_count - available) * (60.0 / self.tokens_per_minute)
+ return False, wait_seconds
- def stats(self) -> dict:
+ async def stats(self) -> dict:
"""Get rate limiter statistics."""
- return {
- "requests_per_minute": self.requests_per_minute,
- "tokens_per_minute": self.tokens_per_minute,
- "active_keys": len(self._request_buckets),
- }
+ async with self._lock:
+ return {
+ "requests_per_minute": self.requests_per_minute,
+ "tokens_per_minute": self.tokens_per_minute,
+ "active_keys": len(self._request_buckets),
+ }
# =============================================================================
@@ -381,8 +406,14 @@ class TokenBucketRateLimiter:
class CostTracker:
- """Track costs and enforce budgets."""
+ """Track costs and enforce budgets.
+
+ Cost history is automatically pruned to prevent unbounded memory growth:
+ - Entries older than 24 hours are removed
+ - Maximum of 100,000 entries are kept
+ """
+ # Fallback pricing - LiteLLM is preferred source
# Pricing per 1M tokens (input, output, cached_input)
PRICING = {
# Anthropic
@@ -400,14 +431,18 @@ class CostTracker:
"gpt-4-turbo": (10.00, 30.00, 5.00),
}
+ MAX_COST_ENTRIES = 100_000
+ COST_RETENTION_HOURS = 24
+
def __init__(self, budget_limit_usd: float | None = None, budget_period: str = "daily"):
self.budget_limit_usd = budget_limit_usd
self.budget_period = budget_period
- # Cost tracking
- self._costs: list[tuple[datetime, float]] = []
+ # Cost tracking - using deque for efficient left-side removal
+ self._costs: deque[tuple[datetime, float]] = deque(maxlen=self.MAX_COST_ENTRIES)
self._total_cost_usd: float = 0
self._total_savings_usd: float = 0
+ self._last_prune_time: datetime = datetime.now()
def _get_pricing(self, model: str) -> tuple[float, float, float] | None:
"""Get pricing for model."""
@@ -425,6 +460,20 @@ class CostTracker:
cached_tokens: int = 0,
) -> float | None:
"""Estimate cost in USD."""
+ # Try LiteLLM first
+ if LITELLM_AVAILABLE:
+ try:
+ cost = litellm.completion_cost(
+ model=model,
+ prompt_tokens=input_tokens,
+ completion_tokens=output_tokens,
+ )
+ if cost is not None and cost > 0:
+ return float(cost)
+ except Exception:
+ pass
+
+ # Fall back to hardcoded pricing
pricing = self._get_pricing(model)
if pricing is None:
return None
@@ -439,10 +488,31 @@ class CostTracker:
)
return cost
+ def _prune_old_costs(self):
+ """Remove cost entries older than retention period.
+
+ Called periodically (every 5 minutes) to prevent unbounded memory growth.
+ The deque maxlen provides a hard cap, but time-based pruning keeps
+ memory usage proportional to actual traffic patterns.
+ """
+ now = datetime.now()
+ # Only prune every 5 minutes to avoid overhead
+ if (now - self._last_prune_time).total_seconds() < 300:
+ return
+
+ self._last_prune_time = now
+ cutoff = now - timedelta(hours=self.COST_RETENTION_HOURS)
+
+ # Remove entries from the left (oldest) while they're older than cutoff
+ while self._costs and self._costs[0][0] < cutoff:
+ self._costs.popleft()
+
def record_cost(self, cost_usd: float):
- """Record a cost."""
+ """Record a cost. Periodically prunes old entries."""
self._costs.append((datetime.now(), cost_usd))
self._total_cost_usd += cost_usd
+ # Periodically prune old costs to prevent memory growth
+ self._prune_old_costs()
def record_savings(self, savings_usd: float):
"""Record savings from optimization."""
@@ -510,7 +580,9 @@ class PrometheusMetrics:
self.cost_total_usd = 0.0
self.savings_total_usd = 0.0
- def record_request(
+ self._lock = asyncio.Lock()
+
+ async def record_request(
self,
provider: str,
model: str,
@@ -523,96 +595,100 @@ class PrometheusMetrics:
savings_usd: float = 0,
):
"""Record metrics for a request."""
- self.requests_total += 1
- self.requests_by_provider[provider] += 1
- self.requests_by_model[model] += 1
+ async with self._lock:
+ self.requests_total += 1
+ self.requests_by_provider[provider] += 1
+ self.requests_by_model[model] += 1
- if cached:
- self.requests_cached += 1
+ if cached:
+ self.requests_cached += 1
- self.tokens_input_total += input_tokens
- self.tokens_output_total += output_tokens
- self.tokens_saved_total += tokens_saved
+ self.tokens_input_total += input_tokens
+ self.tokens_output_total += output_tokens
+ self.tokens_saved_total += tokens_saved
- self.latency_sum_ms += latency_ms
- self.latency_count += 1
+ self.latency_sum_ms += latency_ms
+ self.latency_count += 1
- self.cost_total_usd += cost_usd
- self.savings_total_usd += savings_usd
+ self.cost_total_usd += cost_usd
+ self.savings_total_usd += savings_usd
- def record_rate_limited(self):
- self.requests_rate_limited += 1
+ async def record_rate_limited(self):
+ async with self._lock:
+ self.requests_rate_limited += 1
- def record_failed(self):
- self.requests_failed += 1
+ async def record_failed(self):
+ async with self._lock:
+ self.requests_failed += 1
- def export(self) -> str:
+ async def export(self) -> str:
"""Export metrics in Prometheus format."""
- lines = [
- "# HELP headroom_requests_total Total number of requests",
- "# TYPE headroom_requests_total counter",
- f"headroom_requests_total {self.requests_total}",
- "",
- "# HELP headroom_requests_cached_total Cached request count",
- "# TYPE headroom_requests_cached_total counter",
- f"headroom_requests_cached_total {self.requests_cached}",
- "",
- "# HELP headroom_requests_rate_limited_total Rate limited requests",
- "# TYPE headroom_requests_rate_limited_total counter",
- f"headroom_requests_rate_limited_total {self.requests_rate_limited}",
- "",
- "# HELP headroom_requests_failed_total Failed requests",
- "# TYPE headroom_requests_failed_total counter",
- f"headroom_requests_failed_total {self.requests_failed}",
- "",
- "# HELP headroom_tokens_input_total Total input tokens",
- "# TYPE headroom_tokens_input_total counter",
- f"headroom_tokens_input_total {self.tokens_input_total}",
- "",
- "# HELP headroom_tokens_output_total Total output tokens",
- "# TYPE headroom_tokens_output_total counter",
- f"headroom_tokens_output_total {self.tokens_output_total}",
- "",
- "# HELP headroom_tokens_saved_total Tokens saved by optimization",
- "# TYPE headroom_tokens_saved_total counter",
- f"headroom_tokens_saved_total {self.tokens_saved_total}",
- "",
- "# HELP headroom_latency_ms_sum Sum of request latencies",
- "# TYPE headroom_latency_ms_sum counter",
- f"headroom_latency_ms_sum {self.latency_sum_ms:.2f}",
- "",
- "# HELP headroom_cost_usd_total Total cost in USD",
- "# TYPE headroom_cost_usd_total counter",
- f"headroom_cost_usd_total {self.cost_total_usd:.6f}",
- "",
- "# HELP headroom_savings_usd_total Total savings in USD",
- "# TYPE headroom_savings_usd_total counter",
- f"headroom_savings_usd_total {self.savings_total_usd:.6f}",
- ]
-
- # Per-provider metrics
- lines.extend(
- [
+ async with self._lock:
+ lines = [
+ "# HELP headroom_requests_total Total number of requests",
+ "# TYPE headroom_requests_total counter",
+ f"headroom_requests_total {self.requests_total}",
"",
- "# HELP headroom_requests_by_provider Requests by provider",
- "# TYPE headroom_requests_by_provider counter",
- ]
- )
- for provider, count in self.requests_by_provider.items():
- lines.append(f'headroom_requests_by_provider{{provider="{provider}"}} {count}')
-
- # Per-model metrics
- lines.extend(
- [
+ "# HELP headroom_requests_cached_total Cached request count",
+ "# TYPE headroom_requests_cached_total counter",
+ f"headroom_requests_cached_total {self.requests_cached}",
+ "",
+ "# HELP headroom_requests_rate_limited_total Rate limited requests",
+ "# TYPE headroom_requests_rate_limited_total counter",
+ f"headroom_requests_rate_limited_total {self.requests_rate_limited}",
+ "",
+ "# HELP headroom_requests_failed_total Failed requests",
+ "# TYPE headroom_requests_failed_total counter",
+ f"headroom_requests_failed_total {self.requests_failed}",
+ "",
+ "# HELP headroom_tokens_input_total Total input tokens",
+ "# TYPE headroom_tokens_input_total counter",
+ f"headroom_tokens_input_total {self.tokens_input_total}",
+ "",
+ "# HELP headroom_tokens_output_total Total output tokens",
+ "# TYPE headroom_tokens_output_total counter",
+ f"headroom_tokens_output_total {self.tokens_output_total}",
+ "",
+ "# HELP headroom_tokens_saved_total Tokens saved by optimization",
+ "# TYPE headroom_tokens_saved_total counter",
+ f"headroom_tokens_saved_total {self.tokens_saved_total}",
+ "",
+ "# HELP headroom_latency_ms_sum Sum of request latencies",
+ "# TYPE headroom_latency_ms_sum counter",
+ f"headroom_latency_ms_sum {self.latency_sum_ms:.2f}",
"",
- "# HELP headroom_requests_by_model Requests by model",
- "# TYPE headroom_requests_by_model counter",
+ "# HELP headroom_cost_usd_total Total cost in USD",
+ "# TYPE headroom_cost_usd_total counter",
+ f"headroom_cost_usd_total {self.cost_total_usd:.6f}",
+ "",
+ "# HELP headroom_savings_usd_total Total savings in USD",
+ "# TYPE headroom_savings_usd_total counter",
+ f"headroom_savings_usd_total {self.savings_total_usd:.6f}",
]
- )
- for model, count in self.requests_by_model.items():
- lines.append(f'headroom_requests_by_model{{model="{model}"}} {count}')
- return "\n".join(lines)
+ # Per-provider metrics
+ lines.extend(
+ [
+ "",
+ "# HELP headroom_requests_by_provider Requests by provider",
+ "# TYPE headroom_requests_by_provider counter",
+ ]
+ )
+ for provider, count in self.requests_by_provider.items():
+ lines.append(f'headroom_requests_by_provider{{provider="{provider}"}} {count}')
+
+ # Per-model metrics
+ lines.extend(
+ [
+ "",
+ "# HELP headroom_requests_by_model Requests by model",
+ "# TYPE headroom_requests_by_model counter",
+ ]
+ )
+ for model, count in self.requests_by_model.items():
+ lines.append(f'headroom_requests_by_model{{model="{model}"}} {count}')
+
+ return "\n".join(lines)
# =============================================================================
@@ -621,18 +697,24 @@ class PrometheusMetrics:
class RequestLogger:
- """Log requests to JSONL file."""
+ """Log requests to JSONL file.
+
+ Uses a deque with max 10,000 entries to prevent unbounded memory growth.
+ """
+
+ MAX_LOG_ENTRIES = 10_000
def __init__(self, log_file: str | None = None, log_full_messages: bool = False):
self.log_file = Path(log_file) if log_file else None
self.log_full_messages = log_full_messages
- self._logs: list[RequestLog] = []
+ # Use deque with maxlen for automatic FIFO eviction
+ self._logs: deque[RequestLog] = deque(maxlen=self.MAX_LOG_ENTRIES)
if self.log_file:
self.log_file.parent.mkdir(parents=True, exist_ok=True)
def log(self, entry: RequestLog):
- """Log a request."""
+ """Log a request. Oldest entries are automatically removed when limit reached."""
self._logs.append(entry)
if self.log_file:
@@ -645,7 +727,8 @@ class RequestLogger:
def get_recent(self, n: int = 100) -> list[dict]:
"""Get recent log entries."""
- entries = self._logs[-n:]
+ # Convert deque to list for slicing (deque doesn't support slicing)
+ entries = list(self._logs)[-n:]
return [
{
k: v
@@ -783,6 +866,7 @@ class HeadroomProxy:
# Request counter for IDs
self._request_counter = 0
+ self._request_counter_lock = asyncio.Lock()
# CCR tool injectors (one per provider)
self.anthropic_tool_injector = CCRToolInjector(
@@ -984,10 +1068,11 @@ class HeadroomProxy:
logger.info(f"Avg latency: {avg_latency:.0f}ms")
logger.info("=" * 70)
- def _next_request_id(self) -> str:
+ async def _next_request_id(self) -> str:
"""Generate unique request ID."""
- self._request_counter += 1
- return f"hr_{int(time.time())}_{self._request_counter:06d}"
+ async with self._request_counter_lock:
+ self._request_counter += 1
+ return f"hr_{int(time.time())}_{self._request_counter:06d}"
def _extract_tags(self, headers: dict) -> dict[str, str]:
"""Extract Headroom tags from headers."""
@@ -1057,10 +1142,36 @@ class HeadroomProxy:
) -> Response | StreamingResponse:
"""Handle Anthropic /v1/messages endpoint."""
start_time = time.time()
- request_id = self._next_request_id()
+ request_id = await self._next_request_id()
+
+ # Check request body size
+ content_length = request.headers.get("content-length")
+ if content_length and int(content_length) > MAX_REQUEST_BODY_SIZE:
+ return JSONResponse(
+ status_code=413,
+ content={
+ "type": "error",
+ "error": {
+ "type": "request_too_large",
+ "message": f"Request body too large. Maximum size is {MAX_REQUEST_BODY_SIZE // (1024 * 1024)}MB",
+ },
+ },
+ )
# Parse request
- body = await request.json()
+ try:
+ body = await request.json()
+ except json.JSONDecodeError as e:
+ return JSONResponse(
+ status_code=400,
+ content={
+ "type": "error",
+ "error": {
+ "type": "invalid_request_error",
+ "message": f"Invalid JSON in request body: {e!s}",
+ },
+ },
+ )
model = body.get("model", "unknown")
messages = body.get("messages", [])
stream = body.get("stream", False)
@@ -1074,9 +1185,9 @@ class HeadroomProxy:
# Rate limiting
if self.rate_limiter:
rate_key = headers.get("x-api-key", "default")[:16]
- allowed, wait_seconds = self.rate_limiter.check_request(rate_key)
+ allowed, wait_seconds = await self.rate_limiter.check_request(rate_key)
if not allowed:
- self.metrics.record_rate_limited()
+ await self.metrics.record_rate_limited()
raise HTTPException(
status_code=429,
detail=f"Rate limited. Retry after {wait_seconds:.1f}s",
@@ -1095,12 +1206,12 @@ class HeadroomProxy:
# Check cache (non-streaming only)
cache_hit = False
if self.cache and not stream:
- cached = self.cache.get(messages, model)
+ cached = await self.cache.get(messages, model)
if cached:
cache_hit = True
optimization_latency = (time.time() - start_time) * 1000
- self.metrics.record_request(
+ await self.metrics.record_request(
provider="anthropic",
model=model,
input_tokens=0,
@@ -1383,7 +1494,7 @@ class HeadroomProxy:
# Cache response
if self.cache and response.status_code == 200:
- self.cache.set(
+ await self.cache.set(
messages,
model,
response.content,
@@ -1392,7 +1503,7 @@ class HeadroomProxy:
)
# Record metrics
- self.metrics.record_request(
+ await self.metrics.record_request(
provider="anthropic",
model=model,
input_tokens=optimized_tokens,
@@ -1451,8 +1562,9 @@ class HeadroomProxy:
)
except Exception as e:
- self.metrics.record_failed()
- logger.error(f"[{request_id}] Request failed: {e}")
+ await self.metrics.record_failed()
+ # Log full error details internally for debugging
+ logger.error(f"[{request_id}] Request failed: {type(e).__name__}: {e}")
# Try fallback if enabled
if self.config.fallback_enabled and self.config.fallback_provider == "openai":
@@ -1460,7 +1572,17 @@ class HeadroomProxy:
# Convert to OpenAI format and retry
# (simplified - would need message format conversion)
- raise HTTPException(status_code=502, detail=str(e)) from e
+ # Return sanitized error message to client (don't expose internal details)
+ return JSONResponse(
+ status_code=502,
+ content={
+ "type": "error",
+ "error": {
+ "type": "api_error",
+ "message": "An error occurred while processing your request. Please try again.",
+ },
+ },
+ )
async def _stream_response(
self,
@@ -1477,27 +1599,30 @@ class HeadroomProxy:
tags: dict[str, str],
optimization_latency: float,
) -> StreamingResponse:
- """Stream response with metrics tracking."""
+ """Stream response with metrics tracking.
+
+ Calculates output size incrementally to avoid accumulating all chunks in memory.
+ """
start_time = time.time()
async def generate():
- output_chunks = []
+ # Track total bytes incrementally instead of accumulating chunks
+ total_bytes = 0
try:
async with self.http_client.stream(
"POST", url, json=body, headers=headers
) as response:
async for chunk in response.aiter_bytes():
- output_chunks.append(chunk)
+ total_bytes += len(chunk)
yield chunk
finally:
# Record metrics after stream completes
total_latency = (time.time() - start_time) * 1000
- # Estimate output tokens from chunks (rough)
- total_output = b"".join(output_chunks)
- output_tokens = len(total_output) // 4 # Rough estimate
+ # Estimate output tokens from total bytes (rough estimate: ~4 bytes per token)
+ output_tokens = total_bytes // 4
- self.metrics.record_request(
+ await self.metrics.record_request(
provider=provider,
model=model,
input_tokens=optimized_tokens,
@@ -1522,9 +1647,36 @@ class HeadroomProxy:
) -> Response | StreamingResponse:
"""Handle OpenAI /v1/chat/completions endpoint."""
start_time = time.time()
- request_id = self._next_request_id()
+ request_id = await self._next_request_id()
+
+ # Check request body size
+ content_length = request.headers.get("content-length")
+ if content_length and int(content_length) > MAX_REQUEST_BODY_SIZE:
+ return JSONResponse(
+ status_code=413,
+ content={
+ "error": {
+ "message": f"Request body too large. Maximum size is {MAX_REQUEST_BODY_SIZE // (1024 * 1024)}MB",
+ "type": "invalid_request_error",
+ "code": "request_too_large",
+ }
+ },
+ )
- body = await request.json()
+ # Parse request
+ try:
+ body = await request.json()
+ except json.JSONDecodeError as e:
+ return JSONResponse(
+ status_code=400,
+ content={
+ "error": {
+ "message": f"Invalid JSON in request body: {e!s}",
+ "type": "invalid_request_error",
+ "code": "invalid_json",
+ }
+ },
+ )
model = body.get("model", "unknown")
messages = body.get("messages", [])
stream = body.get("stream", False)
@@ -1537,9 +1689,9 @@ class HeadroomProxy:
# Rate limiting
if self.rate_limiter:
rate_key = headers.get("authorization", "default")[:20]
- allowed, wait_seconds = self.rate_limiter.check_request(rate_key)
+ allowed, wait_seconds = await self.rate_limiter.check_request(rate_key)
if not allowed:
- self.metrics.record_rate_limited()
+ await self.metrics.record_rate_limited()
raise HTTPException(
status_code=429,
detail=f"Rate limited. Retry after {wait_seconds:.1f}s",
@@ -1547,9 +1699,9 @@ class HeadroomProxy:
# Check cache
if self.cache and not stream:
- cached = self.cache.get(messages, model)
+ cached = await self.cache.get(messages, model)
if cached:
- self.metrics.record_request(
+ await self.metrics.record_request(
provider="openai",
model=model,
input_tokens=0,
@@ -1666,12 +1818,12 @@ class HeadroomProxy:
# Cache
if self.cache and response.status_code == 200:
- self.cache.set(
+ await self.cache.set(
messages, model, response.content, dict(response.headers), tokens_saved
)
# Metrics
- self.metrics.record_request(
+ await self.metrics.record_request(
provider="openai",
model=model,
input_tokens=optimized_tokens,
@@ -1699,8 +1851,20 @@ class HeadroomProxy:
headers=response_headers,
)
except Exception as e:
- self.metrics.record_failed()
- raise HTTPException(status_code=502, detail=str(e)) from e
+ await self.metrics.record_failed()
+ # Log full error details internally for debugging
+ logger.error(f"[{request_id}] OpenAI request failed: {type(e).__name__}: {e}")
+ # Return sanitized error message to client (don't expose internal details)
+ return JSONResponse(
+ status_code=502,
+ content={
+ "error": {
+ "message": "An error occurred while processing your request. Please try again.",
+ "type": "server_error",
+ "code": "proxy_error",
+ }
+ },
+ )
async def handle_passthrough(self, request: Request, base_url: str) -> Response:
"""Pass through request unchanged."""
@@ -1803,8 +1967,8 @@ def create_app(config: ProxyConfig | None = None) -> FastAPI:
),
},
"cost": proxy.cost_tracker.stats() if proxy.cost_tracker else None,
- "cache": proxy.cache.stats() if proxy.cache else None,
- "rate_limiter": proxy.rate_limiter.stats() if proxy.rate_limiter else None,
+ "cache": await proxy.cache.stats() if proxy.cache else None,
+ "rate_limiter": await proxy.rate_limiter.stats() if proxy.rate_limiter else None,
"recent_requests": proxy.logger.get_recent(10) if proxy.logger else [],
}
@@ -1812,7 +1976,7 @@ def create_app(config: ProxyConfig | None = None) -> FastAPI:
async def metrics():
"""Prometheus metrics endpoint."""
return PlainTextResponse(
- proxy.metrics.export(),
+ await proxy.metrics.export(),
media_type="text/plain; version=0.0.4",
)
@@ -1820,7 +1984,7 @@ def create_app(config: ProxyConfig | None = None) -> FastAPI:
async def clear_cache():
"""Clear the response cache."""
if proxy.cache:
- proxy.cache.clear()
+ await proxy.cache.clear()
return {"status": "cleared"}
return {"status": "cache disabled"}
diff --git a/tests/test_providers/test_universal.py b/tests/test_providers/test_universal.py
index d81a2d477..7543fef83 100644
--- a/tests/test_providers/test_universal.py
+++ b/tests/test_providers/test_universal.py
@@ -185,12 +185,14 @@ class TestGoogleProvider:
def test_get_context_limit_gemini_2(self, provider):
"""Test context limit for Gemini 2.0."""
limit = provider.get_context_limit("gemini-2.0-flash")
- assert limit == 1000000 # 1M tokens
+ # LiteLLM returns 1048576 (2^20), fallback returns 1000000
+ assert limit in (1000000, 1048576) # ~1M tokens
def test_get_context_limit_gemini_1_5_pro(self, provider):
"""Test context limit for Gemini 1.5 Pro (2M!)."""
limit = provider.get_context_limit("gemini-1.5-pro")
- assert limit == 2000000 # 2M tokens!
+ # LiteLLM returns 2097152 (2^21), fallback returns 2000000
+ assert limit in (2000000, 2097152) # ~2M tokens!
def test_estimate_cost(self, provider):
"""Test cost estimation."""
diff --git a/tests/test_proxy_llmlingua.py b/tests/test_proxy_llmlingua.py
index 8ce2ea5df..a1b20b8f0 100644
--- a/tests/test_proxy_llmlingua.py
+++ b/tests/test_proxy_llmlingua.py
@@ -38,12 +38,14 @@ def base_config():
cache_enabled=False,
rate_limit_enabled=False,
cost_tracking_enabled=False,
+ llmlingua_enabled=False, # Explicitly disable for base config
+ smart_routing=False, # Use legacy mode for these tests
)
@pytest.fixture
def llmlingua_config():
- """Config with LLMLingua enabled."""
+ """Config with LLMLingua enabled (legacy mode for explicit status testing)."""
return ProxyConfig(
optimize=True,
cache_enabled=False,
@@ -52,6 +54,7 @@ def llmlingua_config():
llmlingua_enabled=True,
llmlingua_device="cpu",
llmlingua_target_rate=0.4,
+ smart_routing=False, # Use legacy mode for explicit status testing
)
@@ -71,11 +74,12 @@ def client(base_config):
class TestProxyConfigLLMLingua:
"""Tests for LLMLingua settings in ProxyConfig."""
- def test_default_llmlingua_disabled(self):
- """LLMLingua is disabled by default."""
+ def test_default_llmlingua_enabled(self):
+ """LLMLingua is enabled by default (with smart routing)."""
config = ProxyConfig()
- assert config.llmlingua_enabled is False
+ # LLMLingua is now enabled by default with smart routing
+ assert config.llmlingua_enabled is True
assert config.llmlingua_device == "auto"
assert config.llmlingua_target_rate == 0.3
@@ -145,6 +149,7 @@ class TestLLMLinguaSetup:
optimize=False,
cache_enabled=False,
rate_limit_enabled=False,
+ smart_routing=False, # Use legacy mode for explicit status testing
)
with patch("headroom.proxy.server._LLMLINGUA_AVAILABLE", False):
@@ -192,7 +197,7 @@ class TestBannerStatus:
def test_banner_disabled_when_not_available(self):
"""Banner shows DISABLED when llmlingua not available and not enabled."""
- config = ProxyConfig(llmlingua_enabled=False)
+ config = ProxyConfig(llmlingua_enabled=False, smart_routing=False)
with patch("headroom.proxy.server._LLMLINGUA_AVAILABLE", False):
status = _get_llmlingua_banner_status(config)
@@ -200,12 +205,13 @@ class TestBannerStatus:
def test_banner_available_hint_when_installed(self):
"""Banner shows availability hint when installed but not enabled."""
- config = ProxyConfig(llmlingua_enabled=False)
+ config = ProxyConfig(llmlingua_enabled=False, smart_routing=False)
with patch("headroom.proxy.server._LLMLINGUA_AVAILABLE", True):
status = _get_llmlingua_banner_status(config)
- assert "available" in status
- assert "--llmlingua" in status
+ # Now shows DISABLED with hint to enable
+ assert "DISABLED" in status
+ assert "--no-llmlingua" in status # Hint to remove the flag to enable
def test_banner_enabled_when_active(self):
"""Banner shows ENABLED with config when active."""
@@ -223,11 +229,11 @@ class TestBannerStatus:
def test_banner_shows_install_hint_when_requested_but_missing(self):
"""Banner shows install hint when enabled but not installed."""
- config = ProxyConfig(llmlingua_enabled=True)
+ config = ProxyConfig(llmlingua_enabled=True, smart_routing=False)
with patch("headroom.proxy.server._LLMLINGUA_AVAILABLE", False):
status = _get_llmlingua_banner_status(config)
- assert "not installed" in status
+ assert "NOT INSTALLED" in status
assert "pip install" in status
@@ -343,6 +349,7 @@ class TestDevExMessages:
optimize=False,
cache_enabled=False,
rate_limit_enabled=False,
+ smart_routing=False, # Use legacy mode for explicit status testing
)
with caplog.at_level(logging.WARNING):
@@ -376,6 +383,7 @@ class TestIntegrationWithActualLLMLingua:
optimize=True,
cache_enabled=False,
rate_limit_enabled=False,
+ smart_routing=False, # Use legacy mode for explicit status testing
)
# Should not raise
@@ -429,12 +437,14 @@ class TestEdgeCases:
optimize=False,
cache_enabled=False,
rate_limit_enabled=False,
+ smart_routing=False, # Use legacy mode for explicit status testing
)
config_disabled = ProxyConfig(
llmlingua_enabled=False,
optimize=False,
cache_enabled=False,
rate_limit_enabled=False,
+ smart_routing=False, # Use legacy mode for explicit status testing
)
with patch("headroom.proxy.server._LLMLINGUA_AVAILABLE", True):
diff --git a/uv.lock b/uv.lock
index 83b981664..06d166b9c 100644
--- a/uv.lock
+++ b/uv.lock
@@ -849,7 +849,7 @@ wheels = [
[[package]]
name = "headroom-ai"
-version = "0.2.8"
+version = "0.2.9"
source = { editable = "." }
dependencies = [
{ name = "litellm" },