litellm-rs 0.6.0

A high-performance AI Gateway written in Rust, providing OpenAI-compatible APIs with intelligent routing, load balancing, and enterprise features
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
//! Shared pricing data types.

use serde::{Deserialize, Deserializer, Serialize};
use std::collections::HashMap;
use std::fs;
use std::path::Path;
use std::sync::LazyLock;
use tracing::warn;

const EMBEDDED_MODEL_PRICES: &str = include_str!("../../config/model_prices_extended.json");

/// LiteLLM-compatible model pricing data.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LiteLLMModelInfo {
    /// Maximum total tokens
    #[serde(default, deserialize_with = "deserialize_option_u32_integral_number")]
    pub max_tokens: Option<u32>,
    /// Maximum input tokens
    #[serde(default, deserialize_with = "deserialize_option_u32_integral_number")]
    pub max_input_tokens: Option<u32>,
    /// Maximum output tokens
    #[serde(default, deserialize_with = "deserialize_option_u32_integral_number")]
    pub max_output_tokens: Option<u32>,
    /// Input cost per token
    pub input_cost_per_token: Option<f64>,
    /// Output cost per token
    pub output_cost_per_token: Option<f64>,
    /// Input cost per character (for some providers)
    pub input_cost_per_character: Option<f64>,
    /// Output cost per character (for some providers)
    pub output_cost_per_character: Option<f64>,
    /// Cost per second (for time-based providers)
    pub cost_per_second: Option<f64>,
    /// LiteLLM provider name
    pub litellm_provider: String,
    /// Model mode (chat, completion, embedding, etc.)
    #[serde(default)]
    pub mode: String,
    /// Supports function calling
    pub supports_function_calling: Option<bool>,
    /// Supports vision
    pub supports_vision: Option<bool>,
    /// Supports streaming
    pub supports_streaming: Option<bool>,
    /// Supports parallel function calling
    pub supports_parallel_function_calling: Option<bool>,
    /// Supports system message
    pub supports_system_message: Option<bool>,
    /// Additional metadata
    #[serde(flatten)]
    pub extra: HashMap<String, serde_json::Value>,
}

fn deserialize_option_u32_integral_number<'de, D>(deserializer: D) -> Result<Option<u32>, D::Error>
where
    D: Deserializer<'de>,
{
    let Some(value) = Option::<serde_json::Value>::deserialize(deserializer)? else {
        return Ok(None);
    };

    match value {
        serde_json::Value::Null => Ok(None),
        serde_json::Value::Number(number) => {
            if let Some(value) = number.as_u64() {
                return u32::try_from(value)
                    .map(Some)
                    .map_err(|_| serde::de::Error::custom("token limit exceeds u32::MAX"));
            }

            if let Some(value) = number.as_i64()
                && value < 0
            {
                return Err(serde::de::Error::custom("token limit cannot be negative"));
            }

            let value = number
                .as_f64()
                .ok_or_else(|| serde::de::Error::custom("token limit must be a finite number"))?;
            if !value.is_finite() {
                return Err(serde::de::Error::custom(
                    "token limit must be a finite number",
                ));
            }
            if value < 0.0 {
                return Err(serde::de::Error::custom("token limit cannot be negative"));
            }
            if value.fract() != 0.0 {
                return Err(serde::de::Error::custom(
                    "token limit float must be integral",
                ));
            }
            if value > u32::MAX as f64 {
                return Err(serde::de::Error::custom("token limit exceeds u32::MAX"));
            }

            Ok(Some(value as u32))
        }
        _ => Err(serde::de::Error::custom(
            "token limit must be a JSON number",
        )),
    }
}

/// Compatibility alias for callers that still import provider-base pricing.
pub type ModelPricing = LiteLLMModelInfo;

pub(crate) type PricingModelMap = HashMap<String, ModelPricing>;

