litellm-rs 0.9.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
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
//! Read-only admin inventory of the live routing snapshot.
//!
//! The response DTO is a public-field projection: API keys, auth headers,
//! secrets, and raw provider config are not fields and cannot be serialized.

use super::admin::require_admin;
use crate::config::models::provider::ProviderConfig;
use crate::core::providers::is_provider_selector_supported;
use crate::core::providers::registry::{ProviderDispatchKind, entry_for_name};
use crate::core::router::deployment::current_timestamp;
use crate::core::router::{Deployment, HealthStatus, RoutingSnapshot};
use crate::core::types::model::ProviderCapability;
use crate::server::state::AppState;
use actix_web::{HttpRequest, HttpResponse, web};
use serde::Serialize;
use std::collections::{HashMap, HashSet};
use std::sync::atomic::Ordering::Relaxed;

const ADMIN_ERROR: &str = "Admin role required for routing inventory";

/// `GET /admin/routing/inventory`
pub(super) async fn routing_inventory(
    req: HttpRequest,
    state: web::Data<AppState>,
) -> actix_web::Result<HttpResponse> {
    if let Some(forbidden) = require_admin(&req, &state, "inspect routing inventory", ADMIN_ERROR) {
        return Ok(forbidden);
    }

    let snapshot = state.unified_router().load_routing_snapshot();
    let cfg = state.config.load();
    Ok(HttpResponse::Ok().json(build_inventory(&snapshot, cfg.providers())))
}

pub(super) fn configure_routes(cfg: &mut web::ServiceConfig) {
    cfg.service(
        web::scope("/admin/routing")
            .route("/inventory", web::get().to(routing_inventory))
            .route("/revision", web::get().to(runtime_revision))
            .route(
                "/policy",
                web::get().to(super::admin_routing_policy::get_routing_policy),
            )
            .route(
                "/policy",
                web::put().to(super::admin_routing_policy::put_routing_policy),
            ),
    );
}

/// Sanitized inventory of effective routing state.
#[derive(Debug, Serialize, PartialEq)]
pub struct RoutingInventory {
    pub success: bool,
    pub snapshot_generation: u64,
    pub models: Vec<PublicModelInventory>,
    pub unavailable_providers: Vec<UnavailableProviderInventory>,
}

/// Public model group and the deployments that can serve it.
#[derive(Debug, Serialize, PartialEq)]
pub struct PublicModelInventory {
    pub public_model: String,
    pub aliases: Vec<String>,
    pub deployments: Vec<DeploymentInventory>,
}

/// One deployment's live routing state. Public fields only.
#[derive(Debug, Serialize, PartialEq)]
pub struct DeploymentInventory {
    pub provider: String,
    pub deployment: String,
    pub public_model: String,
    pub model: String,
    pub capabilities: Vec<ProviderCapability>,
    pub health: InventoryHealth,
    pub available: bool,
    pub unavailable_reasons: Vec<UnavailableReason>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cooldown: Option<CooldownInventory>,
    pub rpm: RateWindowInventory,
    pub tpm: RateWindowInventory,
    pub active_requests: u32,
}

/// Configured provider that is not present in the live routing snapshot.
#[derive(Debug, Serialize, PartialEq)]
pub struct UnavailableProviderInventory {
    pub provider: String,
    pub provider_type: String,
    pub public_models: Vec<String>,
    pub available: bool,
    pub unavailable_reasons: Vec<UnavailableReason>,
}

/// Probe-derived health. Missing evidence is `unknown`, never `healthy`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum InventoryHealth {
    Unknown,
    Healthy,
    Degraded,
    Unhealthy,
}

/// Why a deployment or configured provider cannot serve traffic.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum UnavailableReason {
    Cooldown,
    Unhealthy,
    RpmLimitReached,
    TpmLimitReached,
    ParallelLimitReached,
    FeatureGated,
    Unavailable,
}

/// Cooldown remaining from live `cooldown_until`. Omitted when not cooling down.
#[derive(Debug, Serialize, PartialEq, Eq)]
pub struct CooldownInventory {
    pub until_unix_secs: u64,
    pub remaining_secs: u64,
}

/// Configured limit versus live per-minute usage. `configured_limit` is `null`
/// when unlimited; `current_usage` is omitted when this minute has no data.
#[derive(Debug, Serialize, PartialEq, Eq)]
pub struct RateWindowInventory {
    pub configured_limit: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub current_usage: Option<u64>,
}

