use systemprompt_models::profile::{GatewayConfig, ProviderRegistry};
use systemprompt_models::services::ModelPricing;
pub fn resolve(
provider: &str,
model: &str,
gateway: Option<&GatewayConfig>,
registry: &ProviderRegistry,
) -> ModelPricing {
if let Some(gw) = gateway {
if let Some(route) = gw.find_route(model) {
if let Some(p) = route.pricing {
return p;
}
}
}
if let Some(p) = registry_pricing(registry, gateway, model) {
return p;
}
tracing::warn!(
provider = provider,
model = model,
"Gateway pricing lookup: no override and no registry entry — cost_microdollars will be 0"
);
ModelPricing::default()
}
fn registry_pricing(
registry: &ProviderRegistry,
gateway: Option<&GatewayConfig>,
model: &str,
) -> Option<ModelPricing> {
if let Some(route) = gateway.and_then(|gw| gw.find_route(model)) {
if let Some(m) = route
.resolve(registry)
.and_then(|entry| entry.find_model(model))
{
return Some(m.pricing);
}
}
registry
.providers
.iter()
.find_map(|entry| entry.find_model(model))
.map(|m| m.pricing)
}
pub fn cost_microdollars(pricing: ModelPricing, input_tokens: u32, output_tokens: u32) -> i64 {
let input = f64::from(input_tokens);
let output = f64::from(output_tokens);
let input_cost = (input / 1_000_000.0) * pricing.input_per_million;
let output_cost = (output / 1_000_000.0) * pricing.output_per_million;
((input_cost + output_cost) * 1_000_000.0).round() as i64
}