pub struct TokenTracker { /* private fields */ }Expand description
Tracks token usage from LLM API responses. Uses real usage data from API, not estimation.
Cumulative totals are stored for the dimensions consumers care about today
(input/output, cache read/creation, reasoning). The last_usage field
preserves the full TokenUsage from the most recent API call, so audio /
video / prediction dimensions are still accessible without growing
dedicated accumulators until a consumer asks for them.
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 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 last_usage(&self) -> &TokenUsage
pub fn last_usage(&self) -> &TokenUsage
Get the full TokenUsage from the most recent API call. Returns the
default (all zeros / None) before any call has been recorded.
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_cache_read_tokens(&self) -> u64
pub fn total_cache_read_tokens(&self) -> u64
Get total cache-read tokens across all calls
Sourcepub fn total_cache_creation_tokens(&self) -> u64
pub fn total_cache_creation_tokens(&self) -> u64
Get total cache-creation tokens across all calls
Sourcepub fn total_reasoning_tokens(&self) -> u64
pub fn total_reasoning_tokens(&self) -> u64
Get total reasoning tokens across all calls
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 last_usage 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