Skip to content

Context compaction (experimental)

Experimental

Context compaction is experimental. Enable it for long-running, tool-heavy agents where the context window is a risk; skip it for short tasks.

What it does

Declare a - compaction: entry in the middleware: list. It works on every agent, a delegating agent or a plain agent alike. Before each model call, the middleware can:

  1. Truncate large write_file / edit_file arguments in older turns.
  2. Summarize older conversation once usage reaches trigger. The originals are saved first (see Where summarized history goes).
  3. Clear tool results — replace old tool results with a placeholder once a token threshold is reached (no extra LLM call). Off by default.

It is built on the summarization and context-editing middleware that ship with Deep Agents and LangChain, which abt already depends on, so there is nothing extra to install.

This complements MCP response offloading (min_offload_chars under config: in abt.yaml): offloading shrinks individual huge tool payloads; compaction manages accumulated chat history.

Enable globally (abt.yaml)

config:
  middleware:
    - compaction: {}

Defaults: trigger at 170,000 accumulated tokens, keep the 20 most recent messages. Truncating tool-call arguments and clearing tool results are off until you set their trigger. Override keys as needed — see Declarative middleware — compaction.

A typical production setting:

config:
  middleware:
    - compaction:
        trigger: 100000
        keep_messages: 10
        truncate_args:
          trigger: 25000
          max_length: 2000

To also drop the content of stale tool results, enable clear_tool_results. It works on what the model is about to receive, after the summary has replaced older messages, so it keeps only the last keep_recent tool results that are still in that context. Name tools whose results must survive (for example memory lookups) in exclude_tools:

config:
  middleware:
    - compaction:
        trigger: 100000
        clear_tool_results:
          enabled: true
          trigger: 60000
          keep_recent: 5
          exclude_tools: [read_memory]

Token counting

trigger values are compared with a local tiktoken estimate of the prompt: message text, tool-call arguments and tool schemas. It is typically within about 15% below the provider's own count, so leave headroom under the model's context window rather than setting trigger at it. When the agent model's window is known, trigger is clamped to 90% of it.

Per-agent overrides

Override the project's compaction middleware entry by name. In the agent's frontmatter, add:

middleware:
  - compaction:
      trigger: 80000
      custom_instructions: "Preserve all file paths and SQL verbatim."

Precedence follows the middleware cascade: agent middleware: > abt.yaml scoped tier (agents.<name>.config) > config.middleware.

For how collapse and compaction relate, see Choosing collapse vs compaction and Message collapse. Compaction has no collapse step of its own: to trim repetitive read/search traces, declare the collapse middleware too; abt runs it before compaction.

On a delegating agent, declaring compaction also removes Deep Agents' built-in summarization middleware, so only one summarizer manages the history. Its general-purpose subagent gets its own compaction instance the same way.

Manual compaction

Set enable_manual_tool: true to give the agent a compact_conversation tool. It refuses to run until the history is about half of trigger, so the agent cannot compact too early.

Where summarized history goes

Older messages are written to /workspace/conversation_history/<session>.md in the run results directory before the summary replaces them. The summary names that file, so an agent with file tools can re-read the original with read_file when it needs the detail.

When summarizing fails

If the summarization call fails (throttling, an invalid model_id), the error is logged and the model call proceeds with the uncompacted messages. The next model call tries again, so a persistent failure is visible in the log on every call and the context keeps growing.

Tool calls and results stay together

The cutoff between summarized and kept messages never separates an assistant message's tool calls from the results that answer them, so a provider never receives an orphaned tool call.

Further reading