pub struct ContextManager {
pub max_tokens: usize,
pub reserve_ratio: f32,
pub max_output_lines: usize,
pub max_output_bytes: usize,
pub compression_token_threshold: usize,
pub compression_enabled: bool,
}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%).
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.
Implementations§
Source§impl ContextManager
impl ContextManager
pub fn new(context_window: Option<u64>, config: Option<&ContextConfig>) -> Self
Sourcepub fn available_tokens(&self) -> usize
pub fn available_tokens(&self) -> usize
Maximum tokens available for input (total - reserved for response).
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: remove oldest non-system messages by “rounds” (user + assistant + tool_calls + tool_results grouped together).
Auto Trait Implementations§
impl Freeze for ContextManager
impl RefUnwindSafe for ContextManager
impl Send for ContextManager
impl Sync for ContextManager
impl Unpin for ContextManager
impl UnsafeUnpin for ContextManager
impl UnwindSafe for ContextManager
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more