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
Metadata
Release files for langchain-collapse 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| langchain_collapse-0.1.1.tar.gz | 11.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| langchain_collapse-0.1.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 17.8 kB
Release files / langchain_collapse-0.1.1.tar.gz
| Download URL | langchain_collapse-0.1.1.tar.gz |
|---|---|
| Size | 11.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1a6fbcb456ec83b6dbc1f2f68155f76188fb9ede4afa15d87052fd7016970c7b
|
|
BLAKE2b-256 checksum How to use checksums |
4bab0c03a1731d18e4235b600ba073545f05a9e6e3d6a3560632c3d445dc7812
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.3
|
Release files / langchain_collapse-0.1.1-py3-none-any.whl
| Download URL | langchain_collapse-0.1.1-py3-none-any.whl |
|---|---|
| Size | 6.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
fcd38c75f76eeea050acfa4c248548ae22bbff22853269bf831ebc4b580036e0
|
|
BLAKE2b-256 checksum How to use checksums |
f1b875a14826788ea80ace6e65b5e5d41c721a80275199767da5820ebfa70bf5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.3
|