firstpass_core/cost.rs
1//! Cost model: per-model token pricing and the counterfactual baseline (SPEC §9.1).
2//!
3//! The whole product claim is "cheapest model that passes," so cost math is load-bearing.
4//! Two numbers matter per trace: what the escalation ladder actually spent, and the
5//! **counterfactual baseline** — what always calling the top rung would have cost. Their
6//! difference is the savings Firstpass proves.
7
8use crate::error::{Error, Result};
9use serde::{Deserialize, Serialize};
10use std::collections::HashMap;
11
12/// Published price of a model, in USD per 1,000,000 tokens.
13#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
14pub struct ModelPrice {
15 /// USD per 1M input (prompt) tokens.
16 pub input_per_mtok: f64,
17 /// USD per 1M output (completion) tokens.
18 pub output_per_mtok: f64,
19}
20
21impl ModelPrice {
22 /// Cost in USD of `input`/`output` tokens at this price.
23 #[must_use]
24 pub fn cost(&self, input: u64, output: u64) -> f64 {
25 (input as f64 / 1e6) * self.input_per_mtok + (output as f64 / 1e6) * self.output_per_mtok
26 }
27}
28
29/// A lookup from `provider/model` to [`ModelPrice`].
30///
31/// ponytail: the embedded [`PriceTable::defaults`] are a calibration knob, not gospel —
32/// list prices drift and enterprise contracts differ. In prod, load overrides from config
33/// via [`PriceTable::with_override`]; the defaults just make the common case work out of the box.
34#[derive(Debug, Clone, Default)]
35pub struct PriceTable {
36 prices: HashMap<String, ModelPrice>,
37}
38
39impl PriceTable {
40 /// An empty table.
41 #[must_use]
42 pub fn new() -> Self {
43 Self::default()
44 }
45
46 /// Built-in prices for the common frontier models (USD / 1M tokens, approximate list
47 /// prices — a starting point to be overridden per deployment).
48 #[must_use]
49 pub fn defaults() -> Self {
50 let mut prices = HashMap::new();
51 let mut put = |k: &str, i: f64, o: f64| {
52 prices.insert(
53 k.to_owned(),
54 ModelPrice {
55 input_per_mtok: i,
56 output_per_mtok: o,
57 },
58 );
59 };
60 // Anthropic
61 put("anthropic/claude-haiku-4-5", 1.0, 5.0);
62 put("anthropic/claude-sonnet-5", 3.0, 15.0);
63 put("anthropic/claude-opus-4-8", 15.0, 75.0);
64 // OpenAI
65 put("openai/gpt-4.1-mini", 0.4, 1.6);
66 put("openai/gpt-5.5", 5.0, 15.0);
67 // Google. `gemini-3.1-flash`/`-pro` were priced here but never existed on the API —
68 // `generateContent` 404s them — so the shipped Google ladder failed on its first call.
69 // These two are GA per Google's model + pricing docs.
70 // ponytail: Google prices Pro in tiers (>200k-token prompts cost more) and this table
71 // is flat; these are the ≤200k rates. Pin your own with `[[price]]` if you run long
72 // prompts on Pro.
73 put("google/gemini-3.5-flash-lite", 0.3, 2.5);
74 put("google/gemini-3.6-flash", 1.5, 7.5);
75 put("google/gemini-2.5-pro", 1.25, 10.0);
76 Self { prices }
77 }
78
79 /// Insert or replace a model's price, returning `self` for chaining.
80 #[must_use]
81 pub fn with_override(mut self, model: impl Into<String>, price: ModelPrice) -> Self {
82 self.prices.insert(model.into(), price);
83 self
84 }
85
86 /// Look up a model's price by its `provider/model` key.
87 #[must_use]
88 pub fn get(&self, model: &str) -> Option<ModelPrice> {
89 self.prices.get(model).copied()
90 }
91
92 /// Cost in USD of a call to `model` with the given token counts.
93 ///
94 /// # Errors
95 /// Returns [`Error::UnknownModel`] if the model has no price entry.
96 pub fn cost_usd(&self, model: &str, input: u64, output: u64) -> Result<f64> {
97 self.get(model)
98 .map(|p| p.cost(input, output))
99 .ok_or_else(|| Error::UnknownModel(model.to_owned()))
100 }
101
102 /// The counterfactual baseline: what the request would have cost had it gone straight to
103 /// `top_model` (the ladder's top rung) with the served token counts.
104 ///
105 /// This is an estimate — the top model might have emitted a different number of tokens —
106 /// but token counts of served output are the fair, auditable proxy the trace records.
107 ///
108 /// # Errors
109 /// Returns [`Error::UnknownModel`] if `top_model` has no price entry.
110 pub fn baseline_usd(&self, top_model: &str, input: u64, output: u64) -> Result<f64> {
111 self.cost_usd(top_model, input, output)
112 }
113}
114
115#[cfg(test)]
116mod tests {
117 use super::*;
118
119 #[test]
120 fn cost_math_is_correct() {
121 let p = ModelPrice {
122 input_per_mtok: 3.0,
123 output_per_mtok: 15.0,
124 };
125 // 1000 in * $3/M + 500 out * $15/M = 0.003 + 0.0075 = 0.0105
126 assert!((p.cost(1000, 500) - 0.0105).abs() < 1e-12);
127 assert_eq!(p.cost(0, 0), 0.0);
128 }
129
130 #[test]
131 fn table_lookup_and_unknown_model() {
132 let t = PriceTable::defaults();
133 assert!(t.cost_usd("anthropic/claude-haiku-4-5", 1000, 1000).is_ok());
134 match t.cost_usd("acme/nope", 1, 1) {
135 Err(Error::UnknownModel(m)) => assert_eq!(m, "acme/nope"),
136 other => panic!("expected UnknownModel, got {other:?}"),
137 }
138 }
139
140 #[test]
141 fn baseline_exceeds_cheap_rung_for_same_tokens() {
142 // The core value prop, as a math invariant: top rung costs more than the cheap rung.
143 let t = PriceTable::defaults();
144 let (i, o) = (2000, 800);
145 let cheap = t.cost_usd("anthropic/claude-haiku-4-5", i, o).unwrap();
146 let baseline = t.baseline_usd("anthropic/claude-opus-4-8", i, o).unwrap();
147 assert!(baseline > cheap);
148 assert!(baseline - cheap > 0.0); // there are savings to prove
149 }
150
151 #[test]
152 fn overrides_win() {
153 let t = PriceTable::new().with_override(
154 "x/y",
155 ModelPrice {
156 input_per_mtok: 2.0,
157 output_per_mtok: 2.0,
158 },
159 );
160 assert_eq!(t.cost_usd("x/y", 1_000_000, 0).unwrap(), 2.0);
161 }
162}