fn build_inventory(snapshot: &RoutingSnapshot, providers: &[ProviderConfig]) -> RoutingInventory {
    let aliases_by_model = aliases_by_public_model(snapshot);
    let models: Vec<PublicModelInventory> = snapshot
        .model_order
        .iter()
        .filter_map(|public_model| {
            let ids = snapshot.model_index.get(public_model)?;
            let mut deployments: Vec<DeploymentInventory> = ids
                .iter()
                .filter_map(|id| snapshot.deployments.get(id))
                .map(|deployment| project_deployment(snapshot, deployment))
                .collect();
            if deployments.is_empty() {
                return None;
            }
            deployments.sort_by(|left, right| left.deployment.cmp(&right.deployment));
            Some(PublicModelInventory {
                public_model: public_model.clone(),
                aliases: aliases_by_model
                    .get(public_model)
                    .cloned()
                    .unwrap_or_default(),
                deployments,
            })
        })
        .collect();

    RoutingInventory {
        success: true,
        snapshot_generation: snapshot.generation(),
        models,
        unavailable_providers: unavailable_configured_providers(snapshot, providers),
    }
}

fn aliases_by_public_model(snapshot: &RoutingSnapshot) -> HashMap<String, Vec<String>> {
    let mut aliases: HashMap<String, Vec<String>> = HashMap::new();
    for alias in snapshot.model_aliases.keys() {
        let target = snapshot.resolve_model_name(alias);
        aliases.entry(target).or_default().push(alias.clone());
    }
    for names in aliases.values_mut() {
        names.sort();
    }
    aliases
}

fn project_deployment(snapshot: &RoutingSnapshot, deployment: &Deployment) -> DeploymentInventory {
    let now = current_timestamp();
    let in_cooldown = deployment.is_in_cooldown();
    let minute = deployment.state.minute_counters(now);
    let active_requests = deployment.state.active_requests.load(Relaxed);
    let mut unavailable_reasons = Vec::new();

    if in_cooldown {
        unavailable_reasons.push(UnavailableReason::Cooldown);
    }
    if !deployment.is_healthy() {
        match deployment.state.health_status() {
            HealthStatus::Unknown => {}
            HealthStatus::Healthy | HealthStatus::Degraded => {}
            HealthStatus::Unhealthy | HealthStatus::Cooldown => {
                unavailable_reasons.push(UnavailableReason::Unhealthy);
            }
        }
    }
    if let Some(limit) = deployment.config.max_parallel_requests
        && active_requests >= limit
    {
        unavailable_reasons.push(UnavailableReason::ParallelLimitReached);
    }
    if let Some(limit) = deployment.config.rpm_limit
        && minute.rpm >= limit
    {
        unavailable_reasons.push(UnavailableReason::RpmLimitReached);
    }
    if let Some(limit) = deployment.config.tpm_limit
        && minute.tpm >= limit
    {
        unavailable_reasons.push(UnavailableReason::TpmLimitReached);
    }

    let available = unavailable_reasons.is_empty() && deployment.is_healthy() && !in_cooldown;

    DeploymentInventory {
        provider: snapshot
            .provider_names
            .get(&deployment.id)
            .cloned()
            .unwrap_or_else(|| deployment.provider.name().to_string()),
        deployment: deployment.id.clone(),
        public_model: deployment.model_name.clone(),
        model: deployment.model.clone(),
        capabilities: deployment_capabilities(deployment),
        health: inventory_health(deployment.state.probe_health_status()),
        available,
        unavailable_reasons,
        cooldown: cooldown_inventory(deployment, now, in_cooldown),
        rpm: rate_window(
            deployment.config.rpm_limit,
            minute.rpm,
            has_minute_usage(deployment),
        ),
        tpm: rate_window(
            deployment.config.tpm_limit,
            minute.tpm,
            has_minute_usage(deployment),
        ),
        active_requests,
    }
}

fn deployment_capabilities(deployment: &Deployment) -> Vec<ProviderCapability> {
    deployment
        .provider
        .capabilities()
        .iter()
        .filter(|capability| {
            deployment
                .provider
                .supports_capability_for_model(&deployment.model, capability)
        })
        .cloned()
        .collect()
}

fn inventory_health(status: HealthStatus) -> InventoryHealth {
    match status {
        HealthStatus::Unknown => InventoryHealth::Unknown,
        HealthStatus::Healthy => InventoryHealth::Healthy,
        HealthStatus::Degraded => InventoryHealth::Degraded,
        HealthStatus::Unhealthy | HealthStatus::Cooldown => InventoryHealth::Unhealthy,
    }
}

