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.

fix(proxy): count output tokens from CRLF-framed SSE streams too (#4298)

Rudy Mizrahi Celekli755293dd68d4
diff --git a/headroom/proxy/stream_output_tokens.py b/headroom/proxy/stream_output_tokens.py
index 965f7b7ea..55189c180 100644
--- a/headroom/proxy/stream_output_tokens.py
+++ b/headroom/proxy/stream_output_tokens.py
@@ -40,9 +40,12 @@ TEXT_CHARS_PER_TOKEN = 4
def _iter_sse_payloads(sse: str) -> Iterator[Any]:
"""Yield each ``data:`` payload in an SSE stream as a parsed object.
- Tolerates the two things real streams do that a naive split does not:
- an event whose ``data:`` is spread over multiple lines, and ``[DONE]``.
+ Tolerates the things real streams do that a naive split does not: an
+ event whose ``data:`` is spread over multiple lines, ``[DONE]``, and CRLF
+ or bare-CR line endings (both valid SSE; JSON payloads escape their own
+ CRs, so normalising them cannot touch the data).
"""
+ sse = sse.replace("\r\n", "\n").replace("\r", "\n")
for block in sse.split("\n\n"):
lines = [ln for ln in block.split("\n") if ln.startswith("data:")]
if not lines:
diff --git a/tests/test_stream_output_tokens.py b/tests/test_stream_output_tokens.py
index cc3760906..534e35d72 100644
--- a/tests/test_stream_output_tokens.py
+++ b/tests/test_stream_output_tokens.py
@@ -121,6 +121,25 @@ def test_multi_line_data_fields_concatenate() -> None:
assert extract_stream_text(sse) == "joined"
[email protected]("newline", ["\r\n", "\r"], ids=["crlf", "cr"])
+def test_crlf_and_cr_framed_streams_are_parsed(newline: str) -> None:
+ """SSE allows CRLF and bare CR line endings; the proxy forwards both (#649)."""
+ lf = _sse(
+ _chat_delta("Hello world, " * 20),
+ {"choices": [{"index": 0, "delta": {}, "finish_reason": "length"}]},
+ )
+ sse = lf.replace("\n", newline)
+
+ assert extract_stream_text(sse) == extract_stream_text(lf) == "Hello world, " * 20
+ assert estimate_output_tokens(
+ sse_text=sse, total_bytes=len(sse.encode()), body={"max_tokens": 64}
+ ) == (64, "exact_ceiling")
+ assert estimate_output_tokens(sse_text=sse, total_bytes=len(sse.encode())) == (
+ len("Hello world, " * 20) // TEXT_CHARS_PER_TOKEN,
+ "estimated_text",
+ )
+
+
# --------------------------------------------------------------------------- #
# The estimate itself
# --------------------------------------------------------------------------- #