use super::cache::ModelPricing;
const TOKEN_THRESHOLD: i64 = 200_000;
pub fn calculate_cost(
input_tokens: i64,
output_tokens: i64,
cache_read_tokens: i64,
cache_creation_tokens: i64,
pricing: &ModelPricing,
) -> f64 {
let get_price = |tokens: i64, base_price: f64, above_200k_price: f64| -> f64 {
if tokens > TOKEN_THRESHOLD {
above_200k_price
} else {
base_price
}
};
let input_price = get_price(
input_tokens,
pricing.input_cost_per_token,
pricing.input_cost_per_token_above_200k_tokens,
);
let output_price = get_price(
output_tokens,
pricing.output_cost_per_token,
pricing.output_cost_per_token_above_200k_tokens,
);
let cache_read_price = get_price(
cache_read_tokens,
pricing.cache_read_input_token_cost,
pricing.cache_read_input_token_cost_above_200k_tokens,
);
let cache_creation_price = get_price(
cache_creation_tokens,
pricing.cache_creation_input_token_cost,
pricing.cache_creation_input_token_cost_above_200k_tokens,
);
let input_cost = input_tokens as f64 * input_price;
let output_cost = output_tokens as f64 * output_price;
let cache_read_cost = cache_read_tokens as f64 * cache_read_price;
let cache_creation_cost = cache_creation_tokens as f64 * cache_creation_price;
input_cost + output_cost + cache_read_cost + cache_creation_cost
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_calculate_cost() {
let pricing = ModelPricing {
input_cost_per_token: 0.000001,
output_cost_per_token: 0.000002,
cache_read_input_token_cost: 0.0000001,
cache_creation_input_token_cost: 0.0000005,
input_cost_per_token_above_200k_tokens: 0.000002,
output_cost_per_token_above_200k_tokens: 0.000004,
cache_read_input_token_cost_above_200k_tokens: 0.0000002,
cache_creation_input_token_cost_above_200k_tokens: 0.000001,
};
let cost = calculate_cost(1000, 500, 200, 100, &pricing);
assert_eq!(cost, 0.001_000 + 0.001_000 + 0.000_020 + 0.000_050);
let cost_above = calculate_cost(250_000, 250_000, 250_000, 250_000, &pricing);
let expected = 250_000.0 * 0.000002 + 250_000.0 * 0.000004 + 250_000.0 * 0.0000002 + 250_000.0 * 0.000001; assert_eq!(cost_above, expected);
}
#[test]
fn test_calculate_cost_mixed_threshold() {
let pricing = 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, input_cost_per_token_above_200k_tokens: 0.000006, output_cost_per_token_above_200k_tokens: 0.0000225, cache_read_input_token_cost_above_200k_tokens: 0.0000006, cache_creation_input_token_cost_above_200k_tokens: 0.0000075, };
let cost1 = calculate_cost(250_000, 100_000, 150_000, 50_000, &pricing);
let expected1 = 250_000.0 * 0.000006 + 100_000.0 * 0.000015 + 150_000.0 * 0.0000003 + 50_000.0 * 0.00000375; assert_eq!(cost1, expected1);
let cost2 = calculate_cost(100_000, 250_000, 150_000, 50_000, &pricing);
let expected2 = 100_000.0 * 0.000003 + 250_000.0 * 0.0000225 + 150_000.0 * 0.0000003 + 50_000.0 * 0.00000375; assert_eq!(cost2, expected2);
let cost3 = calculate_cost(300_000, 100_000, 250_000, 50_000, &pricing);
let expected3 = 300_000.0 * 0.000006 + 100_000.0 * 0.000015 + 250_000.0 * 0.0000006 + 50_000.0 * 0.00000375; assert_eq!(cost3, expected3);
let cost4 = calculate_cost(50_000, 80_000, 60_000, 40_000, &pricing);
let expected4 = 50_000.0 * 0.000003 + 80_000.0 * 0.000015 + 60_000.0 * 0.0000003 + 40_000.0 * 0.00000375; assert_eq!(cost4, expected4);
}
#[test]
fn test_calculate_cost_exactly_200k() {
let pricing = ModelPricing {
input_cost_per_token: 0.000001,
output_cost_per_token: 0.000002,
cache_read_input_token_cost: 0.0000001,
cache_creation_input_token_cost: 0.0000005,
input_cost_per_token_above_200k_tokens: 0.000002,
output_cost_per_token_above_200k_tokens: 0.000004,
cache_read_input_token_cost_above_200k_tokens: 0.0000002,
cache_creation_input_token_cost_above_200k_tokens: 0.000001,
};
let cost_exact = calculate_cost(200_000, 200_000, 200_000, 200_000, &pricing);
let expected = 200_000.0 * 0.000001 + 200_000.0 * 0.000002 + 200_000.0 * 0.0000001 + 200_000.0 * 0.0000005; assert_eq!(cost_exact, expected);
let cost_above = calculate_cost(200_001, 200_001, 200_001, 200_001, &pricing);
let expected_above = 200_001.0 * 0.000002 + 200_001.0 * 0.000004 + 200_001.0 * 0.0000002 + 200_001.0 * 0.000001; assert_eq!(cost_above, expected_above);
}
#[test]
fn test_calculate_cost_fallback_to_base() {
let mut pricing = ModelPricing {
input_cost_per_token: 0.000001,
output_cost_per_token: 0.000002,
cache_read_input_token_cost: 0.0000001,
cache_creation_input_token_cost: 0.0000005,
..Default::default()
};
pricing.input_cost_per_token_above_200k_tokens = pricing.input_cost_per_token;
pricing.output_cost_per_token_above_200k_tokens = pricing.output_cost_per_token;
pricing.cache_read_input_token_cost_above_200k_tokens = pricing.cache_read_input_token_cost;
pricing.cache_creation_input_token_cost_above_200k_tokens =
pricing.cache_creation_input_token_cost;
let cost = calculate_cost(250_000, 250_000, 250_000, 250_000, &pricing);
let expected = 250_000.0 * 0.000001 + 250_000.0 * 0.000002 + 250_000.0 * 0.0000001 + 250_000.0 * 0.0000005; assert_eq!(cost, expected);
}
}