mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-18 09:45:23 +00:00
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
This commit is contained in:
parent
26a5dcb96d
commit
a880fdeae0
33 changed files with 383 additions and 590 deletions
|
|
@ -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<f64, A2ACostError> {
|
||||
fn amount(value: &Value) -> Result<f64, CostError> {
|
||||
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<f64, A2ACostError> {
|
||||
) -> Result<f64, CostError> {
|
||||
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<f64, A2ACostError> {
|
||||
pub fn calculate_a2a_cost(details: Option<&Value>) -> Result<f64, CostError> {
|
||||
let Some(details) = details else {
|
||||
return Ok(0.0);
|
||||
};
|
||||
|
|
|
|||
|
|
@ -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<u64, UsageError> {
|
||||
fn count(value: Option<&Value>) -> Result<u64, CostError> {
|
||||
match value {
|
||||
None | Some(Value::Null) => Ok(0),
|
||||
Some(Value::Number(number)) => number
|
||||
|
|
@ -26,16 +27,16 @@ fn count(value: Option<&Value>) -> Result<u64, UsageError> {
|
|||
.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<String, Value>], field: &str) -> Result<u64, UsageError> {
|
||||
fn sum_field(entries: &[&Map<String, Value>], field: &str) -> Result<u64, CostError> {
|
||||
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<String, Value>,
|
||||
iterations: &[&Map<String, Value>],
|
||||
total_creation: u64,
|
||||
) -> Result<Option<CacheCreationTokenDetails>, UsageError> {
|
||||
) -> Result<Option<CacheCreationTokenDetails>, 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<u64>,
|
||||
response_has_thinking_block: bool,
|
||||
) -> Result<ChatUsage, UsageError> {
|
||||
let object = usage.as_object().ok_or(UsageError::InvalidShape)?;
|
||||
) -> Result<ChatUsage, CostError> {
|
||||
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 {
|
||||
|
|
|
|||
|
|
@ -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::<u32>().map_err(|_| Error::InvalidQuantity)?;
|
||||
let height = height.parse::<u32>().map_err(|_| Error::InvalidQuantity)?;
|
||||
.ok_or(CostError::InvalidQuantity)?;
|
||||
let width = width
|
||||
.parse::<u32>()
|
||||
.map_err(|_| CostError::InvalidQuantity)?;
|
||||
let height = height
|
||||
.parse::<u32>()
|
||||
.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<f64, Error> {
|
||||
pub fn cost_calculator(request: AzureAiImageRequest<'_>) -> Result<f64, CostError> {
|
||||
if let Some(cost) = calculate_image_response_cost_from_usage(
|
||||
request.image_response,
|
||||
request.model_info,
|
||||
|
|
|
|||
|
|
@ -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<f64> {
|
||||
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<BatchCost, BatchError> {
|
||||
) -> Result<BatchCost, CostError> {
|
||||
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<BatchCost, BatchError> {
|
||||
) -> Result<BatchCost, CostError> {
|
||||
let policy = if uses_inclusive_token_thresholds(provider) {
|
||||
ThresholdPolicy::Inclusive
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -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<CatalogError> for CatalogImageError {
|
||||
fn from(value: CatalogError) -> Self {
|
||||
Self::Catalog(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<NonTokenError> for CatalogImageError {
|
||||
fn from(value: NonTokenError) -> Self {
|
||||
Self::Pricing(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<UsageError> for CatalogImageError {
|
||||
fn from(value: UsageError) -> Self {
|
||||
Self::Usage(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<CatalogError> for CatalogSpeechError {
|
||||
fn from(value: CatalogError) -> Self {
|
||||
Self::Catalog(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<CatalogError> for CatalogCallError {
|
||||
fn from(value: CatalogError) -> Self {
|
||||
Self::Catalog(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<CatalogSpeechError> for CatalogCallError {
|
||||
fn from(value: CatalogSpeechError) -> Self {
|
||||
Self::Speech(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<OcrCostError> for CatalogCallError {
|
||||
fn from(value: OcrCostError) -> Self {
|
||||
Self::Ocr(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<BatchError> for CatalogCallError {
|
||||
fn from(value: BatchError) -> Self {
|
||||
Self::Batch(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<SpeechCostError> for CatalogSpeechError {
|
||||
fn from(value: SpeechCostError) -> Self {
|
||||
Self::Speech(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<CatalogError> for RealtimeCostError {
|
||||
fn from(value: CatalogError) -> Self {
|
||||
Self::Catalog(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<UsageError> for RealtimeCostError {
|
||||
fn from(value: UsageError) -> Self {
|
||||
Self::Usage(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<CatalogError> for CatalogResponseError {
|
||||
fn from(value: CatalogError) -> Self {
|
||||
Self::Catalog(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ResponseCostError> for CatalogResponseError {
|
||||
fn from(value: ResponseCostError) -> Self {
|
||||
Self::ProviderCost(value)
|
||||
}
|
||||
}
|
||||
|
||||
fn select_model_key<'a, T>(
|
||||
entries: &'a HashMap<String, T>,
|
||||
model: &str,
|
||||
|
|
@ -434,7 +309,7 @@ impl ModelInfoCatalog {
|
|||
pub fn prepare_completion_input(
|
||||
&self,
|
||||
request: CompletionInputRequest<'_>,
|
||||
) -> Result<PreparedCompletionInput, UsageError> {
|
||||
) -> Result<PreparedCompletionInput, CostError> {
|
||||
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<f64>,
|
||||
completion_characters: Option<f64>,
|
||||
) -> 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<Option<f64>, CatalogError> {
|
||||
) -> Result<Option<f64>, 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<f64>,
|
||||
) -> 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<f64, RealtimeCostError> {
|
||||
) -> Result<f64, CostError> {
|
||||
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<f64, CatalogError> {
|
||||
) -> Result<f64, CostError> {
|
||||
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<f64, CatalogError> {
|
||||
) -> Result<f64, CostError> {
|
||||
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<f64, CatalogImageError> {
|
||||
) -> Result<f64, CostError> {
|
||||
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<f64, CatalogError> {
|
||||
) -> Result<f64, CostError> {
|
||||
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<f64, CatalogImageError> {
|
||||
) -> Result<f64, CostError> {
|
||||
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<f64, CatalogError> {
|
||||
) -> Result<f64, CostError> {
|
||||
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<f64> {
|
||||
|
|
@ -1227,9 +1085,9 @@ impl ModelInfoCatalog {
|
|||
image_response: &Value,
|
||||
size: Option<&str>,
|
||||
optional_params: &Value,
|
||||
) -> Result<f64, CatalogError> {
|
||||
) -> Result<f64, CostError> {
|
||||
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<f64, CatalogImageError> {
|
||||
) -> Result<f64, CostError> {
|
||||
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<f64, CatalogImageError> {
|
||||
) -> Result<f64, CostError> {
|
||||
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::<u32>()
|
||||
.map_err(|_| NonTokenError::InvalidQuantity)?;
|
||||
.map_err(|_| CostError::InvalidQuantity)?;
|
||||
let width = width
|
||||
.parse::<u32>()
|
||||
.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<CompletionCost, CatalogError> {
|
||||
) -> Result<CompletionCost, CostError> {
|
||||
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<f64, CatalogResponseError> {
|
||||
) -> Result<f64, CostError> {
|
||||
if request.cache_hit {
|
||||
return Ok(0.0);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
use crate::error::CostError;
|
||||
use serde_json::Value;
|
||||
|
||||
fn number(value: &Value) -> Option<f64> {
|
||||
|
|
@ -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<Option<f64>, ResponseCostError> {
|
||||
) -> Result<Option<f64>, 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<f64, ResponseCostError> {
|
||||
) -> Result<f64, CostError> {
|
||||
if cache_hit {
|
||||
return Ok(0.0);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<String, Value>,
|
||||
) -> Result<PreparedCompletionInput, UsageError> {
|
||||
) -> Result<PreparedCompletionInput, CostError> {
|
||||
let response = request.model_selection.response;
|
||||
let call_type =
|
||||
infer_call_type(request.call_type, request.response_kind).unwrap_or("completion");
|
||||
|
|
|
|||
|
|
@ -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<String, f64>,
|
||||
}
|
||||
|
||||
#[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<UsageError> for CompletionResponseCostError {
|
||||
fn from(value: UsageError) -> Self {
|
||||
Self::Usage(value)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
enum CandidatePriceError<E> {
|
||||
MissingModel,
|
||||
|
|
@ -145,20 +120,18 @@ fn price_with_custom(
|
|||
usage: &ChatUsage,
|
||||
pricing: CustomPricing,
|
||||
response_time_ms: Option<f64>,
|
||||
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<f64> {
|
|||
}
|
||||
}
|
||||
|
||||
fn count_text_usage(
|
||||
input: CompletionTextInput<'_>,
|
||||
) -> Result<ChatUsage, CompletionResponseCostError> {
|
||||
fn count_text_usage(input: CompletionTextInput<'_>) -> Result<ChatUsage, CostError> {
|
||||
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<CostCall<'a>, CompletionResponseCostError> {
|
||||
) -> Result<CostCall<'a>, 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<PricedCompletionResponse, CompletionResponseCostError> {
|
||||
) -> Result<PricedCompletionResponse, CostError> {
|
||||
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<PricedCompletionResponse, CompletionResponseCostError> {
|
||||
) -> Result<PricedCompletionResponse, CostError> {
|
||||
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<PricedCompletionResponse, CompletionResponseCostError> {
|
||||
let provider = provider.ok_or(CompletionResponseCostError::MissingProvider)?;
|
||||
) -> Result<PricedCompletionResponse, CostError> {
|
||||
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<PricedCompletionResponse, CompletionResponseCostError> {
|
||||
) -> Result<PricedCompletionResponse, CostError> {
|
||||
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<PricedCompletionResponse, CompletionResponseCostError> {
|
||||
let prepared = catalog
|
||||
.prepare_completion_input(request.input)
|
||||
.map_err(CompletionResponseCostError::Usage)?;
|
||||
) -> Result<PricedCompletionResponse, CostError> {
|
||||
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<f64, CompletionResponseCostError> {
|
||||
) -> Result<f64, CostError> {
|
||||
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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<f64>) -> Option<f64> {
|
||||
value.filter(|number| *number != 0.0)
|
||||
}
|
||||
|
||||
pub fn normalize_cache_usage(raw: RawUsage) -> Result<NormalizedUsage, CustomPricingError> {
|
||||
pub fn normalize_cache_usage(raw: RawUsage) -> Result<NormalizedUsage, CostError> {
|
||||
let values = [
|
||||
Some(raw.prompt_tokens),
|
||||
Some(raw.completion_tokens),
|
||||
|
|
@ -75,7 +67,7 @@ pub fn normalize_cache_usage(raw: RawUsage) -> Result<NormalizedUsage, CustomPri
|
|||
.flatten()
|
||||
.any(|value| !value.is_finite() || value < 0.0)
|
||||
{
|
||||
return Err(CustomPricingError::InvalidUsage);
|
||||
return Err(CostError::InvalidUsage);
|
||||
}
|
||||
let detail_read = raw.details_cached_tokens.unwrap_or(0.0);
|
||||
let detail_creation = nonzero(raw.details_cache_write_tokens)
|
||||
|
|
@ -107,7 +99,7 @@ pub fn normalize_cache_usage(raw: RawUsage) -> Result<NormalizedUsage, CustomPri
|
|||
raw.prompt_tokens
|
||||
};
|
||||
if !prompt_tokens.is_finite() {
|
||||
return Err(CustomPricingError::InvalidUsage);
|
||||
return Err(CostError::InvalidUsage);
|
||||
}
|
||||
Ok(NormalizedUsage {
|
||||
prompt_tokens,
|
||||
|
|
@ -121,7 +113,7 @@ pub fn cost_per_token_custom_pricing_helper(
|
|||
usage: NormalizedUsage,
|
||||
pricing: CustomPricing,
|
||||
response_time_ms: Option<f64>,
|
||||
) -> Result<Option<CustomCost>, CustomPricingError> {
|
||||
) -> Result<Option<CustomCost>, 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<f64>,
|
||||
) -> Result<Option<CustomCost>, CustomPricingError> {
|
||||
) -> Result<Option<CustomCost>, 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;
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<CatalogError> for ImageCostRouteError {
|
||||
fn from(value: CatalogError) -> Self {
|
||||
Self::Catalog(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<CatalogImageError> 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<f64, ImageCostRouteError> {
|
||||
) -> Result<f64, CostError> {
|
||||
let pricing = deployment_pricing(request.supplied_model_info);
|
||||
let resolved_size = request
|
||||
.size
|
||||
|
|
|
|||
|
|
@ -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<String, Value>, key: &str) -> Vec<&'a Map<String,
|
|||
.collect()
|
||||
}
|
||||
|
||||
fn add(existing: Option<u64>, value: u64) -> Result<Option<u64>, UsageError> {
|
||||
fn add(existing: Option<u64>, value: u64) -> Result<Option<u64>, CostError> {
|
||||
existing
|
||||
.unwrap_or(0)
|
||||
.checked_add(value)
|
||||
.map(Some)
|
||||
.ok_or(UsageError::TokenCountOverflow)
|
||||
.ok_or(CostError::TokenCountOverflow)
|
||||
}
|
||||
|
||||
fn sums<'a>(
|
||||
entries: impl IntoIterator<Item = &'a Map<String, Value>>,
|
||||
) -> Result<Modalities, UsageError> {
|
||||
) -> Result<Modalities, CostError> {
|
||||
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<ChatUsage, UsageError> {
|
||||
pub fn transform_interactions_usage_object(usage: &Value) -> Result<ChatUsage, CostError> {
|
||||
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<ChatUsage, U
|
|||
};
|
||||
let prompt_tokens = count(object.get("total_input_tokens"))
|
||||
.checked_add(count(object.get("total_tool_use_tokens")))
|
||||
.ok_or(UsageError::TokenCountOverflow)?;
|
||||
.ok_or(CostError::TokenCountOverflow)?;
|
||||
let completion_tokens = count(object.get("total_output_tokens"))
|
||||
.checked_add(reasoning)
|
||||
.ok_or(UsageError::TokenCountOverflow)?;
|
||||
.ok_or(CostError::TokenCountOverflow)?;
|
||||
let derived_total = prompt_tokens
|
||||
.checked_add(completion_tokens)
|
||||
.ok_or(UsageError::TokenCountOverflow)?;
|
||||
.ok_or(CostError::TokenCountOverflow)?;
|
||||
let total_tokens = match count(object.get("total_tokens")) {
|
||||
0 => derived_total,
|
||||
reported => reported,
|
||||
|
|
@ -148,7 +149,7 @@ pub fn transform_interactions_usage_object(usage: &Value) -> Result<ChatUsage, U
|
|||
.filter(|entry| entry.get("type").and_then(Value::as_str) == Some("google_search"))
|
||||
.try_fold(0_u64, |sum, entry| {
|
||||
sum.checked_add(count(entry.get("count")))
|
||||
.ok_or(UsageError::TokenCountOverflow)
|
||||
.ok_or(CostError::TokenCountOverflow)
|
||||
})?;
|
||||
let prompt_tokens_details = (input.any() || total_cached > 0 || web_search_requests > 0)
|
||||
.then_some(PromptTokenDetails {
|
||||
|
|
|
|||
|
|
@ -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<f64> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn calculate(charges: &[Charge]) -> Result<Cost, Error> {
|
||||
pub fn calculate(charges: &[Charge]) -> Result<Cost, CostError> {
|
||||
let components: Vec<Component> = 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<Cost, Error> {
|
|||
.collect::<Result<_, _>>()?;
|
||||
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<Cost, Error> {
|
||||
pub fn calculate_image(tables: &[ImageRates], usage: ImageUsage) -> Result<Cost, CostError> {
|
||||
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<Cost,
|
|||
})
|
||||
})
|
||||
})
|
||||
.ok_or(Error::MissingRate)?;
|
||||
.ok_or(CostError::MissingRate)?;
|
||||
calculate(&[Charge {
|
||||
unit: selected.0,
|
||||
quantity: selected.1,
|
||||
|
|
@ -150,7 +141,7 @@ pub fn calculate_image(tables: &[ImageRates], usage: ImageUsage) -> Result<Cost,
|
|||
}])
|
||||
}
|
||||
|
||||
pub fn calculate_ocr(rates: OcrRates, usage: OcrUsage) -> Result<Cost, Error> {
|
||||
pub fn calculate_ocr(rates: OcrRates, usage: OcrUsage) -> Result<Cost, CostError> {
|
||||
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<Cost, Error> {
|
|||
calculate(&charges)
|
||||
}
|
||||
|
||||
pub fn calculate_ocr_with_tables(tables: &[OcrRates], usage: OcrUsage) -> Result<Cost, Error> {
|
||||
pub fn calculate_ocr_with_tables(tables: &[OcrRates], usage: OcrUsage) -> Result<Cost, CostError> {
|
||||
let rates = OcrRates {
|
||||
per_credit: tables
|
||||
.iter()
|
||||
|
|
@ -207,7 +198,7 @@ pub fn calculate_ocr_batch(
|
|||
published: Option<OcrBatchRates>,
|
||||
pages: u64,
|
||||
annotation_pages: u64,
|
||||
) -> Result<Cost, Error> {
|
||||
) -> Result<Cost, CostError> {
|
||||
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<Cost, Error> {
|
||||
) -> Result<Cost, CostError> {
|
||||
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<String>
|
|||
.collect();
|
||||
(!suffix.is_empty() && suffix.chars().count() <= 24).then_some(suffix)
|
||||
}
|
||||
use crate::error::CostError;
|
||||
use crate::pricing::Rate;
|
||||
|
|
|
|||
|
|
@ -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))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<f64, Error> {
|
||||
) -> Result<f64, CostError> {
|
||||
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);
|
||||
};
|
||||
|
|
|
|||
|
|
@ -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<f64, UsageError> {
|
||||
fn chat_token_cost(usage: &Value, model_info: &Value, at: Timestamp) -> Result<f64, CostError> {
|
||||
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<f64, UsageError> {
|
||||
) -> Result<f64, CostError> {
|
||||
let Some(usage) = image_response.get("usage").filter(|usage| !usage.is_null()) else {
|
||||
return Ok(flat_image_cost(image_response, model_info));
|
||||
};
|
||||
|
|
|
|||
|
|
@ -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<u64>, second: Option<u64>) -> Result<Option<u64>, UsageError> {
|
||||
fn add_optional(first: Option<u64>, second: Option<u64>) -> Result<Option<u64>, 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<f64>, second: Option<f64>) -> Option<f64> {
|
|||
fn combine_cached_details(
|
||||
first: Option<CachedTokenDetails>,
|
||||
second: Option<CachedTokenDetails>,
|
||||
) -> Result<Option<CachedTokenDetails>, UsageError> {
|
||||
) -> Result<Option<CachedTokenDetails>, 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<CacheCreationTokenDetails>,
|
||||
second: Option<CacheCreationTokenDetails>,
|
||||
) -> Result<Option<CacheCreationTokenDetails>, UsageError> {
|
||||
) -> Result<Option<CacheCreationTokenDetails>, 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<PromptTokenDetails>,
|
||||
second: Option<PromptTokenDetails>,
|
||||
) -> Result<Option<PromptTokenDetails>, UsageError> {
|
||||
) -> Result<Option<PromptTokenDetails>, 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<CompletionTokenDetails>,
|
||||
second: Option<CompletionTokenDetails>,
|
||||
) -> Result<Option<CompletionTokenDetails>, UsageError> {
|
||||
) -> Result<Option<CompletionTokenDetails>, 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<Item = ChatUsage>,
|
||||
) -> Result<ChatUsage, UsageError> {
|
||||
) -> Result<ChatUsage, CostError> {
|
||||
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<ChatUsage, UsageError> {
|
||||
pub(crate) fn event_usage(event: &Value) -> Result<ChatUsage, CostError> {
|
||||
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<Vec<ChatUsage>, UsageError> {
|
||||
) -> Result<Vec<ChatUsage>, 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<ChatUsage, UsageError> {
|
||||
) -> Result<ChatUsage, CostError> {
|
||||
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<Vec<ChatUsage>, UsageError> {
|
||||
) -> Result<Vec<ChatUsage>, 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<ChatUsage, UsageError> {
|
||||
) -> Result<ChatUsage, CostError> {
|
||||
combine_usage_objects(collect_usage_from_responses_ws_results(results)?)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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<String, Value>,
|
||||
}
|
||||
|
||||
#[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<ChatUsage, UsageError> {
|
||||
pub fn transform_response_api_usage_to_chat_usage(usage: &Value) -> Result<ChatUsage, CostError> {
|
||||
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)
|
||||
|
|
|
|||
|
|
@ -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<SpeechCostMetric, SpeechCostError> {
|
||||
pub fn select_cost_metric_for_model(model_info: &Value) -> Result<SpeechCostMetric, CostError> {
|
||||
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(
|
||||
|
|
|
|||
|
|
@ -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::<TranscriptionUsage>(usage.clone()).is_ok()
|
||||
}
|
||||
|
||||
pub fn transform_transcription_usage_object(
|
||||
usage: &Value,
|
||||
) -> Result<Option<ChatUsage>, UsageError> {
|
||||
pub fn transform_transcription_usage_object(usage: &Value) -> Result<Option<ChatUsage>, 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 {
|
||||
|
|
|
|||
|
|
@ -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<u64> {
|
|||
value.as_u64().or_else(|| value.as_bool().map(u64::from))
|
||||
}
|
||||
|
||||
pub fn chat_usage(raw: &Value) -> Result<ChatUsage, UsageError> {
|
||||
let object = raw.as_object().ok_or(UsageError::InvalidShape)?;
|
||||
pub fn chat_usage(raw: &Value) -> Result<ChatUsage, CostError> {
|
||||
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<ChatUsage, UsageError> {
|
|||
let prompt_details: Option<PromptTokenDetails> = 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<ChatUsage, UsageError> {
|
|||
let completion_tokens_details: Option<CompletionTokenDetails> = 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<ChatUsage, UsageError> {
|
|||
})
|
||||
}
|
||||
|
||||
pub fn get_usage_object(response: &Value) -> Result<Option<ChatUsage>, UsageError> {
|
||||
pub fn get_usage_object(response: &Value) -> Result<Option<ChatUsage>, 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<Option<ChatUsage>, UsageErro
|
|||
if usage.is_object() {
|
||||
return Ok(Some(chat_usage(usage)?));
|
||||
}
|
||||
Err(UsageError::InvalidShape)
|
||||
Err(CostError::InvalidShape)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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<Option<f64>, ResponseCostError>,
|
||||
#[case] expected: Result<Option<f64>, CostError>,
|
||||
) {
|
||||
assert_eq!(
|
||||
get_response_cost_from_hidden_params(&hidden_params),
|
||||
|
|
|
|||
|
|
@ -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<f64, CompletionResponseCostError>,
|
||||
#[case] expected: Result<f64, CostError>,
|
||||
) {
|
||||
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)
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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<SpeechCostMetric, SpeechCostError>,
|
||||
#[case] expected: Result<SpeechCostMetric, CostError>,
|
||||
) {
|
||||
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)
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue