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
Cover OpenAI Responses unit cache edge cases
jamesx04164c86826eab19
diff --git a/tests/test_openai_responses_compression_units.py b/tests/test_openai_responses_compression_units.pyindex b9939d468..c0b8329ac 100644--- a/tests/test_openai_responses_compression_units.py+++ b/tests/test_openai_responses_compression_units.py@@ -3,7 +3,9 @@ from __future__ import annotationsimport threadingfrom types import MethodType, SimpleNamespace+from headroom.proxy.handlers import openai as openai_handlerfrom headroom.proxy.handlers.openai import OpenAIHandlerMixin+from headroom.transforms.compression_units import UnitCompressionResultfrom headroom.transforms.content_router import (CompressionStrategy,ContentRouter,@@ -25,6 +27,69 @@ def _handler_with_router(router: ContentRouter) -> OpenAIHandlerMixin:return handler+def test_openai_responses_unit_parallelism_env_defaults_and_clamps(monkeypatch):+ monkeypatch.delenv("HEADROOM_OPENAI_RESPONSES_UNIT_PARALLELISM", raising=False)+ assert openai_handler._openai_responses_unit_parallelism() == 4++ monkeypatch.setenv("HEADROOM_OPENAI_RESPONSES_UNIT_PARALLELISM", "bad")+ assert openai_handler._openai_responses_unit_parallelism() == 4++ monkeypatch.setenv("HEADROOM_OPENAI_RESPONSES_UNIT_PARALLELISM", "0")+ assert openai_handler._openai_responses_unit_parallelism() == 1++ monkeypatch.setenv("HEADROOM_OPENAI_RESPONSES_UNIT_PARALLELISM", "999")+ assert openai_handler._openai_responses_unit_parallelism() == 16+++def test_openai_responses_cached_unit_handles_results_without_router_result():+ result = UnitCompressionResult(+ original="original",+ compressed="compressed",+ modified=True,+ tokens_before=2,+ tokens_after=1,+ tokens_saved=1,+ transforms_applied=[],+ strategy="none",+ router_result=None,+ )++ assert openai_handler._openai_responses_result_with_cache_hit(result) is result+++def test_openai_responses_unit_cache_evicts_oldest_entry(monkeypatch):+ monkeypatch.setattr(openai_handler, "_OPENAI_RESPONSES_UNIT_CACHE_MAX_ENTRIES", 1)+ handler = OpenAIHandlerMixin()+ first = UnitCompressionResult(+ original="first",+ compressed="first compressed",+ modified=True,+ tokens_before=2,+ tokens_after=1,+ tokens_saved=1,+ transforms_applied=[],+ strategy="none",+ router_result=None,+ )+ second = UnitCompressionResult(+ original="second",+ compressed="second compressed",+ modified=True,+ tokens_before=2,+ tokens_after=1,+ tokens_saved=1,+ transforms_applied=[],+ strategy="none",+ router_result=None,+ )++ handler._store_openai_responses_cached_unit("first", first)+ handler._store_openai_responses_cached_unit("second", second)++ assert handler._get_openai_responses_cached_unit("first") is None+ assert handler._get_openai_responses_cached_unit("second") is second++def test_openai_responses_adapter_compresses_only_live_text_slots():router = ContentRouter()@@ -165,6 +230,46 @@ def test_openai_responses_adapter_reuses_exact_tool_output_cache():assert new_payload_two["input"][1]["output"] == "cached output summary"+def test_openai_responses_adapter_reuses_identical_tool_output_in_same_request():+ router = ContentRouter()+ calls = {"count": 0}++ def compress(self, content: str, **_kwargs):+ calls["count"] += 1+ return RouterCompressionResult(+ compressed="same request cached summary",+ original=content,+ strategy_used=CompressionStrategy.KOMPRESS,+ )++ router.compress = MethodType(compress, router)+ handler = _handler_with_router(router)+ long_text = " ".join(f"word{i}" for i in range(180))+ payload = {+ "model": "gpt-5",+ "input": [+ {"type": "function_call_output", "call_id": "c1", "output": long_text},+ {"type": "function_call_output", "call_id": "c2", "output": long_text},+ ],+ }++ new_payload, modified, saved, *_ = (+ handler._compress_openai_responses_live_text_units_with_router(+ payload,+ model="gpt-5",+ request_id="req_same_request_cache",+ )+ )++ assert calls["count"] == 1+ assert modified is True+ assert saved > 0+ assert [item["output"] for item in new_payload["input"]] == [+ "same request cached summary",+ "same request cached summary",+ ]++def test_openai_responses_adapter_parallelizes_cache_misses_preserving_order(monkeypatch):monkeypatch.setenv("HEADROOM_OPENAI_RESPONSES_UNIT_PARALLELISM", "4")router = ContentRouter()