Compaction & Branch Summarization
LLMs have limited context windows. When conversations grow too long, Atomic uses compaction to summarize older content while preserving recent work. This page covers both auto-compaction and branch summarization. Source files (atomic):packages/coding-agent/src/core/compaction/compaction.ts- Auto-compaction logicpackages/coding-agent/src/core/compaction/branch-summarization.ts- Branch summarizationpackages/coding-agent/src/core/compaction/utils.ts- Shared utilities (file tracking, serialization)packages/coding-agent/src/core/session-manager.ts- Entry types (CompactionEntry,BranchSummaryEntry)packages/coding-agent/src/core/extensions/types.ts- Extension event types
node_modules/@bastani/atomic/dist/.
Overview
Atomic has two summarization mechanisms:| Mechanism | Trigger | Purpose |
|---|---|---|
| Compaction | Context exceeds threshold, or /compact | Summarize old messages to free up context |
| Branch summarization | /tree navigation | Preserve context when switching branches |
Compaction
When It Triggers
Auto-compaction triggers when:reserveTokens is 16384 tokens. Configure it in ~/.atomic/agent/settings.json or <project-dir>/.atomic/settings.json; legacy .pi paths are also supported. This leaves room for the LLM’s response.
You can also trigger manually with /compact [instructions], where optional instructions focus the summary.
How It Works
- Find cut point: Walk backwards from newest message, accumulating token estimates until
keepRecentTokensis reached. The default is 20k tokens; configure it in~/.atomic/agent/settings.jsonor<project-dir>/.atomic/settings.json. Legacy.pipaths are also supported. - Extract messages: Collect messages from the previous kept boundary (or session start) up to the cut point
- Generate summary: Call LLM to summarize with structured format, passing the previous summary as iterative context when present
- Append entry: Save
CompactionEntrywith summary andfirstKeptEntryId - Reload: Session reloads, using summary + messages from
firstKeptEntryIdonwards
firstKeptEntryId), not at the compaction entry itself, falling back to the entry after the previous compaction if that kept entry cannot be found in the path. This preserves messages that survived the earlier compaction by including them in the next summarization pass as well. Atomic also recalculates tokensBefore from the rebuilt session context before writing the new CompactionEntry, so the token count reflects the actual pre-compaction context being replaced.
Split Turns
A “turn” starts with a user message and includes all assistant responses and tool calls until the next user message. Normally, compaction cuts at turn boundaries. When a single turn exceedskeepRecentTokens, the cut point lands mid-turn at an assistant message. This is a “split turn”:
- History summary: Previous context (if any)
- Turn prefix summary: The early part of the split turn
Cut Point Rules
Valid cut points are:- User messages
- Assistant messages
- BashExecution messages
- Custom messages (custom_message, branch_summary)
CompactionEntry Structure
Defined insession-manager.ts:
details. The default compaction tracks file operations, but custom extension implementations can use their own structure.
See prepareCompaction() and compact() for the implementation.
Branch Summarization
When It Triggers
When you use/tree to navigate to a different branch, Atomic offers to summarize the work you’re leaving. This injects context from the left branch into the new branch.
How It Works
- Find common ancestor: Deepest node shared by old and new positions
- Collect entries: Walk from old leaf back to common ancestor
- Prepare with budget: Include messages up to token budget (newest first)
- Generate summary: Call LLM with structured format
- Append entry: Save
BranchSummaryEntryat navigation point
Cumulative File Tracking
Both compaction and branch summarization track files cumulatively. When generating a summary, Atomic extracts file operations from:- Tool calls in the messages being summarized
- Previous compaction or branch summary
details(if any)
BranchSummaryEntry Structure
Defined insession-manager.ts:
details.
See collectEntriesForBranchSummary(), prepareBranchEntries(), and generateBranchSummary() for the implementation.
Summary Format
Both compaction and branch summarization use the same structured format:Message Serialization
Before summarization, messages are serialized to text viaserializeConversation():
read and bash) are typically the largest contributors to context size.
Custom Summarization via Extensions
Extensions can intercept and customize both compaction and branch summarization. Seeextensions/types.ts for event type definitions.
session_before_compact
Fired before auto-compaction or/compact. Can cancel or provide custom summary. See SessionBeforeCompactEvent and CompactionPreparation in the types file.
Converting Messages to Text
To generate a summary with your own model, convert messages to text usingserializeConversation:
session_before_tree
Fired before/tree navigation. Always fires regardless of whether user chose to summarize. Can cancel navigation or provide custom summary.
SessionBeforeTreeEvent and TreePreparation in the types file.
Settings
Configure compaction in~/.atomic/agent/settings.json or <project-dir>/.atomic/settings.json (legacy .pi paths are also supported):
| Setting | Default | Description |
|---|---|---|
enabled | true | Enable auto-compaction |
reserveTokens | 16384 | Tokens to reserve for LLM response |
keepRecentTokens | 20000 | Recent tokens to keep (not summarized) |
"enabled": false. You can still compact manually with /compact.