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): preserve signed Anthropic thinking blocks on outbound re-serialize (#2254)

AxelRaydc163bcd1cba
diff --git a/headroom/proxy/body_forwarding.py b/headroom/proxy/body_forwarding.py
index 2897d7d8f..f93c68af9 100644
--- a/headroom/proxy/body_forwarding.py
+++ b/headroom/proxy/body_forwarding.py
@@ -4,6 +4,7 @@ This module owns the small algebra used by Python proxy forwarders to decide
which bytes leave Headroom:
* unmutated body with original bytes -> byte-for-byte passthrough
+* signed thinking blocks with original bytes -> byte-for-byte passthrough
* mutated body or missing original bytes -> canonical JSON bytes
* explicit rollback mode -> legacy httpx-style JSON bytes
"""
@@ -50,6 +51,27 @@ def serialize_body_canonical(body: dict[str, Any]) -> bytes:
return json.dumps(body, separators=(",", ":"), ensure_ascii=False).encode("utf-8")
+def has_signed_thinking_blocks(body: dict[str, Any]) -> bool:
+ """Return whether message history contains Anthropic signed thinking blocks."""
+ messages = body.get("messages")
+ if not isinstance(messages, list):
+ return False
+
+ for message in messages:
+ if not isinstance(message, dict):
+ continue
+ content = message.get("content")
+ if not isinstance(content, list):
+ continue
+ for block in content:
+ if isinstance(block, dict) and block.get("type") in {
+ "thinking",
+ "redacted_thinking",
+ }:
+ return True
+ return False
+
+
class BodyMutationTracker:
"""Records whether a request body was mutated and why."""
@@ -85,6 +107,9 @@ def select_outbound_body(
) -> OutboundBody:
"""Select the exact bytes to forward upstream."""
mode = forwarder_mode if forwarder_mode is not None else get_python_forwarder_mode()
+ if original_body_bytes is not None and has_signed_thinking_blocks(body):
+ return OutboundBody(content=original_body_bytes, source="passthrough")
+
if mode == "legacy_json_kwarg":
content = json.dumps(body, separators=(", ", ": "), ensure_ascii=True).encode("utf-8")
return OutboundBody(content=content, source="legacy")
diff --git a/tests/test_proxy_byte_faithful_forwarding.py b/tests/test_proxy_byte_faithful_forwarding.py
index b02f4e654..7fc2dbd69 100644
--- a/tests/test_proxy_byte_faithful_forwarding.py
+++ b/tests/test_proxy_byte_faithful_forwarding.py
@@ -172,6 +172,89 @@ def test_prepare_outbound_mutated_uses_canonical() -> None:
assert source == "canonical"
[email protected]("block_type", ["thinking", "redacted_thinking"])
+def test_signed_thinking_history_with_original_bytes_uses_passthrough(
+ block_type: str,
+) -> None:
+ body = {
+ "model": "claude-sonnet-4-5",
+ "messages": [
+ {"role": "user", "content": "Solve this"},
+ {
+ "role": "assistant",
+ "content": [
+ {
+ "type": block_type,
+ "thinking": "private reasoning",
+ "signature": "sig123",
+ },
+ {"type": "text", "text": "The answer is 42."},
+ ],
+ },
+ {"role": "user", "content": "Continue"},
+ ],
+ }
+ original = json.dumps(body, indent=2).encode("utf-8")
+
+ outbound = select_outbound_body(
+ body=body,
+ original_body_bytes=original,
+ body_mutated=True,
+ forwarder_mode="byte_faithful",
+ )
+
+ assert outbound.source == "passthrough"
+ assert outbound.content == original
+
+
+def test_signed_thinking_history_without_original_bytes_uses_canonical() -> None:
+ body = {
+ "messages": [
+ {
+ "role": "assistant",
+ "content": [
+ {
+ "type": "thinking",
+ "thinking": "private reasoning",
+ "signature": "sig123",
+ }
+ ],
+ }
+ ]
+ }
+
+ outbound = select_outbound_body(
+ body=body,
+ original_body_bytes=None,
+ body_mutated=True,
+ forwarder_mode="byte_faithful",
+ )
+
+ assert outbound.source == "canonical"
+ assert outbound.content == serialize_body_canonical(body)
+
+
+def test_signed_thinking_history_overrides_legacy_encoder() -> None:
+ body = {
+ "messages": [
+ {
+ "role": "assistant",
+ "content": [{"type": "thinking", "signature": "sig123"}],
+ }
+ ]
+ }
+ original = json.dumps(body, indent=2).encode("utf-8")
+
+ outbound = select_outbound_body(
+ body=body,
+ original_body_bytes=original,
+ body_mutated=True,
+ forwarder_mode="legacy_json_kwarg",
+ )
+
+ assert outbound == OutboundBody(content=original, source="passthrough")
+
+
def test_prepare_outbound_no_original_bytes_uses_canonical() -> None:
out, source = prepare_outbound_body_bytes(
body={"a": 1},