From a57afedff355a4250cbdaf390a95415414ede913 Mon Sep 17 00:00:00 2001 From: Yujong Lee Date: Wed, 23 Sep 2026 20:19:38 -0700 Subject: [PATCH] fix(cost): read batch rates with Python's _batch_rate conversion Co-Authored-By: Claude Opus 5.5 --- litellm-rust/crates/cost/src/batch.rs | 16 +++++---------- .../cost/tests/python_cost_calculator.rs | 20 +++++++++++++++++++ 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/litellm-rust/crates/cost/src/batch.rs b/litellm-rust/crates/cost/src/batch.rs index a1fdf8b2b02..fb743e08470 100644 --- a/litellm-rust/crates/cost/src/batch.rs +++ b/litellm-rust/crates/cost/src/batch.rs @@ -6,6 +6,7 @@ use crate::generic_usage::{get_billable_input_tokens, parse_prompt_tokens_detail use crate::pricing::Rate; use crate::regional_uplift::get_regional_uplift_multiplier; use crate::responses_usage::ChatUsage; +use crate::wire::py_float; #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub enum ThresholdPolicy { @@ -209,17 +210,10 @@ pub fn batch_cost_calculator( } fn model_rate(model_info: &Value, key: &str) -> Rate { - let Some(value) = model_info.get(key) else { - return Rate::Missing; - }; - match value { - Value::Number(value) => value.as_f64().map_or(Rate::Missing, Rate::Value), - Value::String(value) => value - .trim() - .parse::() - .map_or(Rate::Missing, Rate::Value), - _ => Rate::Missing, - } + model_info + .get(key) + .and_then(py_float) + .map_or(Rate::Missing, Rate::Value) } fn threshold(value: &str) -> Option { diff --git a/litellm-rust/crates/cost/tests/python_cost_calculator.rs b/litellm-rust/crates/cost/tests/python_cost_calculator.rs index ec1c51f765a..034b94903ea 100644 --- a/litellm-rust/crates/cost/tests/python_cost_calculator.rs +++ b/litellm-rust/crates/cost/tests/python_cost_calculator.rs @@ -829,3 +829,23 @@ fn default_image_cost_calculator_strips_every_provider_prefix_like_python_replac .unwrap(); assert!((cost - 0.5).abs() < 1e-12); } + +#[rstest] +#[case::boolean_rate(json!(true), 100.0)] +#[case::padded_string_rate(json!(" 0.5 "), 50.0)] +#[case::unparseable_string_is_unset(json!("n/a"), 0.0)] +fn batch_rates_convert_like_python_batch_rate( + #[case] rate: serde_json::Value, + #[case] expected: f64, +) { + let model_info = + json!({"input_cost_per_token_batches": rate, "output_cost_per_token_batches": 0.0}); + let usage = ChatUsage { + prompt_tokens: 100, + completion_tokens: 0, + total_tokens: 100, + ..ChatUsage::default() + }; + let cost = batch_cost_from_model_info(&model_info, &usage, Some("openai"), None).unwrap(); + assert!((cost.prompt - expected).abs() < 1e-12); +}