Compaction Module
The compaction module provides automatic context management for long conversations, including message summarization and intelligent pruning.Installation
Features
- Message summarization
- Context window management
- Token counting and limits
- Intelligent message pruning
Quick Start
Classes
ContextCompactor
Main class for compacting conversation context.Constructor
| Parameter | Type | Default | Description |
|---|---|---|---|
max_tokens | int | 8000 | Maximum context tokens |
strategy | str | "summarize" | Compaction strategy |
preserve_recent | int | 5 | Recent messages to preserve |
Methods
| Method | Description |
|---|---|
compact(messages) | Compact messages to fit budget |
should_compact(messages) | Check if compaction needed |
get_token_count(messages) | Count tokens in messages |
CompactionConfig
Configuration for compaction behavior.Attributes
| Attribute | Type | Description |
|---|---|---|
max_tokens | int | Maximum context tokens |
strategy | str | Compaction strategy |
preserve_system | bool | Keep system messages |
preserve_recent | int | Recent messages to keep |
CompactionStrategy
Available compaction strategies.CompactionResult
Result of a compaction operation.Attributes
| Attribute | Type | Description |
|---|---|---|
messages | list | Compacted messages |
original_tokens | int | Original token count |
compacted_tokens | int | Final token count |
summary | str | Summary of removed content |
Usage Examples
Basic Compaction
With Agent
Summarize Strategy
Sliding Window
Best Practices
- Set appropriate limits - Balance context vs. cost
- Preserve important messages - Keep system prompts and recent context
- Use summarization - Better than truncation for continuity
- Monitor token usage - Track compaction frequency

