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
perf(proxy): cap compression workers to CPU count (#1803)
Rod Boev0a3851b24004
diff --git a/headroom/cli/proxy.py b/headroom/cli/proxy.pyindex 6377407bb..c537309ae 100644--- a/headroom/cli/proxy.py+++ b/headroom/cli/proxy.py@@ -434,7 +434,7 @@ def dashboard(port: int, no_open: bool) -> None:envvar="HEADROOM_COMPRESSION_MAX_WORKERS",help=("Bound the dedicated compression threadpool (CPU-bound Kompress work). "- "Default (unset): min(32, (cpu_count or 1) * 4). Lower it to reduce CPU "+ "Default (unset): cpu_count or 1. Lower it to reduce CPU ""oversubscription under concurrent sessions; a value < 1 is clamped to 1. ""Env: HEADROOM_COMPRESSION_MAX_WORKERS."),diff --git a/headroom/proxy/models.py b/headroom/proxy/models.pyindex b1b9fd0a8..34e9ec974 100644--- a/headroom/proxy/models.py+++ b/headroom/proxy/models.py@@ -395,9 +395,9 @@ class ProxyConfig:# Bound the dedicated compression threadpool. CPU-bound Rust work runs# here; the pool is separate from asyncio's default executor so other# ``asyncio.to_thread`` callers (file IO, etc.) are not contended by- # compression bursts. ``None`` resolves to ``min(32, (cpu_count or 1) * 4)``,- # matching asyncio's default executor sizing today. Lower the cap to- # tighten resource use on multi-tenant hosts; raise it to handle larger+ # compression bursts. ``None`` resolves to ``cpu_count or 1`` so CPU-bound+ # compression work does not oversubscribe hosts by default. Lower the cap+ # to tighten resource use on multi-tenant hosts; raise it to handle larger# bursts. CLI: ``--compression-max-workers``. Env:# ``HEADROOM_COMPRESSION_MAX_WORKERS``.#diff --git a/headroom/proxy/server.py b/headroom/proxy/server.pyindex 6a50ba01f..9bb228f58 100644--- a/headroom/proxy/server.py+++ b/headroom/proxy/server.py@@ -906,7 +906,7 @@ class HeadroomProxy(# are sitting on stuck work._compression_max_cfg = config.compression_max_workersif _compression_max_cfg is None:- _compression_max = min(32, (os.cpu_count() or 1) * 4)+ _compression_max = max(1, os.cpu_count() or 1)else:_compression_max = max(1, _compression_max_cfg)self.compression_max_workers: int = _compression_maxdiff --git a/tests/test_cli_proxy_improvements.py b/tests/test_cli_proxy_improvements.pyindex 3230fc2ec..927088710 100644--- a/tests/test_cli_proxy_improvements.py+++ b/tests/test_cli_proxy_improvements.py@@ -455,7 +455,7 @@ class TestCompressionMaxWorkers:Regression: the field was documented in ProxyConfig and consumed by theserver, but the CLI never defined the option or passed it through, so it- was permanently None (always resolving to the min(32, cpu*4) default).+ was permanently None (always resolving to the automatic server default)."""def test_flag_reaches_config(self, runner: CliRunner, mock_run_server: dict) -> None:diff --git a/tests/test_codex_ws_compression_scheduler.py b/tests/test_codex_ws_compression_scheduler.pyindex 3bc00593e..b8332e8b3 100644--- a/tests/test_codex_ws_compression_scheduler.py+++ b/tests/test_codex_ws_compression_scheduler.py@@ -12,7 +12,7 @@ The fix:* Deletes the per-call inner ``ThreadPoolExecutor``.* Processes routed units serially inside the frame-level worker thread(``self._compression_executor`` already provides frame-level parallelism- via 32 workers sized ``min(32, cpu*4)``).+ via the proxy-wide bounded executor).* Adds a ``PERF`` log emission from ``handle_openai_responses_ws`` soCodex traffic is no longer invisible to ``headroom perf``.@@ -85,9 +85,8 @@ def test_no_per_call_threadpool_inside_compress_routed_units() -> None:source = OPENAI_HANDLER.read_text()assert "concurrent.futures.ThreadPoolExecutor" not in source, ("Per-call ThreadPoolExecutor reintroduced in handlers/openai.py. "- "Submit work to `self._compression_executor` (already 32-worker, "- "instrumented, lifecycle-managed) instead of creating a new pool "- "per frame."+ "Submit work to `self._compression_executor` (instrumented and "+ "lifecycle-managed) instead of creating a new pool per frame.")@@ -214,7 +213,7 @@ async def test_codex_ws_emits_perf_log_with_cache_keys() -> None:# ``_compress_openai_responses_payload`` produced p99 per-call latency of# ~2.4s on a 12-CPU machine because of the 10-slot global semaphore. After# the fix, units run serially within the frame-level worker, but the-# 32-worker frame pool lets 30 frames run in parallel without contention.+# frame-level compression executor lets 30 frames run in parallel without contention.## Pass criteria mirror docs/superpowers/specs/P2-codex-scheduler-fix.md# "Success criteria":@@ -302,6 +301,7 @@ def test_concurrent_compression_has_no_semaphore_tail() -> None:assert not errors, f"Got {len(errors)} errors; first: {errors[0].error}"ratio = p99 / max(p50, 1)+ assert p99 < 250.0, f"p99 is {p99:.0f}ms; expected < 250ms on uniform-size workload."# The p99/p50 ratio only signals contention when the tail is also# *absolutely* large. On a fast/quiet runner p50 rounds toward 0ms, so the# ratio collapses to "p99 in ms" and a few milliseconds of ordinary@@ -309,13 +309,13 @@ def test_concurrent_compression_has_no_semaphore_tail() -> None:# ~5×) that has nothing to do with the semaphore. The deleted semaphore# produced a tail of *tens* of milliseconds (and ~27×); a healthy run keeps# p99 in the single-digit-ms range regardless of ratio. So only treat a high- # ratio as a regression once p99 clears a scheduler-noise floor.+ # ratio as a regression once p50 is measurable and p99 clears a noise floor.SEMAPHORE_TAIL_FLOOR_MS = 25.0- assert ratio < 4.0 or p99 < SEMAPHORE_TAIL_FLOOR_MS, (+ assert p50 < 1.0 or ratio < 4.0 or p99 < SEMAPHORE_TAIL_FLOOR_MS, (f"p99/p50 ratio is {ratio:.1f}× (p50={p50:.0f}ms, p99={p99:.0f}ms). "- f"Expected < 4× on uniform-size workload once p99 clears the "- f"{SEMAPHORE_TAIL_FLOOR_MS:.0f}ms noise floor — a high ratio with a large "- f"absolute tail means the semaphore-induced contention tail is back. "+ f"Expected < 4× on uniform-size workload once p50 is measurable and p99 clears "+ f"the {SEMAPHORE_TAIL_FLOOR_MS:.0f}ms noise floor — a high ratio with a large "+ f"absolute tail means the semaphore-induced contention tail may be back. "f"Pre-fix baseline ratio on this same workload shape was ~27× regardless "f"of CPU speed.")diff --git a/tests/test_proxy_compression_executor.py b/tests/test_proxy_compression_executor.pyindex 8e55c89dc..5077d2c91 100644--- a/tests/test_proxy_compression_executor.py+++ b/tests/test_proxy_compression_executor.py@@ -55,14 +55,14 @@ def _make_proxy(compression_max_workers: int | None = None):return app.state.proxy-def test_compression_executor_default_size_matches_asyncio_default() -> None:+def test_compression_executor_default_size_matches_cpu_count() -> None:"""When ``compression_max_workers`` is None, the resolved size should- match asyncio's default executor sizing (``min(32, (cpu+1)*4)`` style).+ match the host CPU count."""import osproxy = _make_proxy(compression_max_workers=None)- expected = min(32, (os.cpu_count() or 1) * 4)+ expected = max(1, os.cpu_count() or 1)assert proxy.compression_max_workers == expectedassert proxy._compression_executor._max_workers == expected