aither_agent/
cache_stats.rs1use aither_core::llm::Usage;
18
19#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
26pub struct CacheStats {
27 requests: u64,
29 prompt_tokens: u64,
31 cache_read_tokens: u64,
33 cache_write_tokens: u64,
35}
36
37impl CacheStats {
38 #[must_use]
40 pub const fn new() -> Self {
41 Self {
42 requests: 0,
43 prompt_tokens: 0,
44 cache_read_tokens: 0,
45 cache_write_tokens: 0,
46 }
47 }
48
49 pub fn record(&mut self, usage: &Usage) {
57 self.requests = self.requests.saturating_add(1);
58 if let Some(prompt) = usage.prompt_tokens {
59 self.prompt_tokens = self.prompt_tokens.saturating_add(u64::from(prompt));
60 }
61 if let Some(read) = usage.cache_read_tokens {
62 self.cache_read_tokens = self.cache_read_tokens.saturating_add(u64::from(read));
63 }
64 if let Some(write) = usage.cache_write_tokens {
65 self.cache_write_tokens = self.cache_write_tokens.saturating_add(u64::from(write));
66 }
67 }
68
69 pub const fn reset(&mut self) {
71 *self = Self::new();
72 }
73
74 #[must_use]
76 pub const fn requests(&self) -> u64 {
77 self.requests
78 }
79
80 #[must_use]
82 pub const fn prompt_tokens(&self) -> u64 {
83 self.prompt_tokens
84 }
85
86 #[must_use]
88 pub const fn cache_read_tokens(&self) -> u64 {
89 self.cache_read_tokens
90 }
91
92 #[must_use]
94 pub const fn cache_write_tokens(&self) -> u64 {
95 self.cache_write_tokens
96 }
97
98 #[must_use]
105 pub fn hit_rate(&self) -> Option<f64> {
106 if self.prompt_tokens == 0 {
107 return None;
108 }
109 #[allow(clippy::cast_precision_loss)]
110 let rate = self.cache_read_tokens as f64 / self.prompt_tokens as f64;
111 Some(rate)
112 }
113
114 #[must_use]
119 pub const fn snapshot(&self) -> Self {
120 *self
121 }
122}
123
124#[cfg(test)]
125mod tests {
126 use super::*;
127
128 fn usage(prompt: u32, cache_read: u32, cache_write: u32) -> Usage {
129 Usage::new(prompt, 0).with_cache_tokens(cache_read, cache_write)
130 }
131
132 #[test]
133 fn empty_stats_have_no_hit_rate() {
134 let stats = CacheStats::new();
135 assert_eq!(stats.requests(), 0);
136 assert_eq!(stats.hit_rate(), None);
137 }
138
139 #[test]
140 fn record_aggregates_prompt_and_cache_tokens() {
141 let mut stats = CacheStats::new();
142 stats.record(&usage(1_000, 800, 200));
143 stats.record(&usage(500, 450, 50));
144
145 assert_eq!(stats.requests(), 2);
146 assert_eq!(stats.prompt_tokens(), 1_500);
147 assert_eq!(stats.cache_read_tokens(), 1_250);
148 assert_eq!(stats.cache_write_tokens(), 250);
149 assert!((stats.hit_rate().unwrap() - (1_250.0 / 1_500.0)).abs() < 1e-9);
150 }
151
152 #[test]
153 fn missing_fields_do_not_poison_totals() {
154 let mut stats = CacheStats::new();
155 let bare = Usage {
157 prompt_tokens: Some(400),
158 ..Usage::default()
159 };
160 stats.record(&bare);
161
162 assert_eq!(stats.requests(), 1);
163 assert_eq!(stats.prompt_tokens(), 400);
164 assert_eq!(stats.cache_read_tokens(), 0);
165 assert_eq!(stats.hit_rate(), Some(0.0));
166 }
167
168 #[test]
169 fn reset_clears_all_counters() {
170 let mut stats = CacheStats::new();
171 stats.record(&usage(100, 50, 50));
172 stats.reset();
173 assert_eq!(stats, CacheStats::new());
174 }
175
176 #[test]
177 fn snapshot_is_independent_of_the_live_accumulator() {
178 let mut stats = CacheStats::new();
179 stats.record(&usage(100, 50, 50));
180 let snap = stats.snapshot();
181 stats.record(&usage(100, 100, 0));
182 assert_eq!(snap.prompt_tokens(), 100);
183 assert_eq!(snap.cache_read_tokens(), 50);
184 assert_eq!(stats.prompt_tokens(), 200);
185 assert_eq!(stats.cache_read_tokens(), 150);
186 }
187}