/// Usage information for simple pricing database calculations.
///
/// Kept as a stable public type so downstream library consumers can continue
/// to construct `litellm_rs::core::pricing::Usage { prompt_tokens, ..., reasoning_tokens }`
/// with struct-literal syntax. The richer
/// [`crate::core::types::responses::Usage`] (with nested
/// `PromptTokensDetails` / `CompletionTokensDetails` / `ThinkingUsage`) is
/// the canonical shape used elsewhere; conversion helpers below bridge
/// between the two when the per-request cost path needs the canonical
/// metadata. See PR #519 architecture roadmap for the broader convergence.
#[derive(Debug, Clone)]
pub struct Usage {
    pub prompt_tokens: u32,
    pub completion_tokens: u32,
    pub total_tokens: u32,
    pub reasoning_tokens: Option<u32>,
}

impl Usage {
    /// Build a Usage from prompt + completion token counts.
    ///
    /// Auto-computes `total_tokens = prompt + completion` and leaves
    /// `reasoning_tokens` unset. Mirrors the canonical
    /// [`crate::core::types::responses::Usage::new`] signature so internal
    /// pricing call sites can use the same constructor on either type.
    pub fn new(prompt_tokens: u32, completion_tokens: u32) -> Self {
        Self {
            prompt_tokens,
            completion_tokens,
            total_tokens: prompt_tokens + completion_tokens,
            reasoning_tokens: None,
        }
    }
}

impl From<&crate::core::types::responses::Usage> for Usage {
    fn from(usage: &crate::core::types::responses::Usage) -> Self {
        Self {
            prompt_tokens: usage.prompt_tokens,
            completion_tokens: usage.completion_tokens,
            total_tokens: usage.total_tokens,
            reasoning_tokens: usage
                .completion_tokens_details
                .as_ref()
                .and_then(|d| d.reasoning_tokens),
        }
    }
}

/// Pricing database backed by the shared LiteLLM model info shape.
#[derive(Debug, Clone)]
pub struct PricingDatabase {
    models: HashMap<String, ModelPricing>,
}

impl PricingDatabase {
    /// Load pricing data from JSON file.
    pub fn from_json_file<P: AsRef<Path>>(path: P) -> Result<Self, String> {
        let content =
            fs::read_to_string(path).map_err(|e| format!("Failed to read pricing file: {}", e))?;

        let models = parse_litellm_pricing_json(&content)
            .map_err(|e| format!("Failed to parse pricing JSON: {}", e))?;

        Ok(Self { models })
    }

    /// Load from the bundled default pricing source used by gateway configuration.
    pub fn from_default_source() -> Result<Self, String> {
        let models = embedded_default_pricing_models()
            .map_err(|e| format!("Failed to parse embedded pricing JSON: {}", e))?;

        Ok(Self { models })
    }

    /// Load pricing data from the default source.
    ///
    /// Kept as a compatibility wrapper for older call sites/tests.
    pub fn from_python_json() -> Result<Self, String> {
        Self::from_default_source()
    }

    /// Calculate cost for a model and token usage.
    pub fn calculate(&self, model: &str, usage: &Usage) -> f64 {
        if let Some(pricing) = self.models.get(model) {
            return self.calculate_with_pricing(pricing, usage);
        }

        let normalized_model = normalize_model_key(model);
        if normalized_model != model
            && let Some(pricing) = self.models.get(normalized_model)
        {
            return self.calculate_with_pricing(pricing, usage);
        }

        if let Some((_, pricing)) = self
            .models
            .iter()
            .filter(|(key, _)| model_matches_key(normalized_model, key))
            .max_by_key(|(key, _)| key.len())
        {
            return self.calculate_with_pricing(pricing, usage);
        }

        0.0
    }

