From c55f9d7a08e0f88ddfbc3fb6380180ca45b8729c Mon Sep 17 00:00:00 2001 From: Yujong Lee Date: Wed, 23 Sep 2026 20:12:07 -0700 Subject: [PATCH] fix(cost): build the zero-cost warning from the same values Python prints The warning falls back from an empty model group to the model and from an empty provider to unknown, and prints the caller's provider and model instead of inferring them Co-Authored-By: Claude Opus 5.5 --- .../crates/cost/src/zero_cost_diagnostic.rs | 20 ++++----- .../cost/tests/python_zero_cost_diagnostic.rs | 43 +++++++++++++++++++ 2 files changed, 51 insertions(+), 12 deletions(-) diff --git a/litellm-rust/crates/cost/src/zero_cost_diagnostic.rs b/litellm-rust/crates/cost/src/zero_cost_diagnostic.rs index 7dc6d60f71e..603442d9555 100644 --- a/litellm-rust/crates/cost/src/zero_cost_diagnostic.rs +++ b/litellm-rust/crates/cost/src/zero_cost_diagnostic.rs @@ -111,15 +111,7 @@ pub fn zero_cost_finding( pricing_entry, request.calculation_failed, )?; - let model = request - .model_selection - .model - .or_else(|| response.get("model").and_then(Value::as_str))?; - let provider = catalog.get_provider_for_cost_calc( - Some(model), - request.model_selection.provider, - request.model_selection.known_providers, - ); + let model = request.model_selection.model.unwrap_or("None"); let warning = zero_cost_warning( &diagnostic, request @@ -127,7 +119,7 @@ pub fn zero_cost_finding( .and_then(|metadata| metadata.get("model_group")) .and_then(Value::as_str), model, - provider.as_deref(), + request.model_selection.provider, &usage, ); Some((diagnostic, warning)) @@ -245,9 +237,13 @@ pub fn zero_cost_warning( }; format!( "Billable request priced at $0 and logged as such (model_group={} model={} provider={} prompt_tokens={} completion_tokens={}): {}. Counted in {}{{reason=\"{}\"}}", - model_group.unwrap_or(model), + model_group + .filter(|group| !group.is_empty()) + .unwrap_or(model), model, - provider.unwrap_or("unknown"), + provider + .filter(|provider| !provider.is_empty()) + .unwrap_or("unknown"), usage.prompt_tokens, usage.completion_tokens, cause, diff --git a/litellm-rust/crates/cost/tests/python_zero_cost_diagnostic.rs b/litellm-rust/crates/cost/tests/python_zero_cost_diagnostic.rs index 5558480af54..01c8380fc05 100644 --- a/litellm-rust/crates/cost/tests/python_zero_cost_diagnostic.rs +++ b/litellm-rust/crates/cost/tests/python_zero_cost_diagnostic.rs @@ -224,3 +224,46 @@ fn zero_cost_reason_labels_match_python_literal( assert_eq!(reason.as_str(), label); assert_eq!(reason.to_string(), label); } + +#[rstest] +#[case::named_group( + Some("group"), + Some("provider"), + "model_group=group model=model provider=provider" +)] +#[case::empty_group_falls_back_to_model( + Some(""), + Some("provider"), + "model_group=model model=model provider=provider" +)] +#[case::missing_group( + None, + Some("provider"), + "model_group=model model=model provider=provider" +)] +#[case::empty_provider_is_unknown( + Some("group"), + Some(""), + "model_group=group model=model provider=unknown" +)] +#[case::missing_provider_is_unknown( + Some("group"), + None, + "model_group=group model=model provider=unknown" +)] +fn zero_cost_warning_uses_python_or_fallbacks( + #[case] model_group: Option<&str>, + #[case] provider: Option<&str>, + #[case] expected: &str, +) { + let usage = usage(json!({"prompt_tokens": 10, "completion_tokens": 20})); + let diagnostic = diagnose_zero_cost( + &usage, + "deployment", + &json!({"input_cost_per_token": 0.01, "output_cost_per_token": 0.02}), + true, + ) + .unwrap(); + let warning = zero_cost_warning(&diagnostic, model_group, "model", provider, &usage); + assert!(warning.contains(expected), "{warning}"); +}