Expand description
Context compaction for long-running conversations.
This module provides automatic context compaction to allow conversations to continue without hitting context limits. When the message history grows too large, older messages are summarized using the LLM and replaced with a compact summary.
§Overview
The compaction system works as follows:
- Monitor message history size (by token count estimation)
- When approaching the threshold, partition messages into old and recent
- Summarize old messages using the LLM
- Replace history with summary + recent messages
§Example
ⓘ
use agent_sdk::{builder, context::CompactionConfig};
let agent = builder()
.provider(my_provider)
.with_compaction(CompactionConfig::default())
.build();§Configuration
Use CompactionConfig to customize compaction behavior:
threshold_tokens: When to trigger compactionretain_recent: How many recent messages to keep intactmin_messages_for_compaction: Minimum messages before considering compactionauto_compact: Whether to auto-compact or only on explicit request
Structs§
- Compaction
Config - Configuration for context compaction.
- LlmContext
Compactor - LLM-based context compactor.
- Token
Estimator - Estimates token count for messages.
Traits§
- Context
Compactor - Trait for context compaction strategies.