pub struct CacheStats {
pub input_tokens: u64,
pub output_tokens: u64,
pub cache_read_tokens: u64,
pub cache_creation_tokens: u64,
}Expand description
Statistics about cache usage from an API response.
Anthropic returns cache statistics in the usage field of responses:
cache_read_input_tokens: Tokens served from cache (90% cheaper)cache_creation_input_tokens: Tokens used to create the cache
§Cost Model
- Normal input tokens: $0.003 per 1K tokens
- Cached input tokens: $0.0003 per 1K tokens (90% discount)
- Cache creation has a small overhead but pays off after 2-3 uses
§Example
use edgequake_llm::cache_prompt::CacheStats;
let stats = CacheStats {
input_tokens: 10000,
output_tokens: 1000,
cache_read_tokens: 8000,
cache_creation_tokens: 0,
};
println!("Cache hit rate: {:.0}%", stats.cache_hit_rate() * 100.0);
println!("Estimated savings: ${:.4}", stats.savings());Fields§
§input_tokens: u64Total input tokens in the request.
output_tokens: u64Total output tokens in the response.
cache_read_tokens: u64Input tokens served from cache.
cache_creation_tokens: u64Tokens used to create new cache entries.
Implementations§
Source§impl CacheStats
impl CacheStats
Sourcepub fn new(
input_tokens: u64,
output_tokens: u64,
cache_read_tokens: u64,
cache_creation_tokens: u64,
) -> Self
pub fn new( input_tokens: u64, output_tokens: u64, cache_read_tokens: u64, cache_creation_tokens: u64, ) -> Self
Create new cache stats.
Sourcepub fn cache_hit_rate(&self) -> f64
pub fn cache_hit_rate(&self) -> f64
Calculate the cache hit rate as a fraction (0.0 to 1.0).
Returns 0.0 if there are no input tokens.
Sourcepub fn savings(&self) -> f64
pub fn savings(&self) -> f64
Estimate cost savings in dollars.
Based on Anthropic Claude pricing (as of 2024):
- Normal input: $0.003 per 1K tokens
- Cached input: $0.0003 per 1K tokens
Returns the difference between what would have been paid without caching vs with caching.
Sourcepub fn cost_per_call(&self) -> f64
pub fn cost_per_call(&self) -> f64
Calculate the cost per call with current cache stats.
Sourcepub fn is_effective(&self) -> bool
pub fn is_effective(&self) -> bool
Check if caching was effective (hit rate > 50%).
Sourcepub fn merge(&mut self, other: &CacheStats)
pub fn merge(&mut self, other: &CacheStats)
Merge stats from another request.
Trait Implementations§
Source§impl Clone for CacheStats
impl Clone for CacheStats
Source§fn clone(&self) -> CacheStats
fn clone(&self) -> CacheStats
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
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
Returns the “default value” for a type. Read more
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>,
Deserialize this value from the given Serde deserializer. Read more
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
Mutably borrows from an owned value. Read more