    /// Calculate cost for a provider/model pair and token usage.
    ///
    /// Provider dispatch should use this method instead of `calculate` so a
    /// same-named model on another provider does not accidentally supply prices.
    pub fn calculate_for_provider(&self, provider: &str, model: &str, usage: &Usage) -> f64 {
        if let Some(pricing) = self.models.get(model)
            && pricing_matches_provider(model, pricing, provider)
        {
            return self.calculate_with_pricing(pricing, usage);
        }

        let normalized_model = normalize_model_key(model);
        if normalized_model != model
            && let Some(pricing) = self.models.get(normalized_model)
            && pricing_matches_provider(normalized_model, pricing, provider)
        {
            return self.calculate_with_pricing(pricing, usage);
        }

        if let Some((_, pricing)) = self
            .models
            .iter()
            .filter(|(key, pricing)| {
                pricing_matches_provider(key, pricing, provider)
                    && model_matches_key(normalized_model, key)
            })
            .max_by_key(|(key, _)| key.len())
        {
            return self.calculate_with_pricing(pricing, usage);
        }

        0.0
    }

    fn calculate_with_pricing(&self, pricing: &ModelPricing, usage: &Usage) -> f64 {
        let mut cost = 0.0;

        if requires_bidirectional_database_token_pricing(pricing)
            && (pricing.input_cost_per_token.is_none() || pricing.output_cost_per_token.is_none())
        {
            warn!(
                "model pricing row for provider '{}' mode '{}' is missing one side of token pricing; skipping partial billing",
                pricing.litellm_provider, pricing.mode
            );
            return 0.0;
        }

        let input_cost_per_token = tiered_cost_per_token(
            pricing,
            pricing.input_cost_per_token.unwrap_or(0.0),
            "input_cost_per_token_above_",
            usage.prompt_tokens,
        );
        let output_cost_per_token = tiered_cost_per_token(
            pricing,
            pricing.output_cost_per_token.unwrap_or(0.0),
            "output_cost_per_token_above_",
            usage.prompt_tokens,
        );

        cost += usage.prompt_tokens as f64 * input_cost_per_token;
        cost += usage.completion_tokens as f64 * output_cost_per_token;

        if let Some(reasoning_tokens) = usage.reasoning_tokens {
            cost += reasoning_tokens as f64 * extra_f64(pricing, "output_cost_per_reasoning_token");
        }

        cost
    }

    /// Get raw LiteLLM model information for a model.
    pub fn get_model_info(&self, model: &str) -> Option<&ModelPricing> {
        self.models.get(model)
    }

    /// Get the configured max token limit for a model.
    pub fn get_max_tokens(&self, model: &str) -> Option<u32> {
        self.get_model_info(model).and_then(|info| {
            info.max_tokens
                .or(info.max_input_tokens)
                .or(info.max_output_tokens)
        })
    }

    /// Get all models associated with a provider.
    pub fn get_provider_models(&self, provider: &str) -> Vec<String> {
        let provider = normalize_pricing_provider(provider);
        self.models
            .iter()
            .filter_map(|(model_id, pricing)| {
                let pricing_provider = normalize_pricing_provider(&pricing.litellm_provider);
                if pricing_provider == provider {
                    Some(model_id.clone())
                } else {
                    None
                }
            })
            .collect()
    }

    /// Convert shared pricing metadata into public model metadata.
    pub fn to_model_info(
        &self,
        model_id: &str,
        provider: &str,
    ) -> Option<crate::core::types::model::ModelInfo> {
        use crate::core::types::model::ModelInfo;

        let pricing = self.get_model_info(model_id)?;

        Some(ModelInfo {
            id: model_id.to_string(),
            name: model_id.replace(['-', '_'], " "),
            provider: provider.to_string(),
            max_context_length: pricing
                .max_input_tokens
                .unwrap_or_else(|| pricing.max_tokens.unwrap_or(4096)),
            max_output_length: pricing.max_output_tokens,
            supports_streaming: pricing.supports_streaming.unwrap_or(true),
            supports_tools: pricing_supports_tools(pricing),
            supports_multimodal: pricing_supports_multimodal(pricing),
            input_cost_per_1k_tokens: pricing.input_cost_per_token.map(price_per_token_to_per_1k),
            output_cost_per_1k_tokens: pricing.output_cost_per_token.map(price_per_token_to_per_1k),
            currency: "USD".to_string(),
            capabilities: vec![],
            created_at: None,
            updated_at: None,
            metadata: HashMap::new(),
        })
    }

