anyllm_proxy 0.9.3

HTTP proxy translating Anthropic Messages API to OpenAI Chat Completions
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
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
/// LiteLLM config.yaml parser.
///
/// Accepts LiteLLM's YAML config format (model_list, litellm_settings,
/// router_settings, general_settings) and converts it to anyllm-proxy's
/// MultiConfig + ModelRouter.
use std::collections::HashMap;
use std::sync::Arc;

use indexmap::IndexMap;
use serde::Deserialize;

use super::model_router::{Deployment, ModelRouter, RoutingStrategy};
use super::{
    resolve_env_value, validate_base_url, BackendAuth, BackendConfig, BackendKind, ModelMapping,
    MultiConfig, OpenAIApiFormat, TlsConfig,
};

// ---- Serde structs for LiteLLM config.yaml ----

/// Root structure of a LiteLLM `config.yaml` file.
#[derive(Deserialize)]
pub(crate) struct LiteLLMConfig {
    #[serde(default)]
    model_list: Vec<LiteLLMModelEntry>,
    #[serde(default)]
    litellm_settings: Option<LiteLLMSettings>,
    #[serde(default)]
    router_settings: Option<RouterSettings>,
    #[serde(default)]
    general_settings: Option<GeneralSettings>,
}

#[derive(Deserialize)]
struct LiteLLMModelEntry {
    model_name: String,
    litellm_params: LiteLLMParams,
}

#[derive(Deserialize)]
struct LiteLLMParams {
    model: String,
    api_base: Option<String>,
    api_key: Option<String>,
    rpm: Option<u32>,
    tpm: Option<u64>,
    weight: Option<u32>,
    // Azure-specific
    api_version: Option<String>,
    // Bedrock-specific
    aws_access_key_id: Option<String>,
    aws_secret_access_key: Option<String>,
    aws_region_name: Option<String>,
    // Catch unknown fields silently (LiteLLM has many we don't support).
    #[serde(flatten)]
    _extra: serde_json::Map<String, serde_json::Value>,
}

#[derive(Deserialize)]
struct LiteLLMSettings {
    #[serde(default)]
    num_retries: Option<u32>,
    #[serde(default)]
    request_timeout: Option<u64>,
    #[serde(default)]
    callbacks: Vec<String>,
    #[serde(flatten)]
    _extra: serde_json::Map<String, serde_json::Value>,
}

#[derive(Deserialize)]
struct RouterSettings {
    #[serde(default)]
    routing_strategy: Option<String>,
    #[serde(flatten)]
    _extra: serde_json::Map<String, serde_json::Value>,
}

/// Map LiteLLM routing_strategy string to our enum.
pub(crate) fn parse_routing_strategy_str(s: &str) -> RoutingStrategy {
    match s.to_ascii_lowercase().replace('_', "-").as_str() {
        "simple-shuffle" | "round-robin" => RoutingStrategy::RoundRobin,
        "least-busy" => RoutingStrategy::LeastBusy,
        "latency-based-routing" | "latency-based" => RoutingStrategy::LatencyBased,
        "usage-based-routing" | "usage-based" => RoutingStrategy::LeastBusy,
        "weighted" => RoutingStrategy::Weighted,
        "cost-based" => RoutingStrategy::CostBased,
        other => {
            tracing::warn!(
                strategy = %other,
                "unknown routing_strategy, falling back to round-robin"
            );
            RoutingStrategy::RoundRobin
        }
    }
}

#[derive(Deserialize)]
struct GeneralSettings {
    master_key: Option<String>,
    #[serde(flatten)]
    _extra: serde_json::Map<String, serde_json::Value>,
}

// ---- Provider parsing ----

