pub struct TokenTracker { /* private fields */ }Expand description
Tracks token usage from LLM API responses. Uses real usage data from API, not estimation.
Implementations§
Source§impl TokenTracker
impl TokenTracker
pub fn new(context_limit: Option<u32>) -> Self
Sourcepub fn record_usage(
&mut self,
input_tokens: u32,
output_tokens: u32,
cached_input_tokens: Option<u32>,
)
pub fn record_usage( &mut self, input_tokens: u32, output_tokens: u32, cached_input_tokens: Option<u32>, )
Record usage from an LLM API response
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 should_compact(&self, threshold: f64) -> bool
pub fn should_compact(&self, threshold: f64) -> bool
Check if context should be compacted based on the given threshold. This is a convenience method that combines usage ratio check with a minimum context size requirement to avoid unnecessary compaction on small conversations.
Sourcepub fn tokens_remaining(&self) -> Option<u32>
pub fn tokens_remaining(&self) -> Option<u32>
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 context_limit(&self) -> Option<u32>
pub fn context_limit(&self) -> Option<u32>
Get the context limit
Sourcepub fn last_input_tokens(&self) -> u32
pub fn last_input_tokens(&self) -> u32
Get last recorded input tokens (current context size)
Sourcepub fn total_input_tokens(&self) -> u64
pub fn total_input_tokens(&self) -> u64
Get total input tokens across all calls
Sourcepub fn total_output_tokens(&self) -> u64
pub fn total_output_tokens(&self) -> u64
Get total output tokens across all calls
Sourcepub fn total_cached_input_tokens(&self) -> u64
pub fn total_cached_input_tokens(&self) -> u64
Get total cached input tokens across all calls
Sourcepub fn last_cached_input_tokens(&self) -> Option<u32>
pub fn last_cached_input_tokens(&self) -> Option<u32>
Get last recorded cached input tokens
Sourcepub fn reset_current_usage(&mut self)
pub fn reset_current_usage(&mut self)
Reset current usage tracking after context compaction.
Preserves cumulative totals for metrics while clearing the
last_input_tokens to prevent immediate re-triggering of compaction.
Trait Implementations§
Source§impl Clone for TokenTracker
impl Clone for TokenTracker
Source§fn clone(&self) -> TokenTracker
fn clone(&self) -> TokenTracker
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more