Skip to main content

Registry

Struct Registry 

Source
pub struct Registry {
    pub models: HashMap<String, ModelPricing>,
}

Fields§

§models: HashMap<String, ModelPricing>

Implementations§

Source§

impl Registry

Source

pub fn load_embedded() -> Result<Self>

Source

pub fn from_json_str(raw: &str) -> Result<Self>

Source

pub fn get(&self, model: &str) -> Option<&ModelPricing>

Source

pub fn estimate_cost( &self, model: &str, input_tokens: u32, output_tokens: u32, ) -> Option<f64>

Source

pub fn estimate_cost_with_cache( &self, model: &str, input_tokens: u32, output_tokens: u32, cache_read_tokens: u32, cache_creation_tokens: u32, ) -> Option<f64>

Estimates cost including prompt-cache read and cache-creation tokens.

Cache read tokens are priced at cache_read_cost_per_1k and cache creation tokens at cache_creation_cost_per_1k when the model publishes that pricing. When a model has no cache-specific pricing, cache tokens fall back to being billed at the model’s normal input rate (input_cost_per_1k) rather than being treated as free, since providers bill uncached-equivalent tokens at the input rate whenever they don’t publish a discounted cache rate.

Cache-creation tokens are priced at the base cache-write rate, which is the 5-minute TTL rate. Writes with a 1-hour TTL and long-context tier rates (for example *_above_200k_tokens prices) are not modeled, so the estimate is low for those requests.

§Token accounting contract

input_tokens, cache_read_tokens, and cache_creation_tokens must be disjoint counts that sum to the total prompt tokens billed — input_tokens must exclude any tokens already counted in cache_read_tokens or cache_creation_tokens.

Providers report prompt tokens in different shapes, and the crate’s Usage prompt_tokens keeps each provider’s shape as reported:

  • Anthropic: input_tokens already excludes cache_read_input_tokens and cache_creation_input_tokens, which are separate fields. Pass these counts directly.
  • OpenAI-compatible: prompt_tokens (or input_tokens on the Responses API) includes cached tokens; cached_tokens in the details object is a subset of it, not an additive count.
  • Gemini: promptTokenCount includes cachedContentTokenCount.

For OpenAI-compatible and Gemini usage, subtract the cached count from the prompt count before you pass it as input_tokens. If you do not, the cached tokens are billed twice: once at the input rate and once at the cache rate.

Trait Implementations§

Source§

impl Clone for Registry

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Registry

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more