Skip to main content

adk_eval/
pricing.rs

1//! Per-model pricing configuration for cost estimation.
2//!
3//! This module provides pricing tables used by the [`CostTracker`](crate::CostTracker)
4//! to compute estimated dollar costs from token usage. Pricing is specified as
5//! cost per 1,000 tokens for both input and output.
6//!
7//! # Example
8//!
9//! ```rust
10//! use adk_eval::pricing::{ModelPricing, default_pricing};
11//!
12//! // Use built-in pricing for common models
13//! let pricing = default_pricing();
14//! assert!(!pricing.is_empty());
15//!
16//! // Create custom pricing for a specific model
17//! let custom = ModelPricing {
18//!     model_name: "my-custom-model".to_string(),
19//!     input_cost_per_1k: 0.001,
20//!     output_cost_per_1k: 0.002,
21//! };
22//! ```
23
24use serde::{Deserialize, Serialize};
25
26/// Per-model pricing configuration.
27///
28/// Defines the cost per 1,000 input and output tokens for a specific model.
29/// Used by [`CostTracker`](crate::CostTracker) to compute estimated dollar
30/// costs from token counts extracted during evaluation.
31#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
32pub struct ModelPricing {
33    /// Model identifier (e.g., "gemini-3.8-flash", "gpt-6.1-sol")
34    pub model_name: String,
35    /// Cost per 1,000 input tokens in USD
36    pub input_cost_per_1k: f64,
37    /// Cost per 1,000 output tokens in USD
38    pub output_cost_per_1k: f64,
39}
40
41impl ModelPricing {
42    /// Create a new `ModelPricing` entry.
43    ///
44    /// # Arguments
45    ///
46    /// * `model_name` - Model identifier string
47    /// * `input_cost_per_1k` - Cost per 1K input tokens (USD)
48    /// * `output_cost_per_1k` - Cost per 1K output tokens (USD)
49    ///
50    /// # Example
51    ///
52    /// ```rust
53    /// use adk_eval::pricing::ModelPricing;
54    ///
55    /// let pricing = ModelPricing::new("gpt-4o", 0.0025, 0.01);
56    /// assert_eq!(pricing.model_name, "gpt-4o");
57    /// ```
58    pub fn new(
59        model_name: impl Into<String>,
60        input_cost_per_1k: f64,
61        output_cost_per_1k: f64,
62    ) -> Self {
63        Self { model_name: model_name.into(), input_cost_per_1k, output_cost_per_1k }
64    }
65}
66
67/// Returns default pricing tables for current LLM models.
68///
69/// Rates are the vendors' published standard-tier list prices, converted to USD
70/// per 1,000 tokens, verified 2026-08-23 (Gemini 2026-11-29) against:
71///
72/// - <https://aistudio.google.com/docs/pricing>
73/// - <https://developers.openai.com/api/docs/pricing>
74/// - <https://docs.claude.com/en/docs/about-claude/pricing>
75/// - <https://api-docs.deepseek.com/quick_start/pricing>
76///
77/// Only text input and output are modelled. Prompt-cache reads, batch discounts,
78/// long-context tiers, audio and image rates, and DeepSeek's off-peak halving are
79/// not represented; use the provider crates' pricing modules for those. Gemini 4 Carbon and
80/// Argon carry the rates in force today (UTC): introductory through 2026-11-30, doubled
81/// from 2026-12-01.
82///
83/// # Example
84///
85/// ```rust
86/// use adk_eval::pricing::default_pricing;
87///
88/// let pricing = default_pricing();
89/// let flash = pricing.iter().find(|p| p.model_name == "gemini-3.8-flash");
90/// assert!(flash.is_some());
91/// ```
92pub fn default_pricing() -> Vec<ModelPricing> {
93    let gemini_4_change =
94        chrono::NaiveDate::from_ymd_opt(2026, 12, 1).unwrap_or(chrono::NaiveDate::MAX);
95    let gemini_4 = if chrono::Utc::now().date_naive() >= gemini_4_change {
96        (0.004, 0.02)
97    } else {
98        (0.002, 0.01)
99    };
100    vec![
101        // Google Gemini. 3.8, 3.7 and 3.6 Flash carry introductory rates that double
102        // on 2027-01-01; Gemini 4 Carbon and Argon rates double on 2026-12-01.
103        ModelPricing::new("gemini-4-hydrogen", 0.0015, 0.0075),
104        ModelPricing::new("gemini-4-neon", 0.003, 0.015),
105        ModelPricing::new("gemini-4-carbon", gemini_4.0, gemini_4.1),
106        ModelPricing::new("gemini-4-argon", gemini_4.0, gemini_4.1),
107        ModelPricing::new("gemini-3.8-flash", 0.00075, 0.00375),
108        ModelPricing::new("gemini-3.7-flash", 0.00075, 0.00375),
109        ModelPricing::new("gemini-3.6-flash", 0.00075, 0.00375),
110        ModelPricing::new("gemini-3.5-flash", 0.0015, 0.009),
111        ModelPricing::new("gemini-3.5-flash-lite", 0.0003, 0.0025),
112        ModelPricing::new("gemini-3.1-pro-preview", 0.002, 0.012),
113        ModelPricing::new("gemini-3.1-flash-lite", 0.00025, 0.0015),
114        ModelPricing::new("gemini-3-flash-preview", 0.0005, 0.003),
115        ModelPricing::new("gemini-2.5-pro", 0.00125, 0.01),
116        ModelPricing::new("gemini-2.5-flash", 0.0003, 0.0025),
117        ModelPricing::new("gemini-2.5-flash-lite", 0.0001, 0.0004),
118        // OpenAI
119        ModelPricing::new("gpt-6-astra", 0.01, 0.05),
120        ModelPricing::new("gpt-6.1-sol", 0.002, 0.01),
121        ModelPricing::new("gpt-6.1-terra", 0.001, 0.005),
122        ModelPricing::new("gpt-6-luna", 0.0001, 0.0005),
123        ModelPricing::new("gpt-5.6-sol", 0.004, 0.02),
124        ModelPricing::new("gpt-5.6-terra", 0.002, 0.012),
125        ModelPricing::new("gpt-5.6-luna", 0.0002, 0.0012),
126        ModelPricing::new("gpt-5.5", 0.005, 0.03),
127        ModelPricing::new("gpt-5.4", 0.0025, 0.015),
128        ModelPricing::new("gpt-5.4-mini", 0.00075, 0.0045),
129        ModelPricing::new("gpt-5.4-nano", 0.0002, 0.00125),
130        ModelPricing::new("gpt-5.3-codex", 0.00175, 0.014),
131        ModelPricing::new("gpt-5.2", 0.00175, 0.014),
132        ModelPricing::new("gpt-5.1", 0.00125, 0.01),
133        ModelPricing::new("gpt-5", 0.00125, 0.01),
134        ModelPricing::new("gpt-5-mini", 0.00025, 0.002),
135        ModelPricing::new("gpt-5-nano", 0.00005, 0.0004),
136        ModelPricing::new("gpt-4.1", 0.002, 0.008),
137        ModelPricing::new("gpt-4.1-mini", 0.0004, 0.0016),
138        ModelPricing::new("gpt-4o", 0.0025, 0.01),
139        ModelPricing::new("gpt-4o-mini", 0.00015, 0.0006),
140        ModelPricing::new("o3", 0.002, 0.008),
141        // Anthropic Claude
142        ModelPricing::new("claude-fable-5-2", 0.008, 0.04),
143        ModelPricing::new("claude-mythos-5-2", 0.008, 0.04),
144        ModelPricing::new("claude-fable-5-1", 0.01, 0.05),
145        ModelPricing::new("claude-opus-5-5", 0.004, 0.02),
146        ModelPricing::new("claude-sonnet-5-5", 0.002, 0.01),
147        ModelPricing::new("claude-haiku-5-5", 0.0001, 0.0005),
148        ModelPricing::new("claude-fable-5", 0.01, 0.05),
149        ModelPricing::new("claude-mythos-5", 0.01, 0.05),
150        ModelPricing::new("claude-opus-5", 0.005, 0.025),
151        ModelPricing::new("claude-opus-4-8", 0.005, 0.025),
152        ModelPricing::new("claude-sonnet-5", 0.002, 0.01),
153        ModelPricing::new("claude-sonnet-4-6", 0.003, 0.015),
154        ModelPricing::new("claude-haiku-4-5", 0.001, 0.005),
155        // DeepSeek. Peak (cache-miss) rates; off-peak is half.
156        ModelPricing::new("deepseek-flash", 0.0003, 0.0012),
157        // Legacy alias: still accepted by the API and served as deepseek-flash.
158        ModelPricing::new("deepseek-v4-flash", 0.0003, 0.0012),
159        ModelPricing::new("deepseek-v4-pro", 0.00132, 0.00396),
160    ]
161}
162
163#[cfg(test)]
164mod tests {
165    use super::*;
166
167    #[test]
168    fn test_model_pricing_new() {
169        let pricing = ModelPricing::new("test-model", 0.001, 0.002);
170        assert_eq!(pricing.model_name, "test-model");
171        assert_eq!(pricing.input_cost_per_1k, 0.001);
172        assert_eq!(pricing.output_cost_per_1k, 0.002);
173    }
174
175    #[test]
176    fn test_default_pricing_not_empty() {
177        let pricing = default_pricing();
178        assert!(!pricing.is_empty());
179    }
180
181    #[test]
182    fn test_default_pricing_includes_gemini() {
183        let pricing = default_pricing();
184        let gemini = pricing.iter().find(|p| p.model_name == "gemini-3.8-flash");
185        assert!(gemini.is_some());
186        let gemini = gemini.unwrap();
187        assert!(gemini.input_cost_per_1k > 0.0);
188        assert!(gemini.output_cost_per_1k > 0.0);
189    }
190
191    /// <https://aistudio.google.com/docs/pricing#gemini-4-neon>: $3.00 input and $15.00
192    /// output per million tokens; Carbon and Argon cost $2.00 and $10.00, or $4.00 and $20.00
193    /// from 2026-12-01.
194    #[test]
195    fn test_default_pricing_prices_gemini_4() {
196        let pricing = default_pricing();
197        let rates = |model: &str| {
198            pricing
199                .iter()
200                .find(|p| p.model_name == model)
201                .map(|p| (p.input_cost_per_1k, p.output_cost_per_1k))
202        };
203        assert_eq!(rates("gemini-4-neon"), Some((0.003, 0.015)));
204        let carbon = rates("gemini-4-carbon").expect("gemini-4-carbon is priced");
205        let close =
206            |a: (f64, f64), b: (f64, f64)| (a.0 - b.0).abs() < 1e-12 && (a.1 - b.1).abs() < 1e-12;
207        assert!(close(carbon, (0.002, 0.01)) || close(carbon, (0.004, 0.02)), "{carbon:?}");
208        assert_eq!(rates("gemini-4-argon"), Some(carbon));
209    }
210
211    #[test]
212    fn test_default_pricing_includes_openai() {
213        let pricing = default_pricing();
214        let gpt = pricing.iter().find(|p| p.model_name == "gpt-5.6-terra");
215        assert!(gpt.is_some());
216        let gpt = gpt.unwrap();
217        assert!(gpt.input_cost_per_1k > 0.0);
218        assert!(gpt.output_cost_per_1k > 0.0);
219    }
220
221    #[test]
222    fn test_default_pricing_includes_anthropic() {
223        let pricing = default_pricing();
224        let claude = pricing.iter().find(|p| p.model_name == "claude-sonnet-5");
225        assert!(claude.is_some());
226        let claude = claude.unwrap();
227        assert!(claude.input_cost_per_1k > 0.0);
228        assert!(claude.output_cost_per_1k > 0.0);
229    }
230
231    /// The table must not carry models the vendors have shut down, and must not
232    /// duplicate a model ID.
233    #[test]
234    fn test_default_pricing_excludes_retired_models() {
235        let pricing = default_pricing();
236        for retired in [
237            "gemini-2.0-flash",
238            "gemini-2.0-flash-lite",
239            "gemini-3-pro-preview",
240            "claude-3-opus",
241            "claude-3-haiku",
242            "claude-3.5-haiku",
243            "claude-sonnet-4-20250514",
244            "deepseek-chat",
245            "deepseek-reasoner",
246            "gpt-4",
247            "gpt-4-turbo",
248            "gpt-3.5-turbo",
249            "gpt-4.1-nano",
250            "o4-mini",
251            "o3-mini",
252            "o1",
253        ] {
254            assert!(
255                !pricing.iter().any(|p| p.model_name == retired),
256                "{retired} is retired and must not be in the default table"
257            );
258        }
259
260        let mut names: Vec<&str> = pricing.iter().map(|p| p.model_name.as_str()).collect();
261        names.sort_unstable();
262        let count = names.len();
263        names.dedup();
264        assert_eq!(names.len(), count, "duplicate model IDs in default pricing");
265    }
266
267    /// Output must never be cheaper than input for these vendors' text models.
268    /// A swapped pair is the most common transcription error.
269    #[test]
270    fn test_output_never_cheaper_than_input() {
271        for model in default_pricing() {
272            assert!(
273                model.output_cost_per_1k >= model.input_cost_per_1k,
274                "{} output {} is below input {}",
275                model.model_name,
276                model.output_cost_per_1k,
277                model.input_cost_per_1k
278            );
279        }
280    }
281
282    #[test]
283    fn test_default_pricing_all_positive_costs() {
284        let pricing = default_pricing();
285        for model in &pricing {
286            assert!(
287                model.input_cost_per_1k >= 0.0,
288                "Model {} has negative input cost",
289                model.model_name
290            );
291            assert!(
292                model.output_cost_per_1k >= 0.0,
293                "Model {} has negative output cost",
294                model.model_name
295            );
296        }
297    }
298
299    #[test]
300    fn test_model_pricing_serialization_roundtrip() {
301        let pricing = ModelPricing::new("test-model", 0.001, 0.002);
302        let json = serde_json::to_string(&pricing).unwrap();
303        let deserialized: ModelPricing = serde_json::from_str(&json).unwrap();
304        assert_eq!(pricing, deserialized);
305    }
306
307    #[test]
308    fn test_default_pricing_unique_model_names() {
309        let pricing = default_pricing();
310        let mut names: Vec<&str> = pricing.iter().map(|p| p.model_name.as_str()).collect();
311        let original_len = names.len();
312        names.sort();
313        names.dedup();
314        assert_eq!(names.len(), original_len, "Default pricing contains duplicate model names");
315    }
316}