pub struct Registry {
pub models: HashMap<String, ModelPricing>,
}Fields§
§models: HashMap<String, ModelPricing>Implementations§
Source§impl Registry
impl Registry
pub fn load_embedded() -> Result<Self>
pub fn from_json_str(raw: &str) -> Result<Self>
pub fn get(&self, model: &str) -> Option<&ModelPricing>
pub fn estimate_cost( &self, model: &str, input_tokens: u32, output_tokens: u32, ) -> Option<f64>
Sourcepub fn estimate_cost_with_cache(
&self,
model: &str,
input_tokens: u32,
output_tokens: u32,
cache_read_tokens: u32,
cache_creation_tokens: u32,
) -> Option<f64>
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_tokensalready excludescache_read_input_tokensandcache_creation_input_tokens, which are separate fields. Pass these counts directly. - OpenAI-compatible:
prompt_tokens(orinput_tokenson the Responses API) includes cached tokens;cached_tokensin the details object is a subset of it, not an additive count. - Gemini:
promptTokenCountincludescachedContentTokenCount.
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.