Collapse consecutive read/search tool calls to reduce agent context size
Project description
langchain-collapse
Collapse consecutive read/search tool-call groups to save context.
Quick Install
pip install langchain-collapse
🤔 What is this?
Most context management is reactive — it compresses after the context window is already full. This middleware is preventive. It collapses consecutive read/search tool-call groups into a short placeholder before they accumulate, so expensive LLM-based summarization triggers less often.
No LLM calls. No hallucination risk. Stateless.
langchain-collapse benchmark
==================================================
Before After
Messages 37 9
Approx tokens 2531 197
Token reduction 92%
SummarizationMiddleware trigger point (at 85% of 200K):
Without CollapseMiddleware: ~1214 tool calls
With CollapseMiddleware: ~5151 tool calls
Ratio: 4.2x more work before summarization
Usage
from langchain.agents import create_agent
from langchain_collapse import CollapseMiddleware
agent = create_agent(
model="anthropic:claude-sonnet-4-6",
tools=[...],
middleware=[CollapseMiddleware()],
)
Place before SummarizationMiddleware so it reduces message count first:
from langchain.agents.middleware import SummarizationMiddleware
middleware = [
CollapseMiddleware(),
SummarizationMiddleware(
model="anthropic:claude-haiku-4-5-20251001",
trigger=("fraction", 0.85),
),
]
How it works
Five consecutive read_file calls (10 messages) become 3:
Before:
AIMessage(read_file "auth.py") ─┐
ToolMessage(content) │ collapsed
AIMessage(read_file "users.py") │
ToolMessage(content) │
AIMessage(read_file "db.py") │
ToolMessage(content) │
AIMessage(read_file "routes.py") ─┘
ToolMessage(content)
AIMessage(read_file "config.py") ─── kept (most recent)
ToolMessage(content) ─── kept
After:
HumanMessage("[4 tool results omitted — most recent result preserved below]")
AIMessage(read_file "config.py")
ToolMessage(content)
Configuration
CollapseMiddleware(
collapse_tools=frozenset({"read_file", "grep", "glob", "web_search"}), # default
min_group_size=2, # minimum consecutive pairs to collapse
)
📖 Development
git clone https://github.com/johanity/langchain-collapse.git
cd langchain-collapse
pip install -e ".[test]"
pytest
📕 License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file langchain_collapse-0.1.0.tar.gz.
File metadata
- Download URL: langchain_collapse-0.1.0.tar.gz
- Upload date:
- Size: 10.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
82770b67fcae1e3d0eaeafef3c67021b1dc8e8e4105ead47d64eb70d38a325de
|
|
| MD5 |
0bad617ef4227ac65c3e0dc705c5571a
|
|
| BLAKE2b-256 |
4d1c838aba66e504e3d2534c575b60f87918dea025a23e08cbf73bc2b4e7d68b
|
File details
Details for the file langchain_collapse-0.1.0-py3-none-any.whl.
File metadata
- Download URL: langchain_collapse-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a37b63299cc1c7714c8f2ff754054f0e77f90655bea90322601092e38db5a907
|
|
| MD5 |
8b1b293287628b991228f4c856a46184
|
|
| BLAKE2b-256 |
06aeadf57d6ad48ed03858c08d3e90df8c609e3c3630838fa86f61f8d00c75de
|