1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
//! Rolling KV-cache hit-rate statistics for a running agent.
//!
//! Models like Claude expose `cache_read_tokens` and `cache_write_tokens`
//! in their [`aither_core::llm::Usage`] payloads. Prompt caching is
//! dramatically cheaper than uncached input (Anthropic pricing: ~0.30 vs
//! 3.00 USD/MTok, a 10× difference), so a production-grade agent host
//! should track the cumulative hit rate to catch regressions where a
//! stable system prefix is accidentally invalidated between requests.
//!
//! [`CacheStats`] is a pure accumulator: hosts call
//! [`CacheStats::record`] with each observed `Usage` (typically when
//! handling an [`AgentEvent::Usage`](crate::AgentEvent::Usage) event)
//! and read the current rolling metrics back via the getters. The
//! accumulator stores raw totals so hosts can trivially diff two
//! snapshots if they want per-turn ratios.
use aither_core::llm::Usage;
/// Aggregate prompt-caching statistics collected across one agent session.
///
/// The counters are cumulative totals, not per-turn deltas. Hosts wanting
/// per-turn metrics should take snapshots via [`CacheStats::snapshot`]
/// and diff two snapshots; hosts wanting a single rolling rate can read
/// [`CacheStats::hit_rate`] directly.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct CacheStats {
/// Number of usage payloads recorded so far.
requests: u64,
/// Cumulative prompt tokens billed (both cached and fresh).
prompt_tokens: u64,
/// Cumulative prompt tokens that were served from the KV cache.
cache_read_tokens: u64,
/// Cumulative prompt tokens newly written to the KV cache.
cache_write_tokens: u64,
}
impl CacheStats {
/// Creates an empty accumulator.
#[must_use]
pub const fn new() -> Self {
Self {
requests: 0,
prompt_tokens: 0,
cache_read_tokens: 0,
cache_write_tokens: 0,
}
}
/// Records a single `Usage` observation.
///
/// Missing fields on `usage` are treated as zero so providers that
/// do not report cache metrics simply contribute nothing to the
/// cache numerator. The request counter increments on every call
/// regardless of field availability, so [`Self::requests`] is a
/// faithful count of observed usage payloads.
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));
}
}
/// Resets all counters.
pub const fn reset(&mut self) {
*self = Self::new();
}
/// Returns the number of usage payloads recorded.
#[must_use]
pub const fn requests(&self) -> u64 {
self.requests
}
/// Returns cumulative prompt tokens (cached + fresh).
#[must_use]
pub const fn prompt_tokens(&self) -> u64 {
self.prompt_tokens
}
/// Returns cumulative tokens served from the KV cache.
#[must_use]
pub const fn cache_read_tokens(&self) -> u64 {
self.cache_read_tokens
}
/// Returns cumulative tokens freshly written to the KV cache.
#[must_use]
pub const fn cache_write_tokens(&self) -> u64 {
self.cache_write_tokens
}
/// Returns the rolling cache-read share of prompt tokens.
///
/// Computed as `cache_read_tokens / prompt_tokens`. Returns `None`
/// when no prompt tokens have been observed yet — attempting to
/// answer "what's our hit rate?" before any request has run would
/// be misleading.
#[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)
}
/// Returns a copy of the current counters as a plain value.
///
/// Useful for comparing two moments in time without taking a
/// mutable reference to the live accumulator.
#[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();
// Provider that reports no cache fields at all.
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);
}
}