fn cooldown_inventory(
    deployment: &Deployment,
    now: u64,
    in_cooldown: bool,
) -> Option<CooldownInventory> {
    if !in_cooldown {
        return None;
    }
    let until = deployment.state.cooldown_until.load(Relaxed);
    if until == 0 {
        return None;
    }
    Some(CooldownInventory {
        until_unix_secs: until,
        remaining_secs: until.saturating_sub(now),
    })
}

fn has_minute_usage(deployment: &Deployment) -> bool {
    deployment.state.last_request_at.load(Relaxed) > 0
        || deployment.state.total_requests.load(Relaxed) > 0
}

fn rate_window(
    configured_limit: Option<u64>,
    current: u64,
    has_usage: bool,
) -> RateWindowInventory {
    RateWindowInventory {
        configured_limit,
        current_usage: has_usage.then_some(current),
    }
}

fn unavailable_configured_providers(
    snapshot: &RoutingSnapshot,
    providers: &[ProviderConfig],
) -> Vec<UnavailableProviderInventory> {
    let routed: HashSet<&str> = snapshot
        .provider_names
        .values()
        .map(String::as_str)
        .collect();
    let mut unavailable = Vec::new();
    for provider in providers {
        if !provider.enabled || routed.contains(provider.name.as_str()) {
            continue;
        }
        let selector = if provider.provider_type.trim().is_empty() {
            provider.name.as_str()
        } else {
            provider.provider_type.as_str()
        };
        unavailable.push(UnavailableProviderInventory {
            provider: provider.name.clone(),
            provider_type: provider.provider_type.clone(),
            public_models: provider.models.clone(),
            available: false,
            unavailable_reasons: vec![configured_provider_reason(selector)],
        });
    }
    unavailable.sort_by(|left, right| left.provider.cmp(&right.provider));
    unavailable
}

fn configured_provider_reason(selector: &str) -> UnavailableReason {
    if is_provider_selector_supported(selector) {
        return UnavailableReason::Unavailable;
    }
    match entry_for_name(selector) {
        Some(entry) if entry.dispatch_kind == ProviderDispatchKind::UnsupportedEnum => {
            UnavailableReason::FeatureGated
        }
        _ => UnavailableReason::Unavailable,
    }
}