    /// Check whether a model supports a feature.
    pub fn supports_feature(&self, model: &str, feature: &str) -> bool {
        self.get_model_info(model)
            .map(|info| match feature {
                "function_calling" => info.supports_function_calling.unwrap_or(false),
                "vision" => info.supports_vision.unwrap_or(false),
                _ => false,
            })
            .unwrap_or(false)
    }
}

fn pricing_supports_multimodal(pricing: &ModelPricing) -> bool {
    pricing.supports_vision.unwrap_or(false)
        || pricing
            .extra
            .get("supported_modalities")
            .and_then(serde_json::Value::as_array)
            .is_some_and(|modalities| {
                modalities.iter().any(|modality| {
                    modality
                        .as_str()
                        .is_some_and(|value| matches!(value, "image" | "video"))
                })
            })
        || pricing
            .extra
            .get("supports_video_input")
            .and_then(serde_json::Value::as_bool)
            .unwrap_or(false)
}

fn pricing_supports_tools(pricing: &ModelPricing) -> bool {
    pricing.supports_function_calling.unwrap_or(false)
        || pricing
            .extra
            .get("supports_tool_choice")
            .and_then(serde_json::Value::as_bool)
            .unwrap_or(false)
}

fn requires_bidirectional_database_token_pricing(pricing: &ModelPricing) -> bool {
    matches!(pricing.mode.as_str(), "chat" | "completion")
        || (pricing.mode.is_empty() && !has_non_token_database_pricing(pricing))
}

fn has_non_token_database_pricing(pricing: &ModelPricing) -> bool {
    pricing.cost_per_second.is_some()
        || pricing
            .extra
            .get("video_cost_per_second")
            .and_then(serde_json::Value::as_f64)
            .is_some()
        || pricing
            .extra
            .get("audio_cost_per_second")
            .and_then(serde_json::Value::as_f64)
            .is_some()
        || pricing
            .extra
            .get("image_cost_per_token")
            .and_then(serde_json::Value::as_f64)
            .is_some()
        || pricing
            .extra
            .get("output_cost_per_image")
            .and_then(serde_json::Value::as_f64)
            .is_some()
}

fn pricing_matches_provider(_model_key: &str, pricing: &ModelPricing, provider: &str) -> bool {
    let provider = normalize_pricing_provider(provider);
    let pricing_provider = normalize_pricing_provider(&pricing.litellm_provider);

    pricing_provider == provider
}

pub(crate) fn normalize_pricing_provider(provider: &str) -> String {
    match provider.to_ascii_lowercase().replace('-', "_").as_str() {
        "vertexai" | "google" => "vertex_ai".to_string(),
        "zhipu" | "glm" => "zhipuai".to_string(),
        "mimo" | "xiaomi" => "xiaomi_mimo".to_string(),
        "together" | "togetherai" => "together_ai".to_string(),
        "fireworks" | "fireworksai" => "fireworks_ai".to_string(),
        "aiml_api" | "aimlapi" => "aiml".to_string(),
        other => other.to_string(),
    }
}

fn model_matches_key(model: &str, key: &str) -> bool {
    fn model_id_matches(candidate: &str, requested: &str) -> bool {
        if candidate == requested {
            return true;
        }

        candidate
            .strip_prefix(requested)
            .and_then(|suffix| suffix.strip_prefix('-'))
            .is_some_and(alias_suffix_matches)
    }

    model_id_matches(key, model)
        || model_id_matches(model, key)
        || key
            .rsplit_once('/')
            .map(|(_, model_id)| {
                model_id_matches(model_id, model) || model_id_matches(model, model_id)
            })
            .unwrap_or(false)
}

fn alias_suffix_matches(suffix: &str) -> bool {
    if suffix == "latest" {
        return true;
    }

    let digit_prefix_len = suffix.chars().take_while(|ch| ch.is_ascii_digit()).count();
    digit_prefix_len >= 4
        && suffix
            .chars()
            .all(|ch| ch.is_ascii_alphanumeric() || ch == '-' || ch == '_')
}

