Collapse consecutive read/search tool calls to reduce agent context size
Project description
langchain-collapse
Preventive context management for LangChain agents.
When an agent reads ten files in a row, those twenty messages stay in context long after they've been processed. This middleware quietly collapses them into a single line, keeping only the most recent result. The expensive stuff (LLM summarization) triggers less often.
No LLM calls. No hallucination risk. Stateless.
Quick Install
pip install langchain-collapse
🤔 What is this?
Agents burn through context by accumulating tool results they've already processed. A typical file exploration phase (8 reads, 4 greps) can eat thousands of tokens that just sit there. CollapseMiddleware scans for these repetitive groups and replaces the older ones with a short note, keeping the last result visible to the model.
On a realistic coding session, this produces a 92% token reduction. When paired with SummarizationMiddleware, summarization triggers 4.2x later because context fills up more slowly.
from langchain.agents import create_agent
from langchain_collapse import CollapseMiddleware
agent = create_agent(
model="anthropic:claude-sonnet-4-6",
tools=[...],
middleware=[CollapseMiddleware()],
)
With SummarizationMiddleware
Place CollapseMiddleware first. It reduces the message count before summarization decides whether to fire:
from langchain.agents.middleware import SummarizationMiddleware
middleware = [
CollapseMiddleware(),
SummarizationMiddleware(
model="anthropic:claude-haiku-4-5-20251001",
trigger=("fraction", 0.85),
),
]
Configuration
CollapseMiddleware(
collapse_tools=frozenset({"read_file", "grep", "glob", "web_search"}), # default
min_group_size=2, # minimum consecutive pairs to collapse
)
📖 Documentation
- Source (single file, ~150 lines)
- Benchmark (realistic session with token counts)
- Tests (unit tests + property-based invariant tests)
💁 Contributing
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.1.tar.gz.
File metadata
- Download URL: langchain_collapse-0.1.1.tar.gz
- Upload date:
- Size: 11.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1a6fbcb456ec83b6dbc1f2f68155f76188fb9ede4afa15d87052fd7016970c7b
|
|
| MD5 |
16fa706d56c2050c66ec1814dbfabab9
|
|
| BLAKE2b-256 |
4bab0c03a1731d18e4235b600ba073545f05a9e6e3d6a3560632c3d445dc7812
|
File details
Details for the file langchain_collapse-0.1.1-py3-none-any.whl.
File metadata
- Download URL: langchain_collapse-0.1.1-py3-none-any.whl
- Upload date:
- Size: 6.0 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 |
fcd38c75f76eeea050acfa4c248548ae22bbff22853269bf831ebc4b580036e0
|
|
| MD5 |
2e7d8018df8fe36cdbc848ab4942f636
|
|
| BLAKE2b-256 |
f1b875a14826788ea80ace6e65b5e5d41c721a80275199767da5820ebfa70bf5
|