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.

fix(proxy/anthropic): keep tool_result blocks first when neutralizing headroom_retrieve history (#3874)

gglucassccd9fffce93c
diff --git a/headroom/proxy/helpers.py b/headroom/proxy/helpers.py
index 809aa03d8..f24acac5f 100644
--- a/headroom/proxy/helpers.py
+++ b/headroom/proxy/helpers.py
@@ -4286,7 +4286,19 @@ def strip_unsupported_ccr_retrieve_blocks(messages: Any, tools: Any) -> tuple[An
if touched:
changed = True
repaired = dict(message)
- repaired["content"] = new_content
+ # Anthropic requires a user turn's tool_result blocks to lead its
+ # content. When headroom_retrieve ran in parallel with another tool,
+ # neutralizing its result in place leaves [text, tool_result(sibling)]
+ # and the request 400s ("tool_use ids were found without tool_result
+ # blocks immediately after"). Stable-partition so surviving
+ # tool_results stay first; a no-op for assistant turns, which carry none.
+ repaired["content"] = [
+ b for b in new_content if isinstance(b, dict) and b.get("type") == "tool_result"
+ ] + [
+ b
+ for b in new_content
+ if not (isinstance(b, dict) and b.get("type") == "tool_result")
+ ]
out.append(repaired)
else:
out.append(message)
diff --git a/tests/test_ccr_retrieve_history_repair.py b/tests/test_ccr_retrieve_history_repair.py
index bbd840d95..e4f0cd271 100644
--- a/tests/test_ccr_retrieve_history_repair.py
+++ b/tests/test_ccr_retrieve_history_repair.py
@@ -117,3 +117,36 @@ def test_result_falls_back_to_placeholder_without_text() -> None:
out, n = strip_unsupported_ccr_retrieve_blocks(messages, tools=[])
assert n == 2
assert out[1]["content"][0] == {"type": "text", "text": "[headroom_retrieve result omitted]"}
+
+
+def test_parallel_retrieve_keeps_sibling_tool_result_first() -> None:
+ # headroom_retrieve issued in parallel with Bash, its result listed first.
+ # Neutralizing in place produced user [text, tool_result(Bash)], which
+ # Anthropic rejects: "`tool_use` ids were found without `tool_result`
+ # blocks immediately after".
+ messages = [
+ {
+ "role": "assistant",
+ "content": [
+ {"type": "tool_use", "id": "toolu_ccr", "name": CCR_TOOL_NAME, "input": {}},
+ {"type": "tool_use", "id": "toolu_bash", "name": "Bash", "input": {}},
+ ],
+ },
+ {
+ "role": "user",
+ "content": [
+ {"type": "tool_result", "tool_use_id": "toolu_ccr", "content": "expanded"},
+ {"type": "tool_result", "tool_use_id": "toolu_bash", "content": "ok"},
+ {"type": "text", "text": "keep going"},
+ ],
+ },
+ ]
+ out, n = strip_unsupported_ccr_retrieve_blocks(messages, tools=[{"name": "Bash"}])
+
+ assert n == 2
+ assert [b["type"] for b in out[0]["content"]] == ["text", "tool_use"]
+ assert out[1]["content"] == [
+ {"type": "tool_result", "tool_use_id": "toolu_bash", "content": "ok"},
+ {"type": "text", "text": "expanded"},
+ {"type": "text", "text": "keep going"},
+ ]