pub(crate) fn normalize_model_key(model: &str) -> &str {
    model
        .rsplit_once('/')
        .map(|(_, model)| model)
        .unwrap_or(model)
}

fn extra_f64(pricing: &ModelPricing, key: &str) -> f64 {
    pricing
        .extra
        .get(key)
        .and_then(serde_json::Value::as_f64)
        .unwrap_or(0.0)
}

fn tiered_cost_per_token(
    pricing: &ModelPricing,
    base_cost: f64,
    key_prefix: &str,
    prompt_tokens: u32,
) -> f64 {
    pricing
        .extra
        .iter()
        .filter_map(|(key, value)| {
            if !key.starts_with(key_prefix) {
                return None;
            }

            let threshold = extract_tier_threshold(key)?;
            if prompt_tokens > threshold {
                value.as_f64().map(|cost| (threshold, cost))
            } else {
                None
            }
        })
        .max_by_key(|(threshold, _)| *threshold)
        .map(|(_, cost)| cost)
        .unwrap_or(base_cost)
}

fn extract_tier_threshold(key: &str) -> Option<u32> {
    let threshold = key.split("_above_").nth(1)?.split("_tokens").next()?;
    if let Some(number) = threshold.strip_suffix('k') {
        number.parse::<u32>().ok().map(|value| value * 1000)
    } else {
        threshold.parse::<u32>().ok()
    }
}

fn price_per_token_to_per_1k(cost_per_token: f64) -> f64 {
    let cost_per_1k = cost_per_token * 1000.0;
    (cost_per_1k * 1_000_000_000_000.0).round() / 1_000_000_000_000.0
}

fn builtin_model(
    provider: &str,
    input_cost_per_token: f64,
    output_cost_per_token: f64,
    max_tokens: u32,
    max_output_tokens: u32,
    supports_function_calling: bool,
    supports_vision: bool,
) -> ModelPricing {
    ModelPricing {
        max_tokens: Some(max_tokens),
        max_input_tokens: Some(max_tokens),
        max_output_tokens: Some(max_output_tokens),
        input_cost_per_token: Some(input_cost_per_token),
        output_cost_per_token: Some(output_cost_per_token),
        input_cost_per_character: None,
        output_cost_per_character: None,
        cost_per_second: None,
        litellm_provider: provider.to_string(),
        mode: "chat".to_string(),
        supports_function_calling: Some(supports_function_calling),
        supports_vision: Some(supports_vision),
        supports_streaming: Some(true),
        supports_parallel_function_calling: None,
        supports_system_message: Some(true),
        extra: HashMap::new(),
    }
}

fn builtin_gpt55_model(snapshot: bool) -> ModelPricing {
    let mut model = builtin_model("openai", 0.000005, 0.00003, 1_048_576, 128_000, true, true);
    model.extra.insert(
        "cache_read_input_token_cost".to_string(),
        serde_json::Value::from(0.0000005),
    );
    model.extra.insert(
        "input_cost_per_token_above_272k_tokens".to_string(),
        serde_json::Value::from(0.00001),
    );
    model.extra.insert(
        "output_cost_per_token_above_272k_tokens".to_string(),
        serde_json::Value::from(0.000045),
    );
    model.extra.insert(
        "cache_read_input_token_cost_above_272k_tokens".to_string(),
        serde_json::Value::from(0.000001),
    );
    if snapshot {
        model
            .extra
            .insert("snapshot".to_string(), serde_json::Value::from(true));
    }
    model
}

fn builtin_gpt55_pro_model(snapshot: bool) -> ModelPricing {
    let mut model = builtin_model("openai", 0.00003, 0.00018, 1_048_576, 128_000, true, true);
    model.supports_streaming = Some(false);
    model.extra.insert(
        "cache_read_input_token_cost".to_string(),
        serde_json::Value::from(0.00003),
    );
    if snapshot {
        model
            .extra
            .insert("snapshot".to_string(), serde_json::Value::from(true));
    }
    model
}

