fix(cost): fall back to the response model when custom pricing has no model

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Yujong Lee 2026-09-23 19:59:53 -07:00
parent c31c4acf13
commit d28f28e1f0
2 changed files with 28 additions and 13 deletions

View file

@ -118,24 +118,20 @@ pub fn select_model_name_for_cost_calc(
get_hidden_str_for_cost_calc(request.hidden_params, "region_name").or(request.region_name)
})
.flatten();
let selected = if request.custom_pricing {
let explicitly_selected = if request.custom_pricing {
request
.router_model_id
.filter(|id| cost_map.get(*id).is_some_and(has_explicit_pricing))
.or(request.model)
} else {
request
.base_model
.or(private_model)
.or_else(|| {
response_model
.is_none()
.then(|| get_hidden_str_for_cost_calc(request.hidden_params, "model"))
.flatten()
})
.or(response_model)
.or(request.model)
}?;
request.base_model.or(private_model).or_else(|| {
response_model
.is_none()
.then(|| get_hidden_str_for_cost_calc(request.hidden_params, "model"))
.flatten()
})
};
let selected = explicitly_selected.or(response_model).or(request.model)?;
let Some(provider) = provider else {
return Some(selected.to_owned());
};

View file

@ -271,3 +271,22 @@ fn pricing_entry_falls_back_from_unpriced_base_to_served_model() {
Some(("openai/served", &json!({"input_cost_per_token": 0.01})))
);
}
#[rstest]
#[case::no_requested_model(None, Some("openai/gpt-4o"))]
#[case::requested_model_wins(Some("requested"), Some("openai/requested"))]
fn custom_pricing_without_a_priced_router_id_falls_back_to_the_response_model(
#[case] model: Option<&str>,
#[case] expected: Option<&str>,
) {
let catalog = ModelInfoCatalog::new(HashMap::new());
let response = json!({"model": "gpt-4o"});
assert_eq!(
catalog.select_model_name_for_cost_calc(ModelSelectionRequest {
custom_pricing: true,
response: Some(&response),
..request(model, Some("openai"))
}),
expected.map(str::to_owned)
);
}