/// Node-local synchronization status for operators.
async fn runtime_revision(
    req: HttpRequest,
    state: web::Data<AppState>,
) -> actix_web::Result<HttpResponse> {
    if let Some(forbidden) = require_admin(&req, &state, "inspect runtime revision", ADMIN_ERROR) {
        return Ok(forbidden);
    }
    Ok(HttpResponse::Ok().json(state.config_sync_status()))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::Config;
    use crate::core::models::{
        ApiKey, Metadata, UsageStats,
        user::{
            preferences::UserPreferences,
            types::{User, UserProfile, UserRole, UserStatus},
        },
    };
    use crate::core::providers::Provider;
    use crate::core::providers::openai::OpenAIProvider;
    use crate::core::router::{Deployment, DeploymentConfig};
    use crate::server::HttpServer;
    use actix_web::dev::Service;
    use actix_web::{App, HttpMessage, http::StatusCode, test as actix_test};
    use std::collections::BTreeSet;
    use std::sync::atomic::Ordering;

    const SECRET: &str = "sk-inventory-redaction-secret-do-not-leak";

    fn base_test_config(auth_enabled: bool) -> Config {
        let mut config = Config::default();
        config.gateway.auth.enable_jwt = auth_enabled;
        config.gateway.auth.enable_api_key = auth_enabled;
        config.gateway.auth.allow_anonymous = !auth_enabled;
        config.gateway.auth.jwt_secret = "AaaAaaAaaAaaAaaAaaAaaAaaAaaAaa1!".to_string();
        config.gateway.storage.database.enabled = false;
        config.gateway.storage.redis.enabled = false;
        config.gateway.pricing.source = None;
        config.gateway.providers.push(ProviderConfig {
            name: "bootstrap".to_string(),
            provider_type: "openai".to_string(),
            api_key: "sk-test".to_string(),
            enabled: false,
            models: vec!["bootstrap-model".to_string()],
            ..ProviderConfig::default()
        });
        config
    }

    async fn test_state(config: Config) -> web::Data<AppState> {
        let server = match HttpServer::new(&config).await {
            Ok(server) => server,
            Err(error) => panic!("server startup failed: {error}"),
        };
        web::Data::new(server.state().clone())
    }

    fn make_test_user(role: UserRole) -> User {
        User {
            metadata: Metadata::new(),
            username: "testuser".to_string(),
            email: "test@example.com".to_string(),
            display_name: None,
            password_hash: "hash".to_string(),
            role,
            status: UserStatus::Active,
            team_ids: vec![],
            preferences: UserPreferences::default(),
            usage_stats: UsageStats::default(),
            rate_limits: None,
            last_login_at: None,
            email_verified: true,
            two_factor_enabled: false,
            profile: UserProfile::default(),
        }
    }

    fn make_api_key() -> ApiKey {
        ApiKey {
            metadata: Metadata::new(),
            name: "route-key".to_string(),
            key_hash: "hashed".to_string(),
            key_prefix: "gw-ab".to_string(),
            user_id: None,
            team_id: None,
            permissions: vec!["api.chat".to_string()],
            rate_limits: None,
            expires_at: None,
            is_active: true,
            last_used_at: None,
            usage_stats: UsageStats::default(),
        }
    }

    async fn admin_app(
        state: web::Data<AppState>,
        user: Option<User>,
        api_key: Option<ApiKey>,
    ) -> impl Service<
        actix_http::Request,
        Response = actix_web::dev::ServiceResponse,
        Error = actix_web::Error,
    > {
        actix_test::init_service(
            App::new()
                .app_data(state)
                .wrap_fn(move |req, srv| {
                    if let Some(user) = user.clone() {
                        req.extensions_mut().insert(user);
                    }
                    if let Some(api_key) = api_key.clone() {
                        req.extensions_mut().insert(api_key);
                    }
                    srv.call(req)
                })
                .configure(crate::server::routes::admin::configure_routes),
        )
        .await
    }

    async fn get_inventory(
        app: &impl Service<
            actix_http::Request,
            Response = actix_web::dev::ServiceResponse,
            Error = actix_web::Error,
        >,
    ) -> (StatusCode, serde_json::Value) {
        let req = actix_test::TestRequest::get()
            .uri("/admin/routing/inventory")
            .to_request();
        let resp = actix_test::call_service(app, req).await;
        let status = resp.status();
        let body = actix_test::read_body(resp).await;
        let json: serde_json::Value = serde_json::from_slice(&body).unwrap_or_else(|_| {
            panic!(
                "inventory response should be JSON, got {}",
                String::from_utf8_lossy(&body)
            )
        });
        (status, json)
    }

    async fn openai_deployment(id: &str, public_model: &str, wire_model: &str) -> Deployment {
        let provider = Provider::OpenAI(
            OpenAIProvider::with_api_key(SECRET)
                .await
                .expect("test provider"),
        );
        Deployment::new(
            id.to_string(),
            provider,
            wire_model.to_string(),
            public_model.to_string(),
        )
    }

    fn json_keys(value: &serde_json::Value) -> BTreeSet<String> {
        let mut keys = BTreeSet::new();
        collect_keys(value, &mut keys);
        keys
    }

    fn collect_keys(value: &serde_json::Value, keys: &mut BTreeSet<String>) {
        match value {
            serde_json::Value::Object(map) => {
                for (key, child) in map {
                    keys.insert(key.clone());
                    collect_keys(child, keys);
                }
            }
            serde_json::Value::Array(items) => {
                for child in items {
                    collect_keys(child, keys);
                }
            }
            _ => {}
        }
    }

    #[test]
    fn pydantic_ai_is_reported_as_feature_gated() {
        assert_eq!(
            configured_provider_reason("pydantic_ai"),
            UnavailableReason::FeatureGated
        );
        assert_ne!(
            configured_provider_reason("openai"),
            UnavailableReason::FeatureGated
        );
        assert_eq!(
            configured_provider_reason("totally_unknown_provider"),
            UnavailableReason::Unavailable
        );
    }

    #[test]
    fn unknown_probe_health_is_not_healthy() {
        assert_eq!(
            inventory_health(HealthStatus::Unknown),
            InventoryHealth::Unknown
        );
        assert_eq!(
            inventory_health(HealthStatus::Unhealthy),
            InventoryHealth::Unhealthy
        );
        assert_ne!(
            inventory_health(HealthStatus::Unknown),
            InventoryHealth::Healthy
        );
    }

    #[actix_web::test]
    async fn inventory_requires_admin_identity() {
        let state = test_state(base_test_config(true)).await;
        let app = admin_app(state, None, None).await;
        let (status, body) = get_inventory(&app).await;
        assert_eq!(status, StatusCode::FORBIDDEN);
        assert_eq!(body["success"], false);
        assert_eq!(body["error"], ADMIN_ERROR);
    }

    #[actix_web::test]
    async fn inventory_rejects_non_admin_user() {
        let state = test_state(base_test_config(true)).await;
        let app = admin_app(state, Some(make_test_user(UserRole::User)), None).await;
        let (status, _) = get_inventory(&app).await;
        assert_eq!(status, StatusCode::FORBIDDEN);
    }

    #[actix_web::test]
    async fn inventory_rejects_api_key_without_admin_user() {
        let state = test_state(base_test_config(true)).await;
        let app = admin_app(state, None, Some(make_api_key())).await;
        let (status, _) = get_inventory(&app).await;
        assert_eq!(status, StatusCode::FORBIDDEN);
    }

    #[actix_web::test]
    async fn inventory_empty_state() {
        let state = test_state(base_test_config(true)).await;
        let app = admin_app(state, Some(make_test_user(UserRole::Admin)), None).await;
        let (status, body) = get_inventory(&app).await;
        assert_eq!(status, StatusCode::OK);
        assert_eq!(body["success"], true);
        assert_eq!(body["models"], serde_json::json!([]));
        assert_eq!(body["unavailable_providers"], serde_json::json!([]));
        assert!(body["snapshot_generation"].as_u64().is_some());
    }

    #[actix_web::test]
    async fn inventory_redacts_secrets_and_raw_config() {
        let state = test_state(base_test_config(true)).await;
        state.unified_router().add_deployment(
            openai_deployment("secret-dep", "gpt-4", "gpt-4-turbo")
                .await
                .with_config(DeploymentConfig {
                    rpm_limit: Some(50),
                    tpm_limit: Some(10_000),
                    ..DeploymentConfig::default()
                }),
        );

        let mut cfg = (*state.config.load()).clone();
        cfg.gateway.providers.push(ProviderConfig {
            name: "hidden-ollama".to_string(),
            provider_type: "pydantic_ai".to_string(),
            api_key: SECRET.to_string(),
            models: vec!["llama3".to_string()],
            settings: [(
                "headers".to_string(),
                serde_json::json!({"Authorization": format!("Bearer {SECRET}")}),
            )]
            .into_iter()
            .collect(),
            ..ProviderConfig::default()
        });
        state.config.store(cfg);

        let app = admin_app(state, Some(make_test_user(UserRole::Admin)), None).await;
        let (status, body) = get_inventory(&app).await;
        assert_eq!(status, StatusCode::OK);

        let encoded = body.to_string();
        assert!(
            !encoded.contains(SECRET),
            "inventory leaked secret material: {encoded}"
        );
        let keys = json_keys(&body);
        for forbidden in [
            "api_key",
            "api_token",
            "authorization",
            "Authorization",
            "headers",
            "custom_headers",
            "settings",
            "password",
            "secret",
            "credential",
            "key_hash",
        ] {
            assert!(
                !keys.contains(forbidden),
                "inventory serialized forbidden key {forbidden}: {keys:?}"
            );
        }
        assert_eq!(
            body["models"][0]["deployments"][0]["rpm"]["configured_limit"],
            50
        );
        assert!(body["models"][0]["deployments"][0]["rpm"]["current_usage"].is_null());
    }

    #[actix_web::test]
    async fn inventory_reports_aliases_and_multi_deployment() {
        let state = test_state(base_test_config(true)).await;
        let mut primary = openai_deployment("openai-a", "gpt-4", "gpt-4-turbo").await;
        primary.config.rpm_limit = Some(100);
        primary.config.tpm_limit = Some(1_000);
        let secondary = openai_deployment("openai-b", "gpt-4", "gpt-4-turbo").await;
        state.unified_router().add_deployment(primary);
        state.unified_router().add_deployment(secondary);
        state
            .unified_router()
            .add_model_alias("gpt4", "gpt-4")
            .expect("alias");
        state.unified_router().record_success("openai-a", 25, 1_000);

        let app = admin_app(state, Some(make_test_user(UserRole::Admin)), None).await;
        let (status, body) = get_inventory(&app).await;
        assert_eq!(status, StatusCode::OK);
        assert_eq!(body["models"].as_array().map(Vec::len), Some(1));
        assert_eq!(body["models"][0]["public_model"], "gpt-4");
        assert_eq!(body["models"][0]["aliases"], serde_json::json!(["gpt4"]));
        let deployments = body["models"][0]["deployments"].as_array().unwrap();
        assert_eq!(deployments.len(), 2);
        assert_eq!(deployments[0]["deployment"], "openai-a");
        assert_eq!(deployments[1]["deployment"], "openai-b");
        assert_eq!(deployments[0]["rpm"]["configured_limit"], 100);
        assert_eq!(deployments[0]["rpm"]["current_usage"], 1);
        assert_eq!(deployments[0]["tpm"]["configured_limit"], 1_000);
        assert_eq!(deployments[0]["tpm"]["current_usage"], 25);
        assert!(deployments[1]["rpm"]["current_usage"].is_null());
        assert!(deployments[0]["capabilities"].as_array().is_some());
    }

    #[actix_web::test]
    async fn inventory_preserves_snapshot_model_order() {
        let state = test_state(base_test_config(true)).await;
        state
            .unified_router()
            .add_deployment(openai_deployment("dep-zeta", "zeta", "zeta").await);
        state
            .unified_router()
            .add_deployment(openai_deployment("dep-alpha", "alpha", "alpha").await);

        let app = admin_app(state, Some(make_test_user(UserRole::Admin)), None).await;
        let (status, body) = get_inventory(&app).await;
        assert_eq!(status, StatusCode::OK);
        assert_eq!(
            body["models"]
                .as_array()
                .unwrap()
                .iter()
                .map(|model| model["public_model"].as_str().unwrap())
                .collect::<Vec<_>>(),
            vec!["zeta", "alpha"]
        );
    }

    #[actix_web::test]
    async fn inventory_distinguishes_unknown_health_from_unhealthy() {
        let state = test_state(base_test_config(true)).await;
        let unknown = openai_deployment("dep-unknown", "gpt-4", "gpt-4-turbo").await;
        let unhealthy = openai_deployment("dep-unhealthy", "gpt-4", "gpt-4-turbo").await;
        state.unified_router().add_deployment(unknown);
        state.unified_router().add_deployment(unhealthy);
        let stored = state
            .unified_router()
            .get_deployment("dep-unhealthy")
            .expect("unhealthy deployment");
        stored
            .state
            .health
            .store(HealthStatus::Unhealthy as u8, Ordering::Relaxed);
        stored
            .state
            .set_probe_health_status(HealthStatus::Unhealthy);

        let app = admin_app(state, Some(make_test_user(UserRole::Admin)), None).await;
        let (status, body) = get_inventory(&app).await;
        assert_eq!(status, StatusCode::OK);
        let deployments = body["models"][0]["deployments"].as_array().unwrap();
        let unknown = deployments
            .iter()
            .find(|row| row["deployment"] == "dep-unknown")
            .unwrap();
        let unhealthy = deployments
            .iter()
            .find(|row| row["deployment"] == "dep-unhealthy")
            .unwrap();
        assert_eq!(unknown["health"], "unknown");
        assert_ne!(unknown["health"], "healthy");
        assert_eq!(unknown["available"], true);
        assert_eq!(unhealthy["health"], "unhealthy");
        assert_eq!(unhealthy["available"], false);
        assert!(
            unhealthy["unavailable_reasons"]
                .as_array()
                .unwrap()
                .iter()
                .any(|reason| reason == "unhealthy")
        );
    }

    #[actix_web::test]
    async fn inventory_reports_feature_gated_provider_unavailable() {
        let state = test_state(base_test_config(true)).await;
        let mut cfg = (*state.config.load()).clone();
        cfg.gateway.providers.push(ProviderConfig {
            name: "offline-pydantic".to_string(),
            provider_type: "pydantic_ai".to_string(),
            api_key: SECRET.to_string(),
            models: vec!["agent-model".to_string()],
            ..ProviderConfig::default()
        });
        state.config.store(cfg);

        let app = admin_app(state, Some(make_test_user(UserRole::Admin)), None).await;
        let (status, body) = get_inventory(&app).await;
        assert_eq!(status, StatusCode::OK);
        assert_eq!(
            body["unavailable_providers"][0]["provider"],
            "offline-pydantic"
        );
        assert_eq!(body["unavailable_providers"][0]["available"], false);
        assert_eq!(
            body["unavailable_providers"][0]["unavailable_reasons"],
            serde_json::json!(["feature_gated"])
        );
        assert_eq!(
            body["unavailable_providers"][0]["public_models"],
            serde_json::json!(["agent-model"])
        );
        assert!(!body.to_string().contains(SECRET));
    }
}