/// Parse LiteLLM's "provider/model_name" format.
/// No prefix defaults to OpenAI (matches LiteLLM behavior).
/// Returns (kind, model_name, stub_provider) where stub_provider is set for
/// registry-resolved OpenAI-compatible providers so callers can use their default URL.
fn parse_provider_model(
    model: &str,
) -> (
    BackendKind,
    String,
    Option<&'static anyllm_providers::ProviderDef>,
) {
    let (provider, model_name) = model.split_once('/').unwrap_or(("openai", model));
    let mut stub_provider: Option<&'static anyllm_providers::ProviderDef> = None;
    let kind = match provider.to_ascii_lowercase().as_str() {
        "openai" => BackendKind::OpenAI,
        "azure" => BackendKind::AzureOpenAI,
        "vertex_ai" | "vertex" => BackendKind::Vertex,
        "gemini" => BackendKind::Gemini,
        "anthropic" => BackendKind::Anthropic,
        "bedrock" => BackendKind::Bedrock,
        other => {
            // Try the provider registry for known OpenAI-compatible providers
            // (e.g. "groq", "together_ai", "mistral", etc.)
            let prefix_with_slash = format!("{other}/");
            if let Some(p) = anyllm_providers::find_by_litellm_prefix(&prefix_with_slash) {
                let resolved = match anyllm_providers::resolve_backend(p.id) {
                    Some(("openai", _)) => {
                        stub_provider = Some(p);
                        BackendKind::OpenAI
                    }
                    Some(("anthropic", _)) => BackendKind::Anthropic,
                    Some(("gemini", _)) => BackendKind::Gemini,
                    Some(("vertex", _)) => BackendKind::Vertex,
                    Some(("azure", _)) => BackendKind::AzureOpenAI,
                    Some(("bedrock", _)) => BackendKind::Bedrock,
                    _ => {
                        tracing::warn!(provider = %other, "provider found in registry but protocol not mappable, treating as openai-compatible");
                        stub_provider = Some(p);
                        BackendKind::OpenAI
                    }
                };
                resolved
            } else {
                tracing::warn!(
                    provider = %other,
                    "unknown LiteLLM provider, treating as openai-compatible"
                );
                BackendKind::OpenAI
            }
        }
    };
    (kind, model_name.to_string(), stub_provider)
}

// ---- Backend deduplication key ----

/// Unique identity for a backend: same kind + base_url + api_key share one connection pool.
#[derive(Hash, PartialEq, Eq, Clone)]
struct BackendKey {
    kind: String,
    base_url: String,
    /// Hash of the API key (not the key itself) to avoid holding secrets in hash keys.
    api_key_hash: u64,
}

fn hash_string(s: &str) -> u64 {
    use std::hash::{Hash, Hasher};
    let mut hasher = std::collections::hash_map::DefaultHasher::new();
    s.hash(&mut hasher);
    hasher.finish()
}

// ---- Conversion ----

/// Parse a LiteLLM config.yaml string and produce a MultiConfig + ModelRouter.
///
/// # Panics
/// On invalid YAML, missing required fields, or unresolvable env var references.
/// Parsed result from a LiteLLM config file.
pub struct LiteLLMParsed {
    pub multi_config: MultiConfig,
    pub router: ModelRouter,
    /// Webhook callback URLs from litellm_settings.callbacks (non-named entries).
    pub callback_urls: Vec<String>,
    /// True when "langfuse" appears in litellm_settings.callbacks.
    pub langfuse_requested: bool,
    /// Resolved `general_settings.master_key`, if present.
    /// Caller should apply as PROXY_API_KEYS if that var is not already set.
    pub master_key: Option<String>,
}

/// Parse a LiteLLM YAML config and return the multi-backend config + model router pair.
pub fn from_litellm_yaml(yaml: &str) -> (MultiConfig, ModelRouter) {
    let parsed = parse_litellm_yaml(yaml);
    (parsed.multi_config, parsed.router)
}

