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.

feat(proxy): add request timeout config (#738)

nervousappsc0745d4161d1
diff --git a/docs/content/docs/configuration.mdx b/docs/content/docs/configuration.mdx
index 83f5993ef..cfb363414 100644
--- a/docs/content/docs/configuration.mdx
+++ b/docs/content/docs/configuration.mdx
@@ -241,6 +241,7 @@ headroom proxy --learn --min-evidence 3
| `HEADROOM_STRIP_INTERNAL_HEADERS` | Python proxy: whether to strip internal `x-headroom-*` request headers (e.g. `x-headroom-bypass`, `x-headroom-mode`, `x-headroom-user-id`, `x-headroom-stack`, `x-headroom-base-url`) before every upstream forwarder call (PR-A5, fixes P5-49). `enabled` (default) stops fingerprinting / leakage. `disabled` is an explicit operator opt-in for diagnostic shadow tracing — NOT a fallback. Inbound reads of these headers (bypass gating, memory user-id resolution) are unaffected because they read `request.headers` directly. | `enabled` |
| `HEADROOM_PROXY_STRIP_INTERNAL_HEADERS` | Rust proxy: same policy as `HEADROOM_STRIP_INTERNAL_HEADERS` but for the Rust transparent proxy. Stripping happens inside `build_forward_request_headers` so both HTTP and WebSocket upstream calls are gated by one flag. `enabled` default; `disabled` operator opt-in for diagnostic shadow tracing. Response-side `X-Headroom-*` injection (e.g. `x-headroom-tokens-saved`) is unrelated and stays. | `enabled` |
| `HEADROOM_EMBEDDER_RUNTIME` | Set to `pytorch_mps` to run the memory embedder via the torch sentence-transformers backend on the Apple GPU (MPS). Only engages when Apple MPS is actually available; otherwise it logs a warning and uses the existing default embedder selection path. `pytorch_mps` is the only accepted value. Requires the `[pytorch-mps]` extra. See [Memory](/docs/memory#embedding-runtime--gpu-offload-apple-silicon). | default embedder selection |
+| `HEADROOM_REQUEST_TIMEOUT` | Request timeout in seconds | `300` |
| `HEADROOM_BETA_HEADER_STICKY` | Controls per-session `anthropic-beta` / `OpenAI-Beta` re-echo. `enabled` (default): the proxy unions beta tokens across turns within a session — if the client sends a token in turn N and omits it in turn N+1, the proxy re-injects it to preserve prefix-cache stability. `disabled`: the client's value is forwarded verbatim with no accumulation. Any other value raises at request time. See [Session Beta Header Tracking](/docs/configuration#session-beta-header-tracking). | `enabled` |
| `HEADROOM_BETA_TRACKER_MAX_SESSIONS` | LRU capacity of the in-memory session beta tracker. Once full, the oldest session entry is evicted. | `1000` |
diff --git a/docs/content/docs/installation.mdx b/docs/content/docs/installation.mdx
index 4b681a170..4b500f514 100644
--- a/docs/content/docs/installation.mdx
+++ b/docs/content/docs/installation.mdx
@@ -228,6 +228,7 @@ These variables configure Headroom at runtime. Set them in your shell, `.env` fi
| `HEADROOM_HOST` | `127.0.0.1` | Host the proxy binds to |
| `HEADROOM_MODE` | `token` | Default optimization mode: `token` or `cache` |
| `HEADROOM_TELEMETRY` | `off` (opt-in) | Set to `on` to opt in to anonymous telemetry |
+| `HEADROOM_REQUEST_TIMEOUT` | `300` | Request timeout in seconds |
### TypeScript SDK
diff --git a/headroom/cli/proxy.py b/headroom/cli/proxy.py
index 126dfe7e6..804242f76 100644
--- a/headroom/cli/proxy.py
+++ b/headroom/cli/proxy.py
@@ -254,6 +254,17 @@ def _selected_context_tool() -> str:
"Env: HEADROOM_RETRY_MAX_ATTEMPTS."
),
)
+ "--request-timeout-seconds",
+ type=int,
+ default=None,
+ envvar="HEADROOM_REQUEST_TIMEOUT",
+ help=(
+ "Request timeout in seconds (default: 300). "
+ "Useful for slow providers (eg local). "
+ "Env: HEADROOM_REQUEST_TIMEOUT."
+ ),
+)
@click.option(
"--connect-timeout-seconds",
type=click.IntRange(min=1, max=300),
@@ -652,6 +663,7 @@ def proxy(
no_subscription_tracking: bool,
subscription_poll_interval: int | None,
retry_max_attempts: int | None,
+ request_timeout_seconds: int | None,
connect_timeout_seconds: int | None,
anthropic_pre_upstream_concurrency: int | None,
anthropic_pre_upstream_acquire_timeout_seconds: float | None,
@@ -864,6 +876,9 @@ def proxy(
subscription_poll_interval if subscription_poll_interval is not None else 300
),
retry_max_attempts=retry_max_attempts if retry_max_attempts is not None else 3,
+ request_timeout_seconds=request_timeout_seconds
+ if request_timeout_seconds is not None and request_timeout_seconds > 0
+ else 300,
connect_timeout_seconds=connect_timeout_seconds
if connect_timeout_seconds is not None
else 10,
diff --git a/tests/test_cli_proxy_env.py b/tests/test_cli_proxy_env.py
index 013768159..9a4e0d119 100644
--- a/tests/test_cli_proxy_env.py
+++ b/tests/test_cli_proxy_env.py
@@ -512,6 +512,51 @@ class TestCLIProxyEnvVars:
assert captured_config["config"].openai_api_url == "http://my-vllm:4000"
assert captured_config["config"].gemini_api_url == "http://my-gemini:5000"
+ @pytest.mark.parametrize("timeout", [-1, 0, 1, 10000])
+ def test_request_timeout_cli_flags(self, runner, timeout):
+ """Fast-fail CLI flags should map into ProxyConfig."""
+ captured_config = {}
+
+ def mock_run_server(config, **kwargs):
+ captured_config["config"] = config
+
+ with patch("headroom.proxy.server.run_server", mock_run_server):
+ result = runner.invoke(
+ main,
+ ["proxy", "--request-timeout-seconds", f"{timeout}"],
+ catch_exceptions=False,
+ )
+
+ assert result.exit_code == 0, result.output
+ assert (
+ captured_config["config"].request_timeout_seconds == timeout
+ if timeout and timeout > 0
+ else 300
+ )
+
+ @pytest.mark.parametrize("timeout", [-1, 0, 1, 10000])
+ def test_request_timeout_from_env(self, runner, timeout):
+ """HEADROOM_REQUEST_TIMEOUT env var should be passed to ProxyConfig."""
+ captured_config = {}
+
+ def mock_run_server(config, **kwargs):
+ captured_config["config"] = config
+
+ with patch("headroom.proxy.server.run_server", mock_run_server):
+ result = runner.invoke(
+ main,
+ ["proxy"],
+ env={"HEADROOM_REQUEST_TIMEOUT": f"{timeout}"},
+ catch_exceptions=False,
+ )
+
+ assert result.exit_code == 0, result.output
+ assert (
+ captured_config["config"].request_timeout_seconds == timeout
+ if timeout and timeout > 0
+ else 300
+ )
+
def test_retry_and_connect_timeout_cli_flags(self, runner):
"""Fast-fail CLI flags should map into ProxyConfig."""
captured_config = {}
diff --git a/wiki/cli.md b/wiki/cli.md
index becd6c6a0..9eb3f56e2 100644
--- a/wiki/cli.md
+++ b/wiki/cli.md
@@ -245,6 +245,7 @@ headroom proxy --mode cache
| `--no-cache` | off | Disable semantic caching |
| `--no-rate-limit` | off | Disable rate limiting |
| `--retry-max-attempts` | runtime default `3` | Maximum upstream retry attempts |
+| `--request-timeout-seconds` | runtime default `300` | Request timeout in seconds |
| `--connect-timeout-seconds` | runtime default `10` | Upstream connection timeout |
| `--anthropic-pre-upstream-concurrency` | auto `max(2, min(8, cpu_count))` | Cap simultaneous pre-upstream work on `/v1/messages` (body read, deep copy, first compression stage, memory-context lookup, upstream connect). `0` or negative disables (unbounded); any positive integer is honoured verbatim. Prevents cold-start replay storms from starving `/livez`, `/readyz`, and new Codex WS opens. |
| `--anthropic-pre-upstream-acquire-timeout-seconds` | `15.0` | Fail fast when the Anthropic pre-upstream queue is saturated. Requests that wait longer return `503` with `Retry-After` instead of parking indefinitely. |
@@ -278,7 +279,7 @@ headroom proxy --mode cache
Notes:
- `--learn` implies memory unless `--no-learn` is also set.
-- Proxy startup can also read environment variables such as `HEADROOM_HOST`, `HEADROOM_PORT`, `HEADROOM_BUDGET`, `HEADROOM_MODE`, `HEADROOM_ANYLLM_PROVIDER`, `HEADROOM_ANTHROPIC_PRE_UPSTREAM_CONCURRENCY`, `HEADROOM_ANTHROPIC_PRE_UPSTREAM_ACQUIRE_TIMEOUT_SECONDS`, `HEADROOM_ANTHROPIC_PRE_UPSTREAM_MEMORY_CONTEXT_TIMEOUT_SECONDS`, `ANTHROPIC_TARGET_API_URL`, `OPENAI_TARGET_API_URL`, and `GEMINI_TARGET_API_URL`. CLI flags take precedence over environment variables.
+- Proxy startup can also read environment variables such as `HEADROOM_HOST`, `HEADROOM_PORT`, `HEADROOM_BUDGET`, `HEADROOM_MODE`, `HEADROOM_ANYLLM_PROVIDER`, `HEADROOM_ANTHROPIC_PRE_UPSTREAM_CONCURRENCY`, `HEADROOM_ANTHROPIC_PRE_UPSTREAM_ACQUIRE_TIMEOUT_SECONDS`, `HEADROOM_REQUEST_TIMEOUT`, `HEADROOM_ANTHROPIC_PRE_UPSTREAM_MEMORY_CONTEXT_TIMEOUT_SECONDS`, `ANTHROPIC_TARGET_API_URL`, `OPENAI_TARGET_API_URL`, and `GEMINI_TARGET_API_URL`. CLI flags take precedence over environment variables.
- The default Anthropic pre-upstream cap is intentionally conservative for CPU/ONNX-heavy work. Larger containers may want to raise it after checking the resolved runtime values on `/readyz` or `/debug/warmup`.
See also: [Proxy Server](proxy.md), [Configuration](configuration.md)