Skip to content

johanity/langchain-collapse

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

langchain-collapse

PyPI License CI

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.

Before and after

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

About

Preventive context management for LangChain agents. Collapses consecutive tool-call groups before they fill the context window.

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages