From a880fdeae07a134df8df38efb3d2959bce54f1f2 Mon Sep 17 00:00:00 2001 From: Yujong Lee Date: Wed, 23 Sep 2026 17:30:10 -0700 Subject: [PATCH] refactor(cost): replace the nineteen nested error enums with one CostError Every leaf and wrapper error type (UsageError, ResponseCostError, the Catalog* wrappers, SpeechCostError, BatchError, OcrCostError, CustomPricingError, A2ACostError, ImageCostRouteError, NonTokenError, and CompletionResponseCostError) collapses into the single tagged CostError in error.rs, whose Display impl is the one mapper the future bridge maps from. The tuple-variant propagation shimmed by the wrappers disappears with them, so ? passes errors through unchanged across module boundaries --- litellm-rust/crates/cost/src/a2a_cost.rs | 18 +- .../crates/cost/src/anthropic_usage.rs | 25 +- .../crates/cost/src/azure_ai_image_cost.rs | 19 +- litellm-rust/crates/cost/src/batch.rs | 20 +- litellm-rust/crates/cost/src/catalog.rs | 296 +++++------------- .../crates/cost/src/completion_cost.rs | 12 +- .../crates/cost/src/completion_input.rs | 5 +- .../crates/cost/src/completion_response.rs | 170 ++++------ .../crates/cost/src/custom_pricing.rs | 29 +- litellm-rust/crates/cost/src/error.rs | 56 +++- .../crates/cost/src/image_cost_router.rs | 27 +- .../crates/cost/src/interactions_usage.rs | 23 +- litellm-rust/crates/cost/src/non_token.rs | 38 +-- litellm-rust/crates/cost/src/ocr_cost.rs | 19 +- litellm-rust/crates/cost/src/openai_cost.rs | 7 +- .../crates/cost/src/openai_image_cost.rs | 7 +- litellm-rust/crates/cost/src/realtime_cost.rs | 35 ++- .../crates/cost/src/responses_usage.rs | 16 +- litellm-rust/crates/cost/src/speech_cost.rs | 14 +- .../crates/cost/src/transcription_usage.rs | 9 +- .../crates/cost/src/usage_dispatch.rs | 17 +- litellm-rust/crates/cost/tests/non_token.rs | 14 +- .../cost/tests/python_anthropic_usage.rs | 5 +- .../crates/cost/tests/python_azure_ai_cost.rs | 6 +- .../crates/cost/tests/python_call_dispatch.rs | 11 +- .../cost/tests/python_completion_cost.rs | 10 +- .../cost/tests/python_completion_response.rs | 15 +- .../cost/tests/python_custom_pricing.rs | 10 +- .../crates/cost/tests/python_model_lookup.rs | 6 +- .../crates/cost/tests/python_openai_cost.rs | 5 +- .../cost/tests/python_responses_usage.rs | 6 +- .../crates/cost/tests/python_speech_cost.rs | 17 +- .../crates/cost/tests/python_together_cost.rs | 6 +- 33 files changed, 383 insertions(+), 590 deletions(-) diff --git a/litellm-rust/crates/cost/src/a2a_cost.rs b/litellm-rust/crates/cost/src/a2a_cost.rs index b2a4b3dccc1..4f33045fe89 100644 --- a/litellm-rust/crates/cost/src/a2a_cost.rs +++ b/litellm-rust/crates/cost/src/a2a_cost.rs @@ -1,15 +1,11 @@ +use crate::error::CostError; use serde_json::Value; -#[derive(Clone, Copy, Debug, Eq, PartialEq)] -pub enum A2ACostError { - InvalidCost, -} - -fn amount(value: &Value) -> Result { +fn amount(value: &Value) -> Result { match value { - Value::Number(number) => number.as_f64().ok_or(A2ACostError::InvalidCost), - Value::String(number) => number.parse().map_err(|_| A2ACostError::InvalidCost), - _ => Err(A2ACostError::InvalidCost), + Value::Number(number) => number.as_f64().ok_or(CostError::InvalidCost), + Value::String(number) => number.parse().map_err(|_| CostError::InvalidCost), + _ => Err(CostError::InvalidCost), } } @@ -17,7 +13,7 @@ pub fn calculate_token_based_cost( details: &Value, input_rate: Option<&Value>, output_rate: Option<&Value>, -) -> Result { +) -> Result { let Some(usage) = details.get("usage") else { return Ok(0.0); }; @@ -42,7 +38,7 @@ pub fn calculate_token_based_cost( Ok(input_tokens * input + output_tokens * output) } -pub fn calculate_a2a_cost(details: Option<&Value>) -> Result { +pub fn calculate_a2a_cost(details: Option<&Value>) -> Result { let Some(details) = details else { return Ok(0.0); }; diff --git a/litellm-rust/crates/cost/src/anthropic_usage.rs b/litellm-rust/crates/cost/src/anthropic_usage.rs index 5e0aebed892..f4e0829eff7 100644 --- a/litellm-rust/crates/cost/src/anthropic_usage.rs +++ b/litellm-rust/crates/cost/src/anthropic_usage.rs @@ -1,9 +1,10 @@ +use crate::error::CostError; use std::collections::BTreeMap; use serde_json::{Map, Value}; use crate::responses_usage::{ - CacheCreationTokenDetails, ChatUsage, CompletionTokenDetails, PromptTokenDetails, UsageError, + CacheCreationTokenDetails, ChatUsage, CompletionTokenDetails, PromptTokenDetails, }; pub fn is_anthropic_usage_object(usage: &Value) -> bool { @@ -15,7 +16,7 @@ pub fn is_anthropic_usage_object(usage: &Value) -> bool { }) } -fn count(value: Option<&Value>) -> Result { +fn count(value: Option<&Value>) -> Result { match value { None | Some(Value::Null) => Ok(0), Some(Value::Number(number)) => number @@ -26,16 +27,16 @@ fn count(value: Option<&Value>) -> Result { .filter(|value| value.is_finite() && *value >= 0.0 && *value < u64::MAX as f64) .map(|value| value as u64) }) - .ok_or(UsageError::InvalidUsage), + .ok_or(CostError::InvalidUsage), Some(Value::Bool(value)) => Ok(u64::from(*value)), _ => Ok(0), } } -fn sum_field(entries: &[&Map], field: &str) -> Result { +fn sum_field(entries: &[&Map], field: &str) -> Result { entries.iter().try_fold(0_u64, |sum, entry| { sum.checked_add(count(entry.get(field))?) - .ok_or(UsageError::TokenCountOverflow) + .ok_or(CostError::TokenCountOverflow) }) } @@ -43,7 +44,7 @@ fn cache_creation_details( usage: &Map, iterations: &[&Map], total_creation: u64, -) -> Result, UsageError> { +) -> Result, CostError> { let breakdowns: Vec<_> = iterations .iter() .filter_map(|entry| entry.get("cache_creation").and_then(Value::as_object)) @@ -55,7 +56,7 @@ fn cache_creation_details( return Ok(Some(CacheCreationTokenDetails { ephemeral_5m_input_tokens: Some( five.checked_add(undetailed) - .ok_or(UsageError::TokenCountOverflow)?, + .ok_or(CostError::TokenCountOverflow)?, ), ephemeral_1h_input_tokens: Some(one), })); @@ -87,8 +88,8 @@ pub fn transform_anthropic_usage_to_chat_usage( usage: &Value, estimated_reasoning_tokens: Option, response_has_thinking_block: bool, -) -> Result { - let object = usage.as_object().ok_or(UsageError::InvalidShape)?; +) -> Result { + let object = usage.as_object().ok_or(CostError::InvalidShape)?; let iterations: Vec<_> = object .get("iterations") @@ -96,7 +97,7 @@ pub fn transform_anthropic_usage_to_chat_usage( .map_or(Ok(Vec::new()), |entries| { entries .iter() - .map(|entry| entry.as_object().ok_or(UsageError::InvalidUsage)) + .map(|entry| entry.as_object().ok_or(CostError::InvalidUsage)) .collect() })?; let fresh = if iterations.is_empty() { @@ -122,10 +123,10 @@ pub fn transform_anthropic_usage_to_chat_usage( let prompt = fresh .checked_add(cache_read) .and_then(|total| total.checked_add(cache_creation)) - .ok_or(UsageError::TokenCountOverflow)?; + .ok_or(CostError::TokenCountOverflow)?; let total = prompt .checked_add(output) - .ok_or(UsageError::TokenCountOverflow)?; + .ok_or(CostError::TokenCountOverflow)?; let reported = if iterations.is_empty() { thinking_tokens(object) } else { diff --git a/litellm-rust/crates/cost/src/azure_ai_image_cost.rs b/litellm-rust/crates/cost/src/azure_ai_image_cost.rs index bb83b77ae0e..7494bb802c3 100644 --- a/litellm-rust/crates/cost/src/azure_ai_image_cost.rs +++ b/litellm-rust/crates/cost/src/azure_ai_image_cost.rs @@ -1,9 +1,10 @@ use jiff::Timestamp; use serde_json::Value; +use crate::error::CostError; use crate::generic_input::get_cost_per_unit; use crate::image_response_cost::calculate_image_response_cost_from_usage; -use crate::non_token::{Error, ImageRates, ImageUsage, calculate_image}; +use crate::non_token::{ImageRates, ImageUsage, calculate_image}; use crate::pricing::Rate; #[derive(Clone, Copy, Debug)] @@ -38,20 +39,24 @@ pub fn input_cost_per_pixel(resolved: &Value, shared_entry: Option<&Value>) -> f .unwrap_or(0.0) } -fn dimensions(size: &str) -> Result<(u32, u32), Error> { +fn dimensions(size: &str) -> Result<(u32, u32), CostError> { let (width, height) = size .split_once("-x-") .or_else(|| size.split_once('x')) - .ok_or(Error::InvalidQuantity)?; - let width = width.parse::().map_err(|_| Error::InvalidQuantity)?; - let height = height.parse::().map_err(|_| Error::InvalidQuantity)?; + .ok_or(CostError::InvalidQuantity)?; + let width = width + .parse::() + .map_err(|_| CostError::InvalidQuantity)?; + let height = height + .parse::() + .map_err(|_| CostError::InvalidQuantity)?; if width == 0 || height == 0 { - return Err(Error::InvalidQuantity); + return Err(CostError::InvalidQuantity); } Ok((width, height)) } -pub fn cost_calculator(request: AzureAiImageRequest<'_>) -> Result { +pub fn cost_calculator(request: AzureAiImageRequest<'_>) -> Result { if let Some(cost) = calculate_image_response_cost_from_usage( request.image_response, request.model_info, diff --git a/litellm-rust/crates/cost/src/batch.rs b/litellm-rust/crates/cost/src/batch.rs index a10115cd66d..a1fdf8b2b02 100644 --- a/litellm-rust/crates/cost/src/batch.rs +++ b/litellm-rust/crates/cost/src/batch.rs @@ -1,3 +1,4 @@ +use crate::error::CostError; use serde_json::Value; use crate::base_rate_selection::uses_inclusive_token_thresholds; @@ -69,13 +70,6 @@ pub struct BatchCost { pub completion: f64, } -#[derive(Clone, Copy, Debug, Eq, PartialEq)] -pub enum BatchError { - InvalidRate, - InvalidMultiplier, - NonFiniteCost, -} - fn value(rate: Rate) -> Option { rate.value() } @@ -121,7 +115,7 @@ fn valid_rate(rate: Rate) -> bool { value(rate).is_none_or(|rate| rate.is_finite() && rate >= 0.0) } -fn validate(pricing: &BatchPricing<'_>) -> Result<(), BatchError> { +fn validate(pricing: &BatchPricing<'_>) -> Result<(), CostError> { let rates = [ pricing.batch.input, pricing.batch.output, @@ -147,10 +141,10 @@ fn validate(pricing: &BatchPricing<'_>) -> Result<(), BatchError> { .any(|rate| !valid_rate(rate)) }) { - return Err(BatchError::InvalidRate); + return Err(CostError::InvalidRate); } if !pricing.regional_uplift.is_finite() || pricing.regional_uplift < 0.0 { - return Err(BatchError::InvalidMultiplier); + return Err(CostError::InvalidMultiplier); } Ok(()) } @@ -158,7 +152,7 @@ fn validate(pricing: &BatchPricing<'_>) -> Result<(), BatchError> { pub fn batch_cost_calculator( pricing: &BatchPricing<'_>, usage: BatchUsage, -) -> Result { +) -> Result { validate(pricing)?; let batch = get_batch_cost_rates(pricing, usage.prompt_tokens); let prompt = if let Some(input) = value(batch.input) { @@ -209,7 +203,7 @@ pub fn batch_cost_calculator( completion: completion * pricing.regional_uplift, }; if !result.prompt.is_finite() || !result.completion.is_finite() { - return Err(BatchError::NonFiniteCost); + return Err(CostError::NonFiniteCost); } Ok(result) } @@ -298,7 +292,7 @@ pub fn batch_cost_from_model_info( usage: &ChatUsage, provider: Option<&str>, data_residency: Option<&str>, -) -> Result { +) -> Result { let policy = if uses_inclusive_token_thresholds(provider) { ThresholdPolicy::Inclusive } else { diff --git a/litellm-rust/crates/cost/src/catalog.rs b/litellm-rust/crates/cost/src/catalog.rs index 0cca49fe709..0cb9af0212e 100644 --- a/litellm-rust/crates/cost/src/catalog.rs +++ b/litellm-rust/crates/cost/src/catalog.rs @@ -13,7 +13,7 @@ use crate::azure_ai_image_cost::{ }; use crate::azure_cost::output_per_second_cost; use crate::base_rate_selection::uses_inclusive_token_thresholds; -use crate::batch::{BatchError, batch_cost_from_model_info}; +use crate::batch::batch_cost_from_model_info; use crate::bedrock_image_cost::cost_calculator as bedrock_image_cost_calculator; use crate::billed_token_rates::{ BilledRatesRequest, BilledTokenRates, TokenTypeCostBreakdown, @@ -21,16 +21,15 @@ use crate::billed_token_rates::{ get_token_type_cost_breakdown as calculate_token_type_cost_breakdown, }; use crate::completion_cost::{ - CompletionCost, ResponseCostError, completion_cost, get_response_cost_from_hidden_params, + CompletionCost, completion_cost, get_response_cost_from_hidden_params, }; use crate::completion_input::{ CompletionInputRequest, PreparedCompletionInput, prepare_completion_input, }; -use crate::custom_pricing::{ - CustomPricing, CustomPricingError, CustomTokenRates, cost_from_chat_usage, -}; +use crate::custom_pricing::{CustomPricing, CustomTokenRates, cost_from_chat_usage}; use crate::dashscope_cost::cost_per_token as dashscope_cost_per_token; use crate::databricks_cost::registry_key as databricks_registry_key; +use crate::error::CostError; use crate::fal_ai_image_cost::{ cost_calculator as fal_ai_image_cost_calculator, fal_ai_passthrough_cost as calculate_fal_ai_passthrough_cost, @@ -47,8 +46,8 @@ use crate::image_response_cost::{ use crate::model_selection::{ ModelSelectionRequest, get_provider_for_cost_calc, select_model_name_for_cost_calc, }; -use crate::non_token::{Error as NonTokenError, ImageRates, ImageUsage, calculate_image}; -use crate::ocr_cost::{OcrCostError, ocr_cost}; +use crate::non_token::{ImageRates, ImageUsage, calculate_image}; +use crate::ocr_cost::ocr_cost; use crate::openai_cost::video_generation_cost as calculate_video_generation_cost; use crate::openai_image_cost::cost_calculator as openai_image_cost_calculator; use crate::per_second::{has_token_or_tiered_pricing, per_second_pricing_cost}; @@ -63,15 +62,15 @@ use crate::realtime_cost::{ partition_results_by_service_tier, transcription_usage_cost, }; use crate::regional_uplift::get_provider_specific_geo_multiplier; -use crate::responses_usage::{ChatUsage, UsageError}; +use crate::responses_usage::ChatUsage; use crate::retrieval_cost::{rerank_cost, vector_store_search_cost}; use crate::search_cost::{ ParallelAiPricing, effective_mode, parallel_ai_search_cost, provider_usage, search_provider_cost_per_query, }; use crate::speech_cost::{ - SpeechCostError, SpeechCostMetric, cost_per_second, generic_cost_per_character, - lyria_generation_cost, select_cost_metric_for_model, transcription_usage_has_token_details, + SpeechCostMetric, cost_per_second, generic_cost_per_character, lyria_generation_cost, + select_cost_metric_for_model, transcription_usage_has_token_details, }; use crate::together_cost::{ TogetherThresholds, get_model_params_and_category, has_together_registry_pricing, @@ -177,130 +176,6 @@ pub struct DefaultImageCostRequest<'a> { pub supplied_model_info: Option<&'a Value>, } -#[derive(Clone, Copy, Debug, Eq, PartialEq)] -pub enum CatalogError { - ModelNotFound, -} - -#[derive(Clone, Copy, Debug, Eq, PartialEq)] -pub enum CatalogResponseError { - Catalog(CatalogError), - ProviderCost(ResponseCostError), -} - -#[derive(Clone, Copy, Debug, Eq, PartialEq)] -pub enum RealtimeCostError { - Catalog(CatalogError), - Usage(UsageError), -} - -#[derive(Clone, Copy, Debug, Eq, PartialEq)] -pub enum CatalogSpeechError { - Catalog(CatalogError), - Speech(SpeechCostError), -} - -#[derive(Clone, Copy, Debug, Eq, PartialEq)] -pub enum CatalogCallError { - Catalog(CatalogError), - Speech(CatalogSpeechError), - Ocr(OcrCostError), - Batch(BatchError), - MissingProvider, -} - -#[derive(Clone, Copy, Debug, Eq, PartialEq)] -pub enum CatalogCustomError { - Pricing(CustomPricingError), - Catalog(CatalogError), - Call(CatalogCallError), -} - -#[derive(Clone, Copy, Debug, Eq, PartialEq)] -pub enum CatalogImageError { - Catalog(CatalogError), - Pricing(NonTokenError), - Usage(UsageError), -} - -impl From for CatalogImageError { - fn from(value: CatalogError) -> Self { - Self::Catalog(value) - } -} - -impl From for CatalogImageError { - fn from(value: NonTokenError) -> Self { - Self::Pricing(value) - } -} - -impl From for CatalogImageError { - fn from(value: UsageError) -> Self { - Self::Usage(value) - } -} - -impl From for CatalogSpeechError { - fn from(value: CatalogError) -> Self { - Self::Catalog(value) - } -} - -impl From for CatalogCallError { - fn from(value: CatalogError) -> Self { - Self::Catalog(value) - } -} - -impl From for CatalogCallError { - fn from(value: CatalogSpeechError) -> Self { - Self::Speech(value) - } -} - -impl From for CatalogCallError { - fn from(value: OcrCostError) -> Self { - Self::Ocr(value) - } -} - -impl From for CatalogCallError { - fn from(value: BatchError) -> Self { - Self::Batch(value) - } -} - -impl From for CatalogSpeechError { - fn from(value: SpeechCostError) -> Self { - Self::Speech(value) - } -} - -impl From for RealtimeCostError { - fn from(value: CatalogError) -> Self { - Self::Catalog(value) - } -} - -impl From for RealtimeCostError { - fn from(value: UsageError) -> Self { - Self::Usage(value) - } -} - -impl From for CatalogResponseError { - fn from(value: CatalogError) -> Self { - Self::Catalog(value) - } -} - -impl From for CatalogResponseError { - fn from(value: ResponseCostError) -> Self { - Self::ProviderCost(value) - } -} - fn select_model_key<'a, T>( entries: &'a HashMap, model: &str, @@ -434,7 +309,7 @@ impl ModelInfoCatalog { pub fn prepare_completion_input( &self, request: CompletionInputRequest<'_>, - ) -> Result { + ) -> Result { prepare_completion_input(request, &self.entries) } @@ -442,14 +317,10 @@ impl ModelInfoCatalog { &self, request: ModelCostRequest<'_>, pricing: CustomPricing, - ) -> Result<(f64, f64), CatalogCustomError> { - match cost_from_chat_usage(request.usage, pricing, request.response_time_ms) - .map_err(CatalogCustomError::Pricing)? - { + ) -> Result<(f64, f64), CostError> { + match cost_from_chat_usage(request.usage, pricing, request.response_time_ms)? { Some(cost) => Ok((cost.input, cost.output)), - None => self - .cost_per_token(request) - .map_err(CatalogCustomError::Catalog), + None => self.cost_per_token(request), } } @@ -458,14 +329,10 @@ impl ModelInfoCatalog { request: ModelCostRequest<'_>, call: CostCall<'_>, pricing: CustomPricing, - ) -> Result<(f64, f64), CatalogCustomError> { - match cost_from_chat_usage(request.usage, pricing, request.response_time_ms) - .map_err(CatalogCustomError::Pricing)? - { + ) -> Result<(f64, f64), CostError> { + match cost_from_chat_usage(request.usage, pricing, request.response_time_ms)? { Some(cost) => Ok((cost.input, cost.output)), - None => self - .cost_per_token_for_call(request, call) - .map_err(CatalogCustomError::Call), + None => self.cost_per_token_for_call(request, call), } } @@ -473,7 +340,7 @@ impl ModelInfoCatalog { &self, request: ModelCostRequest<'_>, call: CostCall<'_>, - ) -> Result<(f64, f64), CatalogCallError> { + ) -> Result<(f64, f64), CostError> { let provider = request.provider.and_then(LlmProviders::parse); match call { CostCall::Token { @@ -483,12 +350,12 @@ impl ModelInfoCatalog { request_model, } => { if provider == Some(LlmProviders::VERTEX_AI) { - return Ok(self.vertex_cost( + return self.vertex_cost( request, call_type, prompt_characters, completion_characters, - )?); + ); } if provider == Some(LlmProviders::TOGETHER_AI) && matches!( @@ -497,10 +364,10 @@ impl ModelInfoCatalog { | Some(crate::call_type::CallTypes::aembedding) ) { - return Ok(self.together_ai_cost_per_token(request, call_type)?); + return self.together_ai_cost_per_token(request, call_type); } if provider == Some(LlmProviders::AZURE_AI) { - return Ok(self.azure_ai_cost_per_token(request, request_model)?); + return self.azure_ai_cost_per_token(request, request_model); } Ok(self.cost_per_token(request)?) } @@ -511,11 +378,11 @@ impl ModelInfoCatalog { Ok(self.transcription_cost(request, duration_seconds)?) } CostCall::Rerank { billed_units } => { - let provider = request.provider.ok_or(CatalogCallError::MissingProvider)?; + let provider = request.provider.ok_or(CostError::MissingProvider)?; Ok(self.rerank_cost(request.model, provider, request.region, billed_units)) } CostCall::VectorStoreSearch { api_type } => { - let provider = request.provider.ok_or(CatalogCallError::MissingProvider)?; + let provider = request.provider.ok_or(CostError::MissingProvider)?; Ok(self.vector_store_search_cost(provider, api_type)) } CostCall::Search { @@ -575,7 +442,7 @@ impl ModelInfoCatalog { call_type: &str, prompt_characters: Option, completion_characters: Option, - ) -> Result<(f64, f64), CatalogError> { + ) -> Result<(f64, f64), CostError> { if let Some(cost) = self .select_model_key(request.model, request.provider, request.region) .and_then(|key| self.entries.get(key)) @@ -590,7 +457,7 @@ impl ModelInfoCatalog { } let key = self .select_model_key(request.model, request.provider, request.region) - .ok_or(CatalogError::ModelNotFound)?; + .ok_or(CostError::ModelNotFound)?; Ok(vertex_cost_per_character( request.model, request.usage, @@ -605,7 +472,7 @@ impl ModelInfoCatalog { pub fn databricks_cost_per_token( &self, request: ModelCostRequest<'_>, - ) -> Result<(f64, f64), CatalogError> { + ) -> Result<(f64, f64), CostError> { if let Some(cost) = self .select_model_key(request.model, Some("databricks"), request.region) .and_then(|key| self.entries.get(key)) @@ -619,7 +486,7 @@ impl ModelInfoCatalog { Some("databricks"), request.region, ) - .ok_or(CatalogError::ModelNotFound)?; + .ok_or(CostError::ModelNotFound)?; Ok(calculate_generic_cost_from_model_info_with_region( request.usage, &self.entries[key], @@ -639,7 +506,7 @@ impl ModelInfoCatalog { &self, request: ModelCostRequest<'_>, request_model: Option<&str>, - ) -> Result<(f64, f64), CatalogError> { + ) -> Result<(f64, f64), CostError> { let model_info = self .select_model_key(request.model, Some("azure_ai"), request.region) .and_then(|key| self.entries.get(key)); @@ -659,7 +526,7 @@ impl ModelInfoCatalog { request.at, ), None if is_azure_model_router(request.model) => (0.0, 0.0), - None => return Err(CatalogError::ModelNotFound), + None => return Err(CostError::ModelNotFound), }; let fee = self .azure_ai_router_fee(request.model, request_model, request.usage.prompt_tokens)? @@ -672,14 +539,14 @@ impl ModelInfoCatalog { model: &str, request_model: Option<&str>, prompt_tokens: u64, - ) -> Result, CatalogError> { + ) -> Result, CostError> { let Some(fee_name) = router_fee_name(model, request_model) else { return Ok(None); }; let fee_entry = router_fee_entry_name(fee_name); let fee_key = self .select_model_key(fee_entry, Some("azure_ai"), None) - .ok_or(CatalogError::ModelNotFound)?; + .ok_or(CostError::ModelNotFound)?; let fee = calculate_azure_model_router_flat_cost(fee_name, prompt_tokens, &self.entries[fee_key]); Ok((fee > 0.0).then_some(fee)) @@ -689,10 +556,10 @@ impl ModelInfoCatalog { &self, request: ModelCostRequest<'_>, prompt_characters: Option, - ) -> Result<(f64, f64), CatalogSpeechError> { + ) -> Result<(f64, f64), CostError> { let key = self .select_model_key(request.model, request.provider, request.region) - .ok_or(CatalogError::ModelNotFound)?; + .ok_or(CostError::ModelNotFound)?; let model_info = &self.entries[key]; if matches!(request.provider, Some("vertex_ai" | "vertex_ai_beta")) && let Some(cost) = lyria_generation_cost(model_info) @@ -701,12 +568,11 @@ impl ModelInfoCatalog { } match select_cost_metric_for_model(model_info)? { SpeechCostMetric::PerCharacter => { - let characters = - prompt_characters.ok_or(SpeechCostError::MissingPromptCharacters)?; + let characters = prompt_characters.ok_or(CostError::MissingPromptCharacters)?; let (prompt, completion) = generic_cost_per_character(model_info, characters, 0.0, None, Some(0.0)); Ok(( - prompt.ok_or(SpeechCostError::MissingInputCharacterRate)?, + prompt.ok_or(CostError::MissingInputCharacterRate)?, completion.unwrap_or(0.0), )) } @@ -718,10 +584,10 @@ impl ModelInfoCatalog { &self, request: ModelCostRequest<'_>, duration_seconds: f64, - ) -> Result<(f64, f64), CatalogError> { + ) -> Result<(f64, f64), CostError> { let key = self .select_model_key(request.model, request.provider, request.region) - .ok_or(CatalogError::ModelNotFound)?; + .ok_or(CostError::ModelNotFound)?; let model_info = &self.entries[key]; if transcription_usage_has_token_details(request.usage) { return Ok(calculate_generic_cost_from_model_info_with_region( @@ -817,7 +683,7 @@ impl ModelInfoCatalog { region: Option<&str>, data_residency: Option<&str>, at: Timestamp, - ) -> Result { + ) -> Result { partition_results_by_service_tier(results) .into_iter() .map(|(service_tier, events)| { @@ -843,10 +709,7 @@ impl ModelInfoCatalog { .sum() } - pub fn cost_per_token( - &self, - request: ModelCostRequest<'_>, - ) -> Result<(f64, f64), CatalogError> { + pub fn cost_per_token(&self, request: ModelCostRequest<'_>) -> Result<(f64, f64), CostError> { if let Some(cost) = self .select_model_key(request.model, request.provider, request.region) .and_then(|key| self.entries.get(key)) @@ -896,7 +759,7 @@ impl ModelInfoCatalog { self.select_model_key(category, request.provider, request.region) }) }) - .ok_or(CatalogError::ModelNotFound)?; + .ok_or(CostError::ModelNotFound)?; let model_info = apply_provider_cache_read_default(&self.entries[key], request.provider); if let Some(cost) = per_second_pricing_cost(&model_info, request.response_time_ms) { return Ok(cost); @@ -998,7 +861,7 @@ impl ModelInfoCatalog { &self, request: ModelCostRequest<'_>, call_type: &str, - ) -> Result<(f64, f64), CatalogError> { + ) -> Result<(f64, f64), CostError> { if has_together_registry_pricing(request.model, &self.entries) { return self.cost_per_token(request); } @@ -1089,12 +952,12 @@ impl ModelInfoCatalog { image_response: &Value, supplied_model_info: Option<&Value>, at: Timestamp, - ) -> Result { + ) -> Result { let shared = self .select_model_key(model, Some(provider), None) .and_then(|key| self.entries.get(key)); let model_info = resolve_image_model_info(shared, supplied_model_info) - .ok_or(CatalogError::ModelNotFound)?; + .ok_or(CostError::ModelNotFound)?; match provider { "gemini" => Ok(gemini_image_generation_cost( image_response, @@ -1106,7 +969,7 @@ impl ModelInfoCatalog { &model_info, at, )), - _ => Err(CatalogError::ModelNotFound), + _ => Err(CostError::ModelNotFound), } } @@ -1117,38 +980,38 @@ impl ModelInfoCatalog { image_response: &Value, supplied_model_info: Option<&Value>, at: Timestamp, - ) -> Result { + ) -> Result { let shared = self .select_model_key(model, Some(provider), None) .and_then(|key| self.entries.get(key)); match provider { "gemini" => { let info = resolve_image_model_info(shared, supplied_model_info) - .ok_or(CatalogError::ModelNotFound)?; + .ok_or(CostError::ModelNotFound)?; Ok(gemini_image_edit_cost(image_response, &info, at)) } "vertex_ai" => shared .map(|info| vertex_image_edit_cost(image_response, info)) - .ok_or(CatalogError::ModelNotFound), - _ => Err(CatalogError::ModelNotFound), + .ok_or(CostError::ModelNotFound), + _ => Err(CostError::ModelNotFound), } } pub fn azure_ai_image_generation_cost( &self, request: AzureAiImageCatalogRequest<'_>, - ) -> Result { + ) -> Result { let shared = self .select_model_key(request.model, Some("azure_ai"), None) .and_then(|key| self.entries.get(key)); let model_info = resolve_image_model_info(shared, request.supplied_model_info) - .ok_or(CatalogError::ModelNotFound)?; + .ok_or(CostError::ModelNotFound)?; let shared_pricing = model_info .get("key") .and_then(Value::as_str) .and_then(|key| self.entries.get(key)) .or(shared); - Ok(azure_ai_image_cost_calculator(AzureAiImageRequest { + azure_ai_image_cost_calculator(AzureAiImageRequest { image_response: request.image_response, model_info: &model_info, supplied_model_info: request.supplied_model_info, @@ -1157,7 +1020,7 @@ impl ModelInfoCatalog { n: request.n, optional_params: request.optional_params, at: request.at, - })?) + }) } pub fn flat_image_generation_cost( @@ -1166,12 +1029,12 @@ impl ModelInfoCatalog { provider: &str, image_response: &Value, supplied_model_info: Option<&Value>, - ) -> Result { + ) -> Result { let shared = self .select_model_key(model, Some(provider), None) .and_then(|key| self.entries.get(key)); let model_info = resolve_image_model_info(shared, supplied_model_info) - .ok_or(CatalogError::ModelNotFound)?; + .ok_or(CostError::ModelNotFound)?; Ok(flat_image_cost(image_response, &model_info)) } @@ -1182,18 +1045,13 @@ impl ModelInfoCatalog { image_response: &Value, supplied_model_info: Option<&Value>, at: Timestamp, - ) -> Result { + ) -> Result { let shared = self .select_model_key(model, Some(provider), None) .and_then(|key| self.entries.get(key)); let model_info = resolve_image_model_info(shared, supplied_model_info) - .ok_or(CatalogError::ModelNotFound)?; - Ok(openai_image_cost_calculator( - image_response, - &model_info, - provider, - at, - )?) + .ok_or(CostError::ModelNotFound)?; + openai_image_cost_calculator(image_response, &model_info, provider, at) } pub fn fal_ai_image_generation_cost( @@ -1202,7 +1060,7 @@ impl ModelInfoCatalog { image_response: &Value, optional_params: &Value, deployment_prices: Option<&Value>, - ) -> Result { + ) -> Result { fal_ai_image_cost_calculator( model, image_response, @@ -1210,7 +1068,7 @@ impl ModelInfoCatalog { deployment_prices, &self.entries, ) - .ok_or(CatalogError::ModelNotFound) + .ok_or(CostError::ModelNotFound) } pub fn fal_ai_passthrough_cost(&self, model: &str, request_body: &Value) -> Option { @@ -1227,9 +1085,9 @@ impl ModelInfoCatalog { image_response: &Value, size: Option<&str>, optional_params: &Value, - ) -> Result { + ) -> Result { bedrock_image_cost_calculator(model, image_response, size, optional_params, &self.entries) - .ok_or(CatalogError::ModelNotFound) + .ok_or(CostError::ModelNotFound) } pub fn video_generation_cost( @@ -1239,39 +1097,33 @@ impl ModelInfoCatalog { deployment_info: Option<&Value>, duration_seconds: f64, video_resolution: Option<&str>, - ) -> Result { + ) -> Result { let model_info = deployment_info .or_else(|| { self.select_model_key(model, provider, None) .and_then(|key| self.entries.get(key)) }) - .ok_or(CatalogError::ModelNotFound)?; - Ok(calculate_video_generation_cost( - model_info, - duration_seconds, - video_resolution, - )?) + .ok_or(CostError::ModelNotFound)?; + calculate_video_generation_cost(model_info, duration_seconds, video_resolution) } pub fn default_image_cost_calculator( &self, request: DefaultImageCostRequest<'_>, - ) -> Result { + ) -> Result { let raw_size = request.size.unwrap_or("1024-x-1024"); let size = if raw_size.contains("-x-") { raw_size.to_owned() } else { raw_size.replace('x', "-x-") }; - let (height, width) = size - .split_once("-x-") - .ok_or(NonTokenError::InvalidQuantity)?; + let (height, width) = size.split_once("-x-").ok_or(CostError::InvalidQuantity)?; let height = height .parse::() - .map_err(|_| NonTokenError::InvalidQuantity)?; + .map_err(|_| CostError::InvalidQuantity)?; let width = width .parse::() - .map_err(|_| NonTokenError::InvalidQuantity)?; + .map_err(|_| CostError::InvalidQuantity)?; let provider_prefix = request.provider.map(|provider| format!("{provider}/")); let without_provider = provider_prefix .as_deref() @@ -1305,7 +1157,7 @@ impl ModelInfoCatalog { .flatten() .find_map(|candidate| self.entries.get(candidate)); if shared.is_none() && request.supplied_model_info.is_none() { - return Err(CatalogError::ModelNotFound.into()); + return Err(CostError::ModelNotFound); } let rate = |info: &Value, key: &str| { crate::generic_input::get_cost_per_unit(info, key, None) @@ -1354,7 +1206,7 @@ impl ModelInfoCatalog { provider: Option<&str>, number_of_queries: u64, optional_params: &Value, - ) -> Result<(f64, f64), CatalogError> { + ) -> Result<(f64, f64), CostError> { if provider == Some("parallel_ai") { let pricing_model = match effective_mode(optional_params) { "fast" => "parallel_ai/search-fast", @@ -1364,7 +1216,7 @@ impl ModelInfoCatalog { let model_info = self .entries .get(pricing_model) - .ok_or(CatalogError::ModelNotFound)?; + .ok_or(CostError::ModelNotFound)?; let request_cost = model_info .get("input_cost_per_query") .and_then(|value| match value { @@ -1389,7 +1241,7 @@ impl ModelInfoCatalog { let model_info = self .select_model_key(model, provider, None) .and_then(|key| self.entries.get(key)) - .ok_or(CatalogError::ModelNotFound)?; + .ok_or(CostError::ModelNotFound)?; Ok(search_provider_cost_per_query( model_info, number_of_queries, @@ -1413,7 +1265,7 @@ impl ModelInfoCatalog { pub fn completion_cost( &self, request: CompletionCostRequest<'_>, - ) -> Result { + ) -> Result { let (prompt, output) = self.cost_per_token(request.token)?; let built_in_tools = match request.built_in_tools { BuiltInToolCharge::Provided(cost) => cost, @@ -1438,7 +1290,7 @@ impl ModelInfoCatalog { pub fn response_cost_calculator( &self, request: ResponseCostRequest<'_>, - ) -> Result { + ) -> Result { if request.cache_hit { return Ok(0.0); } diff --git a/litellm-rust/crates/cost/src/completion_cost.rs b/litellm-rust/crates/cost/src/completion_cost.rs index 47151e04bee..d87d7b4101f 100644 --- a/litellm-rust/crates/cost/src/completion_cost.rs +++ b/litellm-rust/crates/cost/src/completion_cost.rs @@ -1,3 +1,4 @@ +use crate::error::CostError; use serde_json::Value; fn number(value: &Value) -> Option { @@ -89,14 +90,9 @@ pub fn completion_cost( } } -#[derive(Clone, Copy, Debug, Eq, PartialEq)] -pub enum ResponseCostError { - InvalidProviderCost, -} - pub fn get_response_cost_from_hidden_params( hidden_params: &Value, -) -> Result, ResponseCostError> { +) -> Result, CostError> { let Some(value) = hidden_params .get("additional_headers") .and_then(|headers| headers.get("llm_provider-x-litellm-response-cost")) @@ -108,14 +104,14 @@ pub fn get_response_cost_from_hidden_params( } number(value) .map(Some) - .ok_or(ResponseCostError::InvalidProviderCost) + .ok_or(CostError::InvalidProviderCost) } pub fn response_cost_calculator( cache_hit: bool, hidden_params: &Value, calculated_completion_cost: f64, -) -> Result { +) -> Result { if cache_hit { return Ok(0.0); } diff --git a/litellm-rust/crates/cost/src/completion_input.rs b/litellm-rust/crates/cost/src/completion_input.rs index b5df47f06dd..a0e704c1f58 100644 --- a/litellm-rust/crates/cost/src/completion_input.rs +++ b/litellm-rust/crates/cost/src/completion_input.rs @@ -2,10 +2,11 @@ use std::collections::HashMap; use serde_json::Value; +use crate::error::CostError; use crate::model_selection::{ ModelSelectionRequest, get_response_model, select_model_name_for_cost_calc, }; -use crate::responses_usage::{ChatUsage, UsageError}; +use crate::responses_usage::ChatUsage; use crate::usage_dispatch::get_usage_object; #[derive(Clone, Copy, Debug, Eq, PartialEq)] @@ -104,7 +105,7 @@ pub fn select_service_tier( pub fn prepare_completion_input( request: CompletionInputRequest<'_>, cost_map: &HashMap, -) -> Result { +) -> Result { let response = request.model_selection.response; let call_type = infer_call_type(request.call_type, request.response_kind).unwrap_or("completion"); diff --git a/litellm-rust/crates/cost/src/completion_response.rs b/litellm-rust/crates/cost/src/completion_response.rs index 59c5babfa3a..d41cb60e310 100644 --- a/litellm-rust/crates/cost/src/completion_response.rs +++ b/litellm-rust/crates/cost/src/completion_response.rs @@ -4,20 +4,18 @@ use jiff::Timestamp; use litellm_token_counter::{CountableRequest, TokenCounter}; use serde_json::Value; -use crate::a2a_cost::{A2ACostError, calculate_a2a_cost}; +use crate::a2a_cost::calculate_a2a_cost; use crate::azure_ai_cost::is_azure_model_router; use crate::billed_token_rates::TokenTypeCostBreakdown; -use crate::catalog::{ - CatalogCallError, CatalogError, CatalogImageError, CostCall, ModelCostRequest, ModelInfoCatalog, -}; +use crate::catalog::{CostCall, ModelCostRequest, ModelInfoCatalog}; use crate::completion_cost::{ - CompletionCost, ResponseCostError, completion_cost, get_response_cost_from_hidden_params, + CompletionCost, completion_cost, get_response_cost_from_hidden_params, }; use crate::completion_input::{CompletionInputRequest, PreparedCompletionInput, ResponseKind}; -use crate::custom_pricing::{CustomPricing, CustomPricingError, cost_from_chat_usage}; +use crate::custom_pricing::{CustomPricing, cost_from_chat_usage}; +use crate::error::CostError; use crate::image_cost_router::{ - ImageCostRouteError, ImageCostRouteRequest, call_type_has_image_response, - route_image_generation_cost_calculator, + ImageCostRouteRequest, call_type_has_image_response, route_image_generation_cost_calculator, }; use crate::mcp_cost::calculate_mcp_tool_call_cost; use crate::per_second::{DEFAULT_REPLICATE_GPU_PRICE_PER_SECOND, get_replicate_completion_pricing}; @@ -25,7 +23,7 @@ use crate::realtime_cost::{ collect_and_combine_usage_from_realtime_stream_results, combine_usage_objects, event_usage, partition_results_by_service_tier, }; -use crate::responses_usage::{ChatUsage, UsageError}; +use crate::responses_usage::ChatUsage; use crate::speech_cost::count_characters; use crate::tool_call_cost_tracking::{DefaultToolRates, ResponseKind as ToolResponseKind}; use crate::tool_cost_dispatch::BuiltInToolCostRequest; @@ -84,29 +82,6 @@ pub struct PricedCompletionResponse { pub named_additional_costs: BTreeMap, } -#[derive(Clone, Copy, Debug, Eq, PartialEq)] -pub enum CompletionResponseCostError { - Usage(UsageError), - MissingUsage, - MissingModel, - MissingProvider, - UnsupportedCallType, - Cost(CatalogCallError), - Image(ImageCostRouteError), - Video(CatalogImageError), - Realtime(CatalogError), - A2A(A2ACostError), - TokenCount, - CustomPricing(CustomPricingError), - ProviderCost(ResponseCostError), -} - -impl From for CompletionResponseCostError { - fn from(value: UsageError) -> Self { - Self::Usage(value) - } -} - #[derive(Clone, Copy, Debug, Eq, PartialEq)] enum CandidatePriceError { MissingModel, @@ -145,20 +120,18 @@ fn price_with_custom( usage: &ChatUsage, pricing: CustomPricing, response_time_ms: Option, - catalog_price: impl Fn(&str) -> Result<(f64, f64), CompletionResponseCostError>, -) -> Result<(String, (f64, f64)), CompletionResponseCostError> { - match cost_from_chat_usage(usage, pricing, response_time_ms) - .map_err(CompletionResponseCostError::CustomPricing)? - { + catalog_price: impl Fn(&str) -> Result<(f64, f64), CostError>, +) -> Result<(String, (f64, f64)), CostError> { + match cost_from_chat_usage(usage, pricing, response_time_ms)? { Some(cost) => candidates .iter() .flatten() .next() .cloned() .map(|model| (model, (cost.input, cost.output))) - .ok_or(CompletionResponseCostError::MissingModel), + .ok_or(CostError::MissingModel), None => price_candidates(candidates, catalog_price).map_err(|error| match error { - CandidatePriceError::MissingModel => CompletionResponseCostError::MissingModel, + CandidatePriceError::MissingModel => CostError::MissingModel, CandidatePriceError::Price(error) => error, }), } @@ -186,41 +159,33 @@ fn number(value: Option<&Value>) -> Option { } } -fn count_text_usage( - input: CompletionTextInput<'_>, -) -> Result { +fn count_text_usage(input: CompletionTextInput<'_>) -> Result { let prompt = match input.messages { Some(Value::Array(messages)) if !messages.is_empty() => { let body = serde_json::to_vec(&serde_json::json!({"messages": messages})) - .map_err(|_| CompletionResponseCostError::TokenCount)?; - let request = CountableRequest::parse(&body) - .map_err(|_| CompletionResponseCostError::TokenCount)?; + .map_err(|_| CostError::TokenCount)?; + let request = CountableRequest::parse(&body).map_err(|_| CostError::TokenCount)?; input .counter .count_request(&request) - .map_err(|_| CompletionResponseCostError::TokenCount)? + .map_err(|_| CostError::TokenCount)? .input_tokens } Some(Value::Array(_)) | None => input .counter .count_text(input.prompt) - .map_err(|_| CompletionResponseCostError::TokenCount)?, - Some(_) => return Err(CompletionResponseCostError::TokenCount), + .map_err(|_| CostError::TokenCount)?, + Some(_) => return Err(CostError::TokenCount), }; let completion = input .counter .count_text(input.completion) - .map_err(|_| CompletionResponseCostError::TokenCount)?; - let prompt_tokens = u64::try_from(prompt) - .map_err(|_| CompletionResponseCostError::Usage(UsageError::TokenCountOverflow))?; - let completion_tokens = u64::try_from(completion) - .map_err(|_| CompletionResponseCostError::Usage(UsageError::TokenCountOverflow))?; - let total_tokens = - prompt_tokens - .checked_add(completion_tokens) - .ok_or(CompletionResponseCostError::Usage( - UsageError::TokenCountOverflow, - ))?; + .map_err(|_| CostError::TokenCount)?; + let prompt_tokens = u64::try_from(prompt).map_err(|_| CostError::TokenCountOverflow)?; + let completion_tokens = u64::try_from(completion).map_err(|_| CostError::TokenCountOverflow)?; + let total_tokens = prompt_tokens + .checked_add(completion_tokens) + .ok_or(CostError::TokenCountOverflow)?; Ok(ChatUsage { prompt_tokens, completion_tokens, @@ -268,7 +233,7 @@ fn cost_call<'a>( model: &'a str, request_model: Option<&'a str>, empty_params: &'a Value, -) -> Result, CompletionResponseCostError> { +) -> Result, CostError> { match call_type { "speech" | "aspeech" => Ok(CostCall::Speech { prompt_characters: request.prompt_characters.or_else(|| { @@ -301,7 +266,7 @@ fn cost_call<'a>( .input .model_selection .response - .ok_or(CompletionResponseCostError::MissingUsage)?, + .ok_or(CostError::MissingUsage)?, deployment_info: request .input .model_selection @@ -327,7 +292,7 @@ fn cost_call<'a>( request_model, }) } - _ => Err(CompletionResponseCostError::UnsupportedCallType), + _ => Err(CostError::UnsupportedCallType), } } @@ -337,12 +302,12 @@ fn price_image_response( prepared: PreparedCompletionInput, provider: Option<&str>, deployment_info: Option<&Value>, -) -> Result { +) -> Result { let response = request .input .model_selection .response - .ok_or(CompletionResponseCostError::MissingUsage)?; + .ok_or(CostError::MissingUsage)?; let empty_params = Value::Null; let (model, total) = price_candidates(&prepared.model_candidates, |model| { route_image_generation_cost_calculator( @@ -362,8 +327,8 @@ fn price_image_response( ) }) .map_err(|error| match error { - CandidatePriceError::MissingModel => CompletionResponseCostError::MissingModel, - CandidatePriceError::Price(error) => CompletionResponseCostError::Image(error), + CandidatePriceError::MissingModel => CostError::MissingModel, + CandidatePriceError::Price(error) => error, })?; Ok(flat_priced(prepared, model, total)) } @@ -386,7 +351,7 @@ fn price_video_response( prepared: PreparedCompletionInput, provider: Option<&str>, deployment_info: Option<&Value>, -) -> Result { +) -> Result { let usage = request .input .model_selection @@ -401,7 +366,7 @@ fn price_video_response( .iter() .flatten() .next() - .ok_or(CompletionResponseCostError::MissingModel)? + .ok_or(CostError::MissingModel)? .clone(); return Ok(flat_priced(prepared, model, total)); } @@ -427,8 +392,8 @@ fn price_video_response( .map(|cost| cost * count as f64) }) .map_err(|error| match error { - CandidatePriceError::MissingModel => CompletionResponseCostError::MissingModel, - CandidatePriceError::Price(error) => CompletionResponseCostError::Video(error), + CandidatePriceError::MissingModel => CostError::MissingModel, + CandidatePriceError::Price(error) => error, })?; Ok(flat_priced(prepared, model, total)) } @@ -464,9 +429,7 @@ fn unregistered_replicate_cost( Some((model.clone(), total)) } -fn realtime_results( - request: CompletionResponseCostRequest<'_>, -) -> Result<&[Value], CompletionResponseCostError> { +fn realtime_results(request: CompletionResponseCostRequest<'_>) -> Result<&[Value], CostError> { request .input .model_selection @@ -474,7 +437,7 @@ fn realtime_results( .and_then(|response| response.get("results")) .and_then(Value::as_array) .map(Vec::as_slice) - .ok_or(CompletionResponseCostError::MissingUsage) + .ok_or(CostError::MissingUsage) } fn price_realtime_response( @@ -482,20 +445,19 @@ fn price_realtime_response( request: CompletionResponseCostRequest<'_>, prepared: PreparedCompletionInput, provider: Option<&str>, -) -> Result { - let provider = provider.ok_or(CompletionResponseCostError::MissingProvider)?; +) -> Result { + let provider = provider.ok_or(CostError::MissingProvider)?; let results = realtime_results(request)?; let combined = match prepared.usage.as_ref().or(request.fallback_usage) { Some(usage) => usage.clone(), - None => collect_and_combine_usage_from_realtime_stream_results(results) - .map_err(CompletionResponseCostError::Usage)?, + None => collect_and_combine_usage_from_realtime_stream_results(results)?, }; let model = prepared .model_candidates .iter() .flatten() .next() - .ok_or(CompletionResponseCostError::MissingModel)? + .ok_or(CostError::MissingModel)? .clone(); let total = catalog.handle_realtime_stream_cost_calculation( results, @@ -560,7 +522,7 @@ fn price_responses_websocket( prepared: PreparedCompletionInput, provider: Option<&str>, region: Option<&str>, -) -> Result { +) -> Result { let results = realtime_results(request)?; let partition = partition_results_by_service_tier(results); let groups = if partition.is_empty() { @@ -591,19 +553,17 @@ fn price_responses_websocket( request.response_time_ms, ), |model| { - catalog - .cost_per_token(ModelCostRequest { - model, - provider, - region, - usage, - service_tier: tier, - data_residency: request.data_residency, - vertex_location: request.vertex_location, - at: request.at, - response_time_ms: None, - }) - .map_err(CompletionResponseCostError::Realtime) + catalog.cost_per_token(ModelCostRequest { + model, + provider, + region, + usage, + service_tier: tier, + data_residency: request.data_residency, + vertex_location: request.vertex_location, + at: request.at, + response_time_ms: None, + }) }, )?; let built_in = if index == 0 { @@ -623,7 +583,7 @@ fn price_responses_websocket( } else { &[] }; - Ok::<_, CompletionResponseCostError>(( + Ok::<_, CostError>(( model, completion_cost( prompt, @@ -641,7 +601,7 @@ fn price_responses_websocket( .first() .map(|(model, _)| model.clone()) .or_else(|| prepared.model_candidates.iter().flatten().next().cloned()) - .ok_or(CompletionResponseCostError::MissingModel)?; + .ok_or(CostError::MissingModel)?; let zero = completion_cost(0.0, 0.0, 0.0, &[], None, &Value::Null, &Value::Null); let cost = priced_groups .into_iter() @@ -659,10 +619,8 @@ fn price_responses_websocket( pub fn completion_cost_from_response( catalog: &ModelInfoCatalog, request: CompletionResponseCostRequest<'_>, -) -> Result { - let prepared = catalog - .prepare_completion_input(request.input) - .map_err(CompletionResponseCostError::Usage)?; +) -> Result { + let prepared = catalog.prepare_completion_input(request.input)?; let hidden_params = request.input.model_selection.hidden_params; let explicit_provider = hidden_params .and_then(|hidden| hidden.get("custom_llm_provider")) @@ -700,8 +658,7 @@ pub fn completion_cost_from_response( .next() .cloned() .unwrap_or_default(); - let total = calculate_a2a_cost(request.logging_details) - .map_err(CompletionResponseCostError::A2A)?; + let total = calculate_a2a_cost(request.logging_details)?; return Ok(flat_priced(prepared, model, total)); } if prepared.call_type == "call_mcp_tool" { @@ -711,7 +668,7 @@ pub fn completion_cost_from_response( .flatten() .next() .cloned() - .ok_or(CompletionResponseCostError::MissingModel)?; + .ok_or(CostError::MissingModel)?; let total = calculate_mcp_tool_call_cost(request.logging_details); return Ok(flat_priced(prepared, model, total)); } @@ -849,9 +806,7 @@ pub fn completion_cost_from_response( request.response_time_ms, ), }; - catalog - .cost_per_token_for_call(cost_request, call) - .map_err(CompletionResponseCostError::Cost) + catalog.cost_per_token_for_call(cost_request, call) }, )?; let router_fee = (!is_search && provider == Some("azure_ai") && !is_azure_model_router(&model)) @@ -913,14 +868,13 @@ pub fn response_cost_calculator_from_response( catalog: &ModelInfoCatalog, request: CompletionResponseCostRequest<'_>, cache_hit: bool, -) -> Result { +) -> Result { if cache_hit { return Ok(0.0); } if request.input.model_selection.response.is_some() && let Some(hidden) = request.input.model_selection.hidden_params - && let Some(cost) = get_response_cost_from_hidden_params(hidden) - .map_err(CompletionResponseCostError::ProviderCost)? + && let Some(cost) = get_response_cost_from_hidden_params(hidden)? { return Ok(cost); } diff --git a/litellm-rust/crates/cost/src/custom_pricing.rs b/litellm-rust/crates/cost/src/custom_pricing.rs index 7708ea5fce1..4b03b1a0109 100644 --- a/litellm-rust/crates/cost/src/custom_pricing.rs +++ b/litellm-rust/crates/cost/src/custom_pricing.rs @@ -46,19 +46,11 @@ pub struct CustomCost { pub output: f64, } -#[derive(Clone, Copy, Debug, Eq, PartialEq)] -pub enum CustomPricingError { - InvalidUsage, - InvalidRate, - InvalidDuration, - NonFiniteCost, -} - fn nonzero(value: Option) -> Option { value.filter(|number| *number != 0.0) } -pub fn normalize_cache_usage(raw: RawUsage) -> Result { +pub fn normalize_cache_usage(raw: RawUsage) -> Result { let values = [ Some(raw.prompt_tokens), Some(raw.completion_tokens), @@ -75,7 +67,7 @@ pub fn normalize_cache_usage(raw: RawUsage) -> Result Result, -) -> Result, CustomPricingError> { +) -> Result, CostError> { if pricing.token.is_none() && pricing.per_second.is_none() { return Ok(None); } @@ -135,7 +127,7 @@ pub fn cost_per_token_custom_pricing_helper( .into_iter() .any(|value| !value.is_finite() || value < 0.0) { - return Err(CustomPricingError::InvalidUsage); + return Err(CostError::InvalidUsage); } let cost = match (pricing.token, pricing.per_second) { (Some(rates), _) => { @@ -149,7 +141,7 @@ pub fn cost_per_token_custom_pricing_helper( .flatten() .any(|rate| !rate.is_finite() || rate < 0.0) { - return Err(CustomPricingError::InvalidRate); + return Err(CostError::InvalidRate); } let regular = (usage.prompt_tokens - usage.cached_tokens - usage.cache_creation_tokens).max(0.0); @@ -162,11 +154,11 @@ pub fn cost_per_token_custom_pricing_helper( } (None, Some(rate)) => { if !rate.is_finite() || rate < 0.0 { - return Err(CustomPricingError::InvalidRate); + return Err(CostError::InvalidRate); } let duration = response_time_ms.unwrap_or(0.0); if !duration.is_finite() || duration < 0.0 { - return Err(CustomPricingError::InvalidDuration); + return Err(CostError::InvalidDuration); } CustomCost { input: 0.0, @@ -176,7 +168,7 @@ pub fn cost_per_token_custom_pricing_helper( (None, None) => return Ok(None), }; if !cost.input.is_finite() || !cost.output.is_finite() { - return Err(CustomPricingError::NonFiniteCost); + return Err(CostError::NonFiniteCost); } Ok(Some(cost)) } @@ -185,7 +177,7 @@ pub fn cost_from_chat_usage( usage: &ChatUsage, pricing: CustomPricing, response_time_ms: Option, -) -> Result, CustomPricingError> { +) -> Result, CostError> { if pricing == CustomPricing::NONE { return Ok(None); } @@ -208,6 +200,7 @@ pub fn cost_from_chat_usage( })?; cost_per_token_custom_pricing_helper(normalized, pricing, response_time_ms) } +use crate::error::CostError; use serde_json::Value; use crate::responses_usage::ChatUsage; diff --git a/litellm-rust/crates/cost/src/error.rs b/litellm-rust/crates/cost/src/error.rs index b3c3477e7f1..a187ed9ddd0 100644 --- a/litellm-rust/crates/cost/src/error.rs +++ b/litellm-rust/crates/cost/src/error.rs @@ -1,7 +1,59 @@ +use std::fmt; + #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub enum CostError { - InvalidRate, - TokenCountOverflow, + InvalidShape, InvalidUsage, + InvalidCost, + InvalidDuration, + InvalidMultiplier, + InvalidProviderCost, + InvalidQuantity, + InvalidRate, + InvalidRateBasis, + MissingInputCharacterRate, + MissingMetric, + MissingModel, + MissingPages, + MissingPromptCharacters, + MissingProvider, + MissingRate, + MissingUsage, ModelNotFound, + NonFiniteCost, + TokenCount, + TokenCountOverflow, + UnsupportedCallType, + UnsupportedProvider, +} + +impl fmt::Display for CostError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let text = match self { + Self::InvalidShape => "usage payload has an invalid shape", + Self::InvalidUsage => "usage payload has an uncoercible value", + Self::InvalidCost => "reported cost is not a finite number", + Self::InvalidDuration => "duration is not a finite number of seconds", + Self::InvalidMultiplier => "pricing multiplier is not a usable number", + Self::InvalidProviderCost => "provider-reported cost is not a number", + Self::InvalidQuantity => "quantity is missing or not a positive number", + Self::InvalidRate => "rate is negative, non-finite, or unparseable", + Self::InvalidRateBasis => "rate basis must be per image, per token, or per pixel", + Self::MissingInputCharacterRate => "no input cost per character rate", + Self::MissingMetric => "no cost metric for the model", + Self::MissingModel => "no model in the response or request", + Self::MissingPages => "no page count in the OCR usage", + Self::MissingPromptCharacters => "no prompt character count", + Self::MissingProvider => "no provider for a call that needs one", + Self::MissingRate => "no rate for the priced unit", + Self::MissingUsage => "no usage on the response", + Self::ModelNotFound => "model is not in the cost map", + Self::NonFiniteCost => "computed cost is not finite", + Self::TokenCount => "token count is not a usable number", + Self::TokenCountOverflow => "token counts overflow a u64", + Self::UnsupportedCallType => "call type has no cost path", + Self::UnsupportedProvider => "provider has no image cost path", + }; + f.write_str(text) + } } diff --git a/litellm-rust/crates/cost/src/image_cost_router.rs b/litellm-rust/crates/cost/src/image_cost_router.rs index d839371982b..513ea77aa97 100644 --- a/litellm-rust/crates/cost/src/image_cost_router.rs +++ b/litellm-rust/crates/cost/src/image_cost_router.rs @@ -1,10 +1,8 @@ +use crate::error::CostError; use jiff::Timestamp; use serde_json::Value; -use crate::catalog::{ - AzureAiImageCatalogRequest, CatalogError, CatalogImageError, DefaultImageCostRequest, - ModelInfoCatalog, -}; +use crate::catalog::{AzureAiImageCatalogRequest, DefaultImageCostRequest, ModelInfoCatalog}; use crate::generic_input::get_cost_per_unit; #[derive(Clone, Copy, Debug)] @@ -21,25 +19,6 @@ pub struct ImageCostRouteRequest<'a> { pub at: Timestamp, } -#[derive(Clone, Copy, Debug, Eq, PartialEq)] -pub enum ImageCostRouteError { - Catalog(CatalogError), - Image(CatalogImageError), - UnsupportedProvider, -} - -impl From for ImageCostRouteError { - fn from(value: CatalogError) -> Self { - Self::Catalog(value) - } -} - -impl From for ImageCostRouteError { - fn from(value: CatalogImageError) -> Self { - Self::Image(value) - } -} - pub fn call_type_has_image_response(call_type: &str) -> bool { matches!( call_type, @@ -188,7 +167,7 @@ fn requested_image_size(optional_params: &Value) -> Option<&str> { pub fn route_image_generation_cost_calculator( catalog: &ModelInfoCatalog, request: ImageCostRouteRequest<'_>, -) -> Result { +) -> Result { let pricing = deployment_pricing(request.supplied_model_info); let resolved_size = request .size diff --git a/litellm-rust/crates/cost/src/interactions_usage.rs b/litellm-rust/crates/cost/src/interactions_usage.rs index cae7eb38f82..fbe2019b528 100644 --- a/litellm-rust/crates/cost/src/interactions_usage.rs +++ b/litellm-rust/crates/cost/src/interactions_usage.rs @@ -2,7 +2,8 @@ use std::collections::BTreeMap; use serde_json::{Map, Value}; -use crate::responses_usage::{ChatUsage, CompletionTokenDetails, PromptTokenDetails, UsageError}; +use crate::error::CostError; +use crate::responses_usage::{ChatUsage, CompletionTokenDetails, PromptTokenDetails}; #[derive(Clone, Copy, Debug, Default)] struct Modalities { @@ -68,17 +69,17 @@ fn entries<'a>(usage: &'a Map, key: &str) -> Vec<&'a Map, value: u64) -> Result, UsageError> { +fn add(existing: Option, value: u64) -> Result, CostError> { existing .unwrap_or(0) .checked_add(value) .map(Some) - .ok_or(UsageError::TokenCountOverflow) + .ok_or(CostError::TokenCountOverflow) } fn sums<'a>( entries: impl IntoIterator>, -) -> Result { +) -> Result { entries .into_iter() .try_fold(Modalities::default(), |counts, entry| { @@ -110,11 +111,11 @@ fn sums<'a>( }) } -pub fn transform_interactions_usage_object(usage: &Value) -> Result { +pub fn transform_interactions_usage_object(usage: &Value) -> Result { if !is_interactions_usage_object(usage) { - return Err(UsageError::InvalidShape); + return Err(CostError::InvalidShape); } - let object = usage.as_object().ok_or(UsageError::InvalidShape)?; + let object = usage.as_object().ok_or(CostError::InvalidShape)?; let input = sums( entries(object, "input_tokens_by_modality") .into_iter() @@ -132,13 +133,13 @@ pub fn transform_interactions_usage_object(usage: &Value) -> Result derived_total, reported => reported, @@ -148,7 +149,7 @@ pub fn transform_interactions_usage_object(usage: &Value) -> Result 0 || web_search_requests > 0) .then_some(PromptTokenDetails { diff --git a/litellm-rust/crates/cost/src/non_token.rs b/litellm-rust/crates/cost/src/non_token.rs index dd3b16384bc..2fc0eb970e7 100644 --- a/litellm-rust/crates/cost/src/non_token.rs +++ b/litellm-rust/crates/cost/src/non_token.rs @@ -29,15 +29,6 @@ pub struct Cost { pub total: f64, } -#[derive(Clone, Copy, Debug, Eq, PartialEq)] -pub enum Error { - MissingRate, - InvalidQuantity, - InvalidRate, - InvalidRateBasis, - NonFiniteCost, -} - #[derive(Clone, Copy, Debug, PartialEq)] pub struct ImageRates { pub input_per_image: Rate, @@ -88,22 +79,22 @@ fn priced(rate: Rate) -> Option { } } -pub fn calculate(charges: &[Charge]) -> Result { +pub fn calculate(charges: &[Charge]) -> Result { let components: Vec = charges .iter() .map(|charge| { if !charge.quantity.is_finite() || charge.quantity < 0.0 { - return Err(Error::InvalidQuantity); + return Err(CostError::InvalidQuantity); } if !charge.rate.is_finite() || charge.rate < 0.0 { - return Err(Error::InvalidRate); + return Err(CostError::InvalidRate); } if !charge.units_per_rate.is_finite() || charge.units_per_rate <= 0.0 { - return Err(Error::InvalidRateBasis); + return Err(CostError::InvalidRateBasis); } let amount = charge.quantity * charge.rate / charge.units_per_rate; if !amount.is_finite() { - return Err(Error::NonFiniteCost); + return Err(CostError::NonFiniteCost); } Ok(Component { unit: charge.unit, @@ -113,14 +104,14 @@ pub fn calculate(charges: &[Charge]) -> Result { .collect::>()?; let total: f64 = components.iter().map(|component| component.amount).sum(); if !total.is_finite() { - return Err(Error::NonFiniteCost); + return Err(CostError::NonFiniteCost); } Ok(Cost { components, total }) } -pub fn calculate_image(tables: &[ImageRates], usage: ImageUsage) -> Result { +pub fn calculate_image(tables: &[ImageRates], usage: ImageUsage) -> Result { if usage.width == 0 || usage.height == 0 { - return Err(Error::InvalidQuantity); + return Err(CostError::InvalidQuantity); } let selected = tables .iter() @@ -141,7 +132,7 @@ pub fn calculate_image(tables: &[ImageRates], usage: ImageUsage) -> Result Result Result { +pub fn calculate_ocr(rates: OcrRates, usage: OcrUsage) -> Result { if let (Some(credits), Some(rate)) = (usage.credits, priced(rates.per_credit)) { return calculate(&[Charge { unit: Unit::Credit, @@ -184,7 +175,7 @@ pub fn calculate_ocr(rates: OcrRates, usage: OcrUsage) -> Result { calculate(&charges) } -pub fn calculate_ocr_with_tables(tables: &[OcrRates], usage: OcrUsage) -> Result { +pub fn calculate_ocr_with_tables(tables: &[OcrRates], usage: OcrUsage) -> Result { let rates = OcrRates { per_credit: tables .iter() @@ -207,7 +198,7 @@ pub fn calculate_ocr_batch( published: Option, pages: u64, annotation_pages: u64, -) -> Result { +) -> Result { let family_rate = |select: fn(OcrBatchRates) -> (Rate, Rate)| { [deployment, published] .into_iter() @@ -245,7 +236,7 @@ pub fn calculate_video( rates: &VideoRates<'_>, duration_seconds: f64, resolution: Option<&str>, -) -> Result { +) -> Result { let resolution_rate = resolution .and_then(video_resolution_to_cost_field_suffix) .and_then(|suffix| { @@ -267,7 +258,7 @@ pub fn calculate_video( }]), None => { if !duration_seconds.is_finite() || duration_seconds < 0.0 { - return Err(Error::InvalidQuantity); + return Err(CostError::InvalidQuantity); } calculate(&[]) } @@ -283,4 +274,5 @@ pub fn video_resolution_to_cost_field_suffix(resolution: &str) -> Option .collect(); (!suffix.is_empty() && suffix.chars().count() <= 24).then_some(suffix) } +use crate::error::CostError; use crate::pricing::Rate; diff --git a/litellm-rust/crates/cost/src/ocr_cost.rs b/litellm-rust/crates/cost/src/ocr_cost.rs index 681b15b96c4..6e1ce61062a 100644 --- a/litellm-rust/crates/cost/src/ocr_cost.rs +++ b/litellm-rust/crates/cost/src/ocr_cost.rs @@ -1,15 +1,9 @@ +use crate::error::CostError; use serde_json::Value; -use crate::non_token::{Error as NonTokenError, OcrRates, OcrUsage, calculate_ocr_with_tables}; +use crate::non_token::{OcrRates, OcrUsage, calculate_ocr_with_tables}; use crate::pricing::Rate; -#[derive(Clone, Copy, Debug, Eq, PartialEq)] -pub enum OcrCostError { - MissingUsage, - MissingPages, - Pricing(NonTokenError), -} - fn rate(model_info: &Value, key: &str) -> Rate { model_info .get(key) @@ -36,11 +30,11 @@ pub fn ocr_cost( response: &Value, deployment_info: Option<&Value>, published_info: Option<&Value>, -) -> Result<(f64, f64), OcrCostError> { +) -> Result<(f64, f64), CostError> { let usage = response .get("usage_info") .and_then(Value::as_object) - .ok_or(OcrCostError::MissingUsage)?; + .ok_or(CostError::MissingUsage)?; let pages = usage.get("pages_processed").and_then(Value::as_u64); let annotation_pages = usage .get("pages_processed_annotation") @@ -62,7 +56,7 @@ pub fn ocr_cost( if credit_rate.is_some() || page_rate.is_none() { return Ok((0.0, 0.0)); } - return Err(OcrCostError::MissingPages); + return Err(CostError::MissingPages); } let cost = calculate_ocr_with_tables( &tables, @@ -71,7 +65,6 @@ pub fn ocr_cost( pages: pages.unwrap_or(0), annotation_pages, }, - ) - .map_err(OcrCostError::Pricing)?; + )?; Ok((cost.total, 0.0)) } diff --git a/litellm-rust/crates/cost/src/openai_cost.rs b/litellm-rust/crates/cost/src/openai_cost.rs index 7719188dc98..468774c134c 100644 --- a/litellm-rust/crates/cost/src/openai_cost.rs +++ b/litellm-rust/crates/cost/src/openai_cost.rs @@ -1,6 +1,7 @@ +use crate::error::CostError; use serde_json::Value; -use crate::non_token::{Charge, Error, Unit, calculate}; +use crate::non_token::{Charge, Unit, calculate}; pub use crate::non_token::video_resolution_to_cost_field_suffix; @@ -32,14 +33,14 @@ pub fn video_generation_cost( model_info: &Value, duration_seconds: f64, video_resolution: Option<&str>, -) -> Result { +) -> Result { let rate = model_info .get("output_cost_per_video_per_second") .and_then(Value::as_f64) .or_else(|| video_output_cost_per_second(model_info, video_resolution)); let Some(rate) = rate else { if !duration_seconds.is_finite() || duration_seconds < 0.0 { - return Err(Error::InvalidQuantity); + return Err(CostError::InvalidQuantity); } return Ok(0.0); }; diff --git a/litellm-rust/crates/cost/src/openai_image_cost.rs b/litellm-rust/crates/cost/src/openai_image_cost.rs index 78c8f61f625..21fc934d2f2 100644 --- a/litellm-rust/crates/cost/src/openai_image_cost.rs +++ b/litellm-rust/crates/cost/src/openai_image_cost.rs @@ -1,12 +1,13 @@ +use crate::error::CostError; use jiff::Timestamp; use serde_json::Value; use crate::generic_cost::calculate_generic_cost_from_model_info_with_region; use crate::image_response_cost::{calculate_image_response_cost_from_usage, flat_image_cost}; -use crate::responses_usage::UsageError; + use crate::usage_dispatch::chat_usage; -fn chat_token_cost(usage: &Value, model_info: &Value, at: Timestamp) -> Result { +fn chat_token_cost(usage: &Value, model_info: &Value, at: Timestamp) -> Result { let usage = chat_usage(usage)?; let (input, output) = calculate_generic_cost_from_model_info_with_region( &usage, model_info, None, false, None, None, at, @@ -19,7 +20,7 @@ pub fn cost_calculator( model_info: &Value, provider: &str, at: Timestamp, -) -> Result { +) -> Result { let Some(usage) = image_response.get("usage").filter(|usage| !usage.is_null()) else { return Ok(flat_image_cost(image_response, model_info)); }; diff --git a/litellm-rust/crates/cost/src/realtime_cost.rs b/litellm-rust/crates/cost/src/realtime_cost.rs index 60ed0adf344..f402a874d9e 100644 --- a/litellm-rust/crates/cost/src/realtime_cost.rs +++ b/litellm-rust/crates/cost/src/realtime_cost.rs @@ -1,19 +1,20 @@ +use crate::error::CostError; use serde_json::{Value, json}; use crate::responses_usage::{ CacheCreationTokenDetails, CachedTokenDetails, ChatUsage, CompletionTokenDetails, - PromptTokenDetails, UsageError, + PromptTokenDetails, }; use crate::usage_dispatch::get_usage_object; -fn add_optional(first: Option, second: Option) -> Result, UsageError> { +fn add_optional(first: Option, second: Option) -> Result, CostError> { match (first, second) { (None, None) => Ok(None), (first, second) => first .unwrap_or(0) .checked_add(second.unwrap_or(0)) .map(Some) - .ok_or(UsageError::TokenCountOverflow), + .ok_or(CostError::TokenCountOverflow), } } @@ -24,7 +25,7 @@ fn add_optional_f64(first: Option, second: Option) -> Option { fn combine_cached_details( first: Option, second: Option, -) -> Result, UsageError> { +) -> Result, CostError> { match (first, second) { (None, None) => Ok(None), (first, second) => { @@ -42,7 +43,7 @@ fn combine_cached_details( fn combine_cache_creation_details( first: Option, second: Option, -) -> Result, UsageError> { +) -> Result, CostError> { match (first, second) { (None, None) => Ok(None), (first, second) => { @@ -65,7 +66,7 @@ fn combine_cache_creation_details( fn combine_prompt_details( first: Option, second: Option, -) -> Result, UsageError> { +) -> Result, CostError> { match (first, second) { (None, None) => Ok(None), (first, second) => { @@ -75,7 +76,7 @@ fn combine_prompt_details( cached_tokens: first .cached_tokens .checked_add(second.cached_tokens) - .ok_or(UsageError::TokenCountOverflow)?, + .ok_or(CostError::TokenCountOverflow)?, audio_tokens: add_optional(first.audio_tokens, second.audio_tokens)?, text_tokens: add_optional(first.text_tokens, second.text_tokens)?, image_tokens: add_optional(first.image_tokens, second.image_tokens)?, @@ -127,7 +128,7 @@ fn combine_prompt_details( fn combine_completion_details( first: Option, second: Option, -) -> Result, UsageError> { +) -> Result, CostError> { match (first, second) { (None, None) => Ok(None), (first, second) => { @@ -146,7 +147,7 @@ fn combine_completion_details( pub fn combine_usage_objects( usages: impl IntoIterator, -) -> Result { +) -> Result { usages .into_iter() .try_fold(ChatUsage::default(), |combined, usage| { @@ -154,15 +155,15 @@ pub fn combine_usage_objects( prompt_tokens: combined .prompt_tokens .checked_add(usage.prompt_tokens) - .ok_or(UsageError::TokenCountOverflow)?, + .ok_or(CostError::TokenCountOverflow)?, completion_tokens: combined .completion_tokens .checked_add(usage.completion_tokens) - .ok_or(UsageError::TokenCountOverflow)?, + .ok_or(CostError::TokenCountOverflow)?, total_tokens: combined .total_tokens .checked_add(usage.total_tokens) - .ok_or(UsageError::TokenCountOverflow)?, + .ok_or(CostError::TokenCountOverflow)?, prompt_tokens_details: combine_prompt_details( combined.prompt_tokens_details, usage.prompt_tokens_details, @@ -177,14 +178,14 @@ pub fn combine_usage_objects( }) } -pub(crate) fn event_usage(event: &Value) -> Result { +pub(crate) fn event_usage(event: &Value) -> Result { let usage = event.pointer("/response/usage").unwrap_or(&Value::Null); Ok(get_usage_object(&json!({"usage": usage}))?.unwrap_or_default()) } pub fn collect_usage_from_realtime_stream_results( results: &[Value], -) -> Result, UsageError> { +) -> Result, CostError> { results .iter() .filter(|event| event.get("type").and_then(Value::as_str) == Some("response.done")) @@ -194,7 +195,7 @@ pub fn collect_usage_from_realtime_stream_results( pub fn collect_and_combine_usage_from_realtime_stream_results( results: &[Value], -) -> Result { +) -> Result { combine_usage_objects(collect_usage_from_realtime_stream_results(results)?) } @@ -214,7 +215,7 @@ pub fn billable_responses_ws_events(results: &[Value]) -> Vec<&Value> { pub fn collect_usage_from_responses_ws_results( results: &[Value], -) -> Result, UsageError> { +) -> Result, CostError> { billable_responses_ws_events(results) .into_iter() .map(event_usage) @@ -223,7 +224,7 @@ pub fn collect_usage_from_responses_ws_results( pub fn collect_and_combine_usage_from_responses_ws_results( results: &[Value], -) -> Result { +) -> Result { combine_usage_objects(collect_usage_from_responses_ws_results(results)?) } diff --git a/litellm-rust/crates/cost/src/responses_usage.rs b/litellm-rust/crates/cost/src/responses_usage.rs index fe2edddbfa8..9f175dba0f7 100644 --- a/litellm-rust/crates/cost/src/responses_usage.rs +++ b/litellm-rust/crates/cost/src/responses_usage.rs @@ -1,3 +1,4 @@ +use crate::error::CostError; use std::collections::BTreeMap; use serde::Deserialize; @@ -58,13 +59,6 @@ pub struct ChatUsage { pub extra: BTreeMap, } -#[derive(Clone, Copy, Debug, Eq, PartialEq)] -pub enum UsageError { - InvalidShape, - InvalidUsage, - TokenCountOverflow, -} - #[derive(Deserialize)] struct RawResponseUsage { input_tokens: u64, @@ -103,16 +97,16 @@ pub fn text_tokens_without_nested_reasoning( text_tokens - nested as u64 } -pub fn transform_response_api_usage_to_chat_usage(usage: &Value) -> Result { +pub fn transform_response_api_usage_to_chat_usage(usage: &Value) -> Result { if !is_response_api_usage(usage) { - return Err(UsageError::InvalidShape); + return Err(CostError::InvalidShape); } let raw: RawResponseUsage = - serde_json::from_value(usage.clone()).map_err(|_| UsageError::InvalidUsage)?; + serde_json::from_value(usage.clone()).map_err(|_| CostError::InvalidUsage)?; let total_tokens = raw .input_tokens .checked_add(raw.output_tokens) - .ok_or(UsageError::TokenCountOverflow)?; + .ok_or(CostError::TokenCountOverflow)?; let prompt_tokens_details = raw.input_tokens_details .or(raw.input_token_details) diff --git a/litellm-rust/crates/cost/src/speech_cost.rs b/litellm-rust/crates/cost/src/speech_cost.rs index 05078a2c493..fdb21aa5763 100644 --- a/litellm-rust/crates/cost/src/speech_cost.rs +++ b/litellm-rust/crates/cost/src/speech_cost.rs @@ -1,3 +1,4 @@ +use crate::error::CostError; use serde_json::Value; use crate::responses_usage::ChatUsage; @@ -8,13 +9,6 @@ pub enum SpeechCostMetric { PerToken, } -#[derive(Clone, Copy, Debug, Eq, PartialEq)] -pub enum SpeechCostError { - MissingMetric, - MissingPromptCharacters, - MissingInputCharacterRate, -} - pub fn count_characters(text: &str) -> usize { text.chars() .filter(|character| { @@ -23,9 +17,7 @@ pub fn count_characters(text: &str) -> usize { .count() } -pub fn select_cost_metric_for_model( - model_info: &Value, -) -> Result { +pub fn select_cost_metric_for_model(model_info: &Value) -> Result { if model_info .get("input_cost_per_character") .and_then(Value::as_f64) @@ -40,7 +32,7 @@ pub fn select_cost_metric_for_model( { return Ok(SpeechCostMetric::PerToken); } - Err(SpeechCostError::MissingMetric) + Err(CostError::MissingMetric) } pub fn generic_cost_per_character( diff --git a/litellm-rust/crates/cost/src/transcription_usage.rs b/litellm-rust/crates/cost/src/transcription_usage.rs index adaa37dabd3..a5c959f8fac 100644 --- a/litellm-rust/crates/cost/src/transcription_usage.rs +++ b/litellm-rust/crates/cost/src/transcription_usage.rs @@ -1,7 +1,8 @@ +use crate::error::CostError; use serde::Deserialize; use serde_json::Value; -use crate::responses_usage::{ChatUsage, PromptTokenDetails, UsageError}; +use crate::responses_usage::{ChatUsage, PromptTokenDetails}; #[derive(Deserialize)] #[serde(tag = "type", rename_all = "snake_case")] @@ -28,11 +29,9 @@ pub fn is_transcription_usage_object(usage: &Value) -> bool { serde_json::from_value::(usage.clone()).is_ok() } -pub fn transform_transcription_usage_object( - usage: &Value, -) -> Result, UsageError> { +pub fn transform_transcription_usage_object(usage: &Value) -> Result, CostError> { let parsed: TranscriptionUsage = - serde_json::from_value(usage.clone()).map_err(|_| UsageError::InvalidShape)?; + serde_json::from_value(usage.clone()).map_err(|_| CostError::InvalidShape)?; match parsed { TranscriptionUsage::Duration { .. } => Ok(None), TranscriptionUsage::Tokens { diff --git a/litellm-rust/crates/cost/src/usage_dispatch.rs b/litellm-rust/crates/cost/src/usage_dispatch.rs index 45108b04716..ae7288b1bf8 100644 --- a/litellm-rust/crates/cost/src/usage_dispatch.rs +++ b/litellm-rust/crates/cost/src/usage_dispatch.rs @@ -1,3 +1,4 @@ +use crate::error::CostError; use std::collections::BTreeMap; use serde_json::Value; @@ -7,7 +8,7 @@ use crate::interactions_usage::{ is_interactions_usage_object, transform_interactions_usage_object, }; use crate::responses_usage::{ - ChatUsage, CompletionTokenDetails, PromptTokenDetails, UsageError, is_response_api_usage, + ChatUsage, CompletionTokenDetails, PromptTokenDetails, is_response_api_usage, transform_response_api_usage_to_chat_usage, }; use crate::transcription_usage::{ @@ -25,14 +26,14 @@ fn int_count(value: &Value) -> Option { value.as_u64().or_else(|| value.as_bool().map(u64::from)) } -pub fn chat_usage(raw: &Value) -> Result { - let object = raw.as_object().ok_or(UsageError::InvalidShape)?; +pub fn chat_usage(raw: &Value) -> Result { + let object = raw.as_object().ok_or(CostError::InvalidShape)?; let count = |name: &str| { object .get(name) .filter(|value| !value.is_null()) .map_or(Ok(0), |value| { - lenient_count(value).ok_or(UsageError::InvalidUsage) + lenient_count(value).ok_or(CostError::InvalidUsage) }) }; let prompt_tokens = count("prompt_tokens")?; @@ -51,7 +52,7 @@ pub fn chat_usage(raw: &Value) -> Result { let prompt_details: Option = object .get("prompt_tokens_details") .filter(|value| value.as_object().is_some_and(|details| !details.is_empty())) - .map(|value| serde_json::from_value(value.clone()).map_err(|_| UsageError::InvalidUsage)) + .map(|value| serde_json::from_value(value.clone()).map_err(|_| CostError::InvalidUsage)) .transpose()?; let prompt_tokens_details = match (prompt_details, read, creation) { (None, None, None) => None, @@ -73,7 +74,7 @@ pub fn chat_usage(raw: &Value) -> Result { let completion_tokens_details: Option = object .get("completion_tokens_details") .filter(|value| value.as_object().is_some_and(|details| !details.is_empty())) - .map(|value| serde_json::from_value(value.clone()).map_err(|_| UsageError::InvalidUsage)) + .map(|value| serde_json::from_value(value.clone()).map_err(|_| CostError::InvalidUsage)) .transpose()?; let reasoning = object.get("reasoning_tokens").and_then(lenient_count); let completion_tokens_details = match (completion_tokens_details, reasoning) { @@ -129,7 +130,7 @@ pub fn chat_usage(raw: &Value) -> Result { }) } -pub fn get_usage_object(response: &Value) -> Result, UsageError> { +pub fn get_usage_object(response: &Value) -> Result, CostError> { let Some(usage) = response.as_object().and_then(|object| object.get("usage")) else { return Ok(None); }; @@ -153,5 +154,5 @@ pub fn get_usage_object(response: &Value) -> Result, UsageErro if usage.is_object() { return Ok(Some(chat_usage(usage)?)); } - Err(UsageError::InvalidShape) + Err(CostError::InvalidShape) } diff --git a/litellm-rust/crates/cost/tests/non_token.rs b/litellm-rust/crates/cost/tests/non_token.rs index 14497c78b3a..ec9dbc80bfb 100644 --- a/litellm-rust/crates/cost/tests/non_token.rs +++ b/litellm-rust/crates/cost/tests/non_token.rs @@ -1,6 +1,8 @@ +use litellm_cost::error::CostError; + use litellm_cost::Rate; use litellm_cost::non_token::{ - Charge, Component, Error, ImageRates, ImageUsage, OcrRates, OcrUsage, Unit, calculate, + Charge, Component, ImageRates, ImageUsage, OcrRates, OcrUsage, Unit, calculate, calculate_image, calculate_ocr, }; use rstest::rstest; @@ -86,21 +88,21 @@ fn rejects_invalid_inputs_and_overflow() { quantity: -1.0, ..valid }]), - Err(Error::InvalidQuantity) + Err(CostError::InvalidQuantity) ); assert_eq!( calculate(&[Charge { rate: f64::NAN, ..valid }]), - Err(Error::InvalidRate) + Err(CostError::InvalidRate) ); assert_eq!( calculate(&[Charge { units_per_rate: 0.0, ..valid }]), - Err(Error::InvalidRateBasis) + Err(CostError::InvalidRateBasis) ); assert_eq!( calculate(&[Charge { @@ -108,7 +110,7 @@ fn rejects_invalid_inputs_and_overflow() { rate: f64::MAX, ..valid }]), - Err(Error::NonFiniteCost) + Err(CostError::NonFiniteCost) ); } @@ -180,7 +182,7 @@ fn image_rejects_missing_prices_and_ocr_returns_zero() { width: 10, height: 10, }; - assert_eq!(calculate_image(&[], image), Err(Error::MissingRate)); + assert_eq!(calculate_image(&[], image), Err(CostError::MissingRate)); assert_eq!( calculate_ocr( OcrRates { diff --git a/litellm-rust/crates/cost/tests/python_anthropic_usage.rs b/litellm-rust/crates/cost/tests/python_anthropic_usage.rs index 37550368ee1..5f07cd59664 100644 --- a/litellm-rust/crates/cost/tests/python_anthropic_usage.rs +++ b/litellm-rust/crates/cost/tests/python_anthropic_usage.rs @@ -1,12 +1,11 @@ #![allow(clippy::disallowed_types)] - // mirrors: test_litellm/llms/anthropic/chat/test_anthropic_chat_transformation.py::test_calculate_usage_aggregates_cache_creation_split_across_iterations +use litellm_cost::error::CostError; use litellm_cost::anthropic_usage::{ is_anthropic_usage_object, transform_anthropic_usage_to_chat_usage, }; use litellm_cost::generic_cost::calculate_generic_cost_from_model_info_with_region; -use litellm_cost::responses_usage::UsageError; use rstest::rstest; use serde_json::{Value, json}; @@ -152,7 +151,7 @@ fn iteration_sums_overflow_errors_where_python_yields_a_big_int() { ); assert_eq!( usage.unwrap_err(), - UsageError::TokenCountOverflow, + CostError::TokenCountOverflow, "divergence: Python sums arbitrary-precision ints and returns a huge cost" ); } diff --git a/litellm-rust/crates/cost/tests/python_azure_ai_cost.rs b/litellm-rust/crates/cost/tests/python_azure_ai_cost.rs index f1f40eef506..69492d500e7 100644 --- a/litellm-rust/crates/cost/tests/python_azure_ai_cost.rs +++ b/litellm-rust/crates/cost/tests/python_azure_ai_cost.rs @@ -1,6 +1,6 @@ #![allow(clippy::disallowed_types)] - // mirrors: test_litellm/llms/azure_ai/test_azure_ai_cost_calculator.py::TestAzureModelRouterFlatCost +use litellm_cost::error::CostError; use std::collections::HashMap; @@ -9,7 +9,7 @@ use litellm_cost::azure_ai_cost::{ calculate_azure_model_router_flat_cost, is_azure_model_router, is_router_fee_entry, router_fee_entry_name, router_fee_name, }; -use litellm_cost::catalog::{CatalogError, ModelCostRequest, ModelInfoCatalog}; +use litellm_cost::catalog::{ModelCostRequest, ModelInfoCatalog}; use litellm_cost::responses_usage::ChatUsage; use litellm_cost::usage_dispatch::get_usage_object; use rstest::rstest; @@ -170,7 +170,7 @@ fn azure_ai_cost_per_token_rejects_unknown_non_router_model() { let catalog = catalog(); assert_eq!( catalog.azure_ai_cost_per_token(request("unknown", &ChatUsage::default(), None), None), - Err(CatalogError::ModelNotFound) + Err(CostError::ModelNotFound) ); } diff --git a/litellm-rust/crates/cost/tests/python_call_dispatch.rs b/litellm-rust/crates/cost/tests/python_call_dispatch.rs index 4caabefe85f..156a6d82484 100644 --- a/litellm-rust/crates/cost/tests/python_call_dispatch.rs +++ b/litellm-rust/crates/cost/tests/python_call_dispatch.rs @@ -1,11 +1,10 @@ #![allow(clippy::disallowed_types)] - // mirrors: test_litellm/test_cost_calculator.py::test_per_query_priced_rerank_deployment_completion_cost_is_nonzero +use litellm_cost::error::CostError; use std::collections::HashMap; -use litellm_cost::catalog::{CatalogCallError, CostCall, ModelCostRequest, ModelInfoCatalog}; -use litellm_cost::ocr_cost::OcrCostError; +use litellm_cost::catalog::{CostCall, ModelCostRequest, ModelInfoCatalog}; use litellm_cost::responses_usage::ChatUsage; use litellm_cost::usage_dispatch::get_usage_object; use rstest::rstest; @@ -234,7 +233,7 @@ fn retrieval_and_search_calls_use_their_reported_units() { request("rerank", None, &usage), CostCall::Rerank { billed_units: None }, ), - Err(CatalogCallError::MissingProvider) + Err(CostError::MissingProvider) ); } @@ -300,7 +299,7 @@ fn ocr_call_distinguishes_missing_pages_from_unpriced_usage() { deployment_info: None, }, ), - Err(CatalogCallError::Ocr(OcrCostError::MissingPages)) + Err(CostError::MissingPages) ); assert_eq!( catalog @@ -322,7 +321,7 @@ fn ocr_call_distinguishes_missing_pages_from_unpriced_usage() { deployment_info: None, }, ), - Err(CatalogCallError::Ocr(OcrCostError::MissingUsage)) + Err(CostError::MissingUsage) ); } diff --git a/litellm-rust/crates/cost/tests/python_completion_cost.rs b/litellm-rust/crates/cost/tests/python_completion_cost.rs index b77e455ff70..f853a4b1b35 100644 --- a/litellm-rust/crates/cost/tests/python_completion_cost.rs +++ b/litellm-rust/crates/cost/tests/python_completion_cost.rs @@ -1,6 +1,6 @@ #![allow(clippy::disallowed_types)] - // mirrors: test_litellm/test_cost_calculator.py::test_cost_discount_not_applied_to_other_providers +use litellm_cost::error::CostError; use std::collections::HashMap; @@ -10,8 +10,8 @@ use litellm_cost::catalog::{ ResponseCostRequest, }; use litellm_cost::completion_cost::{ - ResponseCostError, apply_cost_discount, apply_cost_margin, completion_cost, - get_response_cost_from_hidden_params, response_cost_calculator, + apply_cost_discount, apply_cost_margin, completion_cost, get_response_cost_from_hidden_params, + response_cost_calculator, }; use litellm_cost::usage_dispatch::get_usage_object; use rstest::rstest; @@ -71,10 +71,10 @@ fn apply_cost_margin_uses_global_when_provider_has_no_entry() { #[case(json!({"additional_headers": {"llm_provider-x-litellm-response-cost": true}}), Ok(Some(1.0)))] #[case(json!({"additional_headers": {"llm_provider-x-litellm-response-cost": null}}), Ok(None))] #[case(json!({"additional_headers": {"other": "1.25"}}), Ok(None))] -#[case(json!({"additional_headers": {"llm_provider-x-litellm-response-cost": "invalid"}}), Err(ResponseCostError::InvalidProviderCost))] +#[case(json!({"additional_headers": {"llm_provider-x-litellm-response-cost": "invalid"}}), Err(CostError::InvalidProviderCost))] fn get_response_cost_from_hidden_params_parses_provider_value( #[case] hidden_params: serde_json::Value, - #[case] expected: Result, ResponseCostError>, + #[case] expected: Result, CostError>, ) { assert_eq!( get_response_cost_from_hidden_params(&hidden_params), diff --git a/litellm-rust/crates/cost/tests/python_completion_response.rs b/litellm-rust/crates/cost/tests/python_completion_response.rs index d6f7ed5842e..16ae60f9590 100644 --- a/litellm-rust/crates/cost/tests/python_completion_response.rs +++ b/litellm-rust/crates/cost/tests/python_completion_response.rs @@ -1,16 +1,15 @@ #![allow(clippy::disallowed_types)] - // mirrors: test_litellm/test_cost_calculator.py::test_cost_calculator_with_response_cost_in_additional_headers // mirrors: local_testing/test_completion_cost.py +use litellm_cost::error::CostError; use std::collections::HashMap; use litellm_cost::catalog::ModelInfoCatalog; -use litellm_cost::completion_cost::ResponseCostError; use litellm_cost::completion_input::{CompletionInputRequest, ResponseKind}; use litellm_cost::completion_response::{ - BuiltInToolCostConfig, CompletionResponseCostError, CompletionResponseCostRequest, - CompletionTextInput, completion_cost_from_response, response_cost_calculator_from_response, + BuiltInToolCostConfig, CompletionResponseCostRequest, CompletionTextInput, + completion_cost_from_response, response_cost_calculator_from_response, response_time_ms_for_cost, }; use litellm_cost::custom_pricing::{CustomPricing, CustomTokenRates}; @@ -96,11 +95,11 @@ fn request<'a>( #[case(true, json!({"additional_headers": {"llm_provider-x-litellm-response-cost": "invalid"}}), Ok(0.0))] #[case(false, json!({"additional_headers": {"llm_provider-x-litellm-response-cost": "0.5"}}), Ok(0.5))] #[case(false, json!({"additional_headers": {"llm_provider-x-litellm-response-cost": 0.0}}), Ok(0.0))] -#[case(false, json!({"additional_headers": {"llm_provider-x-litellm-response-cost": "invalid"}}), Err(CompletionResponseCostError::ProviderCost(ResponseCostError::InvalidProviderCost)))] +#[case(false, json!({"additional_headers": {"llm_provider-x-litellm-response-cost": "invalid"}}), Err(CostError::InvalidProviderCost))] fn response_cost_calculator_short_circuits_cache_and_provider_cost( #[case] cache_hit: bool, #[case] hidden: Value, - #[case] expected: Result, + #[case] expected: Result, ) { let catalog = ModelInfoCatalog::new(HashMap::new()); let response = json!({}); @@ -456,7 +455,7 @@ fn invalid_messages_do_not_fall_back_to_prompt_pricing() { ..base }, ); - assert_eq!(result, Err(CompletionResponseCostError::TokenCount)); + assert_eq!(result, Err(CostError::TokenCount)); } #[rstest] @@ -1514,7 +1513,7 @@ fn unsupported_call_does_not_silently_bill_as_tokens() { ) } ), - Err(CompletionResponseCostError::UnsupportedCallType) + Err(CostError::UnsupportedCallType) ); } diff --git a/litellm-rust/crates/cost/tests/python_custom_pricing.rs b/litellm-rust/crates/cost/tests/python_custom_pricing.rs index f1884ed645f..c0c52fff26b 100644 --- a/litellm-rust/crates/cost/tests/python_custom_pricing.rs +++ b/litellm-rust/crates/cost/tests/python_custom_pricing.rs @@ -1,13 +1,13 @@ #![allow(clippy::disallowed_types)] - // mirrors: test_litellm/test_cost_calculator.py::test_custom_pricing_anthropic_style_cache_tokens_not_double_counted +use litellm_cost::error::CostError; use std::collections::HashMap; -use litellm_cost::catalog::{CatalogCustomError, CostCall, ModelCostRequest, ModelInfoCatalog}; +use litellm_cost::catalog::{CostCall, ModelCostRequest, ModelInfoCatalog}; use litellm_cost::custom_pricing::{ - CustomPricing, CustomPricingError, CustomTokenRates, RawUsage, - cost_per_token_custom_pricing_helper, normalize_cache_usage, + CustomPricing, CustomTokenRates, RawUsage, cost_per_token_custom_pricing_helper, + normalize_cache_usage, }; use litellm_cost::usage_dispatch::chat_usage; use rstest::rstest; @@ -262,6 +262,6 @@ fn catalog_custom_pricing_falls_back_and_rejects_invalid_rates() { per_second: None, }, ), - Err(CatalogCustomError::Pricing(CustomPricingError::InvalidRate)) + Err(CostError::InvalidRate) ); } diff --git a/litellm-rust/crates/cost/tests/python_model_lookup.rs b/litellm-rust/crates/cost/tests/python_model_lookup.rs index a69416c9f94..a26e4b3715a 100644 --- a/litellm-rust/crates/cost/tests/python_model_lookup.rs +++ b/litellm-rust/crates/cost/tests/python_model_lookup.rs @@ -1,11 +1,11 @@ #![allow(clippy::disallowed_types)] - // mirrors: test_litellm/test_cost_calculator.py::test_cost_per_token_duplicate_openai_prefix_matches_model_cost +use litellm_cost::error::CostError; use std::collections::HashMap; use jiff::Timestamp; -use litellm_cost::catalog::{CatalogError, ModelCostRequest, ModelInfoCatalog}; +use litellm_cost::catalog::{ModelCostRequest, ModelInfoCatalog}; use litellm_cost::usage_dispatch::get_usage_object; use rstest::rstest; use serde_json::json; @@ -114,7 +114,7 @@ fn cost_per_token_uses_the_selected_regional_prices() { fn cost_per_token_reports_an_unmapped_model() { assert_eq!( catalog().cost_per_token(request("missing", Some("openai"), None, &usage())), - Err(CatalogError::ModelNotFound) + Err(CostError::ModelNotFound) ); } diff --git a/litellm-rust/crates/cost/tests/python_openai_cost.rs b/litellm-rust/crates/cost/tests/python_openai_cost.rs index 02c8e3b8a43..9bef5d8020d 100644 --- a/litellm-rust/crates/cost/tests/python_openai_cost.rs +++ b/litellm-rust/crates/cost/tests/python_openai_cost.rs @@ -1,9 +1,8 @@ #![allow(clippy::disallowed_types)] - // mirrors: test_litellm/test_video_generation.py::TestVideoGeneration // mirrors: unit/llms/openai/transcriptions/test_transcription_duration_hidden.py::TestCostCalculatorReadsDurationFromHiddenParams +use litellm_cost::error::CostError; -use litellm_cost::non_token::Error; use litellm_cost::openai_cost::{ cost_router, video_generation_cost, video_output_cost_per_second, video_resolution_to_cost_field_suffix, @@ -75,6 +74,6 @@ fn video_generation_cost_matches_python_rate_precedence( fn video_generation_cost_rejects_invalid_duration() { assert_eq!( video_generation_cost(&json!({"output_cost_per_second": 0.05}), -1.0, None), - Err(Error::InvalidQuantity) + Err(CostError::InvalidQuantity) ); } diff --git a/litellm-rust/crates/cost/tests/python_responses_usage.rs b/litellm-rust/crates/cost/tests/python_responses_usage.rs index e60b5f533f8..7b64b1feb21 100644 --- a/litellm-rust/crates/cost/tests/python_responses_usage.rs +++ b/litellm-rust/crates/cost/tests/python_responses_usage.rs @@ -1,10 +1,10 @@ #![allow(clippy::disallowed_types)] - // mirrors: test_litellm/responses/test_responses_utils.py::TestResponseAPILoggingUtils +use litellm_cost::error::CostError; use litellm_cost::generic_cost::calculate_generic_cost_from_model_info_with_region; use litellm_cost::responses_usage::{ - UsageError, is_response_api_usage, text_tokens_without_nested_reasoning, + is_response_api_usage, text_tokens_without_nested_reasoning, transform_response_api_usage_to_chat_usage, }; use rstest::rstest; @@ -169,7 +169,7 @@ fn is_response_api_usage_requires_both_token_fields() { transform_response_api_usage_to_chat_usage( &json!({"prompt_tokens": 1, "completion_tokens": 2}) ), - Err(UsageError::InvalidShape) + Err(CostError::InvalidShape) ); } diff --git a/litellm-rust/crates/cost/tests/python_speech_cost.rs b/litellm-rust/crates/cost/tests/python_speech_cost.rs index 4e4312bdc74..265fff3cd4d 100644 --- a/litellm-rust/crates/cost/tests/python_speech_cost.rs +++ b/litellm-rust/crates/cost/tests/python_speech_cost.rs @@ -1,16 +1,15 @@ #![allow(clippy::disallowed_types)] - // mirrors: test_litellm/test_cost_calculator.py +use litellm_cost::error::CostError; use std::collections::HashMap; use jiff::Timestamp; -use litellm_cost::catalog::{CatalogSpeechError, ModelCostRequest, ModelInfoCatalog}; +use litellm_cost::catalog::{ModelCostRequest, ModelInfoCatalog}; use litellm_cost::responses_usage::ChatUsage; use litellm_cost::speech_cost::{ - SpeechCostError, SpeechCostMetric, cost_per_second, count_characters, - generic_cost_per_character, lyria_generation_cost, select_cost_metric_for_model, - transcription_usage_has_token_details, + SpeechCostMetric, cost_per_second, count_characters, generic_cost_per_character, + lyria_generation_cost, select_cost_metric_for_model, transcription_usage_has_token_details, }; use litellm_cost::usage_dispatch::get_usage_object; use rstest::rstest; @@ -41,10 +40,10 @@ fn count_characters_excludes_whitespace(#[case] prompt: &str, #[case] expected: #[rstest] #[case(json!({"input_cost_per_character": 0.002, "input_cost_per_token": 0.003}), Ok(SpeechCostMetric::PerCharacter))] #[case(json!({"input_cost_per_character": 0.0, "input_cost_per_token": 0.003}), Ok(SpeechCostMetric::PerToken))] -#[case(json!({"input_cost_per_character": 0.0, "input_cost_per_token": 0.0}), Err(SpeechCostError::MissingMetric))] +#[case(json!({"input_cost_per_character": 0.0, "input_cost_per_token": 0.0}), Err(CostError::MissingMetric))] fn select_cost_metric_for_model_uses_character_first( #[case] model_info: Value, - #[case] expected: Result, + #[case] expected: Result, ) { assert_eq!(select_cost_metric_for_model(&model_info), expected); } @@ -101,9 +100,7 @@ fn speech_cost_selects_character_or_token_pricing_and_requires_characters() { ); assert_eq!( catalog.speech_cost(request("character", "openai", &usage), None), - Err(CatalogSpeechError::Speech( - SpeechCostError::MissingPromptCharacters - )) + Err(CostError::MissingPromptCharacters) ); } diff --git a/litellm-rust/crates/cost/tests/python_together_cost.rs b/litellm-rust/crates/cost/tests/python_together_cost.rs index 8da16a050de..24c97f61826 100644 --- a/litellm-rust/crates/cost/tests/python_together_cost.rs +++ b/litellm-rust/crates/cost/tests/python_together_cost.rs @@ -1,11 +1,11 @@ #![allow(clippy::disallowed_types)] - // mirrors: local_testing/test_completion_cost.py::test_together_ai_qwen_completion_cost +use litellm_cost::error::CostError; use std::collections::HashMap; use jiff::Timestamp; -use litellm_cost::catalog::{CatalogError, ModelCostRequest, ModelInfoCatalog}; +use litellm_cost::catalog::{ModelCostRequest, ModelInfoCatalog}; use litellm_cost::together_cost::{ TogetherThresholds, get_model_params_and_category, get_model_params_and_category_embeddings, has_together_registry_pricing, @@ -159,6 +159,6 @@ fn catalog_does_not_price_metadata_only_exact_row_when_category_is_missing() { let usage = usage(); assert_eq!( catalog.cost_per_token(request("model-7b", &usage)), - Err(CatalogError::ModelNotFound) + Err(CostError::ModelNotFound) ); }