use aither_core::llm::Usage;
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct CacheStats {
requests: u64,
prompt_tokens: u64,
cache_read_tokens: u64,
cache_write_tokens: u64,
}
impl CacheStats {
#[must_use]
pub const fn new() -> Self {
Self {
requests: 0,
prompt_tokens: 0,
cache_read_tokens: 0,
cache_write_tokens: 0,
}
}
pub fn record(&mut self, usage: &Usage) {
self.requests = self.requests.saturating_add(1);
if let Some(prompt) = usage.prompt_tokens {
self.prompt_tokens = self.prompt_tokens.saturating_add(u64::from(prompt));
}
if let Some(read) = usage.cache_read_tokens {
self.cache_read_tokens = self.cache_read_tokens.saturating_add(u64::from(read));
}
if let Some(write) = usage.cache_write_tokens {
self.cache_write_tokens = self.cache_write_tokens.saturating_add(u64::from(write));
}
}
pub const fn reset(&mut self) {
*self = Self::new();
}
#[must_use]
pub const fn requests(&self) -> u64 {
self.requests
}
#[must_use]
pub const fn prompt_tokens(&self) -> u64 {
self.prompt_tokens
}
#[must_use]
pub const fn cache_read_tokens(&self) -> u64 {
self.cache_read_tokens
}
#[must_use]
pub const fn cache_write_tokens(&self) -> u64 {
self.cache_write_tokens
}
#[must_use]
pub fn hit_rate(&self) -> Option<f64> {
if self.prompt_tokens == 0 {
return None;
}
#[allow(clippy::cast_precision_loss)]
let rate = self.cache_read_tokens as f64 / self.prompt_tokens as f64;
Some(rate)
}
#[must_use]
pub const fn snapshot(&self) -> Self {
*self
}
}
#[cfg(test)]
mod tests {
use super::*;
fn usage(prompt: u32, cache_read: u32, cache_write: u32) -> Usage {
Usage::new(prompt, 0).with_cache_tokens(cache_read, cache_write)
}
#[test]
fn empty_stats_have_no_hit_rate() {
let stats = CacheStats::new();
assert_eq!(stats.requests(), 0);
assert_eq!(stats.hit_rate(), None);
}
#[test]
fn record_aggregates_prompt_and_cache_tokens() {
let mut stats = CacheStats::new();
stats.record(&usage(1_000, 800, 200));
stats.record(&usage(500, 450, 50));
assert_eq!(stats.requests(), 2);
assert_eq!(stats.prompt_tokens(), 1_500);
assert_eq!(stats.cache_read_tokens(), 1_250);
assert_eq!(stats.cache_write_tokens(), 250);
assert!((stats.hit_rate().unwrap() - (1_250.0 / 1_500.0)).abs() < 1e-9);
}
#[test]
fn missing_fields_do_not_poison_totals() {
let mut stats = CacheStats::new();
let bare = Usage {
prompt_tokens: Some(400),
..Usage::default()
};
stats.record(&bare);
assert_eq!(stats.requests(), 1);
assert_eq!(stats.prompt_tokens(), 400);
assert_eq!(stats.cache_read_tokens(), 0);
assert_eq!(stats.hit_rate(), Some(0.0));
}
#[test]
fn reset_clears_all_counters() {
let mut stats = CacheStats::new();
stats.record(&usage(100, 50, 50));
stats.reset();
assert_eq!(stats, CacheStats::new());
}
#[test]
fn snapshot_is_independent_of_the_live_accumulator() {
let mut stats = CacheStats::new();
stats.record(&usage(100, 50, 50));
let snap = stats.snapshot();
stats.record(&usage(100, 100, 0));
assert_eq!(snap.prompt_tokens(), 100);
assert_eq!(snap.cache_read_tokens(), 50);
assert_eq!(stats.prompt_tokens(), 200);
assert_eq!(stats.cache_read_tokens(), 150);
}
}