/// Parse a LiteLLM YAML config into the intermediate `LiteLLMParsed` struct.
/// Panics on invalid YAML (startup-time validation; misconfiguration is unrecoverable).
pub fn parse_litellm_yaml(yaml: &str) -> LiteLLMParsed {
    let config: LiteLLMConfig =
        serde_yaml::from_str(yaml).unwrap_or_else(|e| panic!("invalid LiteLLM config YAML: {e}"));

    if config.model_list.is_empty() {
        panic!("LiteLLM config must define at least one entry in model_list");
    }

    // Resolve general_settings.master_key but do not call set_var here.
    // The caller applies it in the consolidated env override block.
    let master_key = if let Some(ref gs) = config.general_settings {
        let mk = gs.master_key.as_ref().map(|mk| {
            resolve_env_value(mk).unwrap_or_else(|e| panic!("general_settings.master_key: {e}"))
        });
        // Log unsupported keys at warn.
        for key in gs._extra.keys() {
            tracing::warn!(key = %key, "unsupported general_settings key (ignored)");
        }
        mk
    } else {
        None
    };

    if let Some(ref ls) = config.litellm_settings {
        for key in ls._extra.keys() {
            tracing::warn!(key = %key, "unsupported litellm_settings key (ignored)");
        }
    }

    if let Some(ref rs) = config.router_settings {
        for key in rs._extra.keys() {
            tracing::warn!(key = %key, "unsupported router_settings key (ignored)");
        }
    }

    let listen_port = std::env::var("LISTEN_PORT")
        .ok()
        .and_then(|v| v.parse().ok())
        .unwrap_or(3000);

    let log_bodies = std::env::var("LOG_BODIES")
        .map(|v| v == "true" || v == "1")
        .unwrap_or(false);

    let tls = TlsConfig::from_env();

    // Group model_list entries into deduplicated backends + deployment list.
    let mut backend_map: HashMap<BackendKey, (String, BackendConfig)> = HashMap::new();
    let mut backend_counter = 0u32;
    // model_name -> Vec<(backend_name, actual_model, rpm, tpm)>
    let mut model_deployments: HashMap<String, Vec<DeploymentSpec>> = HashMap::new();

    for entry in &config.model_list {
        let (kind, actual_model, stub_provider) = parse_provider_model(&entry.litellm_params.model);
        let params = &entry.litellm_params;

        let api_key = super::sanitize_api_key(
            &params
                .api_key
                .as_deref()
                .map(|v| resolve_env_value(v).unwrap_or_else(|e| panic!("model_list api_key: {e}")))
                .unwrap_or_else(|| {
                    // Fall back to the provider's own env vars when no api_key in YAML.
                    stub_provider
                        .and_then(|p| p.env_vars.iter().find_map(|v| std::env::var(v).ok()))
                        .unwrap_or_default()
                }),
        );

        let base_url = resolve_base_url(&kind, params, stub_provider);

        let bk = BackendKey {
            kind: format!("{kind:?}"),
            base_url: base_url.clone(),
            api_key_hash: hash_string(&api_key),
        };

        let backend_name = if let Some((name, _)) = backend_map.get(&bk) {
            name.clone()
        } else {
            let name = format!("litellm_{backend_counter}");
            backend_counter += 1;

            let bc = build_backend_config(
                &name, &kind, &api_key, &base_url, params, &tls, log_bodies, &config,
            );
            backend_map.insert(bk, (name.clone(), bc));
            name
        };

        model_deployments
            .entry(entry.model_name.clone())
            .or_default()
            .push(DeploymentSpec {
                backend_name,
                actual_model,
                rpm: params.rpm,
                tpm: params.tpm,
                weight: params.weight,
            });
    }

    // Build MultiConfig backends (ordered).
    let mut backends = IndexMap::new();
    for (name, bc) in backend_map.values() {
        backends.insert(name.clone(), bc.clone());
    }

    let default_backend = backends
        .keys()
        .next()
        .cloned()
        .expect("at least one backend");

    let multi = MultiConfig {
        listen_port,
        log_bodies,
        default_backend,
        backends,
        expose_degradation_warnings: false, // overridden in MultiConfig::load()
    };

    // Determine routing strategy from router_settings.
    let strategy = config
        .router_settings
        .as_ref()
        .and_then(|rs| rs.routing_strategy.as_deref())
        .map(parse_routing_strategy_str)
        .unwrap_or_default();

    if strategy != RoutingStrategy::RoundRobin {
        tracing::info!(strategy = ?strategy, "using routing strategy from config");
    }

    // Build ModelRouter.
    let mut routes: HashMap<String, Vec<Arc<Deployment>>> = HashMap::new();
    for (model_name, specs) in model_deployments {
        let deployments = specs
            .into_iter()
            .map(|s| {
                Arc::new(Deployment::with_weight(
                    s.backend_name,
                    s.actual_model,
                    s.rpm,
                    s.tpm,
                    s.weight.unwrap_or(1),
                ))
            })
            .collect();
        routes.insert(model_name, deployments);
    }

    let router = ModelRouter::with_strategy(routes, strategy);

    let callbacks = config
        .litellm_settings
        .as_ref()
        .map(|s| s.callbacks.clone())
        .unwrap_or_default();
    let langfuse_requested = callbacks.iter().any(|c| c.eq_ignore_ascii_case("langfuse"));
    let callback_urls: Vec<String> = callbacks
        .into_iter()
        .filter(|c| !c.eq_ignore_ascii_case("langfuse"))
        .collect();

    LiteLLMParsed {
        multi_config: multi,
        router,
        callback_urls,
        langfuse_requested,
        master_key,
    }
}

