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: surface Codex responses traffic in dashboard (#399)
Mohamed EL HAJJAJIecc496709b7f
diff --git a/.github/pr-images/pr-399/dashboard-cache-ttl-main.png b/.github/pr-images/pr-399/dashboard-cache-ttl-main.pngnew file mode 100644index 000000000..68096c1b4Binary files /dev/null and b/.github/pr-images/pr-399/dashboard-cache-ttl-main.png differdiff --git a/headroom/dashboard/templates/dashboard.html b/headroom/dashboard/templates/dashboard.htmlindex 8e83a4d84..62eff345e 100644--- a/headroom/dashboard/templates/dashboard.html+++ b/headroom/dashboard/templates/dashboard.html@@ -2134,15 +2134,26 @@return /^\d+\.\d+\.\d+$/.test(label) ? 'v' + label : label;},+ async fetchJson(url) {+ const separator = url.includes('?') ? '&' : '?';+ const response = await fetch(`${url}${separator}_=${Date.now()}`, {+ cache: 'no-store',+ headers: { 'Cache-Control': 'no-cache' }+ });+ if (!response.ok) {+ throw new Error(`HTTP ${response.status} for ${url}`);+ }+ return await response.json();+ },+async fetchStats() {try {- const [statsRes, healthRes] = await Promise.all([- fetch('/stats?cached=1'),- fetch('/health')+ const [stats, health] = await Promise.all([+ this.fetchJson('/stats?cached=1'),+ this.fetchJson('/health')]);- this.stats = await statsRes.json();- const health = await healthRes.json();+ this.stats = stats;this.healthy = health.status === 'healthy';this.version = health.version || 'unknown';this.log_full_messages = this.stats.log_full_messages || false;@@ -2175,11 +2186,8 @@},async fetchHistoryStats() {try {- const response = await fetch('/stats-history');- if (response.ok) {- this.historyStats = await response.json();- this.lastHistoryFetchMs = Date.now();- }+ this.historyStats = await this.fetchJson('/stats-history');+ this.lastHistoryFetchMs = Date.now();} catch (e) {console.error('Failed to fetch history stats:', e);}@@ -2188,18 +2196,15 @@async fetchTransformations() {try {const prevLen = this.transformations.length;- const response = await fetch('/transformations/feed?limit=50');- if (response.ok) {- const data = await response.json();- const newLen = (data.transformations || []).length;- if (this.feedScrolled_ && newLen > prevLen) {- this.feedNewCount = newLen - prevLen;- }- this.transformations = data.transformations || [];- this.log_full_messages = data.log_full_messages ?? this.log_full_messages;- this.lastFeedFetchMs = Date.now();- this.renderTransformations();+ const data = await this.fetchJson('/transformations/feed?limit=50');+ const newLen = (data.transformations || []).length;+ if (this.feedScrolled_ && newLen > prevLen) {+ this.feedNewCount = newLen - prevLen;}+ this.transformations = data.transformations || [];+ this.log_full_messages = data.log_full_messages ?? this.log_full_messages;+ this.lastFeedFetchMs = Date.now();+ this.renderTransformations();} catch (e) {console.error('Failed to fetch transformations:', e);}diff --git a/headroom/proxy/handlers/openai.py b/headroom/proxy/handlers/openai.pyindex d8d316cf4..23d9ea4c4 100644--- a/headroom/proxy/handlers/openai.py+++ b/headroom/proxy/handlers/openai.py@@ -1835,6 +1835,7 @@ class OpenAIHandlerMixin:return await count_tokens_offloaded(self, model, messages)OPENAI_RESPONSES_ROUTER_MIN_BYTES = 512+ OPENAI_RESPONSES_MESSAGE_ROUTER_MIN_BYTES = 8192OPENAI_RESPONSES_OUTPUT_TYPES = _RESPONSES_OUTPUT_ITEM_TYPESdef _openai_responses_unit_cache(self) -> tuple[Any, OrderedDict[str, Any]]:@@ -2177,12 +2178,22 @@ class OpenAIHandlerMixin:return payload, False, 0, [], {}, [], 0def _slot_texts(item: dict[str, Any]) -> list[tuple[str, tuple[str, int | None]]]:- # Only tool-output items are eligible for in-place compression.- # Message items (user/system/assistant) sit inside the request's- # cacheable prefix; mutating them busts prefix caching on every- # subsequent turn. Role-level guards in compression_units.py- # remain as defense-in-depth.+ # Tool outputs and historical message text are eligible for+ # in-place compression. The current user message is filtered below.type_tag = item.get("type")+ if type_tag == "message":+ content = item.get("content")+ if isinstance(content, str):+ return [(content, ("content", None))]+ if not isinstance(content, list):+ return []+ return [+ (part["text"], ("content_text", index))+ for index, part in enumerate(content)+ if isinstance(part, dict)+ and part.get("type") in {"input_text", "output_text", "text"}+ and isinstance(part.get("text"), str)+ ]if type_tag not in self.OPENAI_RESPONSES_OUTPUT_TYPES:return []output = item.get("output")@@ -2214,6 +2225,20 @@ class OpenAIHandlerMixin:if isinstance(part, dict) and part.get("type") in {"input_text", "output_text"}:part["text"] = replacementreturn True+ if kind == "content":+ item["content"] = replacement+ return True+ if kind == "content_text" and isinstance(index, int):+ content = item.get("content")+ if isinstance(content, list) and 0 <= index < len(content):+ part = content[index]+ if isinstance(part, dict) and part.get("type") in {+ "input_text",+ "output_text",+ "text",+ }:+ part["text"] = replacement+ return Truereturn Falseheadroom_retrieve_call_ids: set[str] = set()@@ -2350,6 +2375,16 @@ class OpenAIHandlerMixin:# normal candidate compression — no ML, byte/data-lossless only.lossless_excluded: list[tuple[int, tuple[str, int | None], str, str]] = []extraction_debug: list[dict[str, Any]] = []+ last_user_item_idx = max(+ (+ idx+ for idx, item in enumerate(items)+ if isinstance(item, dict)+ and item.get("type") == "message"+ and item.get("role") == "user"+ ),+ default=-1,+ )for idx, item in enumerate(items):if not isinstance(item, dict):if debug_enabled:@@ -2492,6 +2527,48 @@ class OpenAIHandlerMixin:"item": item,})+ elif item_type == "message":+ if item.get("role") == "user" and idx == last_user_item_idx:+ if debug_enabled:+ extraction_debug.append(+ {+ "index": idx,+ "eligible": False,+ "reason": "current_user_message_protected",+ "item_type": item_type,+ "role": item.get("role"),+ "item": item,+ }+ )+ continue+ slots = _slot_texts(item)+ for text, slot_ref in slots:+ candidates.append((idx, slot_ref, text))+ if debug_enabled:+ extraction_debug.append(+ {+ "index": idx,+ "eligible": True,+ "item_type": item_type,+ "role": item.get("role"),+ "slot": slot_ref,+ "text_chars": len(text),+ "text_bytes": len(text.encode("utf-8", errors="replace")),+ "text_json_shape": _json_shape(text),+ "item": item,+ "text": text,+ }+ )+ if not slots and debug_enabled:+ extraction_debug.append(+ {+ "index": idx,+ "eligible": False,+ "reason": "supported_type_without_text_slot",+ "item_type": item_type,+ "item": item,+ }+ )else:if debug_enabled:extraction_debug.append(@@ -2556,6 +2633,16 @@ class OpenAIHandlerMixin:item = items[item_idx] if item_idx < len(items) else {}item_type = item.get("type", "unknown") if isinstance(item, dict) else "unknown"role = str(item.get("role") or "tool") if isinstance(item, dict) else "tool"+ metadata: dict[str, str] = {}+ if role == "assistant":+ metadata["compress_assistant"] = "true"+ if role == "user" and item_idx != last_user_item_idx:+ metadata["compress_user"] = "true"+ min_bytes = (+ self.OPENAI_RESPONSES_MESSAGE_ROUTER_MIN_BYTES+ if item_type == "message"+ else self.OPENAI_RESPONSES_ROUTER_MIN_BYTES+ )unit = CompressionUnit(text=original_text,provider="openai",@@ -2564,7 +2651,8 @@ class OpenAIHandlerMixin:item_type=str(item_type),cache_zone="live",mutable=True,- min_bytes=self.OPENAI_RESPONSES_ROUTER_MIN_BYTES,+ min_bytes=min_bytes,+ metadata=metadata,)routed_units.append(RoutedCompressionUnit(unit=unit, slot=(item_idx, slot_ref)))if debug_enabled:diff --git a/headroom/transforms/compression_units.py b/headroom/transforms/compression_units.pyindex f72a668e8..c7296533f 100644--- a/headroom/transforms/compression_units.py+++ b/headroom/transforms/compression_units.py@@ -256,7 +256,7 @@ def compress_unit_with_router(if not unit.mutable:return _with_reason(reason="immutable")- if unit.role == "user":+ if unit.role == "user" and unit.metadata.get("compress_user") != "true":return _with_reason(reason="protected_user_message")if unit.role in {"system", "developer"}:return _with_reason(reason="protected_system_message")diff --git a/headroom/transforms/content_router.py b/headroom/transforms/content_router.pyindex 083d63dd8..6fe26eec6 100644--- a/headroom/transforms/content_router.py+++ b/headroom/transforms/content_router.py@@ -5500,7 +5500,7 @@ class ContentRouter(Transform):protect_analysis = kwargs.get("protect_analysis_context", self.config.protect_analysis_context)- min_tokens = kwargs.get("min_tokens_to_compress", 50)+ min_tokens = int(kwargs.get("min_tokens_to_compress", 50) or 50)# Cache-safety knobs for content-block (Anthropic-format) handling:compress_assistant_text_blocks = kwargs.get("compress_assistant_text_blocks",diff --git a/tests/test_openai_responses_compression_units.py b/tests/test_openai_responses_compression_units.pyindex c94f4fe52..9828153d6 100644--- a/tests/test_openai_responses_compression_units.py+++ b/tests/test_openai_responses_compression_units.py@@ -1365,3 +1365,53 @@ def test_openai_responses_cache_mode_never_batches_old_small_outputs():assert calls == []assert modified is Falseassert second["input"][:4] == first["input"]+++def test_openai_responses_adapter_compresses_historical_messages_not_current_user():+ router = ContentRouter()++ def compress(self, content: str, **_kwargs) -> RouterCompressionResult:+ return RouterCompressionResult(+ compressed="compressed history",+ original=content,+ strategy_used=CompressionStrategy.KOMPRESS,+ )++ router.compress = MethodType(compress, router)+ handler = _handler_with_router(router)+ historical_text = " ".join(f"history{i}" for i in range(2000))+ current_text = " ".join(f"current{i}" for i in range(2000))+ payload = {+ "model": "gpt-5",+ "input": [+ {+ "type": "message",+ "role": "user",+ "content": [{"type": "input_text", "text": historical_text}],+ },+ {+ "type": "message",+ "role": "assistant",+ "content": [{"type": "output_text", "text": historical_text}],+ },+ {+ "type": "message",+ "role": "user",+ "content": [{"type": "input_text", "text": current_text}],+ },+ ],+ }++ new_payload, modified, saved, transforms, units_by_category, _chain, _attempted = (+ handler._compress_openai_responses_live_text_units_with_router(+ payload, model="gpt-5", request_id="req_history"+ )+ )++ assert modified is True+ assert saved > 0+ assert new_payload["input"][0]["content"][0]["text"] == "compressed history"+ assert new_payload["input"][1]["content"][0]["text"] == "compressed history"+ assert new_payload["input"][2]["content"][0]["text"] == current_text+ assert units_by_category == {"applied": 2}+ assert any(t.startswith("router:openai:responses:message:") for t in transforms)diff --git a/tests/test_proxy_dashboard_stats_cache.py b/tests/test_proxy_dashboard_stats_cache.pyindex 23903e99b..e1c236e77 100644--- a/tests/test_proxy_dashboard_stats_cache.py+++ b/tests/test_proxy_dashboard_stats_cache.py@@ -175,7 +175,8 @@ def test_stats_reset_clears_runtime_proxy_counters(monkeypatch: pytest.MonkeyPatdef test_dashboard_uses_cached_stats_and_lazy_history_feed_polling() -> None:html = get_dashboard_html()- assert "fetch('/stats?cached=1')" in html+ assert "this.fetchJson('/stats?cached=1')" in html+ assert "cache: 'no-store'" in htmlassert "version: 'loading'" in htmlassert 'x-text="formatVersion(version)"' in htmlassert "return /^\\d+\\.\\d+\\.\\d+$/.test(label)" in htmldiff --git a/tests/test_proxy_savings_history.py b/tests/test_proxy_savings_history.pyindex d7dfb5a8a..ff8bf6298 100644--- a/tests/test_proxy_savings_history.py+++ b/tests/test_proxy_savings_history.py@@ -1488,7 +1488,7 @@ def test_dashboard_includes_history_toggle_and_endpoint(tmp_path, monkeypatch):html = response.textassert "Session" in htmlassert "Historical" in html- assert "fetch('/stats-history')" in html+ assert "this.fetchJson('/stats-history')" in htmlassert "Export CSV" in htmlassert "Weekly Savings" in htmlassert "Monthly Savings" in html