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(proxy): take a streamed Anthropic turn's uncached input from usage (#3946)
gglucass7f608ea9322f
diff --git a/headroom/proxy/handlers/streaming.py b/headroom/proxy/handlers/streaming.pyindex 810357dc9..2bc0d7731 100644--- a/headroom/proxy/handlers/streaming.py+++ b/headroom/proxy/handlers/streaming.py@@ -348,7 +348,10 @@ class StreamingMixin:msg = data.get("message", {})msg_usage = msg.get("usage", {})if msg_usage:- usage_found["input_tokens"] = msg_usage.get("input_tokens", 0)+ # Absent stays absent: the finalizer falls back to the+ # tokenizer derivation only when input_tokens is None.+ if "input_tokens" in msg_usage:+ usage_found["input_tokens"] = msg_usage["input_tokens"]usage_found["cache_read_input_tokens"] = msg_usage.get("cache_read_input_tokens", 0)@@ -860,9 +863,20 @@ class StreamingMixin:cache_write_tokens = stream_state["cache_creation_input_tokens"] or 0cache_write_5m_tokens = stream_state["cache_creation_ephemeral_5m_input_tokens"] or 0cache_write_1h_tokens = stream_state["cache_creation_ephemeral_1h_input_tokens"] or 0- uncached_input_tokens = max(- effective_optimized_tokens - cache_read_tokens - cache_write_tokens, 0- )+ if provider == "anthropic" and isinstance(provider_input_tokens, int):+ # Anthropic's usage.input_tokens already IS the uncached count+ # (tokens after the last cache breakpoint), the field the+ # non-streaming path records. Deriving it from Headroom's own+ # tokenizer count books the estimate's error as uncached input:+ # where the estimate runs over the provider's count (long+ # screenshot-heavy sessions, ~10%) every turn reported tens of+ # thousands of phantom uncached tokens, and where it runs under,+ # real uncached input clamped to 0.+ uncached_input_tokens = max(provider_input_tokens, 0)+ else:+ uncached_input_tokens = max(+ effective_optimized_tokens - cache_read_tokens - cache_write_tokens, 0+ )# Prefix-tracker mutation is provider-specific state that lives# outside the metric funnel. Run it before the funnel so the nextdiff --git a/tests/test_proxy_handler_helpers.py b/tests/test_proxy_handler_helpers.pyindex e430be17f..0771745d0 100644--- a/tests/test_proxy_handler_helpers.py+++ b/tests/test_proxy_handler_helpers.py@@ -773,6 +773,85 @@ def test_stream_finalizer_records_vertex_provider_for_dashboard() -> None:assert outcome.cache_read_tokens == 2+def _finalize_anthropic_stream(optimized_tokens, input_tokens): # noqa: ANN001, ANN202+ handler = object.__new__(HeadroomProxy)+ handler.config = SimpleNamespace(log_full_messages=False)+ outcomes = []++ async def record(outcome): # noqa: ANN001, ANN202+ outcomes.append(outcome)++ handler._record_request_outcome = record++ asyncio.run(+ handler._finalize_stream_response(+ body={"messages": [{"role": "user", "content": "hello"}]},+ provider="anthropic",+ model="claude-opus-4-1",+ request_id="req_anthropic_stream_final",+ original_tokens=optimized_tokens + 5,+ optimized_tokens=optimized_tokens,+ tokens_saved=5,+ transforms_applied=[],+ optimization_latency=1.0,+ stream_state={+ "input_tokens": input_tokens,+ "output_tokens": 7,+ "cache_read_input_tokens": 360_949,+ "cache_creation_input_tokens": 840,+ "cache_creation_ephemeral_5m_input_tokens": 0,+ "cache_creation_ephemeral_1h_input_tokens": 840,+ "total_bytes": 100,+ "sse_buffer": bytearray(),+ "ttfb_ms": 4.0,+ },+ start_time=0.0,+ )+ )+ (outcome,) = outcomes+ return outcome+++ "optimized_tokens",+ [+ # Local count 50k over the provider's (a production turn reported+ # input_tokens=2 while the old derivation logged 50,663 uncached).+ 412_452,+ # Local count under the provider's: the old derivation clamped to 0.+ 300_000,+ ],+)+def test_stream_finalizer_takes_anthropic_uncached_input_from_usage(optimized_tokens) -> None: # noqa: ANN001+ outcome = _finalize_anthropic_stream(optimized_tokens, input_tokens=2)++ assert outcome.uncached_input_tokens == 2+ assert outcome.cache_read_tokens == 360_949+ assert outcome.cache_write_tokens == 840+++def test_stream_finalizer_derives_anthropic_uncached_input_without_usage() -> None:+ # No message_start usage (an error before the stream began): keep the+ # tokenizer-based derivation.+ outcome = _finalize_anthropic_stream(400_000, input_tokens=None)++ assert outcome.uncached_input_tokens == 400_000 - 360_949 - 840+++def test_sse_parser_leaves_absent_anthropic_input_tokens_absent() -> None:+ # A message_start usage without input_tokens must not read as a provider+ # count of 0, or the finalizer books 0 uncached instead of deriving it.+ proxy = object.__new__(HeadroomProxy)+ event = {"type": "message_start", "message": {"usage": {"cache_read_input_tokens": 9}}}+ state = {"sse_buffer": bytearray(f"data: {json.dumps(event)}\n\n".encode())}++ usage = proxy._parse_sse_usage_from_buffer(state, "anthropic")++ assert usage is not None+ assert "input_tokens" not in usage+ assert usage["cache_read_input_tokens"] == 9++def test_vertex_gemini_non_text_generate_records_dashboard_outcome() -> None:handler = object.__new__(HeadroomProxy)handler.memory_handler = None