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.
Python agentaianthropicclaude-codecompressioncontext-engineeringcontext-windowcursorfastapilangchainllmmcpopenaiprompt-engineeringproxypythonragtoken-optimizationtokenstypescript
fix(beacon): split session failures by status code (#2815)
Tejas Chopra2954e37048f8
diff --git a/deploy/beacon/sample-event.json b/deploy/beacon/sample-event.jsonindex 6d5e13bd5..4bacb0ebc 100644--- a/deploy/beacon/sample-event.json+++ b/deploy/beacon/sample-event.json@@ -284,6 +284,21 @@"value": {"intValue": "2"}+ },+ {+ "key": "failure_statuses",+ "value": {+ "kvlistValue": {+ "values": [+ {+ "key": "529",+ "value": {+ "intValue": "2"+ }+ }+ ]+ }+ }}]}diff --git a/deploy/beacon/worker.js b/deploy/beacon/worker.jsindex d4e2d0ac2..5e8ec015f 100644--- a/deploy/beacon/worker.js+++ b/deploy/beacon/worker.js@@ -42,6 +42,7 @@ const ALLOWED_KEYS = ['providers','models','failures',+ 'failure_statuses',];// Resource attributes we keep. Same rule: allowlist, not denylist.diff --git a/headroom/telemetry/session.py b/headroom/telemetry/session.pyindex 26e45a303..cfa77f44c 100644--- a/headroom/telemetry/session.py+++ b/headroom/telemetry/session.py@@ -246,6 +246,7 @@ class _Session:cache_write_tokens: int = 0uncached_tokens: int = 0failures: int = 0+ failure_statuses: dict[str, int] = field(default_factory=dict)passthrough_turns: int = 0response_cache_hits: int = 0overhead_ms: float = 0.0@@ -392,6 +393,12 @@ class _Session:"providers": sorted(self.providers),"models": sorted(self.models),"failures": self.failures,+ # The same failures split by status, because the count alone cannot+ # answer the only question worth asking about it: a 529 is the+ # provider shedding load (nothing to fix here) and a 500 is usually+ # ours. Keys are the bare status string; the set is closed and tiny+ # (500/502/503/504/529), so this needs no slug bounding.+ "failure_statuses": dict(self.failure_statuses),}self.seq += 1return snapshot@@ -513,8 +520,14 @@ def _fold(sess: _Session, outcome: Any, now: float, source: str = "proxy") -> Nosess.uncached_tokens += int(get("uncached_input_tokens") or 0)sess.overhead_ms += float(get("overhead_ms", 0.0) or 0.0)sess.latency_ms += float(get("total_latency_ms", 0.0) or 0.0)- if int(get("status_code", 200) or 200) >= 500:+ status = int(get("status_code", 200) or 200)+ if status >= 500:sess.failures += 1+ # ponytail: str(status) verbatim for the 5xx range, one bucket for+ # anything outside it. Nothing here can be user data, and the range+ # check is what keeps a garbage status_code from inventing map keys.+ key = str(status) if status < 600 else "other"+ sess.failure_statuses[key] = sess.failure_statuses.get(key, 0) + 1if get("from_response_cache", False):sess.response_cache_hits += 1@@ -855,6 +868,7 @@ def demo() -> None:assert event["compression"]["transforms"] == {"crush": 2, "dedupe": 2}assert event["providers"] == ["anthropic"]assert event["failures"] == 0+ assert event["failure_statuses"] == {}# The new burst is a distinct session, not a continuation.agg.flush_all()@@ -976,10 +990,18 @@ def demo() -> None:class Failed(FakeOutcome):status_code = 529+ class Broke(FakeOutcome):+ status_code = 500+agg2 = SessionAggregator(emitted.append)agg2.record(Failed(), now=2000.0)+ agg2.record(Failed(), now=2001.0)+ agg2.record(Broke(), now=2002.0)agg2.flush_all()- assert emitted[-1]["failures"] == 1+ assert emitted[-1]["failures"] == 3+ # Provider load-shedding and our own 500s have to be separable, or the+ # count says "0.7% of turns failed" and nothing about whose fault it is.+ assert emitted[-1]["failure_statuses"] == {"529": 2, "500": 1}# Flushing an empty aggregator is a no-op, not a null event.before = len(emitted)