Message collapse (experimental)
Experimental
Message collapse is experimental. It depends on
langchain-collapse, which is a young library and may change.
Install the optional extra: uv pip install "abt-core[collapse]".
Enable it for tool-heavy agents that read many files or search repeatedly; skip it for short tasks.
The one collapse implementation
collapse is abt's only message-collapse step — Context compaction does not
collapse tool traces itself. Declare both to get collapse plus summarisation.
What it does
Declare a - collapse: entry in the middleware: list to add CollapseMiddleware. If you also
declare compaction, abt runs collapse first whatever order you list them in. Before each model call the middleware scans the message
history for consecutive groups of single-tool-call AIMessage/ToolMessage pairs where the tool name
is in the configured set (default: read_file, grep, glob, web_search). All but the most recent
pair in each group are replaced with a short HumanMessage placeholder badge tagged
lc_source: "collapse".
No LLM call. No hallucination risk. Stateless.
Tool names in collapse_tools may use abt's <server>__<tool> spec syntax (github__get_file_contents)
or bare names (get_file_contents) — abt strips the server prefix automatically. This is necessary
because MCP tools are stored in message history under their bare name; the <server>__ prefix is
abt's selection-time convention and never reaches the LangChain message history.
A name spelled differently from the bound tool (github__getFileContents for get_file_contents) resolves to it, as in tools:. A <server>__ entry that matches no bound tool logs a warning and never applies.
This complements context compaction: collapse trims repetitive tool noise before compaction evaluates whether summarisation is needed, so long tool-heavy sessions hit the compaction trigger later.
Enable globally (abt.yaml)
config:
middleware:
- collapse: {}
Defaults are tuned for general use (min_group_size: 2, library default tool set). Override as needed:
config:
middleware:
- collapse:
min_group_size: 3 # require at least 3 consecutive pairs before collapsing (default: 2)
collapse_tools: # tools eligible for collapsing (default: read_file, grep, glob, web_search)
- read_file
- grep
- glob
- web_search
- github__get_file_contents # MCP tools use <server>__<tool> naming
- github__search_code
MCP tools (GitHub, Jira, Confluence, etc.)
The default allowlist — read_file, grep, glob, web_search — covers local filesystem and web tools only.
MCP-provided tools are not included by default because their names are server-defined and vary per
integration. If your agents call MCP tools repeatedly (e.g. reading many GitHub files or listing many
Jira issues), you must list those tool names explicitly.
You must configure collapse_tools: for MCP calls to be collapsed
Without an explicit collapse_tools: list, CollapseMiddleware silently skips all MCP tool calls —
even if the agent makes dozens of them.
Use the <server>__<tool> naming convention that abt uses throughout (e.g. github__get_file_contents).
Find the exact names in your MCP server's tool descriptors or in the debug log (abt run --debug).
Common safe candidates for read-only MCP tools:
config:
middleware:
- collapse:
collapse_tools:
# GitHub MCP — read-only tools only; never add write tools (github__push_files, github__create_pull_request, etc.)
- github__get_file_contents
- github__search_code
- github__search_repositories
- github__search_issues
- github__search_pull_requests
- github__list_commits
- github__list_branches
- github__list_pull_requests
- github__list_issues
- github__list_tags
- github__list_releases
- github__issue_read
- github__pull_request_read
- github__get_commit
Never collapse mutating tools
Only add idempotent read tools to collapse_tools:. Collapsing write operations (github__push_files,
github__create_pull_request, github__merge_pull_request, github__add_issue_comment, etc.)
discards earlier results from the model's view, which is unsafe. When in doubt, leave the tool
out of the list.
The collapse_tools: list replaces the default set, so read_file / grep / glob /
web_search are included only if you list them.
Per-agent overrides work the same way: override the collapse entry in the agent's middleware: list
with an agent-specific allowlist.
Tool-name collisions across MCP servers can't reach collapse at all
The <server>__ prefix is abt's selection-time convention — by the time a tool call appears in
the LangChain message history it only carries the bare tool name. Two of an agent's tools
sharing one bare name (e.g. two MCP servers both exposing search_code) fail abt compile,
whether or not collapse is enabled. source__* specs are only expanded under
abt compile --live; otherwise such a collision fails when the agent is built (see MCPs).
Per-agent overrides
Override the project's collapse middleware entry by name. In the agent's frontmatter, add:
middleware:
- collapse:
min_group_size: 3
To opt a single agent out of a project-wide collapse entry, declare collapse with
enabled: false in the agent's own middleware: list. An agent middleware: list merges onto
the project stack — it does not replace it — so leaving collapse out of it changes nothing (see the
Middleware guide for the cascade rules). In the agent's frontmatter, add:
middleware:
- collapse:
enabled: false
Precedence follows the middleware cascade: agent middleware: > abt.yaml scoped tiers (agents.<name>.config) > config.middleware.
Middleware ordering
abt runs collapse before compaction, loop_detection, token_budget, and rubric, whatever
order you declare them in. For example:
config:
middleware:
- collapse: {}
- compaction: {}
When declaring both collapse and compaction entries, collapse trims tool traces and compaction
summarizes, truncates tool-call arguments and can clear stale tool results.
Choosing one vs both
| Scenario | Enable |
|---|---|
| Many repetitive read/search calls, session stays within the context window | - collapse: only — free (no LLM), stateless |
| Long session, context window may fill | - compaction: only — summarisation, argument truncation, optional clearing of stale tool results |
| Tool-heavy run that also grows long | Both — collapse trims first, compaction manages the window |
# Collapse only
config:
middleware:
- collapse: {}
# Compaction only
config:
middleware:
- compaction: {}
# Both — recommended for tool-heavy long runs; requires abt-core[collapse] (compaction is bundled)
config:
middleware:
- collapse: {}
- compaction: {}
Logs
- The badge inserted for each collapsed group is:
[N tool results omitted (most recent result preserved below)].
Further reading
- Upstream design and API: langchain-collapse README.
- Related: Context compaction (experimental).