1use serde::{Deserialize, Serialize};
25
26#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
32pub struct ModelPricing {
33 pub model_name: String,
35 pub input_cost_per_1k: f64,
37 pub output_cost_per_1k: f64,
39}
40
41impl ModelPricing {
42 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
67pub 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 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 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 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 ModelPricing::new("deepseek-flash", 0.0003, 0.0012),
157 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 #[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 #[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 #[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}