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
refactor(proxy): tighten types + formatting from CE review
Adryan Eka Vandra60c44930102e
diff --git a/headroom/proxy/handlers/openai.py b/headroom/proxy/handlers/openai.pyindex 54aaa95f9..0240adca1 100644--- a/headroom/proxy/handlers/openai.py+++ b/headroom/proxy/handlers/openai.py@@ -20,6 +20,7 @@ from typing import TYPE_CHECKING, Anyfrom headroom.proxy.helpers import jitter_delay_msfrom headroom.proxy.stage_timer import StageTimer, emit_stage_timings_logfrom headroom.proxy.ws_session_registry import (+ TerminationCause,WebSocketSessionRegistry,WSSessionHandle,)@@ -1267,7 +1268,7 @@ class OpenAIHandlerMixin:self, "ws_sessions", None)session_handle: WSSessionHandle | None = None- termination_cause: str = "unknown"+ termination_cause: TerminationCause = "unknown"# Forward client headers to upstream, adding required OpenAI-Beta headerws_headers = dict(websocket.headers)diff --git a/headroom/proxy/loopback_guard.py b/headroom/proxy/loopback_guard.pyindex 296959c4d..ce73fe589 100644--- a/headroom/proxy/loopback_guard.py+++ b/headroom/proxy/loopback_guard.py@@ -18,6 +18,8 @@ middleware) because:would be disproportionate."""+from __future__ import annotations+import ipaddresstry:diff --git a/headroom/proxy/stage_timer.py b/headroom/proxy/stage_timer.pyindex ff4a44200..8c957e017 100644--- a/headroom/proxy/stage_timer.py+++ b/headroom/proxy/stage_timer.py@@ -36,7 +36,10 @@ logger = logging.getLogger("headroom.proxy")__all__ = ["StageTimer", "StageMeasurement", "emit_stage_timings_log"]-class StageMeasurement(AbstractContextManager["StageMeasurement"], AbstractAsyncContextManager["StageMeasurement"]):+class StageMeasurement(+ AbstractContextManager["StageMeasurement"],+ AbstractAsyncContextManager["StageMeasurement"],+):"""Context manager that measures a single named stage.Acts as both a synchronous (``with timer.measure(...):``) and andiff --git a/headroom/proxy/ws_session_registry.py b/headroom/proxy/ws_session_registry.pyindex cde4dc919..167ce16d2 100644--- a/headroom/proxy/ws_session_registry.py+++ b/headroom/proxy/ws_session_registry.py@@ -34,7 +34,7 @@ import loggingimport timefrom collections.abc import Iterablefrom dataclasses import dataclass, field-from typing import Any, Protocol+from typing import Any, Literal, Protocollogger = logging.getLogger(__name__)@@ -45,8 +45,15 @@ __all__ = []-TerminationCause = str # "client_disconnect" | "upstream_disconnect" | "upstream_error"-# | "client_error" | "response_completed" | "unknown"+TerminationCause = Literal[+ "client_disconnect",+ "upstream_disconnect",+ "upstream_error",+ "client_error",+ "response_completed",+ "client_timeout",+ "unknown",+]class _TaskLike(Protocol):@@ -69,7 +76,7 @@ class WSSessionHandle:upstream_url: str | None = Nonestarted_at: float = field(default_factory=time.perf_counter)last_activity_at: float = field(default_factory=time.perf_counter)- relay_tasks: list[Any] = field(default_factory=list)+ relay_tasks: list[_TaskLike] = field(default_factory=list)termination_cause: TerminationCause | None = Nonedef mark_activity(self) -> None:@@ -87,9 +94,7 @@ class WSSessionHandle:"age_seconds": self.age_seconds(),"idle_seconds": max(0.0, time.perf_counter() - self.last_activity_at),"relay_task_count": len(self.relay_tasks),- "relay_task_names": [- getattr(t, "get_name", lambda: "")() for t in self.relay_tasks- ],+ "relay_task_names": [t.get_name() for t in self.relay_tasks],"termination_cause": self.termination_cause,}