diff --git a/litellm-rust/crates/cost/src/provider_cache.rs b/litellm-rust/crates/cost/src/provider_cache.rs index 65034f48c4c..c55762d2a25 100644 --- a/litellm-rust/crates/cost/src/provider_cache.rs +++ b/litellm-rust/crates/cost/src/provider_cache.rs @@ -5,7 +5,7 @@ use crate::provider::LlmProviders; fn rate(value: Option<&Value>) -> Option { match value? { Value::Number(value) => value.as_f64(), - Value::String(value) => value.parse().ok(), + Value::String(value) => value.trim().parse().ok(), _ => None, } } diff --git a/litellm-rust/crates/cost/tests/python_custom_pricing.rs b/litellm-rust/crates/cost/tests/python_custom_pricing.rs index 63470306531..1abc6e67eeb 100644 --- a/litellm-rust/crates/cost/tests/python_custom_pricing.rs +++ b/litellm-rust/crates/cost/tests/python_custom_pricing.rs @@ -265,3 +265,46 @@ fn catalog_custom_pricing_falls_back_and_rejects_invalid_rates() { Err(CostError::InvalidRate) ); } + +#[rstest] +#[case::negative_prompt_tokens(usage(-1.0, 0.0), None)] +#[case::nan_completion_tokens(usage(0.0, f64::NAN), None)] +#[case::negative_detail_cache_tokens(RawUsage { details_cached_tokens: Some(-1.0), ..usage(10.0, 0.0) }, None)] +#[case::infinite_fallback_tokens(RawUsage { fallback_cache_read_tokens: Some(f64::INFINITY), ..usage(10.0, 0.0) }, None)] +#[case::overflowing_prompt_sum(RawUsage { top_level_cache_read_tokens: Some(f64::MAX), top_level_cache_creation_tokens: Some(f64::MAX), ..usage(f64::MAX, 0.0) }, None)] +#[case::valid_usage(usage(10.0, 2.0), Some((10.0, 2.0)))] +fn normalize_cache_usage_rejects_negative_or_non_finite_counts( + #[case] raw: RawUsage, + #[case] expected: Option<(f64, f64)>, +) { + let normalized = normalize_cache_usage(raw); + match expected { + Some((prompt, completion)) => { + let normalized = normalized.unwrap(); + assert_eq!( + (normalized.prompt_tokens, normalized.completion_tokens), + (prompt, completion) + ); + } + None => assert_eq!(normalized, Err(CostError::InvalidUsage)), + } +} + +#[rstest] +#[case::negative_token_rate(CustomPricing { token: Some(rates(0.1, -0.1, None, None)), per_second: None }, None, CostError::InvalidRate)] +#[case::nan_cache_read_rate(CustomPricing { token: Some(rates(0.1, 0.1, Some(f64::NAN), None)), per_second: None }, None, CostError::InvalidRate)] +#[case::negative_second_rate(CustomPricing { token: None, per_second: Some(-0.5) }, Some(10.0), CostError::InvalidRate)] +#[case::negative_duration(CustomPricing { token: None, per_second: Some(0.5) }, Some(-10.0), CostError::InvalidDuration)] +#[case::infinite_duration(CustomPricing { token: None, per_second: Some(0.5) }, Some(f64::INFINITY), CostError::InvalidDuration)] +#[case::overflowing_token_cost(CustomPricing { token: Some(rates(f64::MAX, 0.0, None, None)), per_second: None }, None, CostError::NonFiniteCost)] +fn cost_per_token_custom_pricing_helper_rejects_unusable_rates_and_durations( + #[case] pricing: CustomPricing, + #[case] response_time_ms: Option, + #[case] expected: CostError, +) { + let normalized = normalize_cache_usage(usage(1000.0, 0.0)).unwrap(); + assert_eq!( + cost_per_token_custom_pricing_helper(normalized, pricing, response_time_ms), + Err(expected) + ); +} diff --git a/litellm-rust/crates/cost/tests/python_provider_cache.rs b/litellm-rust/crates/cost/tests/python_provider_cache.rs index cf3e53f13a3..199dd26f84a 100644 --- a/litellm-rust/crates/cost/tests/python_provider_cache.rs +++ b/litellm-rust/crates/cost/tests/python_provider_cache.rs @@ -68,3 +68,34 @@ fn model_info_catalog_uses_fireworks_default_for_cached_tokens() { .unwrap(); assert!((actual.0 - (60.0 * 2e-6 + 40.0 * 1e-6)).abs() < 1e-12); } + +#[rstest] +#[case::numeric_string(json!(" 2e-6 "), Some(json!(1e-6)))] +#[case::integer(json!(2), Some(json!(1.0)))] +#[case::boolean_is_not_a_rate(json!(true), None)] +#[case::unparseable_string(json!("free"), None)] +#[case::null(json!(null), None)] +fn with_default_cache_read_rate_coerces_input_rate_like_python_as_rate( + #[case] input_rate: serde_json::Value, + #[case] expected: Option, +) { + let info = json!({"input_cost_per_token": input_rate}); + assert_eq!( + with_default_cache_read_rate(&info) + .get("cache_read_input_token_cost") + .cloned(), + expected + ); +} + +#[rstest] +fn with_default_cache_read_rate_falls_back_to_base_rate_for_off_peak_without_input_rate() { + let info = json!({ + "input_cost_per_token": 2e-6, + "off_peak_pricing": {"hours_utc": "16:00-20:00"} + }); + assert_eq!( + with_default_cache_read_rate(&info)["off_peak_pricing"]["cache_read_input_token_cost"], + json!(1e-6) + ); +}