struct DeploymentSpec {
    backend_name: String,
    actual_model: String,
    rpm: Option<u32>,
    tpm: Option<u64>,
    weight: Option<u32>,
}

/// Extract `general_settings.master_key` from a LiteLLM YAML string without
/// performing full config parsing. Used by the synchronous `fn main()` to apply
/// the key via `set_var` before the tokio runtime spawns worker threads.
pub fn extract_master_key(yaml: &str) -> Option<String> {
    #[derive(Deserialize)]
    struct Probe {
        general_settings: Option<GeneralSettings>,
    }
    let probe: Probe = serde_yaml::from_str(yaml).ok()?;
    let gs = probe.general_settings?;
    let raw = gs.master_key?;
    resolve_env_value(&raw).ok()
}

/// Determine the base URL for a deployment, applying provider-specific defaults.
fn resolve_base_url(
    kind: &BackendKind,
    params: &LiteLLMParams,
    stub_provider: Option<&'static anyllm_providers::ProviderDef>,
) -> String {
    if let Some(ref url) = params.api_base {
        let resolved =
            resolve_env_value(url).unwrap_or_else(|e| panic!("model_list api_base: {e}"));
        return resolved;
    }
    match kind {
        BackendKind::OpenAI => {
            // Use the stub provider's default URL when available (e.g. groq, xai, mistral).
            // Falls back to OpenAI's URL only when the provider has no default or is unknown.
            let url = stub_provider
                .map(|p| p.default_base_url)
                .filter(|u| !u.is_empty())
                .unwrap_or("https://api.openai.com");
            super::strip_v1_suffix(url).to_string()
        }
        BackendKind::Gemini => {
            "https://generativelanguage.googleapis.com/v1beta/openai".to_string()
        }
        BackendKind::Anthropic => "https://api.anthropic.com".to_string(),
        BackendKind::Bedrock => {
            // For Bedrock, base_url stores the region.
            params
                .aws_region_name
                .as_deref()
                .map(|v| v.to_string())
                .or_else(|| std::env::var("AWS_REGION").ok())
                .unwrap_or_else(|| "us-east-1".to_string())
        }
        // Azure and Vertex require api_base in the config.
        BackendKind::AzureOpenAI => {
            panic!("api_base is required for azure deployments in model_list")
        }
        BackendKind::Vertex => {
            panic!("api_base is required for vertex deployments in model_list")
        }
    }
}

