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.

Merge upstream/main into feat/extended-cache-ttl-metrics

JerrettDavis78163199c406
diff --cc headroom/proxy/cost.py
index 0f0bd9011,21d065d17..bc1d7b796
--- a/headroom/proxy/cost.py
+++ b/headroom/proxy/cost.py
@@@ -78,10 -76,7 +78,11 @@@ def build_prefix_cache_stats
totals = {
"cache_read_tokens": 0,
"cache_write_tokens": 0,
+ "cache_write_5m_tokens": 0,
+ "cache_write_1h_tokens": 0,
+ "cache_write_5m_requests": 0,
+ "cache_write_1h_requests": 0,
+ "uncached_input_tokens": 0,
"requests": 0,
"hit_requests": 0,
"bust_count": 0,
@@@ -142,10 -140,7 +150,11 @@@
provider_stats = {
"cache_read_tokens": read_tokens,
"cache_write_tokens": write_tokens,
+ "cache_write_5m_tokens": write_5m_tokens,
+ "cache_write_1h_tokens": write_1h_tokens,
+ "cache_write_5m_requests": write_5m_requests,
+ "cache_write_1h_requests": write_1h_requests,
+ "uncached_input_tokens": uncached_tokens,
"requests": pc["requests"],
"hit_requests": pc["hit_requests"],
"hit_rate": hit_rate,
@@@ -184,10 -159,7 +194,11 @@@
# Accumulate totals
totals["cache_read_tokens"] += read_tokens
totals["cache_write_tokens"] += write_tokens
+ totals["cache_write_5m_tokens"] += write_5m_tokens
+ totals["cache_write_1h_tokens"] += write_1h_tokens
+ totals["cache_write_5m_requests"] += write_5m_requests
+ totals["cache_write_1h_requests"] += write_1h_requests
+ totals["uncached_input_tokens"] += uncached_tokens
totals["requests"] += pc["requests"]
totals["hit_requests"] += pc["hit_requests"]
totals["bust_count"] += pc["bust_count"]
@@@ -198,36 -170,16 +209,43 @@@
totals["net_savings_usd"] = round(totals["savings_usd"], 4)
totals["savings_usd"] = round(totals["savings_usd"], 4)
totals["write_premium_usd"] = round(totals["write_premium_usd"], 4)
+ # Token-level hit rate across all providers
+ _total_input = (
+ totals["cache_read_tokens"] + totals["cache_write_tokens"] + totals["uncached_input_tokens"]
+ )
totals["hit_rate"] = (
+ round(totals["cache_read_tokens"] / _total_input * 100, 1) if _total_input > 0 else 0
+ )
+ totals["request_hit_rate"] = (
round(totals["hit_requests"] / totals["requests"] * 100, 1) if totals["requests"] > 0 else 0
)
+ total_observed_ttl_tokens = totals["cache_write_5m_tokens"] + totals["cache_write_1h_tokens"]
+ totals["observed_ttl_buckets"] = {
+ "5m": {
+ "tokens": totals["cache_write_5m_tokens"],
+ "requests": totals["cache_write_5m_requests"],
+ },
+ "1h": {
+ "tokens": totals["cache_write_1h_tokens"],
+ "requests": totals["cache_write_1h_requests"],
+ },
+ }
+ totals["observed_ttl_mix"] = {
+ "5m_pct": round(totals["cache_write_5m_tokens"] / total_observed_ttl_tokens * 100, 1)
+ if total_observed_ttl_tokens > 0
+ else 0.0,
+ "1h_pct": round(totals["cache_write_1h_tokens"] / total_observed_ttl_tokens * 100, 1)
+ if total_observed_ttl_tokens > 0
+ else 0.0,
+ "active_buckets": [
+ bucket
+ for bucket, tokens in (
+ ("5m", totals["cache_write_5m_tokens"]),
+ ("1h", totals["cache_write_1h_tokens"]),
+ )
+ if tokens > 0
+ ],
+ }
return {
"by_provider": by_provider,
diff --cc headroom/proxy/handlers/anthropic.py
index cf0407466,0a26732a1..ad59da3ee
--- a/headroom/proxy/handlers/anthropic.py
+++ b/headroom/proxy/handlers/anthropic.py
@@@ -1222,16 -855,23 +1222,30 @@@ class AnthropicHandlerMixin
output_tokens = usage.get("output_tokens", 0)
cr_tokens = usage.get("cache_read_input_tokens", 0)
cw_tokens = usage.get("cache_creation_input_tokens", 0)
+ cw_5m_tokens, cw_1h_tokens = self._extract_anthropic_cache_ttl_metrics(usage)
uncached_input_tokens = usage.get("input_tokens", 0)
+ # Track cache bust: tokens that lost their cache discount due to compression.
+ # If we had X tokens cached last turn and only Y hit cache this turn,
+ # then (X - Y) tokens were busted by our modifications.
+ expected_cached = prefix_tracker._cached_token_count
+ if expected_cached > 0 and tokens_saved > 0:
+ bust_tokens = max(0, expected_cached - cr_tokens)
+ if bust_tokens > 0:
+ logger.info(
+ f"[{request_id}] CACHE-BUST: "
+ f"expected_cached={expected_cached:,} actual_read={cr_tokens:,} "
+ f"tokens_lost={bust_tokens:,} tokens_saved={tokens_saved:,}"
+ )
+ await self.metrics.record_cache_bust(bust_tokens)
+
# Update prefix cache tracker for next turn
+ next_original_messages = copy.deepcopy(original_client_messages)
+ next_forwarded_messages = copy.deepcopy(optimized_messages)
+ assistant_message = self._assistant_message_from_response_json(resp_json)
+ if assistant_message is not None:
+ next_original_messages.append(copy.deepcopy(assistant_message))
+ next_forwarded_messages.append(copy.deepcopy(assistant_message))
prefix_tracker.update_from_response(
cache_read_tokens=cr_tokens,
cache_write_tokens=cw_tokens,
diff --cc headroom/proxy/prometheus_metrics.py
index e5889d406,2c573dcd9..f2d6ceb6f
--- a/headroom/proxy/prometheus_metrics.py
+++ b/headroom/proxy/prometheus_metrics.py
@@@ -77,10 -77,7 +77,11 @@@ class PrometheusMetrics
lambda: {
"cache_read_tokens": 0,
"cache_write_tokens": 0,
+ "cache_write_5m_tokens": 0,
+ "cache_write_1h_tokens": 0,
+ "cache_write_5m_requests": 0,
+ "cache_write_1h_requests": 0,
+ "uncached_input_tokens": 0,
"requests": 0,
"hit_requests": 0, # requests with cache_read > 0
"bust_count": 0,
@@@ -184,12 -183,7 +189,13 @@@
pc = self.cache_by_provider[provider]
pc["cache_read_tokens"] += cache_read_tokens
pc["cache_write_tokens"] += cache_write_tokens
+ pc["cache_write_5m_tokens"] += cache_write_5m_tokens
+ pc["cache_write_1h_tokens"] += cache_write_1h_tokens
+ if cache_write_5m_tokens > 0:
+ pc["cache_write_5m_requests"] += 1
+ if cache_write_1h_tokens > 0:
+ pc["cache_write_1h_requests"] += 1
+ pc["uncached_input_tokens"] += uncached_input_tokens
pc["requests"] += 1
if cache_read_tokens > 0:
pc["hit_requests"] += 1