use super::cache::{ModelPricing, TierRange};
pub fn calculate_cost(
input_tokens: i64,
output_tokens: i64,
cache_read_tokens: i64,
cache_creation_5m_tokens: i64,
cache_creation_1h_tokens: i64,
pricing: &ModelPricing,
) -> f64 {
let total_cache_creation = cache_creation_5m_tokens + cache_creation_1h_tokens;
let (input_price, output_price, cache_read_price, cc_5m_price, cc_1h_price_raw) =
if let Some(ranges) = &pricing.ranges {
let r = select_range(ranges, input_tokens);
(
r.map(|r| r.input_cost_per_token).unwrap_or(0.0),
r.map(|r| r.output_cost_per_token).unwrap_or(0.0),
r.map(|r| r.cache_read_input_token_cost).unwrap_or(0.0),
pricing.cache_creation_input_token_cost,
pricing.cache_creation_input_token_cost_above_1hr,
)
} else {
let total_input_context = input_tokens + cache_read_tokens + total_cache_creation;
let active_tier = pricing
.tiers
.iter()
.rev()
.find(|t| total_input_context > t.threshold_tokens);
match active_tier {
Some(t) => (
t.input_cost_per_token,
t.output_cost_per_token,
t.cache_read_input_token_cost,
t.cache_creation_input_token_cost,
t.cache_creation_input_token_cost_above_1hr,
),
None => (
pricing.input_cost_per_token,
pricing.output_cost_per_token,
pricing.cache_read_input_token_cost,
pricing.cache_creation_input_token_cost,
pricing.cache_creation_input_token_cost_above_1hr,
),
}
};
let cc_1h_price = if cc_1h_price_raw > 0.0 {
cc_1h_price_raw
} else {
cc_5m_price
};
input_tokens as f64 * input_price
+ output_tokens as f64 * output_price
+ cache_read_tokens as f64 * cache_read_price
+ cache_creation_5m_tokens as f64 * cc_5m_price
+ cache_creation_1h_tokens as f64 * cc_1h_price
}
fn select_range(ranges: &[TierRange], input_tokens: i64) -> Option<&TierRange> {
ranges
.iter()
.rev()
.find(|r| r.min_tokens <= input_tokens)
.or_else(|| ranges.first())
}
#[cfg(test)]
mod tests {
use super::super::cache::ThresholdTier;
use super::*;
fn flat_pricing() -> ModelPricing {
ModelPricing {
input_cost_per_token: 0.000003,
output_cost_per_token: 0.000015,
cache_read_input_token_cost: 0.0000003,
cache_creation_input_token_cost: 0.00000375,
..Default::default()
}
}
fn sonnet_like_pricing() -> ModelPricing {
ModelPricing {
input_cost_per_token: 0.000003,
output_cost_per_token: 0.000015,
cache_read_input_token_cost: 0.0000003,
cache_creation_input_token_cost: 0.00000375,
tiers: vec![ThresholdTier {
threshold_tokens: 200_000,
input_cost_per_token: 0.000006,
output_cost_per_token: 0.0000225,
cache_read_input_token_cost: 0.0000006,
cache_creation_input_token_cost: 0.0000075,
..Default::default()
}],
ranges: None,
..Default::default()
}
}
#[test]
fn test_flat_pricing_applies_base() {
let p = flat_pricing();
let cost = calculate_cost(1000, 500, 200, 100, 0, &p);
let expected =
1000.0 * 0.000003 + 500.0 * 0.000015 + 200.0 * 0.0000003 + 100.0 * 0.00000375;
assert_eq!(cost, expected);
}
#[test]
fn test_threshold_tier_below_threshold_uses_base() {
let p = sonnet_like_pricing();
let cost = calculate_cost(1000, 500, 200, 100, 0, &p);
let expected =
1000.0 * 0.000003 + 500.0 * 0.000015 + 200.0 * 0.0000003 + 100.0 * 0.00000375;
assert_eq!(cost, expected);
}
#[test]
fn test_threshold_tier_above_threshold_applies_tier() {
let p = sonnet_like_pricing();
let cost = calculate_cost(250_000, 250_000, 250_000, 250_000, 0, &p);
let expected = 250_000.0 * 0.000006
+ 250_000.0 * 0.0000225
+ 250_000.0 * 0.0000006
+ 250_000.0 * 0.0000075;
assert_eq!(cost, expected);
}
#[test]
fn test_threshold_uses_total_input_context_not_output() {
let p = sonnet_like_pricing();
let cost = calculate_cost(50_000, 500_000, 0, 0, 0, &p);
let expected = 50_000.0 * 0.000003 + 500_000.0 * 0.000015;
assert_eq!(cost, expected);
}
#[test]
fn test_exact_200k_stays_on_base() {
let p = sonnet_like_pricing();
let cost_exact = calculate_cost(200_000, 50_000, 0, 0, 0, &p);
assert_eq!(cost_exact, 200_000.0 * 0.000003 + 50_000.0 * 0.000015);
let cost_above = calculate_cost(200_001, 50_000, 0, 0, 0, &p);
assert_eq!(cost_above, 200_001.0 * 0.000006 + 50_000.0 * 0.0000225);
}
#[test]
fn test_multi_tier_picks_highest_exceeded() {
let p = ModelPricing {
input_cost_per_token: 0.000001,
output_cost_per_token: 0.000002,
tiers: vec![
ThresholdTier {
threshold_tokens: 128_000,
input_cost_per_token: 0.000002,
output_cost_per_token: 0.000004,
..Default::default()
},
ThresholdTier {
threshold_tokens: 272_000,
input_cost_per_token: 0.000004,
output_cost_per_token: 0.000008,
..Default::default()
},
],
..Default::default()
};
let c1 = calculate_cost(100_000, 10_000, 0, 0, 0, &p);
assert_eq!(c1, 100_000.0 * 0.000001 + 10_000.0 * 0.000002);
let c2 = calculate_cost(200_000, 10_000, 0, 0, 0, &p);
assert_eq!(c2, 200_000.0 * 0.000002 + 10_000.0 * 0.000004);
let c3 = calculate_cost(300_000, 10_000, 0, 0, 0, &p);
assert_eq!(c3, 300_000.0 * 0.000004 + 10_000.0 * 0.000008);
}
#[test]
fn test_range_based_pricing_dispatches_by_input() {
let p = ModelPricing {
input_cost_per_token: 999.0,
output_cost_per_token: 999.0,
ranges: Some(vec![
TierRange {
min_tokens: 0,
max_tokens: 32_000,
input_cost_per_token: 0.000001,
output_cost_per_token: 0.000005,
..Default::default()
},
TierRange {
min_tokens: 32_000,
max_tokens: 128_000,
input_cost_per_token: 0.0000018,
output_cost_per_token: 0.000009,
..Default::default()
},
TierRange {
min_tokens: 128_000,
max_tokens: 256_000,
input_cost_per_token: 0.000003,
output_cost_per_token: 0.000015,
..Default::default()
},
TierRange {
min_tokens: 256_000,
max_tokens: 1_000_000,
input_cost_per_token: 0.000006,
output_cost_per_token: 0.00006,
..Default::default()
},
]),
..Default::default()
};
let c_low = calculate_cost(20_000, 5_000, 0, 0, 0, &p);
assert_eq!(c_low, 20_000.0 * 0.000001 + 5_000.0 * 0.000005);
let c_hi = calculate_cost(500_000, 5_000, 0, 0, 0, &p);
assert_eq!(c_hi, 500_000.0 * 0.000006 + 5_000.0 * 0.00006);
}
#[test]
fn test_range_based_falls_back_to_last_range_for_overflow() {
let p = ModelPricing {
ranges: Some(vec![TierRange {
min_tokens: 0,
max_tokens: 100_000,
input_cost_per_token: 0.000001,
output_cost_per_token: 0.000002,
..Default::default()
}]),
..Default::default()
};
let cost = calculate_cost(200_000, 0, 0, 0, 0, &p);
assert_eq!(cost, 200_000.0 * 0.000001);
}
#[test]
fn test_zero_tokens() {
let p = sonnet_like_pricing();
assert_eq!(calculate_cost(0, 0, 0, 0, 0, &p), 0.0);
}
#[test]
fn test_1hr_cache_creation_billed_at_extended_rate() {
let p = ModelPricing {
input_cost_per_token: 5e-6,
output_cost_per_token: 2.5e-5,
cache_read_input_token_cost: 5e-7,
cache_creation_input_token_cost: 6.25e-6,
cache_creation_input_token_cost_above_1hr: 1e-5,
..Default::default()
};
let cost = calculate_cost(0, 0, 0, 0, 10_000, &p);
assert_eq!(cost, 10_000.0 * 1e-5);
let cost_mixed = calculate_cost(0, 0, 0, 1_000, 10_000, &p);
assert_eq!(cost_mixed, 1_000.0 * 6.25e-6 + 10_000.0 * 1e-5);
}
#[test]
fn test_1hr_falls_back_to_5m_when_model_has_no_extended_price() {
let p = ModelPricing {
cache_creation_input_token_cost: 6.25e-6,
cache_creation_input_token_cost_above_1hr: 0.0,
..Default::default()
};
let cost = calculate_cost(0, 0, 0, 0, 10_000, &p);
assert_eq!(cost, 10_000.0 * 6.25e-6);
}
#[test]
fn test_1hr_uses_tier_price_when_tier_active() {
let p = ModelPricing {
input_cost_per_token: 3e-6,
cache_creation_input_token_cost: 3.75e-6,
cache_creation_input_token_cost_above_1hr: 6e-6,
tiers: vec![ThresholdTier {
threshold_tokens: 200_000,
input_cost_per_token: 6e-6,
cache_creation_input_token_cost: 7.5e-6,
cache_creation_input_token_cost_above_1hr: 1.2e-5,
..Default::default()
}],
..Default::default()
};
let cost = calculate_cost(250_000, 0, 0, 5_000, 5_000, &p);
let expected = 250_000.0 * 6e-6 + 5_000.0 * 7.5e-6 + 5_000.0 * 1.2e-5;
assert_eq!(cost, expected);
}
}