From ec55adffa9516f1ae2a3302faca0c69b47cd0be1 Mon Sep 17 00:00:00 2001 From: Yujong Lee Date: Wed, 23 Sep 2026 18:24:45 -0700 Subject: [PATCH] fix(cost): count and size images in the router the way Python resolves them route_image_generation_cost_calculator now bills zero images when the response has no data, skips empty size and quality strings like Python's or chain, and dispatches on LlmProviders and CallTypes instead of string literals. The three copies of Python truthiness collapse into wire::is_truthy Co-Authored-By: Claude Opus 5.5 --- .../crates/cost/src/completion_response.rs | 3 +- .../crates/cost/src/image_cost_router.rs | 104 ++++++++++-------- .../crates/cost/src/perplexity_cost.rs | 10 +- litellm-rust/crates/cost/src/ptu_pricing.rs | 17 +-- .../crates/cost/src/tool_cost_dispatch.rs | 20 +--- litellm-rust/crates/cost/src/wire.rs | 28 +++++ .../cost/tests/python_bedrock_image_cost.rs | 3 +- .../cost/tests/python_fal_ai_image_cost.rs | 3 +- .../cost/tests/python_image_cost_router.rs | 66 ++++++++++- .../cost/tests/python_openai_image_cost.rs | 3 +- 10 files changed, 171 insertions(+), 86 deletions(-) diff --git a/litellm-rust/crates/cost/src/completion_response.rs b/litellm-rust/crates/cost/src/completion_response.rs index 669ac0c78fa..632107f95c1 100644 --- a/litellm-rust/crates/cost/src/completion_response.rs +++ b/litellm-rust/crates/cost/src/completion_response.rs @@ -7,6 +7,7 @@ use serde_json::Value; use crate::a2a_cost::calculate_a2a_cost; use crate::azure_ai_cost::{azure_ai_router_fee, is_azure_model_router}; use crate::billed_token_rates::{TokenTypeCostBreakdown, get_token_type_cost_breakdown}; +use crate::call_type::CallTypes; use crate::catalog::{CostCall, ModelCostRequest, ModelInfoCatalog}; use crate::completion_cost::{ CompletionCost, completion_cost, get_response_cost_from_hidden_params, @@ -320,7 +321,7 @@ fn price_image_response( model, provider, image_response: response, - call_type: Some(&prepared.call_type), + call_type: prepared.call_type.parse::().ok(), quality: request.image_quality, size: request.image_size, n: request.image_count, diff --git a/litellm-rust/crates/cost/src/image_cost_router.rs b/litellm-rust/crates/cost/src/image_cost_router.rs index 901785fc592..e87b4148d3a 100644 --- a/litellm-rust/crates/cost/src/image_cost_router.rs +++ b/litellm-rust/crates/cost/src/image_cost_router.rs @@ -5,6 +5,7 @@ use crate::azure_ai_image_cost::{ AzureAiImageRequest, cost_calculator as azure_ai_image_cost_calculator, }; use crate::bedrock_image_cost::cost_calculator as bedrock_image_cost_calculator; +use crate::call_type::CallTypes; use crate::catalog::ModelInfoCatalog; use crate::error::CostError; use crate::fal_ai_image_cost::cost_calculator as fal_ai_image_cost_calculator; @@ -16,13 +17,14 @@ use crate::image_response_cost::{ use crate::non_token::{ImageRates, ImageUsage, calculate_image}; use crate::openai_image_cost::cost_calculator as openai_image_cost_calculator; use crate::pricing::Rate; +use crate::provider::LlmProviders; #[derive(Clone, Copy, Debug)] pub struct ImageCostRouteRequest<'a> { pub model: &'a str, pub provider: Option<&'a str>, pub image_response: &'a Value, - pub call_type: Option<&'a str>, + pub call_type: Option, pub quality: Option<&'a str>, pub size: Option<&'a str>, pub n: Option, @@ -197,40 +199,46 @@ fn requested_image_size(optional_params: &Value) -> Option<&str> { .then_some(size) } -pub fn route_image_generation_cost_calculator( +pub fn route_image_generation_cost_calculator<'a>( catalog: &ModelInfoCatalog, - request: ImageCostRouteRequest<'_>, + request: ImageCostRouteRequest<'a>, ) -> Result { let pricing = deployment_pricing(request.supplied_model_info); - let resolved_size = request - .size - .or_else(|| request.image_response.get("size").and_then(Value::as_str)) + let non_empty = |value: Option<&'a str>| value.filter(|value| !value.is_empty()); + let resolved_size = non_empty(request.size) + .or_else(|| non_empty(request.image_response.get("size").and_then(Value::as_str))) .or_else(|| requested_image_size(request.optional_params)) - .or(Some("1024-x-1024")); - let resolved_quality = request - .quality + .unwrap_or("1024-x-1024"); + let resolved_quality = non_empty(request.quality) .or_else(|| { - request - .image_response - .get("quality") - .and_then(Value::as_str) + non_empty( + request + .image_response + .get("quality") + .and_then(Value::as_str), + ) }) .or_else(|| { - request - .optional_params - .get("quality") - .and_then(Value::as_str) + non_empty( + request + .optional_params + .get("quality") + .and_then(Value::as_str), + ) }) - .or(Some("standard")); - let resolved_n = request.n.or_else(|| { + .unwrap_or("standard"); + let resolved_n = request.n.unwrap_or_else(|| { request .image_response .get("data") .and_then(Value::as_array) - .map(|data| data.len() as u64) + .map_or(0, |data| data.len() as u64) }); - match request.provider { - Some("vertex_ai") => google_image_generation_cost( + let provider = request + .provider + .and_then(|provider| provider.parse::().ok()); + match provider { + Some(LlmProviders::VERTEX_AI) => google_image_generation_cost( catalog, request.model, "vertex_ai", @@ -238,7 +246,12 @@ pub fn route_image_generation_cost_calculator( pricing.as_ref(), request.at, ), - Some("gemini") if matches!(request.call_type, Some("image_edit" | "aimage_edit")) => { + Some(LlmProviders::GEMINI) + if matches!( + request.call_type, + Some(CallTypes::image_edit | CallTypes::aimage_edit) + ) => + { google_image_edit_cost( catalog, request.model, @@ -248,7 +261,7 @@ pub fn route_image_generation_cost_calculator( request.at, ) } - Some("gemini") => google_image_generation_cost( + Some(LlmProviders::GEMINI) => google_image_generation_cost( catalog, request.model, "gemini", @@ -256,28 +269,31 @@ pub fn route_image_generation_cost_calculator( pricing.as_ref(), request.at, ), - Some("azure_ai") => azure_ai_image_generation_cost( + Some(LlmProviders::AZURE_AI) => azure_ai_image_generation_cost( catalog, AzureAiImageCatalogRequest { model: request.model, image_response: request.image_response, - size: resolved_size, - n: resolved_n, + size: Some(resolved_size), + n: Some(resolved_n), optional_params: request.optional_params, supplied_model_info: pricing.as_ref(), at: request.at, }, ), - Some(provider @ ("recraft" | "aiml" | "cometapi" | "runwayml")) => { - flat_image_generation_cost( - catalog, - request.model, - provider, - request.image_response, - pricing.as_ref(), - ) - } - Some("fal_ai") => fal_ai_image_cost_calculator( + Some( + provider @ (LlmProviders::RECRAFT + | LlmProviders::AIML + | LlmProviders::COMETAPI + | LlmProviders::RUNWAYML), + ) => flat_image_generation_cost( + catalog, + request.model, + provider.as_str(), + request.image_response, + pricing.as_ref(), + ), + Some(LlmProviders::FAL_AI) => fal_ai_image_cost_calculator( request.model, request.image_response, request.optional_params, @@ -285,21 +301,21 @@ pub fn route_image_generation_cost_calculator( catalog.entries(), ) .ok_or(CostError::ModelNotFound), - Some("bedrock") => bedrock_image_cost_calculator( + Some(LlmProviders::BEDROCK) => bedrock_image_cost_calculator( request.model, request.image_response, - resolved_size, + Some(resolved_size), request.optional_params, catalog.entries(), ) .ok_or(CostError::ModelNotFound), - Some(provider @ ("openai" | "azure")) + Some(provider @ (LlmProviders::OPENAI | LlmProviders::AZURE)) if request.model.to_ascii_lowercase().contains("gpt-image") => { openai_image_generation_cost( catalog, request.model, - provider, + provider.as_str(), request.image_response, pricing.as_ref(), request.at, @@ -310,9 +326,9 @@ pub fn route_image_generation_cost_calculator( DefaultImageCostRequest { model: request.model, provider: request.provider, - quality: resolved_quality, - n: resolved_n, - size: resolved_size, + quality: Some(resolved_quality), + n: Some(resolved_n), + size: Some(resolved_size), supplied_model_info: pricing.as_ref(), }, ), diff --git a/litellm-rust/crates/cost/src/perplexity_cost.rs b/litellm-rust/crates/cost/src/perplexity_cost.rs index a7cec4e10c4..24a74b9803a 100644 --- a/litellm-rust/crates/cost/src/perplexity_cost.rs +++ b/litellm-rust/crates/cost/src/perplexity_cost.rs @@ -3,6 +3,7 @@ use serde_json::Value; use crate::off_peak::{open_off_peak_block, parse_off_peak_rate}; use crate::responses_usage::ChatUsage; +use crate::wire::is_truthy; fn rate(value: Option<&Value>) -> f64 { match value { @@ -16,14 +17,7 @@ fn rate(value: Option<&Value>) -> f64 { fn cost_per_query(model_info: &Value) -> f64 { let value = model_info .get("search_queries_cost_per_query") - .filter(|value| match value { - Value::Null => false, - Value::Bool(value) => *value, - Value::Number(_) => rate(Some(value)) != 0.0, - Value::String(value) => !value.is_empty(), - Value::Array(values) => !values.is_empty(), - Value::Object(values) => !values.is_empty(), - }) + .filter(|value| is_truthy(value)) .or_else(|| model_info.get("search_context_cost_per_query")); match value { Some(Value::Object(prices)) => rate(prices.get("search_context_size_low")), diff --git a/litellm-rust/crates/cost/src/ptu_pricing.rs b/litellm-rust/crates/cost/src/ptu_pricing.rs index 80ab69ff671..6696970f52e 100644 --- a/litellm-rust/crates/cost/src/ptu_pricing.rs +++ b/litellm-rust/crates/cost/src/ptu_pricing.rs @@ -2,6 +2,8 @@ use jiff::Timestamp; use jiff::civil::{Date, DateTime}; use serde_json::{Map, Value, json}; +use crate::wire::is_truthy; + pub const PTU_COST_ATTRIBUTION_ENV_VAR: &str = "LITELLM_ENABLE_PTU_COST_ATTRIBUTION"; pub const AZURE_SPILLOVER_HEADER: &str = "x-ms-is-spilled-over"; pub const AZURE_SPILLOVER_FROM_HEADER: &str = "x-ms-spillover-from-deployment"; @@ -156,17 +158,6 @@ pub struct AzureSpillover { pub from_deployment: Option, } -fn truthy(value: &Value) -> bool { - match value { - Value::Null => false, - Value::Bool(flag) => *flag, - Value::Number(number) => number.as_f64().is_some_and(|number| number != 0.0), - Value::String(text) => !text.is_empty(), - Value::Array(items) => !items.is_empty(), - Value::Object(fields) => !fields.is_empty(), - } -} - fn py_str(value: &Value) -> String { match value { Value::Null => "None".to_string(), @@ -277,7 +268,7 @@ pub fn ptu_config_error(model_info: &Value, model_name: Option<&str>) -> Option< model_name, )); } - if !truthy(model_info.get("team_id").unwrap_or(&Value::Null)) { + if !is_truthy(model_info.get("team_id").unwrap_or(&Value::Null)) { return Some(named( "team_id is required when PTU fields are set (one model maps to one team)", model_name, @@ -320,7 +311,7 @@ pub fn ptu_identity_error( } pub fn ptu_terms(model_info: &Value) -> Option { - let team_id = model_info.get("team_id").filter(|value| truthy(value))?; + let team_id = model_info.get("team_id").filter(|value| is_truthy(value))?; let ptu_count = py_int(model_info.get("ptu_count").unwrap_or(&Value::Null))?; let cost_per_hour = py_float( model_info diff --git a/litellm-rust/crates/cost/src/tool_cost_dispatch.rs b/litellm-rust/crates/cost/src/tool_cost_dispatch.rs index e80baeede67..73c99b64dd0 100644 --- a/litellm-rust/crates/cost/src/tool_cost_dispatch.rs +++ b/litellm-rust/crates/cost/src/tool_cost_dispatch.rs @@ -14,6 +14,7 @@ use crate::tool_call_cost_tracking::{ response_object_includes_file_search_call, response_object_includes_web_search_call, safe_convert_to_int, }; +use crate::wire::is_truthy; #[derive(Clone, Copy, Debug)] pub struct BuiltInToolCostRequest<'a> { @@ -129,22 +130,11 @@ fn maps_cost(request: BuiltInToolCostRequest<'_>, model_info: Option<&Value>) -> } } -fn truthy(value: &Value) -> bool { - match value { - Value::Null | Value::Bool(false) => false, - Value::Bool(true) => true, - Value::Number(value) => value.as_f64().is_some_and(|value| value != 0.0), - Value::String(value) => !value.is_empty(), - Value::Array(value) => !value.is_empty(), - Value::Object(value) => !value.is_empty(), - } -} - fn file_search_cost(request: BuiltInToolCostRequest<'_>, model_info: Option<&Value>) -> f64 { let file_search = request .params .get("file_search") - .filter(|value| truthy(value)); + .filter(|value| is_truthy(value)); let (storage_gb, days) = file_search .map(extract_file_search_params) .unwrap_or((None, None)); @@ -165,15 +155,15 @@ fn azure_assistant_cost(request: BuiltInToolCostRequest<'_>, model_info: Option< let vector = request .params .get("vector_store_usage") - .filter(|value| truthy(value)); + .filter(|value| is_truthy(value)); let computer = request .params .get("computer_use_usage") - .filter(|value| truthy(value)); + .filter(|value| is_truthy(value)); let sessions = request .params .get("code_interpreter_sessions") - .filter(|value| truthy(value)) + .filter(|value| is_truthy(value)) .and_then(|value| safe_convert_to_int(Some(value))); let (input_tokens, output_tokens) = computer.map(extract_token_counts).unwrap_or((None, None)); get_cost_for_vector_store(vector, request.provider, model_info, request.defaults) diff --git a/litellm-rust/crates/cost/src/wire.rs b/litellm-rust/crates/cost/src/wire.rs index cac632ee388..35ad3f6e6d6 100644 --- a/litellm-rust/crates/cost/src/wire.rs +++ b/litellm-rust/crates/cost/src/wire.rs @@ -72,6 +72,17 @@ pub fn catalog_with_overlays( overlay_catalog(&EMBEDDED_CATALOG, overlays) } +pub fn is_truthy(value: &Value) -> bool { + match value { + Value::Null => false, + Value::Bool(flag) => *flag, + Value::Number(number) => number.as_f64().is_some_and(|number| number != 0.0), + Value::String(text) => !text.is_empty(), + Value::Array(items) => !items.is_empty(), + Value::Object(fields) => !fields.is_empty(), + } +} + pub fn rate(value: Option<&Value>) -> Rate { match value { None => Rate::Missing, @@ -827,6 +838,23 @@ mod tests { )) } + #[rstest] + #[case(serde_json::json!(null), false)] + #[case(serde_json::json!(false), false)] + #[case(serde_json::json!(true), true)] + #[case(serde_json::json!(0), false)] + #[case(serde_json::json!(0.0), false)] + #[case(serde_json::json!(-0.5), true)] + #[case(serde_json::json!(""), false)] + #[case(serde_json::json!("0"), true)] + #[case(serde_json::json!([]), false)] + #[case(serde_json::json!([0]), true)] + #[case(serde_json::json!({}), false)] + #[case(serde_json::json!({"k": null}), true)] + fn is_truthy_follows_python_bool(#[case] value: Value, #[case] expected: bool) { + assert_eq!(is_truthy(&value), expected); + } + #[rstest] fn overlay_merges_fields_into_existing_entries_and_adds_new_ones() { let base = BTreeMap::from([( diff --git a/litellm-rust/crates/cost/tests/python_bedrock_image_cost.rs b/litellm-rust/crates/cost/tests/python_bedrock_image_cost.rs index 341fd3bcf7d..d1796f80a42 100644 --- a/litellm-rust/crates/cost/tests/python_bedrock_image_cost.rs +++ b/litellm-rust/crates/cost/tests/python_bedrock_image_cost.rs @@ -8,6 +8,7 @@ use jiff::Timestamp; use litellm_cost::bedrock_image_cost::{ BedrockImageFamily, cost_calculator, get_config_class, stability1_pricing_key, }; +use litellm_cost::call_type::CallTypes; use litellm_cost::catalog::ModelInfoCatalog; use litellm_cost::image_cost_router::{ ImageCostRouteRequest, route_image_generation_cost_calculator, @@ -105,7 +106,7 @@ fn bedrock_direct_families_bill_returned_images(#[case] model: &str, #[case] rat model, provider: Some("bedrock"), image_response: &response, - call_type: Some("image_generation"), + call_type: Some(CallTypes::image_generation), quality: None, size: Some("1024-x-1024"), n: None, diff --git a/litellm-rust/crates/cost/tests/python_fal_ai_image_cost.rs b/litellm-rust/crates/cost/tests/python_fal_ai_image_cost.rs index 21399130a1d..3bb02cc1e54 100644 --- a/litellm-rust/crates/cost/tests/python_fal_ai_image_cost.rs +++ b/litellm-rust/crates/cost/tests/python_fal_ai_image_cost.rs @@ -5,6 +5,7 @@ use std::collections::HashMap; use jiff::Timestamp; +use litellm_cost::call_type::CallTypes; use litellm_cost::catalog::ModelInfoCatalog; use litellm_cost::fal_ai_image_cost::{ PIXELS_PER_MEGAPIXEL, cost_calculator, fal_ai_passthrough_cost, @@ -162,7 +163,7 @@ fn catalog_fal_image_route_uses_keyed_row_for_image_edit() { model: "openai/model", provider: Some("fal_ai"), image_response: &response, - call_type: Some("aimage_edit"), + call_type: Some(CallTypes::aimage_edit), quality: None, size: None, n: None, diff --git a/litellm-rust/crates/cost/tests/python_image_cost_router.rs b/litellm-rust/crates/cost/tests/python_image_cost_router.rs index 28d51c990f3..e5c16957f69 100644 --- a/litellm-rust/crates/cost/tests/python_image_cost_router.rs +++ b/litellm-rust/crates/cost/tests/python_image_cost_router.rs @@ -5,6 +5,7 @@ use std::collections::HashMap; use jiff::Timestamp; +use litellm_cost::call_type::CallTypes; use litellm_cost::catalog::ModelInfoCatalog; use litellm_cost::image_cost_router::{ ImageCostRouteRequest, call_type_has_image_response, deployment_pricing, @@ -26,7 +27,7 @@ fn request<'a>( model, provider: Some(provider), image_response: response, - call_type: Some("image_generation"), + call_type: Some(CallTypes::image_generation), quality: None, size: None, n: None, @@ -124,7 +125,7 @@ fn route_google_image_calls_keep_token_and_search_pricing( let cost = route_image_generation_cost_calculator( &catalog, ImageCostRouteRequest { - call_type: Some(call_type), + call_type: call_type.parse::().ok(), ..request("model", provider, &response) }, ) @@ -242,3 +243,64 @@ fn deployment_pricing_reads_only_the_python_pricing_field_set() { ); assert_eq!(pricing.get("future_vendor_cost_per_widget"), None); } + +fn per_size_catalog() -> ModelInfoCatalog { + ModelInfoCatalog::new(HashMap::from([ + ( + "standard/1024-x-1024/dall-e".to_owned(), + json!({"input_cost_per_image": 0.04}), + ), + ( + "standard/512-x-512/dall-e".to_owned(), + json!({"input_cost_per_image": 0.02}), + ), + ( + "hd/1024-x-1024/dall-e".to_owned(), + json!({"input_cost_per_image": 0.08}), + ), + ])) +} + +#[rstest] +#[case::count_from_response_data(json!({"data": [{}, {}]}), None, 0.08)] +#[case::explicit_count_wins(json!({"data": [{}, {}]}), Some(3), 0.12)] +#[case::no_data_bills_no_images(json!({}), None, 0.0)] +#[case::empty_data_bills_no_images(json!({"data": []}), None, 0.0)] +fn route_image_generation_counts_images_like_python( + #[case] response: Value, + #[case] n: Option, + #[case] expected: f64, +) { + let cost = route_image_generation_cost_calculator( + &per_size_catalog(), + ImageCostRouteRequest { + n, + ..request("dall-e", "custom", &response) + }, + ) + .unwrap(); + assert!((cost - expected).abs() < 1e-12); +} + +#[rstest] +#[case::empty_size_falls_through_to_response(Some(""), None, json!({"data": [{}], "size": "512x512"}), json!({}), 0.02)] +#[case::empty_quality_falls_through_to_params(None, Some(""), json!({"data": [{}]}), json!({"quality": "hd"}), 0.08)] +fn route_image_generation_skips_empty_strings_like_python_or( + #[case] size: Option<&str>, + #[case] quality: Option<&str>, + #[case] response: Value, + #[case] params: Value, + #[case] expected: f64, +) { + let cost = route_image_generation_cost_calculator( + &per_size_catalog(), + ImageCostRouteRequest { + size, + quality, + optional_params: ¶ms, + ..request("dall-e", "custom", &response) + }, + ) + .unwrap(); + assert!((cost - expected).abs() < 1e-12); +} diff --git a/litellm-rust/crates/cost/tests/python_openai_image_cost.rs b/litellm-rust/crates/cost/tests/python_openai_image_cost.rs index 45ece5913b5..954d90c4d1b 100644 --- a/litellm-rust/crates/cost/tests/python_openai_image_cost.rs +++ b/litellm-rust/crates/cost/tests/python_openai_image_cost.rs @@ -5,6 +5,7 @@ use std::collections::HashMap; use jiff::Timestamp; +use litellm_cost::call_type::CallTypes; use litellm_cost::catalog::ModelInfoCatalog; use litellm_cost::image_cost_router::{ ImageCostRouteRequest, route_image_generation_cost_calculator, @@ -88,7 +89,7 @@ fn image_router_uses_gpt_image_path_and_deployment_flat_rate(#[case] provider: & model: "gpt-image-model", provider: Some(provider), image_response: &response, - call_type: Some("image_generation"), + call_type: Some(CallTypes::image_generation), quality: None, size: None, n: None,