impl Default for PricingDatabase {
    fn default() -> Self {
        let mut models = HashMap::new();

        models.insert("gpt-5.5".to_string(), builtin_gpt55_model(false));

        models.insert("gpt-5.5-2026-04-23".to_string(), builtin_gpt55_model(true));

        models.insert("gpt-5.5-pro".to_string(), builtin_gpt55_pro_model(false));

        models.insert(
            "gpt-5.5-pro-2026-04-23".to_string(),
            builtin_gpt55_pro_model(true),
        );

        models.insert(
            "gpt-4".to_string(),
            builtin_model("openai", 0.00003, 0.00006, 8192, 4096, true, false),
        );

        models.insert(
            "gpt-4-turbo".to_string(),
            builtin_model("openai", 0.00001, 0.00003, 128000, 4096, true, true),
        );

        models.insert(
            "gpt-3.5-turbo".to_string(),
            builtin_model("openai", 0.0000005, 0.0000015, 16385, 4096, true, false),
        );

        models.insert(
            "claude-3-opus".to_string(),
            builtin_model("anthropic", 0.000015, 0.000075, 200000, 4096, true, true),
        );

        models.insert(
            "claude-3-sonnet".to_string(),
            builtin_model("anthropic", 0.000003, 0.000015, 200000, 4096, true, true),
        );

        for model in ["deepseek-v4-flash", "deepseek-chat", "deepseek-reasoner"] {
            models.insert(
                model.to_string(),
                builtin_model(
                    "deepseek", 0.00000014, 0.00000028, 1048576, 393216, true, false,
                ),
            );
        }

        models.insert(
            "deepseek-v4-pro".to_string(),
            builtin_model(
                "deepseek",
                0.000000435,
                0.00000087,
                1048576,
                393216,
                true,
                false,
            ),
        );

        Self { models }
    }
}

/// Global pricing database loaded from the canonical local pricing source.
pub static GLOBAL_PRICING_DB: LazyLock<PricingDatabase> = LazyLock::new(|| {
    PricingDatabase::from_python_json().unwrap_or_else(|e| {
        warn!(
            error = %e,
            "Failed to load pricing data from file, using built-in defaults"
        );
        PricingDatabase::default()
    })
});

/// Get the shared global pricing database.
pub fn get_pricing_db() -> &'static PricingDatabase {
    &GLOBAL_PRICING_DB
}

/// Quick cost calculation for compatibility callers.
pub fn calculate_cost(model: &str, prompt_tokens: u32, completion_tokens: u32) -> f64 {
    let usage = Usage::new(prompt_tokens, completion_tokens);
    GLOBAL_PRICING_DB.calculate(model, &usage)
}

/// Parse LiteLLM pricing JSON into the shared pricing model map.
///
/// LiteLLM pricing files can contain documentation/sample keys next to model
/// entries. Every runtime pricing entry point should apply the same filtering
/// so gateway cost calculations and the pricing service see the same dataset.
pub fn parse_litellm_pricing_json(
    content: &str,
) -> Result<HashMap<String, LiteLLMModelInfo>, serde_json::Error> {
    let all_data: HashMap<String, serde_json::Value> = serde_json::from_str(content)?;
    all_data
        .into_iter()
        .filter(|(key, _)| !is_litellm_pricing_metadata_key(key))
        .map(|(key, value)| serde_json::from_value(value).map(|pricing| (key, pricing)))
        .collect()
}

pub(crate) fn embedded_default_pricing_models() -> serde_json::Result<PricingModelMap> {
    parse_litellm_pricing_json(EMBEDDED_MODEL_PRICES)
}

pub fn is_litellm_pricing_metadata_key(key: &str) -> bool {
    key == "sample_spec" || key.starts_with('_') || key.contains("example")
}

#[cfg(test)]
mod tests;