agent_sdk/context.rs
1//! Context compaction for long-running conversations.
2//!
3//! This module provides automatic context compaction to allow conversations
4//! to continue without hitting context limits. When the message history
5//! grows too large, older messages are summarized using the LLM and
6//! replaced with a compact summary.
7//!
8//! # Overview
9//!
10//! The compaction system works as follows:
11//! 1. Monitor message history size (by token count estimation)
12//! 2. When approaching the threshold, partition messages into old and recent
13//! 3. Summarize old messages using the LLM
14//! 4. Replace history with summary + recent messages
15//!
16//! # Example
17//!
18//! ```ignore
19//! use agent_sdk::{builder, context::CompactionConfig};
20//!
21//! let agent = builder()
22//! .provider(my_provider)
23//! .with_compaction(CompactionConfig::default())
24//! .build();
25//! ```
26//!
27//! # Configuration
28//!
29//! Use [`CompactionConfig`] to customize compaction behavior:
30//! - `threshold_tokens`: When to trigger compaction
31//! - `retain_recent`: How many recent messages to keep intact
32//! - `min_messages_for_compaction`: Minimum messages before considering compaction
33//! - `auto_compact`: Whether to auto-compact or only on explicit request
34
35mod compactor;
36mod config;
37mod estimator;
38
39pub use compactor::{ContextCompactor, LlmContextCompactor};
40pub use config::CompactionConfig;
41pub use estimator::TokenEstimator;