diff --git a/litellm-rust/crates/cost/Cargo.toml b/litellm-rust/crates/cost/Cargo.toml index aaf2894b1f5..02342798847 100644 --- a/litellm-rust/crates/cost/Cargo.toml +++ b/litellm-rust/crates/cost/Cargo.toml @@ -20,3 +20,6 @@ rstest.workspace = true [[bench]] name = "calculate" harness = false + +[lints.clippy] +disallowed-types = "deny" diff --git a/litellm-rust/crates/cost/clippy.toml b/litellm-rust/crates/cost/clippy.toml new file mode 100644 index 00000000000..d31f194ac27 --- /dev/null +++ b/litellm-rust/crates/cost/clippy.toml @@ -0,0 +1,3 @@ +disallowed-types = [ + { path = "serde_json::Value", reason = "wire.rs owns serde_json::Value in litellm-cost; cost code reads typed inputs (ModelPricing, UsageInput)" }, +] diff --git a/litellm-rust/crates/cost/src/lib.rs b/litellm-rust/crates/cost/src/lib.rs index 5301cbabd47..5fc2fc82030 100644 --- a/litellm-rust/crates/cost/src/lib.rs +++ b/litellm-rust/crates/cost/src/lib.rs @@ -1,61 +1,116 @@ +#[allow(clippy::disallowed_types)] pub mod a2a_cost; +#[allow(clippy::disallowed_types)] pub mod anthropic_cost; +#[allow(clippy::disallowed_types)] pub mod anthropic_usage; +#[allow(clippy::disallowed_types)] pub mod azure_ai_cost; +#[allow(clippy::disallowed_types)] pub mod azure_ai_image_cost; +#[allow(clippy::disallowed_types)] pub mod azure_cost; pub mod background_cost_polling; +#[allow(clippy::disallowed_types)] pub mod base_rate_selection; +#[allow(clippy::disallowed_types)] pub mod batch; +#[allow(clippy::disallowed_types)] pub mod bedrock_image_cost; +#[allow(clippy::disallowed_types)] pub mod billed_token_rates; +#[allow(clippy::disallowed_types)] pub mod catalog; +#[allow(clippy::disallowed_types)] pub mod completion_cost; +#[allow(clippy::disallowed_types)] pub mod completion_input; +#[allow(clippy::disallowed_types)] pub mod completion_response; pub mod cost_breakdown; +#[allow(clippy::disallowed_types)] pub mod custom_pricing; +#[allow(clippy::disallowed_types)] pub mod dashscope_cost; pub mod databricks_cost; +#[allow(clippy::disallowed_types)] pub mod fal_ai_image_cost; +#[allow(clippy::disallowed_types)] pub mod fireworks_cost; +#[allow(clippy::disallowed_types)] pub mod gemini_cost; +#[allow(clippy::disallowed_types)] pub mod generic_cost; +#[allow(clippy::disallowed_types)] pub mod generic_input; +#[allow(clippy::disallowed_types)] pub mod generic_output; pub mod generic_usage; +#[allow(clippy::disallowed_types)] pub mod groq_cost; +#[allow(clippy::disallowed_types)] pub mod guardrail_cost; +#[allow(clippy::disallowed_types)] pub mod image_cost_router; +#[allow(clippy::disallowed_types)] pub mod image_response_cost; +#[allow(clippy::disallowed_types)] pub mod interactions_usage; +#[allow(clippy::disallowed_types)] pub mod mcp_cost; +#[allow(clippy::disallowed_types)] pub mod model_selection; pub mod non_token; +#[allow(clippy::disallowed_types)] pub mod ocr_cost; +#[allow(clippy::disallowed_types)] pub mod off_peak; +#[allow(clippy::disallowed_types)] pub mod openai_cost; +#[allow(clippy::disallowed_types)] pub mod openai_image_cost; +#[allow(clippy::disallowed_types)] pub mod per_second; +#[allow(clippy::disallowed_types)] pub mod perplexity_cost; +pub mod pricing; +#[allow(clippy::disallowed_types)] pub mod prompt_caching_savings; +#[allow(clippy::disallowed_types)] pub mod provider_cache; +#[allow(clippy::disallowed_types)] pub mod ptu_pricing; +#[allow(clippy::disallowed_types)] pub mod realtime_cost; +#[allow(clippy::disallowed_types)] pub mod regional_uplift; +#[allow(clippy::disallowed_types)] pub mod responses_usage; +#[allow(clippy::disallowed_types)] pub mod retrieval_cost; +#[allow(clippy::disallowed_types)] pub mod search_cost; +#[allow(clippy::disallowed_types)] pub mod speech_cost; +#[allow(clippy::disallowed_types)] pub mod tiered_pricing; +#[allow(clippy::disallowed_types)] pub mod together_cost; +#[allow(clippy::disallowed_types)] pub mod tool_call_cost_tracking; +#[allow(clippy::disallowed_types)] pub mod tool_cost_dispatch; +#[allow(clippy::disallowed_types)] pub mod transcription_usage; +#[allow(clippy::disallowed_types)] pub mod usage_dispatch; +#[allow(clippy::disallowed_types)] pub mod vertex_cost; +#[allow(clippy::disallowed_types)] pub mod wire; +#[allow(clippy::disallowed_types)] pub mod xai_cost; +#[allow(clippy::disallowed_types)] pub mod zero_cost_diagnostic; #[derive(Clone, Copy, Debug, PartialEq)] diff --git a/litellm-rust/crates/cost/src/pricing.rs b/litellm-rust/crates/cost/src/pricing.rs new file mode 100644 index 00000000000..733712177b3 --- /dev/null +++ b/litellm-rust/crates/cost/src/pricing.rs @@ -0,0 +1,376 @@ +use std::collections::BTreeMap; + +#[derive(Clone, Copy, Debug, Default, PartialEq)] +pub enum Rate { + #[default] + Missing, + Null, + Invalid, + Value(f64), +} + +impl Rate { + pub fn value(self) -> Option { + match self { + Self::Value(value) => Some(value), + Self::Missing | Self::Null | Self::Invalid => None, + } + } +} + +#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] +pub enum ServiceTier { + Auto, + Flex, + Priority, + Fast, + Ultrafast, +} + +impl ServiceTier { + pub const SUFFIXES: [Self; 5] = [ + Self::Ultrafast, + Self::Priority, + Self::Auto, + Self::Flex, + Self::Fast, + ]; + + pub fn as_str(self) -> &'static str { + match self { + Self::Auto => "auto", + Self::Flex => "flex", + Self::Priority => "priority", + Self::Fast => "fast", + Self::Ultrafast => "ultrafast", + } + } +} + +#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] +pub enum Metric { + AnnotationPerPage, + CacheCreationAudioToken, + CacheCreationToken, + CacheCreationToken1hr, + CacheReadAudioToken, + CacheReadImageToken, + CacheReadToken, + CitationToken, + CodeInterpreterPerSession, + ComputerUseInput1kTokens, + ComputerUseOutput1kTokens, + FileSearchPer1kCalls, + FileSearchPerGbPerDay, + GoogleMapsGroundingPerQuery, + InputAudioPerSecond, + InputAudioToken, + InputCharacter, + InputDbuToken, + InputPerImage, + InputPerImageToken, + InputPerPixel, + InputPerQuery, + InputPerRequest, + InputPerSecond, + InputPerToken, + InputPerTokenCacheHit, + InputVideoPerSecond, + InputVideoPerSecond15sInterval, + InputVideoPerSecond8sInterval, + InputVideoToken, + OcrPerCredit, + OcrPerPage, + OutputAudioToken, + OutputCharacter, + OutputDbuToken, + OutputPerImage, + OutputPerImage1024, + OutputPerImage1536, + OutputPerImage512, + OutputPerImageToken, + OutputPerPixel, + OutputPerSecond, + OutputPerSecond1080p, + OutputPerSecond2k, + OutputPerSecond480p, + OutputPerSecond4k, + OutputPerSecond720p, + OutputPerSecond768p, + OutputPerToken, + OutputReasoningToken, + OutputVideoPerSecond, + OutputVideoToken, + VectorStorePerGbPerDay, +} + +impl Metric { + const NAMES: [(Self, &'static str); 53] = [ + (Self::AnnotationPerPage, "annotation_cost_per_page"), + ( + Self::CacheCreationAudioToken, + "cache_creation_input_audio_token_cost", + ), + (Self::CacheCreationToken, "cache_creation_input_token_cost"), + ( + Self::CacheCreationToken1hr, + "cache_creation_input_token_cost_above_1hr", + ), + ( + Self::CacheReadAudioToken, + "cache_read_input_audio_token_cost", + ), + ( + Self::CacheReadImageToken, + "cache_read_input_image_token_cost", + ), + (Self::CacheReadToken, "cache_read_input_token_cost"), + (Self::CitationToken, "citation_cost_per_token"), + ( + Self::CodeInterpreterPerSession, + "code_interpreter_cost_per_session", + ), + ( + Self::ComputerUseInput1kTokens, + "computer_use_input_cost_per_1k_tokens", + ), + ( + Self::ComputerUseOutput1kTokens, + "computer_use_output_cost_per_1k_tokens", + ), + (Self::FileSearchPer1kCalls, "file_search_cost_per_1k_calls"), + ( + Self::FileSearchPerGbPerDay, + "file_search_cost_per_gb_per_day", + ), + ( + Self::GoogleMapsGroundingPerQuery, + "google_maps_grounding_cost_per_query", + ), + (Self::InputAudioPerSecond, "input_cost_per_audio_per_second"), + (Self::InputAudioToken, "input_cost_per_audio_token"), + (Self::InputCharacter, "input_cost_per_character"), + (Self::InputDbuToken, "input_dbu_cost_per_token"), + (Self::InputPerImage, "input_cost_per_image"), + (Self::InputPerImageToken, "input_cost_per_image_token"), + (Self::InputPerPixel, "input_cost_per_pixel"), + (Self::InputPerQuery, "input_cost_per_query"), + (Self::InputPerRequest, "input_cost_per_request"), + (Self::InputPerSecond, "input_cost_per_second"), + (Self::InputPerToken, "input_cost_per_token"), + ( + Self::InputPerTokenCacheHit, + "input_cost_per_token_cache_hit", + ), + (Self::InputVideoPerSecond, "input_cost_per_video_per_second"), + ( + Self::InputVideoPerSecond15sInterval, + "input_cost_per_video_per_second_above_15s_interval", + ), + ( + Self::InputVideoPerSecond8sInterval, + "input_cost_per_video_per_second_above_8s_interval", + ), + (Self::InputVideoToken, "input_cost_per_video_token"), + (Self::OcrPerCredit, "ocr_cost_per_credit"), + (Self::OcrPerPage, "ocr_cost_per_page"), + (Self::OutputAudioToken, "output_cost_per_audio_token"), + (Self::OutputCharacter, "output_cost_per_character"), + (Self::OutputDbuToken, "output_dbu_cost_per_token"), + (Self::OutputPerImage, "output_cost_per_image"), + (Self::OutputPerImage1024, "output_cost_per_image_1024"), + (Self::OutputPerImage1536, "output_cost_per_image_1536"), + (Self::OutputPerImage512, "output_cost_per_image_512"), + (Self::OutputPerImageToken, "output_cost_per_image_token"), + (Self::OutputPerPixel, "output_cost_per_pixel"), + (Self::OutputPerSecond, "output_cost_per_second"), + (Self::OutputPerSecond1080p, "output_cost_per_second_1080p"), + (Self::OutputPerSecond2k, "output_cost_per_second_2k"), + (Self::OutputPerSecond480p, "output_cost_per_second_480p"), + (Self::OutputPerSecond4k, "output_cost_per_second_4k"), + (Self::OutputPerSecond720p, "output_cost_per_second_720p"), + (Self::OutputPerSecond768p, "output_cost_per_second_768p"), + (Self::OutputPerToken, "output_cost_per_token"), + ( + Self::OutputReasoningToken, + "output_cost_per_reasoning_token", + ), + ( + Self::OutputVideoPerSecond, + "output_cost_per_video_per_second", + ), + (Self::OutputVideoToken, "output_cost_per_video_token"), + ( + Self::VectorStorePerGbPerDay, + "vector_store_cost_per_gb_per_day", + ), + ]; + + pub fn parse(name: &str) -> Option { + Self::NAMES + .iter() + .find(|(_, known)| *known == name) + .map(|(metric, _)| *metric) + } + + pub fn as_str(self) -> &'static str { + Self::NAMES + .iter() + .find(|(metric, _)| *metric == self) + .map(|(_, name)| *name) + .expect("every metric carries its name") + } +} + +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub struct RateKey { + pub metric: Metric, + pub threshold: Option, + pub tier: Option, + pub batch: bool, +} + +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub enum TokenizeError { + UnrecognizedMetric, + BadThreshold, +} + +pub fn tokenize(key: &str) -> Result { + let (base, batch) = match key.strip_suffix("_batches") { + Some(base) => (base, true), + None => (key, false), + }; + let (base, tier) = ServiceTier::SUFFIXES + .into_iter() + .find_map(|tier| { + base.strip_suffix(format!("_{}", tier.as_str()).as_str()) + .map(|stripped| (stripped, Some(tier))) + }) + .unwrap_or((base, None)); + let (metric_name, threshold) = match base.strip_suffix("_tokens") { + Some(prefix) => match prefix.rsplit_once("_above") { + Some((metric_name, tail)) => ( + metric_name, + Some(parse_threshold_number( + tail.strip_prefix('_').unwrap_or(tail), + )?), + ), + None => (base, None), + }, + None => (base, None), + }; + let metric = Metric::parse(metric_name).ok_or(TokenizeError::UnrecognizedMetric)?; + Ok(RateKey { + metric, + threshold, + tier, + batch, + }) +} + +fn parse_threshold_number(text: &str) -> Result { + let (digits, multiplier) = match text.strip_suffix('k') { + Some(digits) => (digits, 1_000_u64), + None => (text, 1), + }; + if digits.is_empty() || !digits.bytes().all(|b| b.is_ascii_digit()) { + return Err(TokenizeError::BadThreshold); + } + digits + .parse::() + .ok() + .and_then(|value| value.checked_mul(multiplier)) + .ok_or(TokenizeError::BadThreshold) +} + +#[derive(Clone, Debug, PartialEq)] +pub struct ThresholdPricing { + pub above_tokens: u64, + pub standard: BTreeMap, + pub tiers: BTreeMap<(Metric, ServiceTier), Rate>, + pub batches: BTreeMap, + pub batch_tiers: BTreeMap<(Metric, ServiceTier), Rate>, +} + +#[derive(Clone, Debug, Default, PartialEq)] +pub struct OffPeakWindow { + pub hours_utc: Vec, + pub weekdays: Option>, +} + +#[derive(Clone, Debug, Default, PartialEq)] +pub struct OffPeakPricing { + pub input: Rate, + pub output: Rate, + pub cache_read: Rate, + pub cache_creation: Rate, + pub output_reasoning: Rate, + pub hours_utc: Vec, + pub weekdays: Option>, + pub weekday_timezone: Option, + pub windows: Vec, +} + +#[derive(Clone, Debug, PartialEq)] +pub struct TieredPricingTier { + pub range: (f64, Option), + pub input: Rate, + pub output: Rate, + pub output_reasoning: Rate, + pub cache_read: Rate, + pub cache_creation: Rate, +} + +#[derive(Clone, Copy, Debug, Default, PartialEq)] +pub struct SearchContextCostPerQuery { + pub low: Rate, + pub medium: Rate, + pub high: Rate, +} + +#[derive(Clone, Debug, PartialEq)] +pub struct PtuPricing { + pub count: i64, + pub cost_per_hour: Rate, + pub effective_from: Option, + pub effective_to: Option, +} + +#[derive(Clone, Debug, Default, PartialEq)] +pub struct ModelPricing { + pub base: BTreeMap, + pub tiers: BTreeMap<(Metric, ServiceTier), Rate>, + pub batches: BTreeMap, + pub batch_tiers: BTreeMap<(Metric, ServiceTier), Rate>, + pub thresholds: Vec, + pub off_peak: Option, + pub tiered_pricing: Option>, + pub search_context_cost_per_query: Option, + pub provider_specific_entry: BTreeMap, + pub guardrail_cost_per_unit: BTreeMap, + pub web_search_billing_unit: Option, + pub regional_processing_eu: Rate, + pub regional_processing_us: Rate, + pub regional_endpoint_uplift: Rate, + pub litellm_provider: Option, + pub ptu: Option, + pub extra: BTreeMap, +} + +impl ModelPricing { + pub fn rate(&self, metric: Metric) -> Rate { + self.base.get(&metric).copied().unwrap_or(Rate::Missing) + } + + pub fn tier_rate(&self, metric: Metric, tier: ServiceTier) -> Rate { + self.tiers + .get(&(metric, tier)) + .copied() + .unwrap_or(Rate::Missing) + } + + pub fn batch_rate(&self, metric: Metric) -> Rate { + self.batches.get(&metric).copied().unwrap_or(Rate::Missing) + } +} diff --git a/litellm-rust/crates/cost/src/wire.rs b/litellm-rust/crates/cost/src/wire.rs index dda30be28df..9623cd53dd0 100644 --- a/litellm-rust/crates/cost/src/wire.rs +++ b/litellm-rust/crates/cost/src/wire.rs @@ -9,6 +9,10 @@ use std::sync::LazyLock; use serde::Deserialize; use serde_json::{Map, Value}; +use crate::pricing::{ + ModelPricing, OffPeakPricing, OffPeakWindow, PtuPricing, Rate, SearchContextCostPerQuery, + ServiceTier, ThresholdPricing, TieredPricingTier, tokenize, +}; use crate::responses_usage::{ CacheCreationTokenDetails, CompletionTokenDetails, PromptTokenDetails, }; @@ -68,6 +72,341 @@ pub fn catalog_with_overlays( overlay_catalog(&EMBEDDED_CATALOG, overlays) } +pub fn rate(value: Option<&Value>) -> Rate { + match value { + None => Rate::Missing, + Some(Value::Null) => Rate::Null, + Some(Value::Number(number)) => number.as_f64().map_or(Rate::Invalid, Rate::Value), + Some(Value::Bool(flag)) => Rate::Value(f64::from(*flag)), + Some(Value::String(text)) => text + .trim() + .parse::() + .map_or(Rate::Invalid, Rate::Value), + Some(_) => Rate::Invalid, + } +} + +pub fn cost_per_unit( + fields: &Map, + cost_key: &str, + default: Option, +) -> Option { + match fields.get(cost_key) { + None | Some(Value::Null) => {} + Some(present) => return rate(Some(present)).value().or(default), + } + for tier in ServiceTier::SUFFIXES { + let suffix = format!("_{}", tier.as_str()); + if cost_key.contains(&suffix) { + let base = cost_key.replace(&suffix, ""); + return rate(fields.get(&base)).value().or(default); + } + } + default +} + +const NON_RATE_KEYS: [&str; 76] = [ + "audio_transcription_config", + "bedrock_converse_supports_strict_tools", + "bedrock_output_config_effort_ceiling", + "comment", + "default_reasoning_effort", + "deprecation_date", + "gemini_audio_only_live", + "gemini_native_audio", + "key", + "litellm_provider", + "max_input_tokens", + "max_output_tokens", + "max_tokens", + "metadata", + "mode", + "output_vector_size", + "prompt_cache_min_tokens", + "reasoning_effort_levels", + "rpm", + "source", + "supported_audio_formats", + "supported_endpoints", + "supported_modalities", + "supported_output_modalities", + "supported_regions", + "supported_openai_params", + "supports_adaptive_thinking", + "supports_anthropic_compaction", + "supports_anthropic_thinking_payload", + "supports_assistant_prefill", + "supports_audio_input", + "supports_audio_output", + "supports_computer_use", + "supports_embedding_image_input", + "supports_fast_mode", + "supports_forced_tool_use", + "supports_function_calling", + "supports_image_input", + "supports_image_size", + "supports_legacy_thinking", + "supports_low_reasoning_effort", + "supports_max_reasoning_effort", + "supports_mid_conversation_system", + "supports_minimal_reasoning_effort", + "supports_multimodal", + "supports_native_streaming", + "supports_native_structured_output", + "supports_none_reasoning_effort", + "supports_nova_canvas_image_edit", + "supports_output_config", + "supports_parallel_function_calling", + "supports_parallel_tool_use_config", + "supports_pdf_input", + "supports_prompt_cache_breakpoint", + "supports_prompt_caching", + "supports_reasoning", + "supports_response_schema", + "supports_sampling_params", + "supports_speed", + "supports_system_messages", + "supports_thinking_cache_preservation", + "supports_tool_choice", + "supports_tool_search", + "supports_url_context", + "supports_video_input", + "supports_vision", + "supports_web_search", + "supports_xhigh_reasoning_effort", + "thinking_always_on", + "tpm", + "use_openai_responses_path", + "uses_embed_content", + "vertex_ai_audio_api", + "web_search_billing_unit", + "ptu_effective_from", + "ptu_effective_to", +]; + +const TYPED_KEYS: [&str; 10] = [ + "off_peak_pricing", + "tiered_pricing", + "search_context_cost_per_query", + "provider_specific_entry", + "guardrail_cost_per_unit", + "regional_processing_uplift_multiplier_eu", + "regional_processing_uplift_multiplier_us", + "regional_endpoint_uplift_multiplier", + "ptu_count", + "cost_per_ptu_per_hour", +]; + +pub fn recognized_non_rate_key(key: &str) -> bool { + NON_RATE_KEYS.contains(&key) || TYPED_KEYS.contains(&key) +} + +fn hours_list(value: Option<&Value>) -> Vec { + match value { + Some(Value::String(text)) => vec![text.clone()], + Some(Value::Array(entries)) => entries + .iter() + .filter_map(Value::as_str) + .map(str::to_owned) + .collect(), + _ => Vec::new(), + } +} + +fn weekdays(value: Option<&Value>) -> Option> { + match value { + Some(Value::Array(entries)) => Some( + entries + .iter() + .filter_map(Value::as_str) + .map(str::to_owned) + .collect(), + ), + Some(Value::String(text)) => Some(vec![text.clone()]), + _ => None, + } +} + +fn parse_off_peak(block: &Value) -> OffPeakPricing { + OffPeakPricing { + input: rate(block.get("input_cost_per_token")), + output: rate(block.get("output_cost_per_token")), + cache_read: rate(block.get("cache_read_input_token_cost")), + cache_creation: rate(block.get("cache_creation_input_token_cost")), + output_reasoning: rate(block.get("output_cost_per_reasoning_token")), + hours_utc: hours_list(block.get("hours_utc")), + weekdays: weekdays(block.get("weekdays")), + weekday_timezone: block + .get("weekday_timezone") + .and_then(Value::as_str) + .map(str::to_owned), + windows: block + .get("windows") + .and_then(Value::as_array) + .map_or(Vec::new(), |rules| { + rules + .iter() + .map(|rule| OffPeakWindow { + hours_utc: hours_list(rule.get("hours_utc")), + weekdays: weekdays(rule.get("weekdays")), + }) + .collect() + }), + } +} + +fn parse_tiered_pricing(tiers: &Value) -> Vec { + tiers.as_array().map_or(Vec::new(), |entries| { + entries + .iter() + .map(|entry| { + let range = + entry + .get("range") + .and_then(Value::as_array) + .map_or((0.0, None), |bounds| { + let start = bounds.first().and_then(Value::as_f64).unwrap_or(0.0); + let end = bounds.get(1).and_then(Value::as_f64); + (start, end) + }); + TieredPricingTier { + range, + input: rate(entry.get("input_cost_per_token")), + output: rate(entry.get("output_cost_per_token")), + output_reasoning: rate(entry.get("output_cost_per_reasoning_token")), + cache_read: rate(entry.get("cache_read_input_token_cost")), + cache_creation: rate(entry.get("cache_creation_input_token_cost")), + } + }) + .collect() + }) +} + +fn parse_search_context(block: &Value) -> SearchContextCostPerQuery { + SearchContextCostPerQuery { + low: rate(block.get("search_context_size_low")), + medium: rate(block.get("search_context_size_medium")), + high: rate(block.get("search_context_size_high")), + } +} + +fn parse_rate_map(block: &Value) -> BTreeMap { + block.as_object().map_or(BTreeMap::new(), |entries| { + entries + .iter() + .map(|(key, value)| (key.clone(), rate(Some(value)))) + .collect() + }) +} + +fn parse_ptu(fields: &Map) -> Option { + fields + .get("cost_per_ptu_per_hour") + .map(|cost_per_hour| PtuPricing { + count: fields.get("ptu_count").and_then(Value::as_i64).unwrap_or(0), + cost_per_hour: rate(Some(cost_per_hour)), + effective_from: fields + .get("ptu_effective_from") + .and_then(Value::as_str) + .map(str::to_owned), + effective_to: fields + .get("ptu_effective_to") + .and_then(Value::as_str) + .map(str::to_owned), + }) +} + +impl RawCatalogEntry { + pub fn pricing(&self) -> ModelPricing { + let fields = self.fields(); + let mut pricing = ModelPricing { + web_search_billing_unit: fields + .get("web_search_billing_unit") + .and_then(Value::as_str) + .map(str::to_owned), + regional_processing_eu: rate(fields.get("regional_processing_uplift_multiplier_eu")), + regional_processing_us: rate(fields.get("regional_processing_uplift_multiplier_us")), + regional_endpoint_uplift: rate(fields.get("regional_endpoint_uplift_multiplier")), + litellm_provider: fields + .get("litellm_provider") + .and_then(Value::as_str) + .map(str::to_owned), + off_peak: fields.get("off_peak_pricing").map(parse_off_peak), + tiered_pricing: fields.get("tiered_pricing").map(parse_tiered_pricing), + search_context_cost_per_query: fields + .get("search_context_cost_per_query") + .map(parse_search_context), + provider_specific_entry: fields + .get("provider_specific_entry") + .map(parse_rate_map) + .unwrap_or_default(), + guardrail_cost_per_unit: fields + .get("guardrail_cost_per_unit") + .map(parse_rate_map) + .unwrap_or_default(), + ptu: parse_ptu(fields), + ..ModelPricing::default() + }; + let mut thresholds: BTreeMap = BTreeMap::new(); + for (key, value) in fields { + if NON_RATE_KEYS.contains(&key.as_str()) || TYPED_KEYS.contains(&key.as_str()) { + continue; + } + let Ok(rate_key) = tokenize(key) else { + pricing.extra.insert(key.clone(), rate(Some(value))); + continue; + }; + let parsed_rate = rate(Some(value)); + match (rate_key.threshold, rate_key.tier, rate_key.batch) { + (None, None, false) => { + pricing.base.insert(rate_key.metric, parsed_rate); + } + (None, Some(tier), false) => { + pricing.tiers.insert((rate_key.metric, tier), parsed_rate); + } + (None, None, true) => { + pricing.batches.insert(rate_key.metric, parsed_rate); + } + (None, Some(tier), true) => { + pricing + .batch_tiers + .insert((rate_key.metric, tier), parsed_rate); + } + (Some(above_tokens), tier, batch) => { + let entry = + thresholds + .entry(above_tokens) + .or_insert_with(|| ThresholdPricing { + above_tokens, + standard: BTreeMap::new(), + tiers: BTreeMap::new(), + batches: BTreeMap::new(), + batch_tiers: BTreeMap::new(), + }); + match (tier, batch) { + (None, false) => { + entry.standard.insert(rate_key.metric, parsed_rate); + } + (Some(tier), false) => { + entry.tiers.insert((rate_key.metric, tier), parsed_rate); + } + (None, true) => { + entry.batches.insert(rate_key.metric, parsed_rate); + } + (Some(tier), true) => { + entry + .batch_tiers + .insert((rate_key.metric, tier), parsed_rate); + } + } + } + } + } + pricing.thresholds = thresholds.into_values().collect(); + pricing + } +} + fn count_value(value: &Value) -> Option { match value { Value::Null => Some(0), diff --git a/litellm-rust/crates/cost/tests/generate_python_fixtures.py b/litellm-rust/crates/cost/tests/generate_python_fixtures.py index 013081e04a5..e7cf9018e69 100644 --- a/litellm-rust/crates/cost/tests/generate_python_fixtures.py +++ b/litellm-rust/crates/cost/tests/generate_python_fixtures.py @@ -21,7 +21,7 @@ from litellm.litellm_core_utils.llm_cost_calc.utils import ( get_billed_token_rates, ) from litellm.responses.utils import ResponseAPILoggingUtils -from litellm.utils import register_model +from litellm.utils import get_model_info, register_model from litellm.types.utils import Choices, Message, ModelResponse, Usage from openai.types.images_response import ImagesResponse @@ -1057,6 +1057,95 @@ def billed_rates_surface() -> list[dict[str, Any]]: return rows +def pinning_threshold_cases() -> list[str]: + return [ + "input_cost_per_token_above_128k_tokens", + "input_cost_per_token_above_200k_tokens", + "input_cost_per_token_above_256k_tokens", + "input_cost_per_token_above_272k_tokens", + "input_cost_per_token_above_512k_tokens", + "input_cost_per_token_above_1k_tokens", + "input_cost_per_token_above_500_tokens", + "cache_creation_input_token_cost_above_1hr_above_200k_tokens", + "cache_creation_input_token_cost_above_1hr", + "input_cost_per_video_per_second_above_15s_interval", + "input_cost_per_token", + ] + + +PINNING_COST_PER_UNIT_CASES: tuple[tuple[dict[str, Any], str, float | None], ...] = ( + ({"input_cost_per_token": 3e-6}, "input_cost_per_token", None), + ({"input_cost_per_token": 3}, "input_cost_per_token", None), + ({"input_cost_per_token": True}, "input_cost_per_token", None), + ({"input_cost_per_token": False}, "input_cost_per_token", None), + ({"input_cost_per_token": " 0.5 "}, "input_cost_per_token", None), + ({"input_cost_per_token": "garbage"}, "input_cost_per_token", None), + ({"input_cost_per_token": "garbage"}, "input_cost_per_token", 0.0), + ({}, "input_cost_per_token", None), + ({}, "input_cost_per_token", 0.0), + ({"input_cost_per_token": None}, "input_cost_per_token", None), + ({"input_cost_per_token": [1]}, "input_cost_per_token", None), + ({"input_cost_per_token": 1.0}, "input_cost_per_token_priority", None), + ({"input_cost_per_token": 1.0}, "input_cost_per_token_auto", None), + ({"input_cost_per_token": 1.0}, "input_cost_per_token_flex", None), + ({"input_cost_per_token_priority": 2.0}, "input_cost_per_token_priority", None), + ({"input_cost_per_token_flex": 2.0}, "input_cost_per_token_priority", None), + ({"input_cost_per_token": 1.0, "input_cost_per_token_priority": 2.0}, "input_cost_per_token_priority", None), + ({"input_cost_per_token_ultrafast": 2.0}, "input_cost_per_token_fast", None), +) + + +def pinning_surface() -> dict[str, Any]: + from litellm.litellm_core_utils.llm_cost_calc import utils as cost_utils + + thresholds: list[dict[str, Any]] = [] + for key in pinning_threshold_cases(): + try: + thresholds.append({"key": key, "threshold": cost_utils._parse_above_token_threshold(key)}) + except Exception as error: + thresholds.append({"key": key, "error": type(error).__name__}) + + cost_per_unit: list[dict[str, Any]] = [] + for model_info, cost_key, default in PINNING_COST_PER_UNIT_CASES: + result = cost_utils._get_cost_per_unit(model_info, cost_key, default) + cost_per_unit.append( + { + "model_info": model_info, + "cost_key": cost_key, + "default": default, + "result": result, + } + ) + return { + "thresholds": thresholds, + "service_tier_suffixes": list(cost_utils._SERVICE_TIER_SUFFIXES), + "batch_tier_key": cost_utils._BATCH_TIER_KEY.pattern, + "cost_per_unit": cost_per_unit, + } + + +def model_info_projection_surface() -> dict[str, Any]: + entry = { + "max_tokens": 4_096, + "max_input_tokens": 4_096, + "max_output_tokens": 1_024, + "input_cost_per_token": 1e-6, + "output_cost_per_token": 2e-6, + "input_cost_per_token_above_128k_tokens": 5e-7, + "input_cost_per_token_above_128k_tokens_priority": 6e-7, + "cache_read_input_token_cost": 1e-7, + "litellm_provider": "synthetic", + "mode": "chat", + "supports_function_calling": True, + "rpm": 100, + "made_up_capability": "x", + "input_cost_per_gadget": 0.5, + } + register_model({"synthetic-pinning-model": entry}) + info = get_model_info(model="synthetic-pinning-model", custom_llm_provider="synthetic") + return {"registered": sorted(entry.keys()), "projected": sorted(info.keys())} + + def main() -> None: revision = subprocess.check_output(("git", "rev-parse", "HEAD"), text=True).strip() fixture = { @@ -1070,10 +1159,16 @@ def main() -> None: "anthropic_usage": anthropic_usage_surface(), "wire_usage": wire_usage_surface(), "wire_response": wire_response_surface(), + "pinning": pinning_surface(), + "model_info_projection": model_info_projection_surface(), } target = Path(__file__).parent / "python_fixtures.json" target.write_text(json.dumps(fixture, indent=1) + "\n") - counts = {key: len(value) for key, value in fixture.items() if isinstance(value, list)} + counts = { + key: len(value) + for key, value in fixture.items() + if isinstance(value, (list, dict)) and key != "model_info_projection" + } print(json.dumps(counts)) diff --git a/litellm-rust/crates/cost/tests/pricing_grammar.rs b/litellm-rust/crates/cost/tests/pricing_grammar.rs new file mode 100644 index 00000000000..12a50ebc644 --- /dev/null +++ b/litellm-rust/crates/cost/tests/pricing_grammar.rs @@ -0,0 +1,181 @@ +#![allow(clippy::disallowed_types)] + +use std::sync::LazyLock; + +use litellm_cost::pricing::{Metric, Rate, ServiceTier, tokenize}; +use litellm_cost::wire::{EMBEDDED_CATALOG, rate}; + +#[test] +fn every_embedded_key_is_recognized_by_the_tokenizer() { + let mut offenders: Vec = Vec::new(); + for (model, entry) in EMBEDDED_CATALOG.iter() { + let pricing = entry.pricing(); + for key in pricing.extra.keys() { + offenders.push(format!("{model}: {key}")); + } + } + assert!( + offenders.is_empty(), + "unrecognized keys in the embedded catalog (a new key shape would ship silently unpriced): {}", + offenders.join(", ") + ); +} + +#[test] +fn embedded_catalog_yields_decorated_and_plain_rates() { + let mut thresholds = 0_usize; + let mut tiers = 0_usize; + let mut batches = 0_usize; + let mut plain = 0_usize; + for entry in EMBEDDED_CATALOG.values() { + let pricing = entry.pricing(); + thresholds += pricing.thresholds.len(); + tiers += pricing.tiers.len(); + batches += pricing.batches.len(); + plain += pricing.base.len(); + } + assert!(thresholds > 0); + assert!(tiers > 0); + assert!(batches > 0); + assert!(plain > 0); +} + +#[test] +fn tokenizer_applies_the_rate_key_grammar() { + let plain = tokenize("input_cost_per_token").expect("plain rate key"); + assert_eq!(plain.metric, Metric::InputPerToken); + assert_eq!(plain.threshold, None); + assert_eq!(plain.tier, None); + assert!(!plain.batch); + + let threshold = tokenize("input_cost_per_token_above_128k_tokens").expect("threshold key"); + assert_eq!(threshold.metric, Metric::InputPerToken); + assert_eq!(threshold.threshold, Some(128_000)); + + let plain_threshold = + tokenize("input_cost_per_token_above_512k_tokens").expect("threshold key"); + assert_eq!(plain_threshold.threshold, Some(512_000)); + + let one_hour = tokenize("cache_creation_input_token_cost_above_1hr").expect("1hr is a metric"); + assert_eq!(one_hour.metric, Metric::CacheCreationToken1hr); + assert_eq!(one_hour.threshold, None); + + let one_hour_threshold = + tokenize("cache_creation_input_token_cost_above_1hr_above_200k_tokens") + .expect("1hr metric with threshold"); + assert_eq!(one_hour_threshold.metric, Metric::CacheCreationToken1hr); + assert_eq!(one_hour_threshold.threshold, Some(200_000)); + + let interval = tokenize("input_cost_per_video_per_second_above_15s_interval") + .expect("interval-suffixed metric stays whole"); + assert_eq!(interval.metric, Metric::InputVideoPerSecond15sInterval); + assert_eq!(interval.threshold, None); + + let tiered = tokenize("input_cost_per_token_priority").expect("tier key"); + assert_eq!(tiered.tier, Some(ServiceTier::Priority)); + + let auto = tokenize("input_cost_per_token_auto").expect("auto is a grammar tier"); + assert_eq!(auto.tier, Some(ServiceTier::Auto)); + + let full = tokenize("cache_read_input_token_cost_above_272k_tokens_flex") + .expect("threshold plus tier"); + assert_eq!(full.metric, Metric::CacheReadToken); + assert_eq!(full.threshold, Some(272_000)); + assert_eq!(full.tier, Some(ServiceTier::Flex)); + + let batch = tokenize("cache_read_input_token_cost_above_272k_tokens_batches") + .expect("threshold plus batch"); + assert_eq!(batch.threshold, Some(272_000)); + assert!(batch.batch); +} + +#[test] +fn tokenizer_rejects_unknown_metrics_and_malformed_decorations() { + assert!(tokenize("input_cost_per_gadget").is_err()); + assert!(tokenize("input_cost_per_token_standard").is_err()); + assert!(tokenize("input_cost_per_token_above_128.5k_tokens").is_err()); + assert!(tokenize("input_cost_per_token_above_tokens").is_err()); + assert!(tokenize("max_tokens").is_err()); +} + +#[test] +fn rate_parser_mirrors_get_cost_per_unit_coercions() { + use serde_json::json; + assert_eq!(rate(None), Rate::Missing); + assert_eq!(rate(Some(&json!(null))), Rate::Null); + assert_eq!(rate(Some(&json!(true))), Rate::Value(1.0)); + assert_eq!(rate(Some(&json!(false))), Rate::Value(0.0)); + assert_eq!(rate(Some(&json!(3))), Rate::Value(3.0)); + assert_eq!(rate(Some(&json!(3.5))), Rate::Value(3.5)); + assert_eq!(rate(Some(&json!(" 0.5 "))), Rate::Value(0.5)); + assert_eq!(rate(Some(&json!("not a number"))), Rate::Invalid); + assert_eq!(rate(Some(&json!([1]))), Rate::Invalid); + assert_eq!(rate(Some(&json!({"cost": 1}))), Rate::Invalid); +} + +#[test] +fn parsed_pricing_buckets_a_synthetic_overlay_entry() { + let entry: litellm_cost::wire::RawCatalogEntry = serde_json::from_value(serde_json::json!({ + "input_cost_per_token": 3e-6, + "output_cost_per_token": 15e-6, + "input_cost_per_token_above_128k_tokens": 1.5e-6, + "input_cost_per_token_priority": 6e-6, + "input_cost_per_token_batches": 1.5e-6, + "cache_creation_input_token_cost_above_1hr_above_200k_tokens": 1e-6, + "off_peak_pricing": { + "input_cost_per_token": 1e-6, + "hours_utc": "22:00-06:00" + }, + "tiered_pricing": [ + {"range": [0, 16000], "input_cost_per_token": 1e-6, "output_cost_per_token": 2e-6} + ], + "search_context_cost_per_query": {"search_context_size_medium": 3e-6}, + "provider_specific_entry": {"us": 1.1}, + "litellm_provider": "synthetic", + "max_tokens": 8192, + "supports_function_calling": true + })) + .expect("synthetic entry parses"); + let pricing = entry.pricing(); + assert_eq!(pricing.rate(Metric::InputPerToken), Rate::Value(3e-6)); + assert_eq!( + pricing.tier_rate(Metric::InputPerToken, ServiceTier::Priority), + Rate::Value(6e-6) + ); + assert_eq!( + pricing.batch_rate(Metric::InputPerToken), + Rate::Value(1.5e-6) + ); + let threshold = &pricing.thresholds[0]; + assert_eq!(threshold.above_tokens, 128_000); + assert_eq!( + threshold.standard.get(&Metric::InputPerToken), + Some(&Rate::Value(1.5e-6)) + ); + let one_hour = &pricing.thresholds[1]; + assert_eq!(one_hour.above_tokens, 200_000); + assert!( + one_hour + .standard + .contains_key(&Metric::CacheCreationToken1hr) + ); + let off_peak = pricing.off_peak.as_ref().expect("off peak parsed"); + assert_eq!(off_peak.input, Rate::Value(1e-6)); + assert_eq!(off_peak.hours_utc, vec!["22:00-06:00".to_string()]); + let tier = &pricing.tiered_pricing.as_ref().expect("tiered parsed")[0]; + assert_eq!(tier.range, (0.0, Some(16_000.0))); + assert_eq!( + pricing + .search_context_cost_per_query + .expect("search context") + .medium, + Rate::Value(3e-6) + ); + assert_eq!( + pricing.provider_specific_entry.get("us"), + Some(&Rate::Value(1.1)) + ); + assert_eq!(pricing.litellm_provider.as_deref(), Some("synthetic")); + assert!(pricing.extra.is_empty(), "recognized non-rate keys drop"); + let _: LazyLock<()> = LazyLock::new(|| ()); +} diff --git a/litellm-rust/crates/cost/tests/python_a2a_mcp_cost.rs b/litellm-rust/crates/cost/tests/python_a2a_mcp_cost.rs index 2c11baa7848..6113529422a 100644 --- a/litellm-rust/crates/cost/tests/python_a2a_mcp_cost.rs +++ b/litellm-rust/crates/cost/tests/python_a2a_mcp_cost.rs @@ -1,3 +1,5 @@ +#![allow(clippy::disallowed_types)] + use litellm_cost::a2a_cost::calculate_a2a_cost; use litellm_cost::mcp_cost::calculate_mcp_tool_call_cost; use rstest::rstest; diff --git a/litellm-rust/crates/cost/tests/python_anthropic_cost.rs b/litellm-rust/crates/cost/tests/python_anthropic_cost.rs index 484640b89bb..f6199e8163d 100644 --- a/litellm-rust/crates/cost/tests/python_anthropic_cost.rs +++ b/litellm-rust/crates/cost/tests/python_anthropic_cost.rs @@ -1,3 +1,5 @@ +#![allow(clippy::disallowed_types)] + use std::collections::HashMap; use jiff::Timestamp; diff --git a/litellm-rust/crates/cost/tests/python_anthropic_usage.rs b/litellm-rust/crates/cost/tests/python_anthropic_usage.rs index 94ccdde8ea6..0011fe4c65b 100644 --- a/litellm-rust/crates/cost/tests/python_anthropic_usage.rs +++ b/litellm-rust/crates/cost/tests/python_anthropic_usage.rs @@ -1,3 +1,5 @@ +#![allow(clippy::disallowed_types)] + use litellm_cost::anthropic_usage::{ is_anthropic_usage_object, transform_anthropic_usage_to_chat_usage, }; diff --git a/litellm-rust/crates/cost/tests/python_azure_ai_cost.rs b/litellm-rust/crates/cost/tests/python_azure_ai_cost.rs index 6f82cd7cbf8..bd015804c31 100644 --- a/litellm-rust/crates/cost/tests/python_azure_ai_cost.rs +++ b/litellm-rust/crates/cost/tests/python_azure_ai_cost.rs @@ -1,3 +1,5 @@ +#![allow(clippy::disallowed_types)] + use std::collections::HashMap; use jiff::Timestamp; diff --git a/litellm-rust/crates/cost/tests/python_azure_ai_image_cost.rs b/litellm-rust/crates/cost/tests/python_azure_ai_image_cost.rs index ed748b3f867..d85714eaae7 100644 --- a/litellm-rust/crates/cost/tests/python_azure_ai_image_cost.rs +++ b/litellm-rust/crates/cost/tests/python_azure_ai_image_cost.rs @@ -1,3 +1,5 @@ +#![allow(clippy::disallowed_types)] + use std::collections::HashMap; use jiff::Timestamp; diff --git a/litellm-rust/crates/cost/tests/python_base_rate_selection.rs b/litellm-rust/crates/cost/tests/python_base_rate_selection.rs index e294114b082..adc8695d038 100644 --- a/litellm-rust/crates/cost/tests/python_base_rate_selection.rs +++ b/litellm-rust/crates/cost/tests/python_base_rate_selection.rs @@ -1,3 +1,5 @@ +#![allow(clippy::disallowed_types)] + use litellm_cost::base_rate_selection::{ TokenBaseRates, get_tiered_reasoning_rate, get_token_base_cost_without_off_peak, uses_inclusive_token_thresholds, diff --git a/litellm-rust/crates/cost/tests/python_bedrock_image_cost.rs b/litellm-rust/crates/cost/tests/python_bedrock_image_cost.rs index 10aea4b7398..a0e5d2ba872 100644 --- a/litellm-rust/crates/cost/tests/python_bedrock_image_cost.rs +++ b/litellm-rust/crates/cost/tests/python_bedrock_image_cost.rs @@ -1,3 +1,5 @@ +#![allow(clippy::disallowed_types)] + use std::collections::HashMap; use jiff::Timestamp; diff --git a/litellm-rust/crates/cost/tests/python_billed_token_rates.rs b/litellm-rust/crates/cost/tests/python_billed_token_rates.rs index 02ef4e13c23..9a4259cbc55 100644 --- a/litellm-rust/crates/cost/tests/python_billed_token_rates.rs +++ b/litellm-rust/crates/cost/tests/python_billed_token_rates.rs @@ -1,3 +1,5 @@ +#![allow(clippy::disallowed_types)] + use std::collections::HashMap; use jiff::Timestamp; diff --git a/litellm-rust/crates/cost/tests/python_call_dispatch.rs b/litellm-rust/crates/cost/tests/python_call_dispatch.rs index 85f16080e15..e010583ff95 100644 --- a/litellm-rust/crates/cost/tests/python_call_dispatch.rs +++ b/litellm-rust/crates/cost/tests/python_call_dispatch.rs @@ -1,3 +1,5 @@ +#![allow(clippy::disallowed_types)] + use std::collections::HashMap; use litellm_cost::catalog::{CatalogCallError, CostCall, ModelCostRequest, ModelInfoCatalog}; diff --git a/litellm-rust/crates/cost/tests/python_completion_cost.rs b/litellm-rust/crates/cost/tests/python_completion_cost.rs index 0484e807b74..d207999ed43 100644 --- a/litellm-rust/crates/cost/tests/python_completion_cost.rs +++ b/litellm-rust/crates/cost/tests/python_completion_cost.rs @@ -1,3 +1,5 @@ +#![allow(clippy::disallowed_types)] + use std::collections::HashMap; use jiff::Timestamp; diff --git a/litellm-rust/crates/cost/tests/python_completion_input.rs b/litellm-rust/crates/cost/tests/python_completion_input.rs index f628cff7830..202b00ebddb 100644 --- a/litellm-rust/crates/cost/tests/python_completion_input.rs +++ b/litellm-rust/crates/cost/tests/python_completion_input.rs @@ -1,3 +1,5 @@ +#![allow(clippy::disallowed_types)] + use std::collections::HashMap; use jiff::Timestamp; diff --git a/litellm-rust/crates/cost/tests/python_completion_response.rs b/litellm-rust/crates/cost/tests/python_completion_response.rs index 77e414021d2..aa0ad9a388c 100644 --- a/litellm-rust/crates/cost/tests/python_completion_response.rs +++ b/litellm-rust/crates/cost/tests/python_completion_response.rs @@ -1,3 +1,5 @@ +#![allow(clippy::disallowed_types)] + use std::collections::HashMap; use litellm_cost::catalog::ModelInfoCatalog; diff --git a/litellm-rust/crates/cost/tests/python_cost_breakdown.rs b/litellm-rust/crates/cost/tests/python_cost_breakdown.rs index 064ba1358cf..c7c12a76b1c 100644 --- a/litellm-rust/crates/cost/tests/python_cost_breakdown.rs +++ b/litellm-rust/crates/cost/tests/python_cost_breakdown.rs @@ -1,3 +1,5 @@ +#![allow(clippy::disallowed_types)] + use std::collections::BTreeMap; use litellm_cost::billed_token_rates::{BilledTokenRates, TokenTypeCostBreakdown}; diff --git a/litellm-rust/crates/cost/tests/python_cost_calculator.rs b/litellm-rust/crates/cost/tests/python_cost_calculator.rs index e6e48769b9f..fe65c60844a 100644 --- a/litellm-rust/crates/cost/tests/python_cost_calculator.rs +++ b/litellm-rust/crates/cost/tests/python_cost_calculator.rs @@ -1,3 +1,5 @@ +#![allow(clippy::disallowed_types)] + use std::collections::HashMap; use litellm_cost::batch::{ diff --git a/litellm-rust/crates/cost/tests/python_custom_pricing.rs b/litellm-rust/crates/cost/tests/python_custom_pricing.rs index 9b04a17c2bd..4b6db67b941 100644 --- a/litellm-rust/crates/cost/tests/python_custom_pricing.rs +++ b/litellm-rust/crates/cost/tests/python_custom_pricing.rs @@ -1,3 +1,5 @@ +#![allow(clippy::disallowed_types)] + use std::collections::HashMap; use litellm_cost::catalog::{CatalogCustomError, CostCall, ModelCostRequest, ModelInfoCatalog}; diff --git a/litellm-rust/crates/cost/tests/python_dashscope_cost.rs b/litellm-rust/crates/cost/tests/python_dashscope_cost.rs index 26a99076829..df3e3182f92 100644 --- a/litellm-rust/crates/cost/tests/python_dashscope_cost.rs +++ b/litellm-rust/crates/cost/tests/python_dashscope_cost.rs @@ -1,3 +1,5 @@ +#![allow(clippy::disallowed_types)] + use std::collections::HashMap; use jiff::Timestamp; diff --git a/litellm-rust/crates/cost/tests/python_fal_ai_image_cost.rs b/litellm-rust/crates/cost/tests/python_fal_ai_image_cost.rs index 0e79beff1f5..23fcdb9e3c2 100644 --- a/litellm-rust/crates/cost/tests/python_fal_ai_image_cost.rs +++ b/litellm-rust/crates/cost/tests/python_fal_ai_image_cost.rs @@ -1,3 +1,5 @@ +#![allow(clippy::disallowed_types)] + use std::collections::HashMap; use jiff::Timestamp; diff --git a/litellm-rust/crates/cost/tests/python_fireworks_cost.rs b/litellm-rust/crates/cost/tests/python_fireworks_cost.rs index f4fc164d30f..be911604b41 100644 --- a/litellm-rust/crates/cost/tests/python_fireworks_cost.rs +++ b/litellm-rust/crates/cost/tests/python_fireworks_cost.rs @@ -1,3 +1,5 @@ +#![allow(clippy::disallowed_types)] + use std::collections::HashMap; use jiff::Timestamp; diff --git a/litellm-rust/crates/cost/tests/python_fixture_replay.rs b/litellm-rust/crates/cost/tests/python_fixture_replay.rs index 9eb535d71af..562c4088c9e 100644 --- a/litellm-rust/crates/cost/tests/python_fixture_replay.rs +++ b/litellm-rust/crates/cost/tests/python_fixture_replay.rs @@ -1,3 +1,5 @@ +#![allow(clippy::disallowed_types)] + use std::collections::HashMap; use jiff::Timestamp; diff --git a/litellm-rust/crates/cost/tests/python_fixtures.json b/litellm-rust/crates/cost/tests/python_fixtures.json index 7e4e6e5df12..cb299fd5d4a 100644 --- a/litellm-rust/crates/cost/tests/python_fixtures.json +++ b/litellm-rust/crates/cost/tests/python_fixtures.json @@ -1,5 +1,5 @@ { - "python_commit": "492ac2e9c43ed8fc9ae18650f6d41987ff7ae0fc", + "python_commit": "a8725892a4d1986e09e6db7a736f872083479de1", "generic": [ { "id": "audio_mixed_modalities", @@ -2122,5 +2122,373 @@ } } } - ] + ], + "pinning": { + "thresholds": [ + { + "key": "input_cost_per_token_above_128k_tokens", + "threshold": 128000.0 + }, + { + "key": "input_cost_per_token_above_200k_tokens", + "threshold": 200000.0 + }, + { + "key": "input_cost_per_token_above_256k_tokens", + "threshold": 256000.0 + }, + { + "key": "input_cost_per_token_above_272k_tokens", + "threshold": 272000.0 + }, + { + "key": "input_cost_per_token_above_512k_tokens", + "threshold": 512000.0 + }, + { + "key": "input_cost_per_token_above_1k_tokens", + "threshold": 1000.0 + }, + { + "key": "input_cost_per_token_above_500_tokens", + "threshold": 500.0 + }, + { + "key": "cache_creation_input_token_cost_above_1hr_above_200k_tokens", + "error": "ValueError" + }, + { + "key": "cache_creation_input_token_cost_above_1hr", + "error": "ValueError" + }, + { + "key": "input_cost_per_video_per_second_above_15s_interval", + "error": "ValueError" + }, + { + "key": "input_cost_per_token", + "error": "IndexError" + } + ], + "service_tier_suffixes": [ + "_ultrafast", + "_priority", + "_auto", + "_flex", + "_fast" + ], + "batch_tier_key": "^(input_cost_per_token|output_cost_per_token|cache_read_input_token_cost|cache_creation_input_token_cost)_above_(\\d+k?)_tokens_batches$", + "cost_per_unit": [ + { + "model_info": { + "input_cost_per_token": 3e-06 + }, + "cost_key": "input_cost_per_token", + "default": null, + "result": 3e-06 + }, + { + "model_info": { + "input_cost_per_token": 3 + }, + "cost_key": "input_cost_per_token", + "default": null, + "result": 3.0 + }, + { + "model_info": { + "input_cost_per_token": true + }, + "cost_key": "input_cost_per_token", + "default": null, + "result": 1.0 + }, + { + "model_info": { + "input_cost_per_token": false + }, + "cost_key": "input_cost_per_token", + "default": null, + "result": 0.0 + }, + { + "model_info": { + "input_cost_per_token": " 0.5 " + }, + "cost_key": "input_cost_per_token", + "default": null, + "result": 0.5 + }, + { + "model_info": { + "input_cost_per_token": "garbage" + }, + "cost_key": "input_cost_per_token", + "default": null, + "result": null + }, + { + "model_info": { + "input_cost_per_token": "garbage" + }, + "cost_key": "input_cost_per_token", + "default": 0.0, + "result": 0.0 + }, + { + "model_info": {}, + "cost_key": "input_cost_per_token", + "default": null, + "result": null + }, + { + "model_info": {}, + "cost_key": "input_cost_per_token", + "default": 0.0, + "result": 0.0 + }, + { + "model_info": { + "input_cost_per_token": null + }, + "cost_key": "input_cost_per_token", + "default": null, + "result": null + }, + { + "model_info": { + "input_cost_per_token": [ + 1 + ] + }, + "cost_key": "input_cost_per_token", + "default": null, + "result": null + }, + { + "model_info": { + "input_cost_per_token": 1.0 + }, + "cost_key": "input_cost_per_token_priority", + "default": null, + "result": 1.0 + }, + { + "model_info": { + "input_cost_per_token": 1.0 + }, + "cost_key": "input_cost_per_token_auto", + "default": null, + "result": 1.0 + }, + { + "model_info": { + "input_cost_per_token": 1.0 + }, + "cost_key": "input_cost_per_token_flex", + "default": null, + "result": 1.0 + }, + { + "model_info": { + "input_cost_per_token_priority": 2.0 + }, + "cost_key": "input_cost_per_token_priority", + "default": null, + "result": 2.0 + }, + { + "model_info": { + "input_cost_per_token_flex": 2.0 + }, + "cost_key": "input_cost_per_token_priority", + "default": null, + "result": null + }, + { + "model_info": { + "input_cost_per_token": 1.0, + "input_cost_per_token_priority": 2.0 + }, + "cost_key": "input_cost_per_token_priority", + "default": null, + "result": 2.0 + }, + { + "model_info": { + "input_cost_per_token_ultrafast": 2.0 + }, + "cost_key": "input_cost_per_token_fast", + "default": null, + "result": null + } + ] + }, + "model_info_projection": { + "registered": [ + "cache_read_input_token_cost", + "input_cost_per_gadget", + "input_cost_per_token", + "input_cost_per_token_above_128k_tokens", + "input_cost_per_token_above_128k_tokens_priority", + "litellm_provider", + "made_up_capability", + "max_input_tokens", + "max_output_tokens", + "max_tokens", + "mode", + "output_cost_per_token", + "rpm", + "supports_function_calling" + ], + "projected": [ + "annotation_cost_per_page", + "annotation_cost_per_page_batches", + "bedrock_converse_supports_strict_tools", + "bedrock_output_config_effort_ceiling", + "cache_creation_input_token_cost", + "cache_creation_input_token_cost_above_1hr", + "cache_creation_input_token_cost_above_200k_tokens", + "cache_creation_input_token_cost_above_272k_tokens", + "cache_creation_input_token_cost_above_272k_tokens_batches", + "cache_creation_input_token_cost_above_272k_tokens_flex", + "cache_creation_input_token_cost_above_272k_tokens_priority", + "cache_creation_input_token_cost_batches", + "cache_creation_input_token_cost_flex", + "cache_creation_input_token_cost_priority", + "cache_creation_input_token_cost_ultrafast", + "cache_read_input_audio_token_cost", + "cache_read_input_token_cost", + "cache_read_input_token_cost_above_200k_tokens", + "cache_read_input_token_cost_above_200k_tokens_priority", + "cache_read_input_token_cost_above_272k_tokens", + "cache_read_input_token_cost_above_272k_tokens_batches", + "cache_read_input_token_cost_above_272k_tokens_flex", + "cache_read_input_token_cost_above_272k_tokens_priority", + "cache_read_input_token_cost_above_512k_tokens", + "cache_read_input_token_cost_batches", + "cache_read_input_token_cost_flex", + "cache_read_input_token_cost_priority", + "cache_read_input_token_cost_ultrafast", + "citation_cost_per_token", + "default_reasoning_effort", + "google_maps_grounding_cost_per_query", + "input_cost_per_audio_per_second", + "input_cost_per_audio_token", + "input_cost_per_audio_token_batches", + "input_cost_per_character", + "input_cost_per_image", + "input_cost_per_image_token", + "input_cost_per_image_token_batches", + "input_cost_per_query", + "input_cost_per_second", + "input_cost_per_token", + "input_cost_per_token_above_128k_tokens", + "input_cost_per_token_above_200k_tokens", + "input_cost_per_token_above_200k_tokens_priority", + "input_cost_per_token_above_272k_tokens", + "input_cost_per_token_above_272k_tokens_batches", + "input_cost_per_token_above_272k_tokens_flex", + "input_cost_per_token_above_272k_tokens_priority", + "input_cost_per_token_above_512k_tokens", + "input_cost_per_token_batches", + "input_cost_per_token_flex", + "input_cost_per_token_priority", + "input_cost_per_token_ultrafast", + "input_cost_per_video_per_second", + "input_cost_per_video_token", + "input_cost_per_video_token_batches", + "key", + "litellm_provider", + "max_input_tokens", + "max_output_tokens", + "max_tokens", + "mode", + "ocr_cost_per_credit", + "ocr_cost_per_page", + "ocr_cost_per_page_batches", + "off_peak_pricing", + "output_cost_per_audio_token", + "output_cost_per_character", + "output_cost_per_character_above_128k_tokens", + "output_cost_per_image", + "output_cost_per_image_token", + "output_cost_per_pixel", + "output_cost_per_reasoning_token", + "output_cost_per_reasoning_token_flex", + "output_cost_per_reasoning_token_priority", + "output_cost_per_second", + "output_cost_per_second_1080p", + "output_cost_per_second_2k", + "output_cost_per_second_480p", + "output_cost_per_second_4k", + "output_cost_per_second_720p", + "output_cost_per_second_768p", + "output_cost_per_token", + "output_cost_per_token_above_128k_tokens", + "output_cost_per_token_above_200k_tokens", + "output_cost_per_token_above_200k_tokens_priority", + "output_cost_per_token_above_272k_tokens", + "output_cost_per_token_above_272k_tokens_batches", + "output_cost_per_token_above_272k_tokens_flex", + "output_cost_per_token_above_272k_tokens_priority", + "output_cost_per_token_above_512k_tokens", + "output_cost_per_token_batches", + "output_cost_per_token_flex", + "output_cost_per_token_priority", + "output_cost_per_token_ultrafast", + "output_cost_per_video_per_second", + "output_cost_per_video_token", + "output_vector_size", + "prompt_cache_min_tokens", + "provider_specific_entry", + "reasoning_effort_levels", + "regional_endpoint_uplift_multiplier", + "regional_processing_uplift_multiplier_eu", + "regional_processing_uplift_multiplier_us", + "rpm", + "search_context_cost_per_query", + "supported_audio_formats", + "supported_endpoints", + "supported_openai_params", + "supports_adaptive_thinking", + "supports_anthropic_compaction", + "supports_anthropic_thinking_payload", + "supports_assistant_prefill", + "supports_audio_input", + "supports_audio_output", + "supports_computer_use", + "supports_embedding_image_input", + "supports_function_calling", + "supports_image_size", + "supports_legacy_thinking", + "supports_low_reasoning_effort", + "supports_max_reasoning_effort", + "supports_mid_conversation_system", + "supports_minimal_reasoning_effort", + "supports_native_streaming", + "supports_native_structured_output", + "supports_none_reasoning_effort", + "supports_parallel_function_calling", + "supports_pdf_input", + "supports_prompt_cache_breakpoint", + "supports_prompt_caching", + "supports_reasoning", + "supports_response_schema", + "supports_system_messages", + "supports_thinking_cache_preservation", + "supports_tool_choice", + "supports_tool_search", + "supports_url_context", + "supports_vision", + "supports_web_search", + "supports_xhigh_reasoning_effort", + "thinking_always_on", + "tiered_pricing", + "tpm", + "uses_embed_content", + "vertex_ai_audio_api", + "web_search_billing_unit" + ] + } } diff --git a/litellm-rust/crates/cost/tests/python_gemini_cost.rs b/litellm-rust/crates/cost/tests/python_gemini_cost.rs index fa3447a85e6..ad25eb2b99c 100644 --- a/litellm-rust/crates/cost/tests/python_gemini_cost.rs +++ b/litellm-rust/crates/cost/tests/python_gemini_cost.rs @@ -1,3 +1,5 @@ +#![allow(clippy::disallowed_types)] + use std::collections::BTreeMap; use litellm_cost::gemini_cost::{ diff --git a/litellm-rust/crates/cost/tests/python_generic_cost.rs b/litellm-rust/crates/cost/tests/python_generic_cost.rs index 482ac504ec0..2541b853a2f 100644 --- a/litellm-rust/crates/cost/tests/python_generic_cost.rs +++ b/litellm-rust/crates/cost/tests/python_generic_cost.rs @@ -1,3 +1,5 @@ +#![allow(clippy::disallowed_types)] + use jiff::Timestamp; use litellm_cost::generic_cost::{ ResolvedTokenRates, billable_prompt_details, calculate_generic_cost_from_model_info, diff --git a/litellm-rust/crates/cost/tests/python_generic_input.rs b/litellm-rust/crates/cost/tests/python_generic_input.rs index 30710dca14e..f55430be6dd 100644 --- a/litellm-rust/crates/cost/tests/python_generic_input.rs +++ b/litellm-rust/crates/cost/tests/python_generic_input.rs @@ -1,3 +1,5 @@ +#![allow(clippy::disallowed_types)] + use litellm_cost::generic_input::{ InputBaseRates, calculate_cache_writing_cost, calculate_cost_component, calculate_input_cost, get_cost_per_unit, diff --git a/litellm-rust/crates/cost/tests/python_generic_output.rs b/litellm-rust/crates/cost/tests/python_generic_output.rs index 289f626a1e3..1b9562deb73 100644 --- a/litellm-rust/crates/cost/tests/python_generic_output.rs +++ b/litellm-rust/crates/cost/tests/python_generic_output.rs @@ -1,3 +1,5 @@ +#![allow(clippy::disallowed_types)] + use litellm_cost::generic_output::{calculate_output_cost, resolve_reasoning_token_cost}; use litellm_cost::usage_dispatch::get_usage_object; use rstest::rstest; diff --git a/litellm-rust/crates/cost/tests/python_generic_provider_routes.rs b/litellm-rust/crates/cost/tests/python_generic_provider_routes.rs index c82c39b5b17..e83996f30ec 100644 --- a/litellm-rust/crates/cost/tests/python_generic_provider_routes.rs +++ b/litellm-rust/crates/cost/tests/python_generic_provider_routes.rs @@ -1,3 +1,5 @@ +#![allow(clippy::disallowed_types)] + use std::collections::HashMap; use litellm_cost::catalog::{ModelCostRequest, ModelInfoCatalog}; diff --git a/litellm-rust/crates/cost/tests/python_generic_usage.rs b/litellm-rust/crates/cost/tests/python_generic_usage.rs index 91d7c212f51..055a74b217d 100644 --- a/litellm-rust/crates/cost/tests/python_generic_usage.rs +++ b/litellm-rust/crates/cost/tests/python_generic_usage.rs @@ -1,3 +1,5 @@ +#![allow(clippy::disallowed_types)] + use litellm_cost::generic_usage::{ get_billable_input_tokens, parse_completion_tokens_details, parse_prompt_tokens_details, }; diff --git a/litellm-rust/crates/cost/tests/python_groq_cost.rs b/litellm-rust/crates/cost/tests/python_groq_cost.rs index 9a70e423354..bd5ffdc66aa 100644 --- a/litellm-rust/crates/cost/tests/python_groq_cost.rs +++ b/litellm-rust/crates/cost/tests/python_groq_cost.rs @@ -1,3 +1,5 @@ +#![allow(clippy::disallowed_types)] + use litellm_cost::groq_cost::cost_per_web_search_request; use litellm_cost::usage_dispatch::get_usage_object; use rstest::rstest; diff --git a/litellm-rust/crates/cost/tests/python_guardrail_cost.rs b/litellm-rust/crates/cost/tests/python_guardrail_cost.rs index 0bee756f3fa..e3fb36c4453 100644 --- a/litellm-rust/crates/cost/tests/python_guardrail_cost.rs +++ b/litellm-rust/crates/cost/tests/python_guardrail_cost.rs @@ -1,3 +1,5 @@ +#![allow(clippy::disallowed_types)] + use std::collections::BTreeMap; use litellm_cost::guardrail_cost::{ diff --git a/litellm-rust/crates/cost/tests/python_image_cost_router.rs b/litellm-rust/crates/cost/tests/python_image_cost_router.rs index 24f45900688..65895c294fc 100644 --- a/litellm-rust/crates/cost/tests/python_image_cost_router.rs +++ b/litellm-rust/crates/cost/tests/python_image_cost_router.rs @@ -1,3 +1,5 @@ +#![allow(clippy::disallowed_types)] + use std::collections::HashMap; use jiff::Timestamp; diff --git a/litellm-rust/crates/cost/tests/python_image_response_cost.rs b/litellm-rust/crates/cost/tests/python_image_response_cost.rs index fffb7536abb..008f919e72c 100644 --- a/litellm-rust/crates/cost/tests/python_image_response_cost.rs +++ b/litellm-rust/crates/cost/tests/python_image_response_cost.rs @@ -1,3 +1,5 @@ +#![allow(clippy::disallowed_types)] + use std::collections::HashMap; use jiff::Timestamp; diff --git a/litellm-rust/crates/cost/tests/python_interactions_usage.rs b/litellm-rust/crates/cost/tests/python_interactions_usage.rs index 7f83cf3f231..d1113504f49 100644 --- a/litellm-rust/crates/cost/tests/python_interactions_usage.rs +++ b/litellm-rust/crates/cost/tests/python_interactions_usage.rs @@ -1,3 +1,5 @@ +#![allow(clippy::disallowed_types)] + use litellm_cost::interactions_usage::{ is_interactions_usage_object, transform_interactions_usage_object, }; diff --git a/litellm-rust/crates/cost/tests/python_model_lookup.rs b/litellm-rust/crates/cost/tests/python_model_lookup.rs index 864e7fd8e6f..4c257bc4526 100644 --- a/litellm-rust/crates/cost/tests/python_model_lookup.rs +++ b/litellm-rust/crates/cost/tests/python_model_lookup.rs @@ -1,3 +1,5 @@ +#![allow(clippy::disallowed_types)] + use std::collections::HashMap; use jiff::Timestamp; diff --git a/litellm-rust/crates/cost/tests/python_model_selection.rs b/litellm-rust/crates/cost/tests/python_model_selection.rs index 9fe40274bca..8a7ebb976e6 100644 --- a/litellm-rust/crates/cost/tests/python_model_selection.rs +++ b/litellm-rust/crates/cost/tests/python_model_selection.rs @@ -1,3 +1,5 @@ +#![allow(clippy::disallowed_types)] + use std::collections::HashMap; use litellm_cost::catalog::ModelInfoCatalog; diff --git a/litellm-rust/crates/cost/tests/python_off_peak.rs b/litellm-rust/crates/cost/tests/python_off_peak.rs index ae7e9cedd0c..797b3052f7f 100644 --- a/litellm-rust/crates/cost/tests/python_off_peak.rs +++ b/litellm-rust/crates/cost/tests/python_off_peak.rs @@ -1,3 +1,5 @@ +#![allow(clippy::disallowed_types)] + use jiff::Timestamp; use litellm_cost::base_rate_selection::get_token_base_cost; use litellm_cost::off_peak::{ diff --git a/litellm-rust/crates/cost/tests/python_openai_cost.rs b/litellm-rust/crates/cost/tests/python_openai_cost.rs index 035498f064b..f3754e4a1e3 100644 --- a/litellm-rust/crates/cost/tests/python_openai_cost.rs +++ b/litellm-rust/crates/cost/tests/python_openai_cost.rs @@ -1,3 +1,5 @@ +#![allow(clippy::disallowed_types)] + use litellm_cost::non_token::Error; use litellm_cost::openai_cost::{ cost_router, video_generation_cost, video_output_cost_per_second, diff --git a/litellm-rust/crates/cost/tests/python_openai_image_cost.rs b/litellm-rust/crates/cost/tests/python_openai_image_cost.rs index 26027cd583b..0f9e61ff747 100644 --- a/litellm-rust/crates/cost/tests/python_openai_image_cost.rs +++ b/litellm-rust/crates/cost/tests/python_openai_image_cost.rs @@ -1,3 +1,5 @@ +#![allow(clippy::disallowed_types)] + use std::collections::HashMap; use jiff::Timestamp; diff --git a/litellm-rust/crates/cost/tests/python_per_second.rs b/litellm-rust/crates/cost/tests/python_per_second.rs index bdbd74f72d7..9c74623c559 100644 --- a/litellm-rust/crates/cost/tests/python_per_second.rs +++ b/litellm-rust/crates/cost/tests/python_per_second.rs @@ -1,3 +1,5 @@ +#![allow(clippy::disallowed_types)] + use std::collections::HashMap; use jiff::Timestamp; diff --git a/litellm-rust/crates/cost/tests/python_perplexity_cost.rs b/litellm-rust/crates/cost/tests/python_perplexity_cost.rs index de0a201fd17..0494646873a 100644 --- a/litellm-rust/crates/cost/tests/python_perplexity_cost.rs +++ b/litellm-rust/crates/cost/tests/python_perplexity_cost.rs @@ -1,3 +1,5 @@ +#![allow(clippy::disallowed_types)] + use std::collections::HashMap; use jiff::Timestamp; diff --git a/litellm-rust/crates/cost/tests/python_pricing_pinning.rs b/litellm-rust/crates/cost/tests/python_pricing_pinning.rs new file mode 100644 index 00000000000..60ed09a54cc --- /dev/null +++ b/litellm-rust/crates/cost/tests/python_pricing_pinning.rs @@ -0,0 +1,173 @@ +// mirrors: crates/cost/tests/generate_python_fixtures.py (pinning + model_info_projection surfaces) + +#![allow(clippy::disallowed_types)] + +use serde_json::Value; + +use litellm_cost::pricing::{Metric, ServiceTier, tokenize}; +use litellm_cost::wire::{RawCatalogEntry, cost_per_unit}; + +fn fixture() -> Value { + serde_json::from_str(include_str!("python_fixtures.json")).expect("fixture parses") +} + +fn pinning() -> Value { + fixture()["pinning"].clone() +} + +#[test] +fn tokenizer_agrees_with_parse_above_token_threshold_where_python_parses() { + for row in pinning()["thresholds"].as_array().expect("threshold rows") { + let key = row["key"].as_str().expect("key"); + if let Some(value) = row["threshold"].as_f64() { + let rate_key = tokenize(key).expect("python-parsed keys tokenize"); + assert_eq!(rate_key.threshold, Some(value as u64), "key {key}"); + } + } + let no_threshold_keys = [ + "input_cost_per_video_per_second_above_15s_interval", + "cache_creation_input_token_cost_above_1hr", + "input_cost_per_token", + ]; + for key in no_threshold_keys { + assert_eq!( + tokenize(key).expect("tokenizes").threshold, + None, + "no token threshold for {key}" + ); + } + assert_eq!( + tokenize("cache_creation_input_token_cost_above_1hr_above_200k_tokens") + .expect("tokenizes") + .threshold, + Some(200_000) + ); +} + +#[test] +fn service_tier_suffixes_match_python() { + let pinning = pinning(); + let pinned: Vec<&str> = pinning["service_tier_suffixes"] + .as_array() + .expect("suffix list") + .iter() + .map(|value| value.as_str().expect("suffix string")) + .collect(); + let rust: Vec = ServiceTier::SUFFIXES + .into_iter() + .map(|tier| format!("_{}", tier.as_str())) + .collect(); + assert_eq!(rust, pinned); +} + +#[test] +fn batch_tier_grammar_matches_python_regex_language() { + let pattern = pinning()["batch_tier_key"] + .as_str() + .expect("batch pattern") + .to_string(); + let regex = regex::Regex::new(&pattern).expect("python batch pattern compiles"); + for prefix in [ + "input_cost_per_token", + "output_cost_per_token", + "cache_read_input_token_cost", + "cache_creation_input_token_cost", + ] { + for magnitude in ["1k", "128k", "500"] { + let key = format!("{prefix}_above_{magnitude}_tokens_batches"); + let rate_key = tokenize(&key).unwrap_or_else(|_| panic!("{key} tokenizes")); + assert!(regex.is_match(&key), "{key} matches python pattern"); + assert!(rate_key.batch); + assert_eq!( + rate_key.metric, + Metric::parse(prefix).expect("known metric"), + "{key}" + ); + } + } + let rejected = [ + "input_cost_per_token_above_128.5k_tokens_batches", + "input_cost_per_audio_token_above_128k_tokens_batches", + "input_cost_per_token_above_128k_tokens", + ]; + for key in rejected { + assert!(!regex.is_match(key), "{key} outside python pattern"); + } +} + +#[test] +fn cost_per_unit_replays_python_coercion_and_tier_fallback() { + for row in pinning()["cost_per_unit"] + .as_array() + .expect("cost_per_unit rows") + { + let fields = row["model_info"].as_object().expect("model_info object"); + let cost_key = row["cost_key"].as_str().expect("cost key"); + let default = row["default"].as_f64(); + let expected = row["result"].as_f64(); + let actual = cost_per_unit(fields, cost_key, default); + assert_eq!(actual, expected, "key {cost_key} default {default:?}"); + } +} + +#[test] +fn model_info_projection_passes_rate_keys_the_tokenizer_recognizes() { + let fixture = fixture(); + let projected = fixture["model_info_projection"]["projected"] + .as_array() + .expect("projected keys"); + let mut unrecognized: Vec<&str> = Vec::new(); + for key in projected.iter().filter_map(Value::as_str) { + let known = tokenize(key).is_ok() + || litellm_cost::wire::recognized_non_rate_key(key) + || matches!( + key, + "off_peak_pricing" + | "tiered_pricing" + | "search_context_cost_per_query" + | "provider_specific_entry" + | "guardrail_cost_per_unit" + | "regional_processing_uplift_multiplier_eu" + | "regional_processing_uplift_multiplier_us" + | "regional_endpoint_uplift_multiplier" + ); + if !known { + unrecognized.push(key); + } + } + assert!( + unrecognized.is_empty(), + "projected keys the tokenizer or allowlist must recognize: {unrecognized:?}" + ); +} + +#[test] +fn registered_entry_parses_into_model_pricing_with_projected_rates() { + let entry: RawCatalogEntry = serde_json::from_value(serde_json::json!({ + "input_cost_per_token": 1e-6, + "output_cost_per_token": 2e-6, + "input_cost_per_token_above_128k_tokens": 5e-7, + "input_cost_per_token_above_128k_tokens_priority": 6e-7, + "cache_read_input_token_cost": 1e-7, + })) + .expect("synthetic entry parses"); + let pricing = entry.pricing(); + assert_eq!(pricing.rate(Metric::InputPerToken).value(), Some(1e-6)); + assert_eq!(pricing.rate(Metric::CacheReadToken).value(), Some(1e-7)); + let threshold = &pricing.thresholds[0]; + assert_eq!(threshold.above_tokens, 128_000); + assert_eq!( + threshold + .standard + .get(&Metric::InputPerToken) + .and_then(|r| r.value()), + Some(5e-7) + ); + assert_eq!( + threshold + .tiers + .get(&(Metric::InputPerToken, ServiceTier::Priority)) + .and_then(|r| r.value()), + Some(6e-7) + ); +} diff --git a/litellm-rust/crates/cost/tests/python_prompt_caching_savings.rs b/litellm-rust/crates/cost/tests/python_prompt_caching_savings.rs index a569f5edaaa..4029789006a 100644 --- a/litellm-rust/crates/cost/tests/python_prompt_caching_savings.rs +++ b/litellm-rust/crates/cost/tests/python_prompt_caching_savings.rs @@ -1,3 +1,5 @@ +#![allow(clippy::disallowed_types)] + use std::collections::HashMap; use jiff::Timestamp; diff --git a/litellm-rust/crates/cost/tests/python_provider_cache.rs b/litellm-rust/crates/cost/tests/python_provider_cache.rs index 441cc14e1ad..b6a8c9387e4 100644 --- a/litellm-rust/crates/cost/tests/python_provider_cache.rs +++ b/litellm-rust/crates/cost/tests/python_provider_cache.rs @@ -1,3 +1,5 @@ +#![allow(clippy::disallowed_types)] + use std::collections::HashMap; use jiff::Timestamp; diff --git a/litellm-rust/crates/cost/tests/python_provider_routes.rs b/litellm-rust/crates/cost/tests/python_provider_routes.rs index 1a8dbcbbbc7..e39f68fefce 100644 --- a/litellm-rust/crates/cost/tests/python_provider_routes.rs +++ b/litellm-rust/crates/cost/tests/python_provider_routes.rs @@ -1,3 +1,5 @@ +#![allow(clippy::disallowed_types)] + use std::collections::HashMap; use jiff::Timestamp; diff --git a/litellm-rust/crates/cost/tests/python_ptu_pricing.rs b/litellm-rust/crates/cost/tests/python_ptu_pricing.rs index 80329e00efe..cfd794e99e6 100644 --- a/litellm-rust/crates/cost/tests/python_ptu_pricing.rs +++ b/litellm-rust/crates/cost/tests/python_ptu_pricing.rs @@ -1,3 +1,5 @@ +#![allow(clippy::disallowed_types)] + use litellm_cost::ptu_pricing::{ MAX_COST_PER_PTU_PER_HOUR, MAX_PTU_COUNT, PTU_ZEROED_PRICING_FIELDS, SEARCH_CONTEXT_SIZES, azure_spillover, declares_ptu, is_spilled_over_ptu_request, ptu_config_error, diff --git a/litellm-rust/crates/cost/tests/python_realtime_cost.rs b/litellm-rust/crates/cost/tests/python_realtime_cost.rs index d1797024caa..62c914c0ec8 100644 --- a/litellm-rust/crates/cost/tests/python_realtime_cost.rs +++ b/litellm-rust/crates/cost/tests/python_realtime_cost.rs @@ -1,3 +1,5 @@ +#![allow(clippy::disallowed_types)] + use std::collections::HashMap; use jiff::Timestamp; diff --git a/litellm-rust/crates/cost/tests/python_regional_uplift.rs b/litellm-rust/crates/cost/tests/python_regional_uplift.rs index f52a432c9c8..538e2b34e4e 100644 --- a/litellm-rust/crates/cost/tests/python_regional_uplift.rs +++ b/litellm-rust/crates/cost/tests/python_regional_uplift.rs @@ -1,3 +1,5 @@ +#![allow(clippy::disallowed_types)] + use jiff::Timestamp; use litellm_cost::generic_cost::calculate_generic_cost_from_model_info_with_region; use litellm_cost::regional_uplift::{ diff --git a/litellm-rust/crates/cost/tests/python_responses_usage.rs b/litellm-rust/crates/cost/tests/python_responses_usage.rs index 0b2b6894580..a54339d628f 100644 --- a/litellm-rust/crates/cost/tests/python_responses_usage.rs +++ b/litellm-rust/crates/cost/tests/python_responses_usage.rs @@ -1,3 +1,5 @@ +#![allow(clippy::disallowed_types)] + use litellm_cost::responses_usage::{ UsageError, is_response_api_usage, text_tokens_without_nested_reasoning, transform_response_api_usage_to_chat_usage, diff --git a/litellm-rust/crates/cost/tests/python_retrieval_cost.rs b/litellm-rust/crates/cost/tests/python_retrieval_cost.rs index efb01dd77e0..26b77ae53ce 100644 --- a/litellm-rust/crates/cost/tests/python_retrieval_cost.rs +++ b/litellm-rust/crates/cost/tests/python_retrieval_cost.rs @@ -1,3 +1,5 @@ +#![allow(clippy::disallowed_types)] + use litellm_cost::retrieval_cost::{rerank_cost, vector_store_search_cost}; use rstest::rstest; use serde_json::json; diff --git a/litellm-rust/crates/cost/tests/python_search_cost.rs b/litellm-rust/crates/cost/tests/python_search_cost.rs index bac373597d2..1390ad2164e 100644 --- a/litellm-rust/crates/cost/tests/python_search_cost.rs +++ b/litellm-rust/crates/cost/tests/python_search_cost.rs @@ -1,3 +1,5 @@ +#![allow(clippy::disallowed_types)] + use std::collections::HashMap; use litellm_cost::catalog::ModelInfoCatalog; diff --git a/litellm-rust/crates/cost/tests/python_speech_cost.rs b/litellm-rust/crates/cost/tests/python_speech_cost.rs index 57c14243a7d..5b20dcd0dfd 100644 --- a/litellm-rust/crates/cost/tests/python_speech_cost.rs +++ b/litellm-rust/crates/cost/tests/python_speech_cost.rs @@ -1,3 +1,5 @@ +#![allow(clippy::disallowed_types)] + use std::collections::HashMap; use jiff::Timestamp; diff --git a/litellm-rust/crates/cost/tests/python_tiered_pricing.rs b/litellm-rust/crates/cost/tests/python_tiered_pricing.rs index 14e5d6bd35a..dfe4e2e2c48 100644 --- a/litellm-rust/crates/cost/tests/python_tiered_pricing.rs +++ b/litellm-rust/crates/cost/tests/python_tiered_pricing.rs @@ -1,3 +1,5 @@ +#![allow(clippy::disallowed_types)] + use litellm_cost::tiered_pricing::{select_tier_for_input, tier_rate}; use rstest::rstest; use serde_json::{Value, json}; diff --git a/litellm-rust/crates/cost/tests/python_together_cost.rs b/litellm-rust/crates/cost/tests/python_together_cost.rs index 2cf506252c4..ff10a88cbf8 100644 --- a/litellm-rust/crates/cost/tests/python_together_cost.rs +++ b/litellm-rust/crates/cost/tests/python_together_cost.rs @@ -1,3 +1,5 @@ +#![allow(clippy::disallowed_types)] + use std::collections::HashMap; use jiff::Timestamp; diff --git a/litellm-rust/crates/cost/tests/python_tool_call_cost_tracking.rs b/litellm-rust/crates/cost/tests/python_tool_call_cost_tracking.rs index 9a9af0e9ff9..05e0598f0cf 100644 --- a/litellm-rust/crates/cost/tests/python_tool_call_cost_tracking.rs +++ b/litellm-rust/crates/cost/tests/python_tool_call_cost_tracking.rs @@ -1,3 +1,5 @@ +#![allow(clippy::disallowed_types)] + use litellm_cost::tool_call_cost_tracking::{ DefaultToolRates, ResponseKind, chat_completion_response_includes_annotations, count_web_search_calls, extract_file_search_params, extract_token_counts, diff --git a/litellm-rust/crates/cost/tests/python_tool_cost_dispatch.rs b/litellm-rust/crates/cost/tests/python_tool_cost_dispatch.rs index 0f39cfd81c4..a062adfbe20 100644 --- a/litellm-rust/crates/cost/tests/python_tool_cost_dispatch.rs +++ b/litellm-rust/crates/cost/tests/python_tool_cost_dispatch.rs @@ -1,3 +1,5 @@ +#![allow(clippy::disallowed_types)] + use litellm_cost::tool_call_cost_tracking::{DefaultToolRates, ResponseKind}; use litellm_cost::tool_cost_dispatch::{BuiltInToolCostRequest, get_cost_for_built_in_tools}; use litellm_cost::usage_dispatch::get_usage_object; diff --git a/litellm-rust/crates/cost/tests/python_transcription_usage.rs b/litellm-rust/crates/cost/tests/python_transcription_usage.rs index 1aa36e9964e..0e0a2fa1e6d 100644 --- a/litellm-rust/crates/cost/tests/python_transcription_usage.rs +++ b/litellm-rust/crates/cost/tests/python_transcription_usage.rs @@ -1,3 +1,5 @@ +#![allow(clippy::disallowed_types)] + use litellm_cost::transcription_usage::{ is_transcription_usage_object, transform_transcription_usage_object, }; diff --git a/litellm-rust/crates/cost/tests/python_usage_dispatch.rs b/litellm-rust/crates/cost/tests/python_usage_dispatch.rs index 564ab4a3882..8e6d0da6b20 100644 --- a/litellm-rust/crates/cost/tests/python_usage_dispatch.rs +++ b/litellm-rust/crates/cost/tests/python_usage_dispatch.rs @@ -1,3 +1,5 @@ +#![allow(clippy::disallowed_types)] + use litellm_cost::usage_dispatch::get_usage_object; use rstest::rstest; use serde_json::{Value, json}; diff --git a/litellm-rust/crates/cost/tests/python_vertex_cost.rs b/litellm-rust/crates/cost/tests/python_vertex_cost.rs index 208158f2b88..61590c780d4 100644 --- a/litellm-rust/crates/cost/tests/python_vertex_cost.rs +++ b/litellm-rust/crates/cost/tests/python_vertex_cost.rs @@ -1,3 +1,5 @@ +#![allow(clippy::disallowed_types)] + use std::collections::HashMap; use jiff::Timestamp; diff --git a/litellm-rust/crates/cost/tests/python_wire_contract.rs b/litellm-rust/crates/cost/tests/python_wire_contract.rs index 2103c4a66c9..2bbfda34b73 100644 --- a/litellm-rust/crates/cost/tests/python_wire_contract.rs +++ b/litellm-rust/crates/cost/tests/python_wire_contract.rs @@ -1,3 +1,5 @@ +#![allow(clippy::disallowed_types)] + // mirrors: crates/cost/tests/generate_python_fixtures.py (wire_usage + wire_response surfaces) use serde_json::Value; diff --git a/litellm-rust/crates/cost/tests/python_xai_cost.rs b/litellm-rust/crates/cost/tests/python_xai_cost.rs index bab6b090d75..8d82b89de89 100644 --- a/litellm-rust/crates/cost/tests/python_xai_cost.rs +++ b/litellm-rust/crates/cost/tests/python_xai_cost.rs @@ -1,3 +1,5 @@ +#![allow(clippy::disallowed_types)] + use std::collections::HashMap; use jiff::Timestamp; diff --git a/litellm-rust/crates/cost/tests/python_zero_cost_diagnostic.rs b/litellm-rust/crates/cost/tests/python_zero_cost_diagnostic.rs index b15fa701966..3b1c61366de 100644 --- a/litellm-rust/crates/cost/tests/python_zero_cost_diagnostic.rs +++ b/litellm-rust/crates/cost/tests/python_zero_cost_diagnostic.rs @@ -1,3 +1,5 @@ +#![allow(clippy::disallowed_types)] + use std::collections::HashMap; use litellm_cost::catalog::ModelInfoCatalog;