Skip to main content

Module cache_stats

Module cache_stats 

Source
Expand description

Aggregated prompt cache statistics for a run or a window of events.

CacheStats reduces a sequence of AgentEvent values (typically replayed from a RuntimeEventStore) into the totals needed to evaluate cache effectiveness:

  • Total input / output tokens consumed
  • Total cache writes (Anthropic cache_creation_input_tokens)
  • Total cache reads (Anthropic / DeepSeek cache_read_input_tokens)
  • Total cached input (OpenAI prompt_tokens_details.cached_tokens)
  • Model call count

Use CacheStats::cache_hit_rate to get a normalized 0.0..=1.0 score describing what fraction of input tokens came from cache.

§Example

use behest_runtime::cache_stats::CacheStats;
use behest_runtime::event::AgentEvent;
use behest_runtime::event_store::{RuntimeEventStore, MemoryRuntimeEventStore};
use behest_runtime::run::RunId;
let store = MemoryRuntimeEventStore::new();
// ... append events to `store` for a given run ...
let run_id = behest_runtime::run::RunId::new();
let envelopes = store.list_after(run_id, None, 1024).await?;
let stats = CacheStats::from_envelopes(&envelopes);
println!("hit rate: {:.1}%", stats.cache_hit_rate() * 100.0);

Structs§

CacheStats
Aggregated prompt-cache statistics over a window of events.