pub struct ContextManager {
pub max_tokens: usize,
pub reserve_ratio: f32,
pub truncation_ratio: f32,
pub min_keep_rounds: usize,
pub token_safety_margin: f32,
pub max_output_lines: usize,
pub max_output_bytes: usize,
pub compression_token_threshold: usize,
pub compression_enabled: bool,
pub max_tool_calls_per_turn: usize,
}Expand description
Manages the context window, truncating history when approaching token limits.
Fields§
§max_tokens: usizeModel’s context window size in tokens.
reserve_ratio: f32Ratio of context window to reserve for LLM response (default 0.2 = 20%).
truncation_ratio: f32Fraction of max_tokens at which truncation triggers (default 0.7).
min_keep_rounds: usizeMinimum conversation rounds to keep after truncation (default 3).
token_safety_margin: f32Safety multiplier for token estimates (default 1.3).
max_output_lines: usizeMax output lines for tool results.
max_output_bytes: usizeMax output bytes for tool results.
compression_token_threshold: usizeToken threshold for triggering compression.
compression_enabled: boolWhether compression is enabled.
max_tool_calls_per_turn: usizeMaximum tool calls per turn (default 30).
Implementations§
Source§impl ContextManager
impl ContextManager
pub fn new(context_window: Option<u64>, config: Option<&ContextConfig>) -> Self
Sourcepub fn truncation_threshold(&self) -> usize
pub fn truncation_threshold(&self) -> usize
Maximum tokens at which truncation is triggered.
Uses truncation_ratio (default 0.7) to trigger earlier than the
absolute limit, leaving headroom for estimation errors and LLM response.
Sourcepub fn available_tokens(&self) -> usize
pub fn available_tokens(&self) -> usize
Maximum tokens available for input (total - reserved for response).
Note: this is the absolute upper bound; truncation actually triggers
earlier via truncation_threshold().
Sourcepub fn truncate_tool_output(&self, content: &str) -> String
pub fn truncate_tool_output(&self, content: &str) -> String
Truncate tool output using the configured limits.
Sourcepub fn maybe_truncate(
&self,
messages: &mut Vec<ChatCompletionRequestMessage>,
) -> TruncationResult
pub fn maybe_truncate( &self, messages: &mut Vec<ChatCompletionRequestMessage>, ) -> TruncationResult
Check if history needs truncation and perform it if necessary.
Returns TruncationResult with removed messages for async compression.
Strategy:
- Uses
truncation_threshold()(default 70% of max_tokens) as trigger point - Removes oldest non-system rounds first
- Always keeps at least
min_keep_roundsrecent rounds - Applies
token_safety_marginto estimates to avoid underestimation