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): discard partial SSE tails during finalization (#3411)
宇宙的山峰6c0e817bcc0a
diff --git a/headroom/proxy/handlers/streaming.py b/headroom/proxy/handlers/streaming.pyindex b364c3c7a..03a698fe7 100644--- a/headroom/proxy/handlers/streaming.py+++ b/headroom/proxy/handlers/streaming.py@@ -947,34 +947,15 @@ class StreamingMixin:# Per-chunk SSE parsing only flushes events terminated by ``\n\n``.# When upstream truncates mid-event (client disconnect, network- # drop, connection reset), the message_start (cache_read /- # cache_creation) or message_delta (output_tokens) usage events- # can sit in the residual buffer and never be parsed — surfacing- # as cache_read=cache_write=0 in PERF logs and poisoning the- # downstream freeze heuristic for the next request. Append the- # terminator so the buffer parser drains whatever's there. The- # per-event try/except in the parser swallows incomplete JSON,- # so this is safe even when the truncation cut mid-payload.+ # drop, connection reset), the residual bytes are not a complete+ # SSE event. Do not append a synthetic terminator here: doing so+ # turns a partial UTF-8 sequence or partial event into input for+ # the strict decoder and can raise while finalizing the stream.+ # Complete usage events have already been parsed in the read loop;+ # discard the incomplete tail instead of treating it as authoritative.sse_buffer = stream_state.get("sse_buffer")if isinstance(sse_buffer, bytearray) and len(sse_buffer) > 0:- sse_buffer.extend(b"\n\n")- late_usage = self._parse_sse_usage_from_buffer(stream_state, provider) or {}- for key in (- "input_tokens",- "output_tokens",- "cache_read_input_tokens",- "cache_creation_input_tokens",- "cache_creation_ephemeral_5m_input_tokens",- "cache_creation_ephemeral_1h_input_tokens",- ):- if key not in late_usage:- continue- current = stream_state.get(key)- # Only fill in unset (None) or default-zero slots so a- # real cache_read=0 from earlier in the stream isn't- # clobbered by a later partial event.- if current is None or current == 0:- stream_state[key] = late_usage[key]+ sse_buffer.clear()output_tokens = stream_state["output_tokens"]output_tokens_source = "provider"@@ -2215,14 +2196,13 @@ class StreamingMixin:yield f"data: {json.dumps(error_data)}\n\n".encode()yield b"data: [DONE]\n\n"finally:- # Late-flush: if upstream truncated the stream mid-event,- # the buffer parser hasn't seen the closing ``\n\n`` yet.- # Mirror _finalize_stream_response: append the terminator- # and drain anything still parseable.+ # A residual buffer without an SSE terminator is an+ # incomplete event. Complete usage frames were parsed in+ # the read loop; never synthesize ``\n\n`` here, because+ # that can make partial UTF-8 look like a complete event.buf = stream_state["sse_buffer"]if len(buf) > 0:- buf.extend(b"\n\n")- _absorb(self._parse_sse_usage_from_buffer(stream_state, "openai"))+ buf.clear()# Mirror the non-streaming sibling (``_extract_responses_usage``# in handlers/openai.py): only infer cache metrics whendiff --git a/tests/test_proxy_streaming_request_logger.py b/tests/test_proxy_streaming_request_logger.pyindex f39aa4b87..f87e4a778 100644--- a/tests/test_proxy_streaming_request_logger.py+++ b/tests/test_proxy_streaming_request_logger.py@@ -274,14 +274,8 @@ async def test_finalize_openai_responses_stream_uses_provider_usage_for_dashboar@pytest.mark.asyncio-async def test_finalize_stream_response_recovers_usage_from_truncated_buffer() -> None:- """When upstream truncates mid-event (no trailing \\n\\n), the per-chunk- parser leaves the message_start usage event sitting in sse_buffer and- PERF logs cache_read=cache_write=0 — which then poisons the freeze- heuristic on the next request. The finalizer must flush the residual- buffer so the real cache_read / cache_creation tokens still land in- the log even on aborted streams.- """+async def test_finalize_stream_response_discards_truncated_utf8_tail() -> None:+ """An interrupted SSE event must not be made complete by finalization."""proxy = _build_proxy_with_real_logger(log_full_messages=False)partial_message_start = (@@ -291,6 +285,7 @@ async def test_finalize_stream_response_recovers_usage_from_truncated_buffer() -b'"content":[],"stop_reason":null,"usage":{'b'"input_tokens":1234,"cache_read_input_tokens":50000,'b'"cache_creation_input_tokens":2500,"output_tokens":1}}}'+ b"\xe5")state = {@@ -319,9 +314,10 @@ async def test_finalize_stream_response_recovers_usage_from_truncated_buffer() -start_time=0.0,)- assert state["input_tokens"] == 1234- assert state["cache_read_input_tokens"] == 50000- assert state["cache_creation_input_tokens"] == 2500+ assert state["input_tokens"] is None+ assert state["cache_read_input_tokens"] == 0+ assert state["cache_creation_input_tokens"] == 0+ assert state["sse_buffer"] == bytearray()@pytest.mark.asyncio