Spaces:
Build error
Build error
Commit ·
f3043c1
1
Parent(s): 6838f75
feat: add compression_cache stats to /stats endpoint
Browse filesCo-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- headroom/proxy/server.py +26 -0
headroom/proxy/server.py
CHANGED
|
@@ -6398,6 +6398,31 @@ def create_app(config: ProxyConfig | None = None) -> FastAPI:
|
|
| 6398 |
# Calculate total tokens before compression
|
| 6399 |
total_tokens_before = m.tokens_input_total + m.tokens_saved_total
|
| 6400 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6401 |
# Build unified savings summary (all layers)
|
| 6402 |
compression_tokens = m.tokens_saved_total
|
| 6403 |
cache_net_usd = prefix_cache_stats.get("totals", {}).get("net_savings_usd", 0.0)
|
|
@@ -6489,6 +6514,7 @@ def create_app(config: ProxyConfig | None = None) -> FastAPI:
|
|
| 6489 |
"compressed_tokens_cached": compression_stats.get("total_compressed_tokens", 0),
|
| 6490 |
"ccr_retrievals": compression_stats.get("total_retrievals", 0),
|
| 6491 |
},
|
|
|
|
| 6492 |
"telemetry": {
|
| 6493 |
"enabled": telemetry_stats.get("enabled", False),
|
| 6494 |
"total_compressions": telemetry_stats.get("total_compressions", 0),
|
|
|
|
| 6398 |
# Calculate total tokens before compression
|
| 6399 |
total_tokens_before = m.tokens_input_total + m.tokens_saved_total
|
| 6400 |
|
| 6401 |
+
# Compression cache stats (token_headroom mode)
|
| 6402 |
+
compression_cache_stats: dict = {}
|
| 6403 |
+
if proxy.config.mode == "token_headroom" and proxy._compression_caches:
|
| 6404 |
+
total_entries = 0
|
| 6405 |
+
total_hits = 0
|
| 6406 |
+
total_misses = 0
|
| 6407 |
+
total_tokens_saved = 0
|
| 6408 |
+
for cache in proxy._compression_caches.values():
|
| 6409 |
+
s = cache.get_stats()
|
| 6410 |
+
total_entries += s.get("entries", 0)
|
| 6411 |
+
total_hits += s.get("hits", 0)
|
| 6412 |
+
total_misses += s.get("misses", 0)
|
| 6413 |
+
total_tokens_saved += s.get("total_tokens_saved", 0)
|
| 6414 |
+
compression_cache_stats = {
|
| 6415 |
+
"mode": "token_headroom",
|
| 6416 |
+
"active_sessions": len(proxy._compression_caches),
|
| 6417 |
+
"total_entries": total_entries,
|
| 6418 |
+
"total_hits": total_hits,
|
| 6419 |
+
"total_misses": total_misses,
|
| 6420 |
+
"hit_rate": round(total_hits / max(1, total_hits + total_misses) * 100, 1),
|
| 6421 |
+
"total_tokens_saved": total_tokens_saved,
|
| 6422 |
+
}
|
| 6423 |
+
else:
|
| 6424 |
+
compression_cache_stats = {"mode": "cost_savings"}
|
| 6425 |
+
|
| 6426 |
# Build unified savings summary (all layers)
|
| 6427 |
compression_tokens = m.tokens_saved_total
|
| 6428 |
cache_net_usd = prefix_cache_stats.get("totals", {}).get("net_savings_usd", 0.0)
|
|
|
|
| 6514 |
"compressed_tokens_cached": compression_stats.get("total_compressed_tokens", 0),
|
| 6515 |
"ccr_retrievals": compression_stats.get("total_retrievals", 0),
|
| 6516 |
},
|
| 6517 |
+
"compression_cache": compression_cache_stats,
|
| 6518 |
"telemetry": {
|
| 6519 |
"enabled": telemetry_stats.get("enabled", False),
|
| 6520 |
"total_compressions": telemetry_stats.get("total_compressions", 0),
|