pub struct TokenTracker { /* private fields */ }Expand description
One agent’s context window, tracked from real LLM API usage rather than
estimation. Owns the ContextUsage snapshot the agent publishes and
evaluates the compaction policy against it.
Implementations§
Source§impl TokenTracker
impl TokenTracker
pub fn new(context_limit: Option<u32>) -> Self
Sourcepub fn record_usage(&mut self, sample: TokenUsage)
pub fn record_usage(&mut self, sample: TokenUsage)
Record usage from an LLM API response.
Sourcepub fn snapshot(&self) -> &ContextUsage
pub fn snapshot(&self) -> &ContextUsage
The current window as published on ContextEvent::UsageUpdated.
Sourcepub fn usage_ratio(&self) -> Option<f64>
pub fn usage_ratio(&self) -> Option<f64>
Current context usage as a ratio (0.0 - 1.0)
Sourcepub fn exceeds_threshold(&self, threshold: f64) -> bool
pub fn exceeds_threshold(&self, threshold: f64) -> bool
Whether current usage exceeds the given threshold
Sourcepub fn needs_compaction(&self, estimated_tokens: u32, threshold: f64) -> bool
pub fn needs_compaction(&self, estimated_tokens: u32, threshold: f64) -> bool
Whether the context needs compaction
Sourcepub fn tokens_remaining(&self) -> Option<Tokens>
pub fn tokens_remaining(&self) -> Option<Tokens>
Tokens remaining before hitting limit
Sourcepub fn set_context_limit(&mut self, limit: Option<u32>)
pub fn set_context_limit(&mut self, limit: Option<u32>)
Update the context limit (e.g. when switching models)
Sourcepub fn reset_current_usage(&mut self)
pub fn reset_current_usage(&mut self)
Forget the last call after context compaction so it cannot immediately re-trigger compaction.
Trait Implementations§
Source§impl Clone for TokenTracker
impl Clone for TokenTracker
Source§fn clone(&self) -> TokenTracker
fn clone(&self) -> TokenTracker
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for TokenTracker
impl Debug for TokenTracker
Source§impl Default for TokenTracker
impl Default for TokenTracker
Source§fn default() -> TokenTracker
fn default() -> TokenTracker
Returns the “default value” for a type. Read more
Auto Trait Implementations§
impl Freeze for TokenTracker
impl RefUnwindSafe for TokenTracker
impl Send for TokenTracker
impl Sync for TokenTracker
impl Unpin for TokenTracker
impl UnsafeUnpin for TokenTracker
impl UnwindSafe for TokenTracker
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
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> IntoMaybeUndefined<T> for T
impl<T> IntoMaybeUndefined<T> for T
Source§fn into_maybe_undefined(self) -> MaybeUndefined<T>
fn into_maybe_undefined(self) -> MaybeUndefined<T>
Converts this value into a three-state builder argument.
Source§impl<T> IntoOption<T> for T
impl<T> IntoOption<T> for T
Source§fn into_option(self) -> Option<T>
fn into_option(self) -> Option<T>
Converts this value into an optional builder argument.