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
//! Deployment selection logic
//!
//! This module contains the core routing logic for selecting
//! the best deployment for a given model.

use super::admission::{AdmissionBackend, AdmissionHold, AdmissionReserve};
use super::config::RoutingStrategy;
use super::deployment::{Deployment, DeploymentId, current_timestamp};
use super::error::RouterError;
use super::execution::router_error_to_provider_error;
use super::strategy_impl::{self, RoutingContext};
use super::unified::Router;
use super::{RoutingSnapshot, RuntimeHandle};
use crate::core::types::model::ProviderCapability;
use dashmap::DashMap;
use std::sync::Arc;
use std::sync::atomic::{
    AtomicUsize,
    Ordering::{AcqRel, Acquire, Relaxed},
};

/// Active-request reservation for a selected deployment.
///
/// Dropping the lease releases the selected deployment unless ownership was
/// explicitly converted back into the legacy deployment-id API.
#[derive(Debug)]
pub struct DeploymentLease {
    deployment: Arc<Deployment>,
    release_on_drop: bool,
    admission: AdmissionBackend,
    hold: Option<AdmissionHold>,
    async_cleanup: bool,
}

impl DeploymentLease {
    fn new(
        deployment: Arc<Deployment>,
        admission: AdmissionBackend,
        hold: Option<AdmissionHold>,
    ) -> Self {
        Self {
            deployment,
            release_on_drop: true,
            admission,
            hold,
            async_cleanup: true,
        }
    }

    #[cfg(all(test, feature = "gateway"))]
    pub(crate) fn commit_admission(&mut self, actual_tokens: u64) {
        if let Some(hold) = self.hold.take() {
            self.admission.settle(&hold, actual_tokens);
        }
    }

    pub(crate) fn preserve_admission_usage(&self, actual_tokens: u64) {
        if let Some(hold) = &self.hold {
            hold.prepare_settlement(actual_tokens);
        }
    }

    pub(crate) fn record_success(&self, tokens: u64, latency_us: u64) {
        self.deployment
            .record_success_with_admission(tokens, latency_us, self.hold.as_ref());
    }

    pub(crate) fn record_interrupted_usage(&self, tokens: u64) {
        self.deployment
            .record_interrupted_usage_with_admission(tokens, self.hold.as_ref());
    }

    pub(crate) fn preserve_admission_reservation(&self, minimum_tokens: u64) {
        if let Some(hold) = &self.hold {
            hold.prepare_retention(minimum_tokens);
        }
    }

    pub(crate) async fn retain_admission_async(&mut self, minimum_tokens: u64) {
        if let Some(hold) = self.hold.take() {
            self.admission.retain_async(&hold, minimum_tokens).await;
        }
    }

    #[cfg(feature = "gateway")]
    pub(crate) fn clone_admission_hold(&self) -> Option<AdmissionHold> {
        self.hold.clone()
    }

    pub(crate) async fn commit_admission_async(&mut self, actual_tokens: u64) {
        if let Some(hold) = self.hold.take() {
            self.admission.settle_async(&hold, actual_tokens).await;
        }
    }

    pub(crate) async fn cancel_admission_async(&mut self) {
        if let Some(hold) = self.hold.take() {
            self.admission.cancel_async(&hold).await;
        }
    }

    fn synchronous_cleanup(mut self) -> Self {
        self.async_cleanup = false;
        self
    }

    #[cfg(feature = "gateway")]
    pub(crate) fn take_admission(&mut self) -> (AdmissionBackend, Option<AdmissionHold>) {
        (self.admission.clone(), self.hold.take())
    }

    pub fn deployment_id(&self) -> &str {
        &self.deployment.id
    }

    pub fn deployment(&self) -> &Deployment {
        &self.deployment
    }

    pub fn clone_deployment_id(&self) -> DeploymentId {
        self.deployment_id().to_string()
    }

    pub fn clone_deployment(&self) -> Arc<Deployment> {
        self.deployment.clone()
    }

