Skip to main content

llm/usage/
pricing.rs

1use schemars::JsonSchema;
2use serde::{Deserialize, Serialize};
3
4use super::{TokenUsage, Usd};
5
6/// Provider/model prices sourced from models.dev, denominated in USD per million tokens.
7#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, JsonSchema)]
8pub struct ModelPricing {
9    pub input_per_million: f64,
10    pub output_per_million: f64,
11    pub cache_read_per_million: Option<f64>,
12    pub cache_write_per_million: Option<f64>,
13}
14
15impl ModelPricing {
16    /// Estimated cost of token usage
17    pub fn estimate_cost(self, usage: TokenUsage) -> UsageCost {
18        let cache_read = usage.cache_read_tokens.unwrap_or_default();
19        let cache_creation = usage.cache_creation_tokens.unwrap_or_default();
20        let regular_input = usage.input_tokens.saturating_sub(cache_read + cache_creation);
21        let per_million = 1_000_000.0;
22        let input_usd = Usd::new(f64::from(regular_input) * self.input_per_million / per_million);
23        let output_usd = Usd::new(f64::from(usage.output_tokens) * self.output_per_million / per_million);
24        let cache_read_usd = Usd::new(
25            f64::from(cache_read) * self.cache_read_per_million.unwrap_or(self.input_per_million) / per_million,
26        );
27        let cache_creation_usd = Usd::new(
28            f64::from(cache_creation) * self.cache_write_per_million.unwrap_or(self.input_per_million) / per_million,
29        );
30        UsageCost {
31            input_usd,
32            output_usd,
33            cache_read_usd,
34            cache_creation_usd,
35            total_usd: input_usd + output_usd + cache_read_usd + cache_creation_usd,
36        }
37    }
38}
39
40/// Estimated USD cost of one call, split by token pool.
41#[derive(Debug, Clone, Copy, Default, PartialEq, Serialize, Deserialize, JsonSchema)]
42pub struct UsageCost {
43    pub input_usd: Usd,
44    pub output_usd: Usd,
45    pub cache_read_usd: Usd,
46    pub cache_creation_usd: Usd,
47    pub total_usd: Usd,
48}