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): inject memory context past a trailing system message (#3948)
Kenneth Wong3948dbe409eb
diff --git a/headroom/proxy/handlers/anthropic.py b/headroom/proxy/handlers/anthropic.pyindex 06e138bac..2974e602f 100644--- a/headroom/proxy/handlers/anthropic.py+++ b/headroom/proxy/handlers/anthropic.py@@ -636,6 +636,11 @@ class AnthropicHandlerMixin:first text block of the latest user message is mutated, which is bydefinition the live zone.+ Trailing ``role: "system"`` messages are skipped when locating that+ turn: Claude Code appends one after the user message (environment+ context carrying the cache breakpoint), and it is not a conversational+ turn. It is left byte-identical.+Returns the input list unchanged if no eligible user text blockexists (e.g., the last message is an assistant turn or a toolresult, or the user message has no text block).@@ -644,7 +649,9 @@ class AnthropicHandlerMixin:return messagesi = len(messages) - 1- if i < frozen_message_count:+ while i >= 0 and messages[i].get("role") == "system":+ i -= 1+ if i < 0 or i < frozen_message_count:return messagesmsg = messages[i]if msg.get("role") != "user":diff --git a/tests/test_proxy_handler_helpers.py b/tests/test_proxy_handler_helpers.pyindex 240b614d1..e430be17f 100644--- a/tests/test_proxy_handler_helpers.py+++ b/tests/test_proxy_handler_helpers.py@@ -945,6 +945,50 @@ def test_anthropic_tool_sort_and_context_append_helpers() -> None:) == [{"role": "user", "content": [{"type": "text", "text": "hello\n\nctx"}]}]+def test_append_context_skips_trailing_system_message() -> None:+ # Claude Code 2.1.x request shape: the user turn is followed by a+ # role="system" message carrying the environment and the cache breakpoint.+ trailing_system = {+ "role": "system",+ "content": [+ {"type": "text", "text": "# Environment", "cache_control": {"type": "ephemeral"}}+ ],+ }+ user_turn = {+ "role": "user",+ "content": [+ {"type": "text", "text": "<system-reminder>ctx</system-reminder>"},+ {"type": "text", "text": "question"},+ ],+ }+ inject = AnthropicHandlerMixin._append_context_to_latest_non_frozen_user_turn++ assert inject([user_turn, trailing_system], "memory", frozen_message_count=0) == [+ {+ "role": "user",+ "content": [+ {"type": "text", "text": "<system-reminder>ctx</system-reminder>\n\nmemory"},+ {"type": "text", "text": "question"},+ ],+ },+ trailing_system,+ ]+ # The user turn is still subject to the frozen prefix.+ messages = [user_turn, trailing_system]+ assert inject(messages, "memory", frozen_message_count=1) is messages+ # Skipping system messages never reaches past a non-user turn.+ messages = [user_turn, {"role": "assistant", "content": "ok"}, trailing_system]+ assert inject(messages, "memory", frozen_message_count=0) is messages+ # Tool-result-only turns have no text block to extend.+ messages = [+ {"role": "user", "content": [{"type": "tool_result", "tool_use_id": "t1", "content": "x"}]},+ trailing_system,+ ]+ assert inject(messages, "memory", frozen_message_count=0) is messages+ messages = [trailing_system]+ assert inject(messages, "memory", frozen_message_count=0) is messages++def test_anthropic_image_compression_helper_only_rewrites_latest_eligible_turn() -> None:image_message = {"role": "user",