/// Build a BackendConfig from LiteLLM model_list params.
#[allow(clippy::too_many_arguments)]
fn build_backend_config(
    name: &str,
    kind: &BackendKind,
    api_key: &str,
    base_url: &str,
    params: &LiteLLMParams,
    tls: &TlsConfig,
    log_bodies: bool,
    config: &LiteLLMConfig,
) -> BackendConfig {
    let backend_auth = match kind {
        BackendKind::AzureOpenAI => BackendAuth::AzureApiKey(api_key.to_string()),
        BackendKind::Gemini | BackendKind::Vertex => BackendAuth::GoogleApiKey(api_key.to_string()),
        _ => BackendAuth::BearerToken(api_key.to_string()),
    };

    // For Azure, build deployment URL from api_base.
    let effective_url = if *kind == BackendKind::AzureOpenAI {
        let api_version = params.api_version.as_deref().unwrap_or("2024-10-21");
        // LiteLLM api_base for Azure is the resource endpoint.
        // We need to append the deployment path.
        if base_url.contains("/openai/deployments/") {
            // Already a full deployment URL.
            base_url.to_string()
        } else {
            format!(
                "{}/openai/deployments/chat/completions?api-version={api_version}",
                base_url.trim_end_matches('/')
            )
        }
    } else {
        // Validate non-Azure URLs.
        if *kind != BackendKind::Bedrock {
            if let Err(e) = validate_base_url(base_url) {
                panic!("backend '{name}' base_url rejected: {e}");
            }
        }
        base_url.to_string()
    };

    // Bedrock credentials.
    let bedrock_credentials = if *kind == BackendKind::Bedrock {
        let region = params
            .aws_region_name
            .as_deref()
            .map(|v| resolve_env_value(v).unwrap_or_else(|e| panic!("backend '{name}': {e}")))
            .or_else(|| std::env::var("AWS_REGION").ok())
            .unwrap_or_else(|| "us-east-1".to_string());

        let access_key = params
            .aws_access_key_id
            .as_deref()
            .map(|v| resolve_env_value(v).unwrap_or_else(|e| panic!("backend '{name}': {e}")))
            .or_else(|| std::env::var("AWS_ACCESS_KEY_ID").ok())
            .unwrap_or_else(|| panic!("backend '{name}': aws_access_key_id required for bedrock"));

        let secret_key = params
            .aws_secret_access_key
            .as_deref()
            .map(|v| resolve_env_value(v).unwrap_or_else(|e| panic!("backend '{name}': {e}")))
            .or_else(|| std::env::var("AWS_SECRET_ACCESS_KEY").ok())
            .unwrap_or_else(|| {
                panic!("backend '{name}': aws_secret_access_key required for bedrock")
            });

        // Store region as base_url for Bedrock (matches existing convention).
        // The effective_url was already set to the region string.
        let _ = region; // region is used as base_url via resolve_base_url

        Some(aws_credential_types::Credentials::new(
            access_key,
            secret_key,
            None, // session token not commonly in LiteLLM configs
            None,
            "litellm-config",
        ))
    } else {
        None
    };

    // Placeholder model mapping: with model router, these are not used for routing.
    // They serve as fallback for Anthropic model name translation if needed.
    let model_mapping = ModelMapping {
        big_model: String::new(),
        small_model: String::new(),
    };

    let _num_retries = config.litellm_settings.as_ref().and_then(|s| s.num_retries);
    let _request_timeout = config
        .litellm_settings
        .as_ref()
        .and_then(|s| s.request_timeout);

    BackendConfig {
        kind: kind.clone(),
        api_key: api_key.to_string(),
        base_url: effective_url,
        api_format: OpenAIApiFormat::Chat,
        model_mapping,
        tls: tls.clone(),
        backend_auth,
        log_bodies,
        omit_stream_options: false,
        stream_timeout_secs: std::env::var("REQUEST_TIMEOUT_SECS")
            .ok()
            .and_then(|v| v.parse().ok())
            .unwrap_or(900u64),
        bedrock_credentials,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parse_provider_model_openai() {
        let (kind, model, _) = parse_provider_model("openai/gpt-4o");
        assert_eq!(kind, BackendKind::OpenAI);
        assert_eq!(model, "gpt-4o");
    }

    #[test]
    fn parse_provider_model_azure() {
        let (kind, model, _) = parse_provider_model("azure/gpt-4o-eu");
        assert_eq!(kind, BackendKind::AzureOpenAI);
        assert_eq!(model, "gpt-4o-eu");
    }

    #[test]
    fn parse_provider_model_no_prefix() {
        let (kind, model, _) = parse_provider_model("gpt-4o");
        assert_eq!(kind, BackendKind::OpenAI);
        assert_eq!(model, "gpt-4o");
    }

    #[test]
    fn parse_provider_model_vertex_ai() {
        let (kind, model, _) = parse_provider_model("vertex_ai/gemini-pro");
        assert_eq!(kind, BackendKind::Vertex);
        assert_eq!(model, "gemini-pro");
    }

    #[test]
    fn parse_provider_model_bedrock() {
        let (kind, model, _) = parse_provider_model("bedrock/anthropic.claude-v2");
        assert_eq!(kind, BackendKind::Bedrock);
        assert_eq!(model, "anthropic.claude-v2");
    }

    #[test]
    fn parse_provider_model_unknown_treated_as_openai() {
        let (kind, model, _) = parse_provider_model("groq/llama-70b");
        assert_eq!(kind, BackendKind::OpenAI);
        assert_eq!(model, "llama-70b");
    }

    #[test]
    fn minimal_litellm_config() {
        let yaml = r#"
model_list:
  - model_name: gpt-4o
    litellm_params:
      model: openai/gpt-4o
      api_key: sk-test-key
"#;

        let (multi, router) = from_litellm_yaml(yaml);
        assert_eq!(multi.backends.len(), 1);
        assert!(router.has_model("gpt-4o"));

        let routed = router.route("gpt-4o").unwrap();
        assert_eq!(routed.actual_model, "gpt-4o");
    }

    #[test]
    fn multiple_deployments_same_model() {
        let yaml = r#"
model_list:
  - model_name: gpt-4o
    litellm_params:
      model: openai/gpt-4o
      api_key: sk-key-1
      rpm: 100
  - model_name: gpt-4o
    litellm_params:
      model: openai/gpt-4o
      api_key: sk-key-2
      rpm: 200
"#;

        let (multi, router) = from_litellm_yaml(yaml);
        // Different api_keys = different backends
        assert_eq!(multi.backends.len(), 2);
        assert!(router.has_model("gpt-4o"));

        // Should round-robin between the two
        let r0 = router.route("gpt-4o").unwrap();
        let r1 = router.route("gpt-4o").unwrap();
        assert_ne!(r0.backend_name, r1.backend_name);
    }

    #[test]
    fn backend_deduplication() {
        let yaml = r#"
model_list:
  - model_name: gpt-4o
    litellm_params:
      model: openai/gpt-4o
      api_key: sk-same-key
  - model_name: gpt-4o-mini
    litellm_params:
      model: openai/gpt-4o-mini
      api_key: sk-same-key
"#;

        let (multi, router) = from_litellm_yaml(yaml);
        // Same provider + base_url + api_key = one backend
        assert_eq!(multi.backends.len(), 1);
        assert!(router.has_model("gpt-4o"));
        assert!(router.has_model("gpt-4o-mini"));
    }

    #[test]
    fn os_environ_syntax_in_litellm_yaml() {
        // Set env var for test
        unsafe { std::env::set_var("TEST_LITELLM_KEY", "sk-from-env") };

        let yaml = r#"
model_list:
  - model_name: test-model
    litellm_params:
      model: openai/gpt-4o
      api_key: "os.environ/TEST_LITELLM_KEY"
"#;

        let (multi, _) = from_litellm_yaml(yaml);
        let bc = multi.backends.values().next().unwrap();
        assert_eq!(bc.api_key, "sk-from-env");

        unsafe { std::env::remove_var("TEST_LITELLM_KEY") };
    }

    #[test]
    fn unknown_settings_are_accepted() {
        let yaml = r#"
model_list:
  - model_name: gpt-4o
    litellm_params:
      model: openai/gpt-4o
      api_key: sk-test
      some_unknown_param: true

litellm_settings:
  drop_params: true
  some_future_setting: 42

general_settings:
  some_unknown_general: "value"
"#;

        // Should not panic; unknown fields are captured by serde(flatten).
        let (multi, router) = from_litellm_yaml(yaml);
        assert_eq!(multi.backends.len(), 1);
        assert!(router.has_model("gpt-4o"));
    }

    #[test]
    fn routing_strategy_parsed() {
        let yaml = r#"
model_list:
  - model_name: gpt-4o
    litellm_params:
      model: openai/gpt-4o
      api_key: sk-test

router_settings:
  routing_strategy: least-busy
"#;
        let (_, router) = from_litellm_yaml(yaml);
        assert_eq!(router.strategy(), RoutingStrategy::LeastBusy);
    }

    #[test]
    fn routing_strategy_latency() {
        let yaml = r#"
model_list:
  - model_name: gpt-4o
    litellm_params:
      model: openai/gpt-4o
      api_key: sk-test

router_settings:
  routing_strategy: latency-based-routing
"#;
        let (_, router) = from_litellm_yaml(yaml);
        assert_eq!(router.strategy(), RoutingStrategy::LatencyBased);
    }

    #[test]
    fn routing_strategy_cost_based() {
        let yaml = r#"
model_list:
  - model_name: gpt-4o
    litellm_params:
      model: openai/gpt-4o
      api_key: sk-test

router_settings:
  routing_strategy: cost-based
"#;
        let (_, router) = from_litellm_yaml(yaml);
        assert_eq!(router.strategy(), RoutingStrategy::CostBased);
    }

    #[test]
    fn routing_strategy_defaults_to_round_robin() {
        let yaml = r#"
model_list:
  - model_name: gpt-4o
    litellm_params:
      model: openai/gpt-4o
      api_key: sk-test
"#;
        let (_, router) = from_litellm_yaml(yaml);
        assert_eq!(router.strategy(), RoutingStrategy::RoundRobin);
    }

    #[test]
    fn weight_field_parsed() {
        let yaml = r#"
model_list:
  - model_name: gpt-4o
    litellm_params:
      model: openai/gpt-4o
      api_key: sk-key-1
      weight: 3
  - model_name: gpt-4o
    litellm_params:
      model: openai/gpt-4o
      api_key: sk-key-2
      weight: 1

router_settings:
  routing_strategy: weighted
"#;
        let (_, router) = from_litellm_yaml(yaml);
        assert_eq!(router.strategy(), RoutingStrategy::Weighted);
        assert!(router.has_model("gpt-4o"));
    }

    #[test]
    fn langfuse_callback_sets_flag() {
        let yaml = r#"
model_list:
  - model_name: gpt-4o
    litellm_params:
      model: openai/gpt-4o
      api_key: sk-test
litellm_settings:
  callbacks:
    - langfuse
"#;
        let parsed = parse_litellm_yaml(yaml);
        assert!(parsed.langfuse_requested);
        assert!(parsed.callback_urls.is_empty()); // "langfuse" filtered out
    }

    #[test]
    fn webhook_url_not_flagged_as_langfuse() {
        let yaml = r#"
model_list:
  - model_name: gpt-4o
    litellm_params:
      model: openai/gpt-4o
      api_key: sk-test
litellm_settings:
  callbacks:
    - https://my-webhook.example.com/hook
"#;
        let parsed = parse_litellm_yaml(yaml);
        assert!(!parsed.langfuse_requested);
        assert_eq!(parsed.callback_urls.len(), 1);
    }

    #[test]
    #[should_panic(expected = "at least one entry")]
    fn empty_model_list_panics() {
        let yaml = r#"
model_list: []
"#;
        from_litellm_yaml(yaml);
    }

    #[test]
    fn gemini_provider() {
        let yaml = r#"
model_list:
  - model_name: gemini-pro
    litellm_params:
      model: gemini/gemini-pro
      api_key: AIzaSy-test
"#;

        let (multi, router) = from_litellm_yaml(yaml);
        assert_eq!(multi.backends.len(), 1);
        let bc = multi.backends.values().next().unwrap();
        assert_eq!(bc.kind, BackendKind::Gemini);
        assert!(router.has_model("gemini-pro"));
    }
}