Compaction Internals Deep Dive
Deep analysis of OpenClaw's session history compaction system (src/agents/compaction.ts, 406 lines).
Core Constants
Key Functions
computeAdaptiveChunkRatio()
Dynamically adjusts chunk ratio based on average message size:
Behavior:
- Default: 40% of context per chunk
- Large messages (> 10% of context): ratio reduces toward 15%
- Prevents oversized chunks that could cause overflow
chunkMessagesByMaxTokens()
Splits messages into chunks respecting token limits:
Key Features:
- Applies
SAFETY_MARGINto compensate for token estimation inaccuracy - Splits oversized individual messages immediately
- Guarantees no chunk exceeds effective max
splitMessagesByTokenShare()
Distributes messages across N chunks targeting equal token share:
Strategy:
- Targets equal token distribution across chunks
- Last chunk absorbs remaining messages
- Used by
summarizeInStages()for parallel summarization
summarizeInStages()
Progressive summarization with multi-stage merging:
Decision Logic:
pruneHistoryForContextShare()
Budget-based pruning with tool_use/tool_result pairing repair:
Key Features:
- Allocates budget:
maxHistoryShare * maxContextTokens(default 50%) - Drops oldest chunk first
- Calls
repairToolUseResultPairing()after each drop to handle orphaned tool_results - Stops when budget satisfied or no more chunks to drop
Optimization Insights
Tuning Chunk Ratio for Different Use Cases
Large Context Models (e.g., Claude Opus 4.6 200k):
- Use higher
BASE_CHUNK_RATIO(0.5-0.6) - Fewer summarization rounds
- Better preservation of nuance
Smaller Context Models (e.g., 32k):
- Keep
BASE_CHUNK_RATIOat 0.4 or lower - More aggressive adaptive reduction
- Prioritize fitting within constraints
Token-Heavy Domains (code, logs):
- Lower
BASE_CHUNK_RATIO(0.3) - Increase
MIN_CHUNK_RATIOfloor (0.2) - Compensate for higher token density
Safety Margin Rationale
The SAFETY_MARGIN = 1.2 (20% buffer) compensates for:
- Char/4 heuristic missing multi-byte characters
- Special tokens (BOS, EOS, etc.)
- Code tokens (higher than average density)
- Formatting tokens (markdown, XML tags)