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(proxy): preserve Responses memory continuations with store=false (#1103)
Ian Ker-Seymercdfeeacc63e6
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex bac7870d8..08a561473 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -29,6 +29,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0### Bug Fixes+* **proxy:** force Responses API `store=true` when Headroom injects memory tools so `previous_response_id` continuations work after memory tool calls from clients that requested `store=false` ([#1103](https://github.com/chopratejas/headroom/pull/1103)).* **proxy:** build SSL contexts for custom CA bundles so enterprise/private PKI roots work with Python/OpenSSL strict verification.* **proxy:** route Codex OAuth image generation and edit requests through the ChatGPT Codex image backend, while preserving OpenAI API-key image passthrough ([#1215](https://github.com/chopratejas/headroom/pull/1215)).* **wrap (codex):** keep RTK guidance in the global Codex `AGENTS.md` instead of modifying the shared project `AGENTS.md` ([#1235](https://github.com/chopratejas/headroom/issues/1235)).diff --git a/headroom/proxy/handlers/openai.py b/headroom/proxy/handlers/openai.pyindex 96f0ca0f1..d61ced6ee 100644--- a/headroom/proxy/handlers/openai.py+++ b/headroom/proxy/handlers/openai.py@@ -321,6 +321,29 @@ def _compact_openai_responses_tools(return updated, True, before, after+def _ensure_responses_store_for_memory_tools(+ payload: dict[str, Any],+ *,+ memory_tools_injected: bool,+) -> bool:+ """Keep Responses API memory-tool continuations addressable.++ Memory tools are transparent to clients: Headroom executes the emitted+ function_call, then sends function_call_output in a continuation request+ using previous_response_id. OpenAI only allows that continuation when the+ previous response was stored. Clients such as pi/Codex can set store=false+ to avoid retaining ordinary responses, but that makes memory-tool+ continuations fail with previous_response_not_found.++ Return True when this function changes the payload.+ """++ if memory_tools_injected and payload.get("store") is False:+ payload["store"] = True+ return True+ return False++def _responses_input_item_text_bytes(item: Any) -> int:if not isinstance(item, dict):return _json_byte_len(item)@@ -3093,6 +3116,14 @@ class OpenAIHandlerMixin:if mem_tools_injected:body["tools"] = resp_toolslogger.info(f"[{request_id}] Memory: Injected memory tools (openai/responses)")++ if _ensure_responses_store_for_memory_tools(+ body,+ memory_tools_injected=True,+ ):+ logger.info(+ f"[{request_id}] Memory: forced store=true for Responses memory tool continuation"+ )except Exception as e:logger.warning(f"[{request_id}] Memory injection failed (responses): {e}")elif self.memory_handler and memory_user_id and _bypass:diff --git a/tests/test_openai_responses_context_compaction.py b/tests/test_openai_responses_context_compaction.pyindex 264d693a3..6f5eff3ec 100644--- a/tests/test_openai_responses_context_compaction.py+++ b/tests/test_openai_responses_context_compaction.py@@ -6,6 +6,7 @@ from typing import Anyfrom headroom.proxy.handlers.openai import (OpenAIHandlerMixin,_compact_openai_responses_tools,+ _ensure_responses_store_for_memory_tools,_openai_responses_context_budget,)from headroom.transforms.content_router import (@@ -435,3 +436,50 @@ def test_content_router_retries_kompress_when_structured_strategy_noops(monkeypaassert compressed_tokens == 2# The fallback chain must record both strategies it tried.assert strategy_chain == ["smart_crusher", "kompress"]+++def test_responses_memory_tools_store_false_regression() -> None:+ """Regression: store=false makes previous_response_id continuations fail."""++ payload = {"model": "gpt-5.5", "input": "remember this", "store": False}++ changed = _ensure_responses_store_for_memory_tools(+ payload,+ memory_tools_injected=True,+ )++ assert changed is True+ assert payload["store"] is True+++def test_responses_memory_tools_do_not_change_unrelated_requests() -> None:+ no_memory_payload = {"model": "gpt-5.5", "input": "plain", "store": False}+ already_stored_payload = {"model": "gpt-5.5", "input": "plain", "store": True}+ default_store_payload = {"model": "gpt-5.5", "input": "plain"}++ assert (+ _ensure_responses_store_for_memory_tools(+ no_memory_payload,+ memory_tools_injected=False,+ )+ is False+ )+ assert no_memory_payload["store"] is False++ assert (+ _ensure_responses_store_for_memory_tools(+ already_stored_payload,+ memory_tools_injected=True,+ )+ is False+ )+ assert already_stored_payload["store"] is True++ assert (+ _ensure_responses_store_for_memory_tools(+ default_store_payload,+ memory_tools_injected=True,+ )+ is False+ )+ assert "store" not in default_store_payload