pub struct CompactionConfig {
pub threshold_tokens: usize,
pub retain_recent: usize,
pub min_messages_for_compaction: usize,
pub auto_compact: bool,
pub max_retained_tail_tokens: usize,
pub summary_max_tokens: usize,
}Expand description
Configuration for context compaction.
Controls when and how context compaction occurs.
§Example
use agent_sdk::context::CompactionConfig;
let config = CompactionConfig::default()
.with_threshold_tokens(100_000)
.with_retain_recent(20);Fields§
§threshold_tokens: usizeToken threshold to trigger compaction. When estimated tokens exceed this, compaction is triggered. Default: 80,000 (conservative for 128K context models)
retain_recent: usizeNumber of recent messages to keep intact (not summarized). These messages remain in full to preserve immediate context.
The retained tail is additionally bounded by
max_retained_tail_tokens: if keeping
retain_recent messages would exceed that token budget, the oldest of
those messages are summarized instead so the tail stays within the cap.
Default: 10
min_messages_for_compaction: usizeMinimum messages before compaction is considered. Prevents compaction when conversation is still short. Default: 20
auto_compact: boolWhether to automatically compact when threshold is reached. If false, compaction only occurs on explicit request. Default: true
max_retained_tail_tokens: usizeSoft cap on the estimated tokens kept in the retained tail.
Bounds retain_recent: the most recent messages
are kept only until this token budget is reached, after which older
messages are folded into the summary instead. The cap is soft because an
indivisible tool_use/tool_result pair may push the tail slightly
over. Raise it on large-context models, or lower it for more aggressive
compaction.
Default: 20,000
summary_max_tokens: usizemax_tokens budget for the LLM summarization call.
If the summarizer hits this ceiling (a MaxTokens stop reason), the
compactor logs a warning and retries once with a larger budget before
marking the summary as truncated.
Default: 4,096
Implementations§
Source§impl CompactionConfig
impl CompactionConfig
Sourcepub const fn with_threshold_tokens(self, threshold: usize) -> Self
pub const fn with_threshold_tokens(self, threshold: usize) -> Self
Set the token threshold for compaction.
Sourcepub const fn with_retain_recent(self, count: usize) -> Self
pub const fn with_retain_recent(self, count: usize) -> Self
Set the number of recent messages to retain.
Sourcepub const fn with_min_messages(self, count: usize) -> Self
pub const fn with_min_messages(self, count: usize) -> Self
Set the minimum messages for compaction.
Sourcepub const fn with_auto_compact(self, auto: bool) -> Self
pub const fn with_auto_compact(self, auto: bool) -> Self
Set whether to auto-compact.
Sourcepub const fn with_max_retained_tail_tokens(self, tokens: usize) -> Self
pub const fn with_max_retained_tail_tokens(self, tokens: usize) -> Self
Set the soft cap on tokens kept in the retained tail.
Bounds retain_recent. See the field docs for
the precise semantics.
Sourcepub const fn with_summary_max_tokens(self, tokens: usize) -> Self
pub const fn with_summary_max_tokens(self, tokens: usize) -> Self
Set the max_tokens budget for the summarization call.
Trait Implementations§
Source§impl Clone for CompactionConfig
impl Clone for CompactionConfig
Source§fn clone(&self) -> CompactionConfig
fn clone(&self) -> CompactionConfig
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more