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(savings): key a Responses conversation on its thread, not the shared session (#4440)
gglucass11f743dd90e9
diff --git a/headroom/proxy/conversation_savings.py b/headroom/proxy/conversation_savings.pyindex df2de82fa..5ebeeb148 100644--- a/headroom/proxy/conversation_savings.py+++ b/headroom/proxy/conversation_savings.py@@ -75,6 +75,10 @@ def savings_conversation_key(body: Any, *, session_id: str | None = None) -> strtop-level ``conversation_id``/``session_id``/``thread_id``, one ofthose inside ``metadata``/``client_metadata``, or a transport-scoped``session_id`` the caller vouches for (a WebSocket, a session header).+ A ``thread_id`` wins over a ``session_id`` beside it: Codex sends both,+ and its ``session_id`` is the root thread's on every request of the+ session, so a guardian or subagent request would otherwise reset the+ root's running total and make its next turn re-book its history.Two things are deliberately NOT accepted. The holdout key's fallbacks(the instructions prefix, the literal ``responses``): two independentconversations with the same instructions would share a running total@@ -101,7 +105,7 @@ def savings_conversation_key(body: Any, *, session_id: str | None = None) -> strreturn Noneidentity = ""- for key in ("conversation_id", "session_id", "thread_id"):+ for key in ("conversation_id", "thread_id", "session_id"):value = _explicit_id(body.get(key))if value:identity = f"{key}:{value}"@@ -114,8 +118,8 @@ def savings_conversation_key(body: Any, *, session_id: str | None = None) -> strfor key in ("conversation_id","conversation_key",- "session_id","thread_id",+ "session_id","codex_session_id",):value = _explicit_id(container.get(key))diff --git a/tests/test_proxy/test_conversation_savings.py b/tests/test_proxy/test_conversation_savings.pyindex 52a826c04..d28d03253 100644--- a/tests/test_proxy/test_conversation_savings.py+++ b/tests/test_proxy/test_conversation_savings.py@@ -322,6 +322,36 @@ def test_explicit_ids_and_caller_vouched_sessions() -> None:)+def _codex_body(text: str, thread_id: str, *, session_id: str = "root-thread") -> dict[str, Any]:+ """Codex's shape: both ids flat in ``client_metadata``, the session id+ being the root thread's id on every request of the session."""+ return _responses_body(text, client_metadata={"session_id": session_id, "thread_id": thread_id})+++def test_a_thread_id_wins_over_the_session_beside_it() -> None:+ root = savings_conversation_key(_codex_body("fix it", "root-thread"))+ guardian = savings_conversation_key(_codex_body("review this", "guardian-thread"))+ subagent = savings_conversation_key(_codex_body("explore", "subagent-thread"))+ assert None not in (root, guardian, subagent)+ assert len({root, guardian, subagent}) == 3+ top = _responses_body("x", session_id="s-1", thread_id="t-1")+ assert savings_conversation_key(top) == savings_conversation_key(+ _responses_body("y", thread_id="t-1")+ )+++def test_a_side_thread_in_the_session_does_not_re_book_the_root() -> None:+ """Root turns at running totals 1000 then 1200, with the guardian's+ request (total 0) in between. Keyed on the shared session the guardian+ reset the total and the root booked 1000 + 0 + 1200; keyed per thread+ the root books 1000 + 200."""+ ledger = ConversationSavings()+ root = savings_conversation_key(_codex_body("fix it", "root-thread"))+ guardian = savings_conversation_key(_codex_body("review this", "guardian-thread"))+ booked = [ledger.novel(root, 1000), ledger.novel(guardian, 0), ledger.novel(root, 1200)]+ assert booked == [1000, 0, 200]++def test_server_side_state_means_incremental_input() -> None:"""With ``previous_response_id`` or a ``conversation`` the provider holdsthe history and each payload is a fresh increment: its ``tokens_saved``