pub struct CacheStats {
pub call_count: u64,
pub total_input_tokens: u64,
pub total_output_tokens: u64,
pub total_cache_creation_input_tokens: u64,
pub total_cache_read_input_tokens: u64,
pub total_cached_input_tokens: u64,
}Expand description
Aggregated prompt-cache statistics over a window of events.
Fields§
§call_count: u64Number of UsageRecorded events observed.
total_input_tokens: u64Sum of UsageRecorded.usage.input_tokens across the window.
total_output_tokens: u64Sum of UsageRecorded.usage.output_tokens across the window.
total_cache_creation_input_tokens: u64Sum of CacheMetrics::cache_creation_input_tokens.
Populated by Anthropic for input tokens that were written to a
cache entry during this run. None per-event is treated as 0.
total_cache_read_input_tokens: u64Sum of CacheMetrics::cache_read_input_tokens.
Populated by Anthropic and DeepSeek for input tokens served from an existing cache entry.
total_cached_input_tokens: u64Sum of CacheMetrics::cached_input_tokens.
Populated by OpenAI’s prompt_tokens_details.cached_tokens.
Implementations§
Source§impl CacheStats
impl CacheStats
Sourcepub fn from_events(events: &[AgentEvent]) -> Self
pub fn from_events(events: &[AgentEvent]) -> Self
Computes aggregate stats by walking a slice of AgentEvent
values in arrival order.
Events that are neither UsageRecorded nor CacheMetrics are
ignored. Order does not matter for accumulation but is preserved
for deterministic output in the returned Debug print.
Sourcepub fn from_envelopes(envelopes: &[RuntimeEventEnvelope]) -> Self
pub fn from_envelopes(envelopes: &[RuntimeEventEnvelope]) -> Self
Computes aggregate stats from a slice of RuntimeEventEnvelope
values (the shape returned by
RuntimeEventStore::list_after).
Sourcepub const fn total_cache_tokens(&self) -> u64
pub const fn total_cache_tokens(&self) -> u64
Returns the total cache-related input tokens across all three provider-specific fields.
Sourcepub fn cache_hit_rate(&self) -> f64
pub fn cache_hit_rate(&self) -> f64
Returns the fraction of input tokens that came from cache.
The denominator is input_tokens + cache_creation + cache_read + cached — the total input tokens actually processed by the
provider, whether fresh or served from cache. The numerator is
cache_read + cached (tokens that were served from a cache
entry instead of recomputed).
Returns 0.0 when the window contains no input tokens.
Sourcepub fn merge(self, other: Self) -> Self
pub fn merge(self, other: Self) -> Self
Combines two CacheStats by summing every field. Useful for
aggregating per-run stats into a session-level or
session-store-wide total.
Uses saturating_add to match Self::from_events and to remain
panic-free at the u64 boundary when aggregating very long runs.
Trait Implementations§
Source§impl Clone for CacheStats
impl Clone for CacheStats
Source§fn clone(&self) -> CacheStats
fn clone(&self) -> CacheStats
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for CacheStats
impl Debug for CacheStats
Source§impl Default for CacheStats
impl Default for CacheStats
Source§fn default() -> CacheStats
fn default() -> CacheStats
Source§impl<'de> Deserialize<'de> for CacheStats
impl<'de> Deserialize<'de> for CacheStats
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl Display for CacheStats
impl Display for CacheStats
impl Eq for CacheStats
Source§impl PartialEq for CacheStats
impl PartialEq for CacheStats
Source§fn eq(&self, other: &CacheStats) -> bool
fn eq(&self, other: &CacheStats) -> bool
self and other values to be equal, and is used by ==.Source§impl Serialize for CacheStats
impl Serialize for CacheStats
impl StructuralPartialEq for CacheStats
Auto Trait Implementations§
impl Freeze for CacheStats
impl RefUnwindSafe for CacheStats
impl Send for CacheStats
impl Sync for CacheStats
impl Unpin for CacheStats
impl UnsafeUnpin for CacheStats
impl UnwindSafe for CacheStats
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.