pub struct ContextBudget {
pub window_size: usize,
}Expand description
Context budget configuration and enforcement.
Fields§
§window_size: usizeMaximum tokens in the context window.
Implementations§
Source§impl ContextBudget
impl ContextBudget
Sourcepub fn estimate_tokens(
&self,
messages: &[Message],
tools: &[ToolDefinition],
) -> usize
pub fn estimate_tokens( &self, messages: &[Message], tools: &[ToolDefinition], ) -> usize
Estimate the token count of a set of messages and tool definitions.
Uses the chars/4 heuristic: each character is roughly 0.25 tokens. This is conservative (overestimates) which is safer than underestimating.
Sourcepub fn estimate_message_tokens(&self, messages: &[Message]) -> usize
pub fn estimate_message_tokens(&self, messages: &[Message]) -> usize
Estimate tokens for messages only (no tool definitions).
Sourcepub fn per_result_cap(&self) -> usize
pub fn per_result_cap(&self) -> usize
Maximum chars allowed per individual tool result.
Sourcepub fn single_result_max(&self) -> usize
pub fn single_result_max(&self) -> usize
Absolute maximum chars for a single tool result.
Sourcepub fn total_tool_headroom(&self) -> usize
pub fn total_tool_headroom(&self) -> usize
Total chars available for all tool results combined.
Sourcepub fn truncate_result(text: &str, max_chars: usize) -> String
pub fn truncate_result(text: &str, max_chars: usize) -> String
Truncate a tool result string to fit within max_chars.
If truncation occurs, appends a [truncated] marker.
Sourcepub fn apply_context_guard(&self, messages: &mut [Message]) -> bool
pub fn apply_context_guard(&self, messages: &mut [Message]) -> bool
Apply the context guard to messages: trim oldest tool results when total tool result content exceeds headroom.
Returns the (possibly modified) messages and whether trimming occurred.
Sourcepub fn check_trim_needed(
&self,
messages: &[Message],
tools: &[ToolDefinition],
) -> Option<TrimAction>
pub fn check_trim_needed( &self, messages: &[Message], tools: &[ToolDefinition], ) -> Option<TrimAction>
Determine the trim action needed based on current token estimate.
Returns None if no trimming needed, or the trim action to take.
Sourcepub fn apply_trim(&self, messages: &mut Vec<Message>, action: TrimAction)
pub fn apply_trim(&self, messages: &mut Vec<Message>, action: TrimAction)
Apply a trim action to messages. Returns the trimmed messages.
Preserves the first message (usually the user’s initial prompt) and system markers, then keeps the last N messages.
Trait Implementations§
Source§impl Clone for ContextBudget
impl Clone for ContextBudget
Source§fn clone(&self) -> ContextBudget
fn clone(&self) -> ContextBudget
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for ContextBudget
impl Debug for ContextBudget
Auto Trait Implementations§
impl Freeze for ContextBudget
impl RefUnwindSafe for ContextBudget
impl Send for ContextBudget
impl Sync for ContextBudget
impl Unpin for ContextBudget
impl UnsafeUnpin for ContextBudget
impl UnwindSafe for ContextBudget
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more