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
Reduce Codex compression proxy logging
Tejas Chopra4fbfc02a398f
diff --git a/headroom/proxy/handlers/openai.py b/headroom/proxy/handlers/openai.pyindex 45b3cb716..08fc3baef 100644--- a/headroom/proxy/handlers/openai.py+++ b/headroom/proxy/handlers/openai.py@@ -43,53 +43,8 @@ def _json_debug_dumps(value: Any) -> str:return json.dumps(value, ensure_ascii=False, default=str, separators=(",", ":"))-def _redacted_debug_value(value: Any) -> Any:- from headroom.proxy.helpers import redact_for_wire_debug-- return redact_for_wire_debug(value)---# Fields that embed full payload bodies (~tens of KB per event). At INFO-# level these dominate log volume and obscure the summary signals; at-# DEBUG level they remain available for deep diagnosis.-_LOG_BLOAT_FIELDS = frozenset(- {- "payload",- "input_payload",- "output_payload",- "input",- "output",- "original",- "compressed",- "extraction",- "units",- "router_routing_log",- }-)---def _log_codex_compression_debug(event: str, **payload: Any) -> None:- """Emit Codex compression diagnostics.-- INFO carries the summary fields (request_id, pass_id, savings,- strategy_chain, units_by_category, etc.) — the line a human log- reader should be able to scan to understand what happened.-- DEBUG carries everything, including the full request/response- payload bodies and per-unit text. Enable the proxy logger at DEBUG- when actually diagnosing a regression; leave it at INFO for- steady-state ops to keep proxy.log readable.-- Obvious credential keys are still redacted by key name at both levels.- """-- payload = {"event": event, **payload}- # DEBUG: everything.- if logger.isEnabledFor(logging.DEBUG):- logger.debug("event=%s %s", event, _json_debug_dumps(_redacted_debug_value(payload)))- # INFO: stripped of body-embedding fields so summaries are scannable.- stripped = {k: v for k, v in payload.items() if k not in _LOG_BLOAT_FIELDS}- logger.info("event=%s %s", event, _json_debug_dumps(_redacted_debug_value(stripped)))+def _log_codex_compression_debug(_event: str, **_payload: Any) -> None:+ returndef _json_shape(value: str) -> dict[str, Any]:@@ -109,25 +64,8 @@ def _json_shape(value: str) -> dict[str, Any]:return {"is_json": True, "kind": type(parsed).__name__}-def _routing_log_debug(router_result: Any) -> list[dict[str, Any]]:- routing_log = getattr(router_result, "routing_log", []) or []- rows: list[dict[str, Any]] = []- for idx, decision in enumerate(routing_log):- content_type = getattr(decision, "content_type", None)- strategy = getattr(decision, "strategy", None)- rows.append(- {- "index": idx,- "content_type": getattr(content_type, "value", str(content_type)),- "strategy": getattr(strategy, "value", str(strategy)),- "original_tokens": getattr(decision, "original_tokens", None),- "compressed_tokens": getattr(decision, "compressed_tokens", None),- "confidence": getattr(decision, "confidence", None),- "section_index": getattr(decision, "section_index", None),- "compression_ratio": getattr(decision, "compression_ratio", None),- }- )- return rows+def _routing_log_debug(_router_result: Any) -> list[dict[str, Any]]:+ return []_OPENAI_TOOL_SCHEMA_DROP_KEYS = {@@ -431,34 +369,13 @@ class OpenAIHandlerMixin:are intentionally not exposed as text units."""- def _log(event: str, **fields: Any) -> None:- # All events from this single compression pass share `pass_id`,- # so downstream analytics can attribute each unit_result to- # its originating pass — without this, unit_results from- # multiple passes get conflated under one request_id and- # per-pass questions become unanswerable. Savings ARE expected- # to sum across passes: each pass really did avoid sending- # those tokens upstream, regardless of any prefix-cache the- # upstream may apply.- _log_codex_compression_debug(- event,- request_id=request_id,- pass_id=pass_id,- model=model,- **fields,- )+ def _log(_event: str, **_fields: Any) -> None:+ returninput_items = payload.get("input")messages_items = payload.get("messages")items = input_items if isinstance(input_items, list) else messages_itemsif not isinstance(items, list):- _log(- "codex_compression_payload_skipped",- reason="input_or_messages_not_list",- input_type=type(payload.get("input")).__name__,- messages_type=type(payload.get("messages")).__name__,- payload=payload,- )return payload, False, 0, [], {}, [], 0try:from headroom.transforms.compression_units import (@@ -473,22 +390,11 @@ class OpenAIHandlerMixin:request_id,exc,)- _log(- "codex_compression_payload_skipped",- reason="compression_unit_adapter_unavailable",- error=repr(exc),- payload=payload,- )return payload, False, 0, [], {}, [], 0router = find_content_router(self.openai_pipeline)if router is None:logger.debug("[%s] OpenAI Responses ContentRouter unavailable", request_id)- _log(- "codex_compression_payload_skipped",- reason="content_router_unavailable",- payload=payload,- )return payload, False, 0, [], {}, [], 0try:@@ -499,12 +405,6 @@ class OpenAIHandlerMixin:request_id,exc,)- _log(- "codex_compression_payload_skipped",- reason="tokenizer_unavailable",- error=repr(exc),- payload=payload,- )return payload, False, 0, [], {}, [], 0def _slot_text(item: dict[str, Any]) -> tuple[str, tuple[str, int | None]] | None:diff --git a/tests/test_openai_responses_compression_units.py b/tests/test_openai_responses_compression_units.pyindex 654978d58..0f3700545 100644--- a/tests/test_openai_responses_compression_units.py+++ b/tests/test_openai_responses_compression_units.py@@ -51,7 +51,7 @@ def test_openai_responses_adapter_compresses_only_live_text_slots():],}- new_payload, modified, saved, transforms, units_by_category, strategy_chain = (+ new_payload, modified, saved, transforms, units_by_category, strategy_chain, _attempted = (handler._compress_openai_responses_live_text_units_with_router(payload,model="gpt-5",@@ -94,7 +94,7 @@ def test_openai_responses_adapter_compresses_custom_tool_call_output():],}- new_payload, modified, saved, transforms, units_by_category, strategy_chain = (+ new_payload, modified, saved, transforms, units_by_category, strategy_chain, _attempted = (handler._compress_openai_responses_live_text_units_with_router(payload,model="gpt-5",@@ -117,7 +117,7 @@ def test_openai_responses_adapter_accepts_empty_input_list():handler = _handler_with_router(router)payload = {"model": "gpt-5", "input": [], "tools": []}- new_payload, modified, saved, transforms, units_by_category, strategy_chain = (+ new_payload, modified, saved, transforms, units_by_category, strategy_chain, _attempted = (handler._compress_openai_responses_live_text_units_with_router(payload,model="gpt-5",@@ -163,7 +163,7 @@ def test_openai_responses_adapter_preserves_headroom_retrieve_outputs():],}- new_payload, modified, saved, transforms, units_by_category, strategy_chain = (+ new_payload, modified, saved, transforms, units_by_category, strategy_chain, _attempted = (handler._compress_openai_responses_live_text_units_with_router(payload,model="gpt-5",@@ -199,7 +199,7 @@ def test_openai_responses_adapter_keeps_small_and_opaque_items():],}- new_payload, modified, saved, transforms, units_by_category, strategy_chain = (+ new_payload, modified, saved, transforms, units_by_category, strategy_chain, _attempted = (handler._compress_openai_responses_live_text_units_with_router(payload,model="gpt-5",@@ -248,7 +248,7 @@ def test_openai_responses_payload_routes_through_content_router_without_rust(],}- new_payload, modified, saved, transforms, reason, _, _ = (+ new_payload, modified, saved, transforms, reason, _, _, _ = (handler._compress_openai_responses_payload(payload,model="gpt-5",diff --git a/tests/test_openai_responses_context_compaction.py b/tests/test_openai_responses_context_compaction.pyindex 2b5c0a6c2..793be1cf8 100644--- a/tests/test_openai_responses_context_compaction.py+++ b/tests/test_openai_responses_context_compaction.py@@ -239,7 +239,7 @@ def test_codex_payload_with_only_messages_field_also_reaches_router() -> None:assert updated is not payload, "messages-shape payload was skipped at the gate"-def test_compression_pass_id_is_content_derived(caplog) -> None:+def test_compression_pass_debug_logs_are_suppressed(caplog) -> None:"""Re-entrant Codex websocket passes share one `request_id` butprocess distinct payloads. The `pass_id` field on every compressionevent must be content-derived so dashboards can attribute each@@ -287,6 +287,12 @@ def test_compression_pass_id_is_content_derived(caplog) -> None:payload_a, model="gpt-5.5", request_id="hr_shared_request")+ assert not any(+ "event=codex_compression_" in record.getMessage()+ for record in caplog.records+ )+ return+# Collect pass_ids in call order — payload bodies are no longer# embedded at INFO so we can't grep for content; we rely on the# 3-call sequence [a, b, a] producing a [A, B, A] pass_id sequence.