    /// Convert this lease into the legacy deployment-id API.
    ///
    /// Prefer keeping the lease alive so drop releases the exact snapshot
    /// deployment. Deprecated ID selectors retain only the local active count
    /// until `release_deployment`; shared admission requires an owned lease.
    pub fn into_deployment_id(mut self) -> DeploymentId {
        self.release_on_drop = false;
        if let Some(hold) = self.hold.take() {
            // The ID cannot carry a shared reservation's cleanup obligation.
            // Failed cancellation leaves the hold's Drop retry intact.
            self.admission.cancel(&hold);
        }
        self.deployment_id().to_string()
    }
}

impl Drop for DeploymentLease {
    fn drop(&mut self) {
        if self.release_on_drop {
            Router::release_selected_deployment(self.deployment());
            if !self.async_cleanup
                && let Some(hold) = self.hold.take()
            {
                self.admission.cancel(&hold);
            }
        }
    }
}

impl Router {
    /// Select the best deployment for a given model (core routing method)
    ///
    /// # Flow
    ///
    /// 1. Resolve model_name (handle aliases)
    /// 2. Get all deployment IDs for this model
    /// 3. Filter: healthy + not in cooldown + not rate limited
    /// 4. Select based on routing strategy
    /// 5. Increment active_requests counter
    #[deprecated(
        since = "0.5.0",
        note = "Use select_deployment_lease so the selected snapshot deployment is released by RAII"
    )]
    pub fn select_deployment(&self, model_name: &str) -> Result<DeploymentId, RouterError> {
        self.select_deployment_lease(model_name)
            .map(DeploymentLease::into_deployment_id)
    }

    /// Select and reserve the best deployment for a given model.
    ///
    /// The returned lease releases the deployment on drop, which protects
    /// router-owned execution paths from cancellation leaks.
    pub fn select_deployment_lease(
        &self,
        model_name: &str,
    ) -> Result<DeploymentLease, RouterError> {
        super::sync_compat::wait(self.select_deployment_lease_async(model_name))
            .map(DeploymentLease::synchronous_cleanup)
    }

    /// Select without blocking the caller's executor on shared Redis state.
    pub async fn select_deployment_lease_async(
        &self,
        model_name: &str,
    ) -> Result<DeploymentLease, RouterError> {
        let snapshot = self.load_routing_snapshot();
        self.select_deployment_matching(snapshot.as_ref(), model_name, |_| true, None, 0)
            .await
    }

    pub(crate) async fn select_deployment_lease_matching_in_snapshot<F>(
        &self,
        snapshot: &RoutingSnapshot,
        model_name: &str,
        is_candidate: F,
    ) -> Result<DeploymentLease, RouterError>
    where
        F: Fn(&Deployment) -> bool,
    {
        self.select_deployment_matching(snapshot, model_name, is_candidate, None, 0)
            .await
    }

    /// Select the best deployment for a model that supports `capability`.
    ///
    /// This uses the same health, cooldown, concurrency, rate-limit, and
    /// strategy filters as `select_deployment`, then reserves the selected
    /// deployment by incrementing its active request count.
    #[deprecated(
        since = "0.5.0",
        note = "Use select_deployment_lease_for_capability so the selected snapshot deployment is released by RAII"
    )]
    pub fn select_deployment_for_capability(
        &self,
        model_name: &str,
        capability: &ProviderCapability,
    ) -> Result<DeploymentId, RouterError> {
        self.select_deployment_lease_for_capability(model_name, capability)
            .map(DeploymentLease::into_deployment_id)
    }

    /// Select and reserve the best deployment for a model that supports
    /// `capability`.
    pub fn select_deployment_lease_for_capability(
        &self,
        model_name: &str,
        capability: &ProviderCapability,
    ) -> Result<DeploymentLease, RouterError> {
        self.select_deployment_lease_for_capability_matching(model_name, capability, |_| true)
    }

    /// Select and reserve the best deployment for a model that supports
    /// `capability` and passes an additional route-level predicate.
    pub fn select_deployment_lease_for_capability_matching<F>(
        &self,
        model_name: &str,
        capability: &ProviderCapability,
        is_candidate: F,
    ) -> Result<DeploymentLease, RouterError>
    where
        F: Fn(&Deployment) -> bool,
    {
        super::sync_compat::wait(self.select_deployment_lease_for_capability_matching_async(
            model_name,
            capability,
            is_candidate,
        ))
        .map(DeploymentLease::synchronous_cleanup)
    }

    /// Capability selection with asynchronous shared admission and circuit checks.
    pub async fn select_deployment_lease_for_capability_matching_async<F>(
        &self,
        model_name: &str,
        capability: &ProviderCapability,
        is_candidate: F,
    ) -> Result<DeploymentLease, RouterError>
    where
        F: Fn(&Deployment) -> bool,
    {
        let snapshot = self.load_routing_snapshot();
        self.select_deployment_lease_for_capability_matching_in_snapshot(
            snapshot.as_ref(),
            model_name,
            capability,
            is_candidate,
        )
        .await
    }

    pub(crate) async fn select_deployment_lease_for_capability_matching_in_snapshot<F>(
        &self,
        snapshot: &RoutingSnapshot,
        model_name: &str,
        capability: &ProviderCapability,
        is_candidate: F,
    ) -> Result<DeploymentLease, RouterError>
    where
        F: Fn(&Deployment) -> bool,
    {
        self.select_deployment_lease_for_capability_matching_with_estimate(
            snapshot,
            model_name,
            capability,
            is_candidate,
            0,
        )
        .await
    }

    pub(crate) async fn select_deployment_lease_for_capability_matching_with_estimate<F>(
        &self,
        snapshot: &RoutingSnapshot,
        model_name: &str,
        capability: &ProviderCapability,
        is_candidate: F,
        estimated_tokens: u64,
    ) -> Result<DeploymentLease, RouterError>
    where
        F: Fn(&Deployment) -> bool,
    {
        let resolved_name = snapshot.resolve_model_name(model_name);
        let candidates = snapshot
            .model_index
            .get(&resolved_name)
            .into_iter()
            .flatten()
            .filter_map(|id| snapshot.deployments.get(id));
        let (candidate_count, missing_mapping_count) =
            candidates.fold((0usize, 0usize), |(count, missing), deployment| {
                let missing_mapping = deployment
                    .provider
                    .deployment_model_identity()
                    .is_some_and(|identity| identity.capability_catalog_model().is_none());
                (count + 1, missing + usize::from(missing_mapping))
            });
        let no_matching_candidate_error = if candidate_count > 0
            && candidate_count == missing_mapping_count
        {
            RouterError::InvalidConfiguration(format!(
                "model '{model_name}' requires settings.model_identity_mappings.{resolved_name}.capability_catalog_model before capability routing"
            ))
        } else {
            RouterError::UnsupportedCapability {
                model: model_name.to_string(),
                capability: format!("{capability:?}"),
            }
        };

        self.select_deployment_matching(
            snapshot,
            model_name,
            |deployment| {
                deployment
                    .provider
                    .supports_capability_for_model(&deployment.model, capability)
                    && is_candidate(deployment)
            },
            Some(no_matching_candidate_error),
            estimated_tokens,
        )
        .await
    }

    /// Select a deployment ID from pre-built routing contexts.
    ///
    /// This is the shared routing-policy entry point used by the full
    /// `UnifiedRouter` and lightweight adapters such as the SDK client.
    pub fn select_from_routing_contexts<'id>(
        strategy: RoutingStrategy,
        model_name: &str,
        routing_contexts: &[RoutingContext<'id>],
        round_robin_counters: &DashMap<String, AtomicUsize>,
    ) -> Option<&'id DeploymentId> {
        match strategy {
            RoutingStrategy::SimpleShuffle => {
                strategy_impl::weighted_random_from_context(routing_contexts)
            }
            RoutingStrategy::LeastBusy => strategy_impl::least_busy_from_context(routing_contexts),
            RoutingStrategy::UsageBased => {
                strategy_impl::lowest_usage_from_context(routing_contexts)
            }
            RoutingStrategy::LatencyBased => {
                strategy_impl::lowest_latency_from_context(routing_contexts)
            }
            RoutingStrategy::PriorityBased => {
                strategy_impl::lowest_priority_from_context(routing_contexts)
            }
            RoutingStrategy::RateLimitAware => {
                strategy_impl::rate_limit_aware_from_context(routing_contexts)
            }
            RoutingStrategy::RoundRobin => strategy_impl::round_robin_from_context(
                model_name,
                routing_contexts,
                round_robin_counters,
            ),
        }
    }

    async fn select_deployment_matching<F>(
        &self,
        snapshot: &RoutingSnapshot,
        model_name: &str,
        is_candidate: F,
        no_matching_candidate_error: Option<RouterError>,
        estimated_tokens: u64,
    ) -> Result<DeploymentLease, RouterError>
    where
        F: Fn(&Deployment) -> bool,
    {
        // 1. Resolve model aliases and deployment indexes from one immutable
        // routing generation.
        let resolved_name = snapshot.resolve_model_name(model_name);

        // 2. Get all deployment IDs for this model.
        let deployment_ids_ref = snapshot
            .model_index
            .get(&resolved_name)
            .ok_or_else(|| RouterError::ModelNotFound(model_name.to_string()))?;

        if deployment_ids_ref.is_empty() {
            return Err(RouterError::ModelNotFound(model_name.to_string()));
        }

        // 3. Filter and build routing contexts in one pass to avoid cloning a
        // temporary candidate ID vector and then looking everything up again.
        let total_deployments = deployment_ids_ref.len();
        let mut existing_deployments = 0;
        let mut matching_deployments = 0;
        let mut routing_contexts = Vec::with_capacity(total_deployments);
        let now = current_timestamp();

        for id in deployment_ids_ref.iter() {
            let Some(deployment) = snapshot.deployments.get(id.as_str()) else {
                continue;
            };

            if deployment.model_name != resolved_name {
                tracing::debug!(
                    deployment_id = id.as_str(),
                    requested_model = %model_name,
                    resolved_model = %resolved_name,
                    deployment_model = %deployment.model_name,
                    "deployment index entry points at a different model"
                );
                continue;
            }
            existing_deployments += 1;

            if !is_candidate(deployment) {
                tracing::trace!(
                    deployment_id = id.as_str(),
                    model = %resolved_name,
                    reason = "capability_mismatch",
                    "deployment excluded from routing candidates"
                );
                continue;
            }
            matching_deployments += 1;

            // Shared circuit (or local cooldown/health) owns selection eligibility.
            if !self.deployment_is_selectable_async(deployment).await {
                tracing::trace!(
                    deployment_id = id.as_str(),
                    model = %resolved_name,
                    reason = "circuit_or_unhealthy",
                    "deployment excluded from routing candidates"
                );
                continue;
            }

            let active_requests = deployment.state.active_requests.load(Relaxed);
            if let Some(limit) = deployment.config.max_parallel_requests
                && active_requests >= limit
            {
                tracing::trace!(
                    deployment_id = id.as_str(),
                    model = %resolved_name,
                    reason = "parallel_limit_reached",
                    "deployment excluded from routing candidates"
                );
                continue;
            }

            let minute = deployment.state.minute_counters(now);
            let rpm_current = minute.rpm;
            if let Some(limit) = deployment.config.rpm_limit
                && rpm_current >= limit
            {
                tracing::trace!(
                    deployment_id = id.as_str(),
                    model = %resolved_name,
                    reason = "rate_limited",
                    "deployment excluded from routing candidates"
                );
                continue;
            }

            let tpm_current = deployment.state.admission_tpm(now);
            if let Some(limit) = deployment.config.tpm_limit
                && tpm_current >= limit
            {
                tracing::trace!(
                    deployment_id = id.as_str(),
                    model = %resolved_name,
                    reason = "rate_limited",
                    "deployment excluded from routing candidates"
                );
                continue;
            }

            routing_contexts.push(strategy_impl::RoutingContext {
                deployment_id: id,
                weight: deployment.config.weight,
                priority: deployment.config.priority,
                active_requests,
                tpm_current,
                tpm_limit: deployment.config.tpm_limit,
                rpm_current,
                rpm_limit: deployment.config.rpm_limit,
                avg_latency_us: deployment.state.avg_latency_us.load(Relaxed),
            });
        }

        if routing_contexts.is_empty() {
            if existing_deployments > 0
                && matching_deployments == 0
                && let Some(err) = no_matching_candidate_error
            {
                return Err(err);
            }

            tracing::warn!(
                model = %model_name,
                total_deployments = total_deployments,
                "no available deployments after filtering"
            );
            return Err(RouterError::NoAvailableDeployment(model_name.to_string()));
        }

        // 4. Select based on immutable routing contexts, then atomically reserve
        // the selected deployment. If another caller wins the race for the last
        // slot, remove that candidate and retry within this selection attempt.
        while !routing_contexts.is_empty() {
            let selected_id = Self::select_from_routing_contexts(
                self.config.routing_strategy,
                &resolved_name,
                &routing_contexts,
                &self.round_robin_counters,
            )
            .ok_or_else(|| RouterError::NoAvailableDeployment(model_name.to_string()))?
            .clone();

            let Some(deployment) = snapshot.deployments.get(&selected_id).cloned() else {
                continue;
            };

            if let Ok(hold) = self
                .try_reserve_deployment(&deployment, &resolved_name, estimated_tokens)
                .await
            {
                self.provider_selected_count.fetch_add(1, Relaxed);
                self.strategy_used_count.fetch_add(1, Relaxed);

                tracing::debug!(
                    model = %model_name,
                    strategy = ?self.config.routing_strategy,
                    candidate_count = routing_contexts.len(),
                    selected_id = %selected_id,
                    "deployment selected for routing"
                );

                return Ok(DeploymentLease::new(
                    deployment,
                    self.admission.clone(),
                    hold,
                ));
            }

            if let Some(pos) = routing_contexts
                .iter()
                .position(|ctx| ctx.deployment_id == &selected_id)
            {
                routing_contexts.swap_remove(pos);
            } else {
                break;
            }
        }

        tracing::warn!(
            model = %model_name,
            "no available deployments after reservation"
        );
        Err(RouterError::NoAvailableDeployment(model_name.to_string()))
    }

    async fn try_reserve_deployment(
        &self,
        deployment: &Deployment,
        expected_model: &str,
        estimated_tokens: u64,
    ) -> Result<Option<AdmissionHold>, RouterError> {
        if deployment.model_name != expected_model {
            return Err(RouterError::DeploymentNotFound(deployment.id.clone()));
        }

        let hold = match self
            .admission
            .reserve_async(deployment, estimated_tokens)
            .await
        {
            AdmissionReserve::Skipped => None,
            AdmissionReserve::Denied => {
                return Err(RouterError::RateLimitExceeded(expected_model.into()));
            }
            #[cfg(any(feature = "gateway", test))]
            AdmissionReserve::Unavailable => {
                return Err(RouterError::NoAvailableDeployment(expected_model.into()));
            }
            AdmissionReserve::Granted(hold) => Some(hold),
        };

        let local_ok = match deployment.config.max_parallel_requests {
            Some(limit) => {
                let mut current = deployment.state.active_requests.load(Acquire);
                loop {
                    if current >= limit {
                        break false;
                    }

                    match deployment.state.active_requests.compare_exchange_weak(
                        current,
                        current + 1,
                        AcqRel,
                        Acquire,
                    ) {
                        Ok(_) => break true,
                        Err(next) => current = next,
                    }
                }
            }
            None => {
                deployment.state.active_requests.fetch_add(1, Relaxed);
                true
            }
        };

        if local_ok {
            Ok(hold)
        } else {
            if let Some(hold) = hold {
                self.admission.cancel_async(&hold).await;
            }
            Err(RouterError::RateLimitExceeded(expected_model.into()))
        }
    }

    #[cfg(test)]
    pub(crate) fn select_deployment_lease_with_tokens(
        &self,
        model_name: &str,
        estimated_tokens: u64,
    ) -> Result<DeploymentLease, RouterError> {
        let snapshot = self.load_routing_snapshot();
        super::sync_compat::wait(self.select_deployment_matching(
            snapshot.as_ref(),
            model_name,
            |_| true,
            None,
            estimated_tokens,
        ))
        .map(DeploymentLease::synchronous_cleanup)
    }

    #[cfg(all(test, feature = "gateway", feature = "websockets"))]
    pub(crate) fn select_pinned_response_lease(
        &self,
        deployment: &Deployment,
        estimated_tokens: u64,
    ) -> Result<DeploymentLease, RouterError> {
        super::sync_compat::wait(
            self.select_pinned_response_lease_async(deployment, estimated_tokens),
        )
        .map(DeploymentLease::synchronous_cleanup)
    }

    #[cfg(all(feature = "gateway", feature = "websockets"))]
    pub(crate) async fn select_pinned_response_lease_async(
        &self,
        deployment: &Deployment,
        estimated_tokens: u64,
    ) -> Result<DeploymentLease, RouterError> {
        let snapshot = self.load_routing_snapshot();
        let current = snapshot
            .deployments
            .get(&deployment.id)
            .filter(|current| std::ptr::eq(current.as_ref(), deployment))
            .ok_or_else(|| RouterError::DeploymentNotFound(deployment.id.clone()))?;
        if !self.deployment_is_selectable_async(current).await {
            return Err(RouterError::NoAvailableDeployment(
                deployment.model_name.clone(),
            ));
        }
        let minute = current.state.minute_counters(current_timestamp());
        if current
            .config
            .rpm_limit
            .is_some_and(|limit| minute.rpm >= limit)
            || current
                .config
                .tpm_limit
                .is_some_and(|limit| current.state.admission_tpm(current_timestamp()) >= limit)
        {
            return Err(RouterError::RateLimitExceeded(
                deployment.model_name.clone(),
            ));
        }
        let hold = self
            .try_reserve_deployment(current, &deployment.model_name, estimated_tokens)
            .await?;
        self.provider_selected_count.fetch_add(1, Relaxed);
        self.strategy_used_count.fetch_add(1, Relaxed);
        Ok(DeploymentLease::new(
            current.clone(),
            self.admission.clone(),
            hold,
        ))
    }

    /// Release a deployment after request completion
    ///
    /// Decrements the active_requests counter for the deployment.
    #[deprecated(
        since = "0.5.0",
        note = "Use select_deployment_lease so release targets the selected snapshot deployment"
    )]
    pub fn release_deployment(&self, deployment_id: &str) {
        let snapshot = self.load_routing_snapshot();
        if let Some(deployment) = snapshot.deployments.get(deployment_id) {
            Self::release_selected_deployment(deployment);
        }
    }

    pub(crate) fn release_selected_deployment(deployment: &Deployment) {
        let result = deployment
            .state
            .active_requests
            .fetch_update(Relaxed, Relaxed, |v| Some(v.saturating_sub(1)));
        debug_assert!(result.is_ok());
    }
}

impl RuntimeHandle {
    /// Select and reserve a capable deployment from this handle's pinned generation.
    #[allow(dead_code)]
    pub(crate) fn select_deployment_lease_for_capability_typed(
        &self,
        model_name: &str,
        capability: &ProviderCapability,
    ) -> Result<DeploymentLease, crate::core::providers::ProviderError> {
        super::sync_compat::wait(
            self.binding
                .router
                .select_deployment_lease_for_capability_matching_in_snapshot(
                    self.snapshot.as_ref(),
                    model_name,
                    capability,
                    |_| true,
                ),
        )
        .map(DeploymentLease::synchronous_cleanup)
        .map_err(router_error_to_provider_error)
    }
}

impl RuntimeHandle {
    pub fn select_deployment_lease(
        &self,
        model_name: &str,
    ) -> Result<DeploymentLease, RouterError> {
        super::sync_compat::wait(self.select_deployment_lease_async(model_name))
            .map(DeploymentLease::synchronous_cleanup)
    }

    pub async fn select_deployment_lease_async(
        &self,
        model_name: &str,
    ) -> Result<DeploymentLease, RouterError> {
        self.binding
            .router
            .select_deployment_matching(self.snapshot.as_ref(), model_name, |_| true, None, 0)
            .await
    }
}