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(langgraph): replace the compressed ToolMessage in graph state (#4620)

Tejas Chopra70a8f0979e58
diff --git a/headroom/integrations/langchain/langgraph.py b/headroom/integrations/langchain/langgraph.py
index 93cab2577..d844338c1 100644
--- a/headroom/integrations/langchain/langgraph.py
+++ b/headroom/integrations/langchain/langgraph.py
@@ -313,11 +313,9 @@ def compress_tool_messages(
tokens_after = _estimate_tokens(compressed_text)
if was_modified and tokens_after < tokens_before:
- # Create new ToolMessage with compressed content, preserving tool_call_id
- compressed_msg = ToolMessage(
- content=compressed_text,
- tool_call_id=msg.tool_call_id,
- )
+ # Copy so id, name and tool_call_id survive: LangGraph's add_messages
+ # replaces a message by id, and appends one without it as a duplicate.
+ compressed_msg = msg.model_copy(update={"content": compressed_text})
result_messages.append(compressed_msg)
tokens_saved = tokens_before - tokens_after
diff --git a/tests/test_integrations/langchain/test_langgraph.py b/tests/test_integrations/langchain/test_langgraph.py
index 8e3dbd69c..d3dd3b707 100644
--- a/tests/test_integrations/langchain/test_langgraph.py
+++ b/tests/test_integrations/langchain/test_langgraph.py
@@ -314,6 +314,31 @@ class TestCreateCompressToolMessagesNode:
assert result_state["messages"][2].tool_call_id == "call_xyz"
+ def test_node_replaces_tool_message_in_graph_state(self):
+ """In a real graph the compressed result replaces the original, not joins it."""
+ from typing import Annotated, TypedDict
+
+ langgraph_graph = pytest.importorskip("langgraph.graph")
+ from langgraph.graph.message import add_messages
+
+ from headroom.integrations.langchain.langgraph import create_compress_tool_messages_node
+
+ class State(TypedDict):
+ messages: Annotated[list, add_messages]
+
+ large_output = _make_large_tool_output(200)
+ graph = langgraph_graph.StateGraph(State)
+ graph.add_node("compress", create_compress_tool_messages_node())
+ graph.add_edge(langgraph_graph.START, "compress")
+ graph.add_edge("compress", langgraph_graph.END)
+
+ out = graph.compile().invoke({"messages": _make_messages_with_tool_output(large_output)})
+
+ tool_messages = [m for m in out["messages"] if isinstance(m, ToolMessage)]
+ assert len(out["messages"]) == 3
+ assert len(tool_messages) == 1
+ assert len(tool_messages[0].content) < len(large_output)
+
def test_node_handles_empty_state(self):
"""Node should handle empty messages gracefully."""
from headroom.integrations.langchain.langgraph import create_compress_tool_messages_node