pub struct CacheCost {
pub uncached_input: u64,
pub cache_reads: u64,
pub cache_writes: u64,
pub storage_token_hours: f64,
}Expand description
A request’s or a run’s input tokens by how they are billed, plus the
token-hours its cache resources were stored. Sum the costs of a run’s
calls (and of any cache resources beside them) and price the total with
CacheCost::usd.
A run priced the same way on any provider, with the caller’s rates from the provider’s price list:
use rig_core::completion::{CacheCost, CacheRates, Usage};
use rig_core::providers::gemini::{AutoCache, CacheBook};
let run: CacheCost = calls.iter().map(CacheCost::from_usage).sum();
let opus = CacheRates { input: 4.0, cached_read: 0.2, cache_write: 5.0, storage_per_hour: 0.0 };
println!("${:.3}, {:.1}% saved", run.usd(&opus), run.saving(&opus) * 100.0);
// Gemini: the calls, plus the cache book's creation and storage.
let gemini = run + CacheCost::from(&book.report());
let flash = CacheRates { input: 0.75, cached_read: 0.075, cache_write: 0.75, storage_per_hour: 0.5 };
println!("${:.3}", gemini.usd(&flash));Fields§
§uncached_input: u64Input billed at the uncached price.
cache_reads: u64Input read from a cache.
cache_writes: u64Input written to a cache.
storage_token_hours: f64Σ tokens × hours of stored cache resources.
Implementations§
Source§impl CacheCost
impl CacheCost
Sourcepub fn from_usage(usage: &Usage) -> Self
pub fn from_usage(usage: &Usage) -> Self
The billed input of one call: the input neither read from nor written
to a cache is input_tokens - cached_input_tokens - cache_creation_input_tokens. An unreported counter counts as zero;
reads and writes above input_tokens leave no uncached input rather
than a negative one.
Sourcepub fn prompt_tokens(&self) -> u64
pub fn prompt_tokens(&self) -> u64
Every input token, however it was billed.
Sourcepub fn usd(&self, rates: &CacheRates) -> f64
pub fn usd(&self, rates: &CacheRates) -> f64
Input cost in USD at rates: uncached, read, written and stored.
Sourcepub fn uncached_usd(&self, rates: &CacheRates) -> f64
pub fn uncached_usd(&self, rates: &CacheRates) -> f64
Input cost in USD at rates had every prompt token been uncached.
Sourcepub fn saving(&self, rates: &CacheRates) -> f64
pub fn saving(&self, rates: &CacheRates) -> f64
The share of the uncached input cost caching saved: 1 - usd / uncached_usd. Negative when caching cost more; zero without input.
Trait Implementations§
Source§impl AddAssign for CacheCost
impl AddAssign for CacheCost
Source§fn add_assign(&mut self, other: Self)
fn add_assign(&mut self, other: Self)
+= operation. Read moreimpl Copy for CacheCost
Source§impl<'de> Deserialize<'de> for CacheCost
impl<'de> Deserialize<'de> for CacheCost
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 From<&CacheReport> for CacheCost
The book’s caches as billed beside the calls: every created token as a
cache write (priced at the input price) and token_hours of storage. Add
it to the calls’ CacheCost::from_usage to price a run.
impl From<&CacheReport> for CacheCost
The book’s caches as billed beside the calls: every created token as a
cache write (priced at the input price) and token_hours of storage. Add
it to the calls’ CacheCost::from_usage to price a run.
Source§fn from(report: &CacheReport) -> Self
fn from(report: &CacheReport) -> Self
impl StructuralPartialEq for CacheCost
Auto Trait Implementations§
impl Freeze for CacheCost
impl RefUnwindSafe for CacheCost
impl Send for CacheCost
impl Sync for CacheCost
impl Unpin for CacheCost
impl UnsafeUnpin for CacheCost
impl UnwindSafe for CacheCost
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<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more