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
//! Async gateway bridge for the synchronous budget SDK.
//!
//! Admission owns one slot until the reservation is settled, cancelled, or its
//! detached cleanup finishes. Dropping an HTTP future never drops an in-flight
//! Redis result: the worker still owns it and cancels an undelivered reservation.

use std::sync::{Arc, OnceLock};
use std::time::Duration;

use tokio::sync::{OwnedSemaphorePermit, Semaphore, oneshot};

use super::{
    ModelBudgetManager, ProviderBudgetManager, ResponseBudgetLeases, UnifiedBudgetLimits,
    UnifiedBudgetReservation,
};
use crate::core::budget::{BudgetReservationError, BudgetStatus};
use crate::core::request_ledger::{current_facts, update_billing};

/// Bounds queued work, active reservations, and their terminal cleanup together.
/// A reservation can own at most two drop-cancel tasks (provider and model).
const MAX_ASYNC_BUDGET_RESERVATIONS: usize = 1024;
const ASYNC_BUDGET_REPLY_TIMEOUT: Duration = Duration::from_secs(30);

async fn receive_with_timeout<T>(
    reply: oneshot::Receiver<T>,
    operation: &'static str,
    timeout: Duration,
) -> Result<T, BudgetReservationError> {
    match tokio::time::timeout(timeout, reply).await {
        Ok(Ok(value)) => Ok(value),
        Ok(Err(_)) => Err(BudgetReservationError::BackendUnavailable),
        Err(_) => {
            // Closing this receiver does not cancel dispatched accounting.
            // The worker retains its original reservation and capacity slot.
            tracing::warn!(operation, "async budget reply deadline exceeded");
            Err(BudgetReservationError::BackendUnavailable)
        }
    }
}

async fn receive<T>(
    reply: oneshot::Receiver<T>,
    operation: &'static str,
) -> Result<T, BudgetReservationError> {
    receive_with_timeout(reply, operation, ASYNC_BUDGET_REPLY_TIMEOUT).await
}

pub(crate) struct AsyncBudgetPermit {
    _permit: OwnedSemaphorePermit,
}

pub(crate) fn try_budget_permit() -> Result<Arc<AsyncBudgetPermit>, BudgetReservationError> {
    static SLOTS: OnceLock<Arc<Semaphore>> = OnceLock::new();
    let slots = SLOTS.get_or_init(|| Arc::new(Semaphore::new(MAX_ASYNC_BUDGET_RESERVATIONS)));
    Arc::clone(slots)
        .try_acquire_owned()
        .map(|permit| Arc::new(AsyncBudgetPermit { _permit: permit }))
        .map_err(|_| {
            tracing::warn!(
                capacity = MAX_ASYNC_BUDGET_RESERVATIONS,
                "async budget capacity is full; rejecting new work without queueing"
            );
            BudgetReservationError::BackendUnavailable
        })
}

fn budget_worker_handle() -> tokio::runtime::Handle {
    static RUNTIME: OnceLock<tokio::runtime::Runtime> = OnceLock::new();
    RUNTIME
        .get_or_init(|| {
            tokio::runtime::Builder::new_multi_thread()
                .worker_threads(1)
                .max_blocking_threads(32)
                .enable_all()
                .thread_name("budget-sdk")
                .build()
                .expect("budget SDK worker runtime")
        })
        .handle()
        .clone()
}

fn dispatch<T: Send + 'static>(
    permit: Arc<AsyncBudgetPermit>,
    work: impl FnOnce() -> T + Send + 'static,
) -> oneshot::Receiver<T> {
    let (tx, rx) = oneshot::channel();
    // Separate from the Redis I/O runtime: a full SDK pool must not starve
    // connection setup (including Tokio's blocking DNS resolver) on that runtime.
    budget_worker_handle().spawn_blocking(move || {
        let _permit = permit;
        // Settlement/cancellation must finish even when the caller goes away.
        let result = work();
        let _ = tx.send(result);
    });
    rx
}

fn record_reservation(
    facts: Option<&crate::core::request_ledger::SharedRequestLedgerFacts>,
    reservation: &UnifiedBudgetReservation,
) {
    if let Some(facts) = facts {
        let mut facts = facts
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner());
        facts.provider = Some(reservation.provider().to_string());
        facts.model = Some(reservation.model().to_string());
        facts
            .deployment
            .get_or_insert_with(|| reservation.provider().to_string());
    }
    if !reservation.provider.tracked && !reservation.model.tracked {
        return;
    }
    update_billing(facts, |billing| {
        if reservation.provider.tracked {
            billing.provider_reserved_amount = Some(reservation.provider.reserved_amount());
            billing
                .provider_lease_id
                .clone_from(&reservation.provider.lease_id);
            billing.provider_settlement = Some("reserved".into());
        }
        if reservation.model.tracked {
            billing.model_reserved_amount = Some(reservation.model.reserved_amount());
            billing
                .model_lease_id
                .clone_from(&reservation.model.lease_id);
            billing.model_settlement = Some("reserved".into());
        }
        billing.reserved_at = Some(chrono::Utc::now());
        billing.unknown_reason = Some("awaiting_provider_usage".into());
        billing.awaiting_since.get_or_insert_with(chrono::Utc::now);
    });
}

fn record_settlement_result(
    facts: Option<&crate::core::request_ledger::SharedRequestLedgerFacts>,
    amount: f64,
    acknowledged: bool,
    provider: bool,
    model: bool,
) {
    if !provider && !model {
        return;
    }
    update_billing(facts, |billing| {
        let status = if acknowledged {
            "settled"
        } else {
            "settlement_unconfirmed"
        };
        if provider {
            billing.provider_settlement = Some(status.into());
            if acknowledged {
                billing.provider_charge_amount = Some(amount);
            }
        }
        if model {
            billing.model_settlement = Some(status.into());
            if acknowledged {
                billing.model_charge_amount = Some(amount);
            }
        }
        billing.settlement_updated_at = Some(chrono::Utc::now());
    });
}

fn record_cancellation(
    facts: Option<&crate::core::request_ledger::SharedRequestLedgerFacts>,
    distributed: bool,
    provider: bool,
    model: bool,
) {
    if !provider && !model {
        return;
    }
    update_billing(facts, |billing| {
        let status = if distributed {
            "cancellation_unconfirmed"
        } else {
            "released"
        };
        if provider {
            billing.provider_settlement = Some(status.into());
            if !distributed {
                billing.provider_charge_amount = Some(0.0);
            }
        }
        if model {
            billing.model_settlement = Some(status.into());
            if !distributed {
                billing.model_charge_amount = Some(0.0);
            }
        }
        billing.settlement_updated_at = Some(chrono::Utc::now());
    });
}

impl ProviderBudgetManager {
    /// Reset a distributed budget without blocking a gateway admin handler.
    pub async fn reset_provider_budget_async(
        &self,
        provider: &str,
    ) -> Result<bool, BudgetReservationError> {
        if !self.backend.is_distributed() {
            return Ok(self.reset_provider_budget(provider));
        }
        let permit = try_budget_permit()?;
        let manager = self.clone();
        let provider = provider.to_owned();
        receive(
            dispatch(permit, move || manager.reset_provider_budget(&provider)),
            "reset_provider",
        )
        .await
    }
}

impl ModelBudgetManager {
    /// Reset a distributed budget without blocking a gateway admin handler.
    pub async fn reset_model_budget_async(
        &self,
        model: &str,
    ) -> Result<bool, BudgetReservationError> {
        if !self.backend.is_distributed() {
            return Ok(self.reset_model_budget(model));
        }
        let permit = try_budget_permit()?;
        let manager = self.clone();
        let model = model.to_owned();
        receive(
            dispatch(permit, move || manager.reset_model_budget(&model)),
            "reset_model",
        )
        .await
    }
}

impl UnifiedBudgetLimits {
    /// Reserve provider and model budgets without blocking an async executor.
    ///
    /// Saturation fails before enqueueing. Once dispatched, cancellation of the
    /// caller transfers responsibility to the worker, which cancels a successful
    /// reservation if its receiver has disappeared. The bridge does not retry
    /// ambiguous Redis writes.
    pub async fn reserve_spend_async(
        &self,
        provider: &str,
        model: &str,
        max_amount: f64,
    ) -> Result<UnifiedBudgetReservation, BudgetReservationError> {
        let facts = current_facts();
        if !self.providers.backend.is_distributed() && !self.models.backend.is_distributed() {
            let result = self.reserve_spend(provider, model, max_amount);
            if let Ok(reservation) = &result {
                record_reservation(facts.as_ref(), reservation);
            }
            return result;
        }
        let permit = try_budget_permit()?;
        let limits = self.clone();
        let provider = provider.to_owned();
        let model = model.to_owned();
        let (tx, rx) = oneshot::channel();
        budget_worker_handle().spawn_blocking(move || {
            // A queued request which was already cancelled never reaches Redis.
            if tx.is_closed() {
                return;
            }
            let result = limits.reserve_spend_with_permit(
                &provider,
                &model,
                max_amount,
                Some(Arc::clone(&permit)),
            );
            if let Ok(reservation) = &result {
                record_reservation(facts.as_ref(), reservation);
            }
            if let Err(Ok(reservation)) = tx.send(result) {
                // Keep the permit while both terminal operations run. A failed
                // cancellation also passes that permit to its existing fallback.
                let provider = reservation.provider.tracked;
                let model = reservation.model.tracked;
                reservation.cancel();
                record_cancellation(facts.as_ref(), true, provider, model);
            }
        });
        receive(rx, "reserve").await?
    }

    pub(crate) async fn settle_response_leases_async(
        &self,
        provider: &str,
        model: &str,
        leases: &ResponseBudgetLeases,
        cost: f64,
    ) -> Result<(), BudgetReservationError> {
        let permit = try_budget_permit()?;
        let limits = self.clone();
        let provider = provider.to_owned();
        let model = model.to_owned();
        let leases = leases.clone();
        // SQL retains the receipt until acknowledgement. If the caller aborts
        // after dispatch, this worker finishes; the existing durable retry is
        // safe because settle_response is receipt-idempotent.
        receive(
            dispatch(permit, move || {
                limits.settle_response_leases(&provider, &model, &leases, cost)
            }),
            "settle_response",
        )
        .await?
    }
}

impl UnifiedBudgetReservation {
    fn has_distributed_lease(&self) -> bool {
        self.provider.lease_id.is_some() || self.model.lease_id.is_some()
    }

    fn async_permit(&mut self) -> Result<Arc<AsyncBudgetPermit>, BudgetReservationError> {
        let permit = self
            .provider
            .async_permit
            .as_ref()
            .or(self.model.async_permit.as_ref())
            .cloned()
            .map(Ok)
            .unwrap_or_else(try_budget_permit)?;
        // A synchronous SDK reservation entering an async terminal operation
        // must pass the newly acquired slot to any fallback Drop cleanup too.
        if self.provider.lease_id.is_some() {
            self.provider.async_permit = Some(Arc::clone(&permit));
        }
        if self.model.lease_id.is_some() {
            self.model.async_permit = Some(Arc::clone(&permit));
        }
        Ok(permit)
    }

    /// Settle on the budget worker, retaining ownership if the caller aborts.
    pub async fn settle_async(
        mut self,
        actual_amount: f64,
    ) -> Result<(Option<BudgetStatus>, Option<BudgetStatus>), BudgetReservationError> {
        let facts = current_facts();
        let provider = self.provider.tracked;
        let model = self.model.tracked;
        if provider || model {
            update_billing(facts.as_ref(), |billing| {
                if provider {
                    billing
                        .provider_reserved_amount
                        .get_or_insert(self.provider.reserved_amount());
                    billing.provider_settlement = Some("settlement_pending".into());
                }
                if model {
                    billing
                        .model_reserved_amount
                        .get_or_insert(self.model.reserved_amount());
                    billing.model_settlement = Some("settlement_pending".into());
                }
                billing.settlement_updated_at = Some(chrono::Utc::now());
            });
        }
        if !self.has_distributed_lease() {
            let result = self.settle(actual_amount);
            record_settlement_result(
                facts.as_ref(),
                actual_amount,
                result.is_ok(),
                provider,
                model,
            );
            return result;
        }
        let permit = match self.async_permit() {
            Ok(permit) => permit,
            Err(error) => {
                // Only a reservation created with the synchronous SDK can lack
                // its own slot. Do not refund potentially billable work when
                // that legacy caller attempts async settlement under overload.
                self.provider.settled = true;
                self.model.settled = true;
                record_settlement_result(facts.as_ref(), actual_amount, false, provider, model);
                return Err(error);
            }
        };
        receive(
            dispatch(permit, move || {
                let result = self.settle(actual_amount);
                record_settlement_result(
                    facts.as_ref(),
                    actual_amount,
                    result.is_ok(),
                    provider,
                    model,
                );
                result
            }),
            "settle",
        )
        .await?
    }

    /// Cancel without blocking the async executor, even if this future is dropped.
    pub async fn cancel_async(mut self) -> Result<(), BudgetReservationError> {
        let facts = current_facts();
        let distributed = self.has_distributed_lease();
        let provider = self.provider.tracked;
        let model = self.model.tracked;
        if provider || model {
            update_billing(facts.as_ref(), |billing| {
                if provider {
                    billing.provider_settlement = Some("cancellation_pending".into());
                }
                if model {
                    billing.model_settlement = Some("cancellation_pending".into());
                }
                billing.settlement_updated_at = Some(chrono::Utc::now());
            });
        }
        if !distributed {
            self.cancel();
            record_cancellation(facts.as_ref(), false, provider, model);
            return Ok(());
        }
        let permit = self.async_permit()?;
        receive(
            dispatch(permit, move || {
                self.cancel();
                // The synchronous cancel API logs backend errors but returns no ACK.
                record_cancellation(facts.as_ref(), true, provider, model);
            }),
            "cancel",
        )
        .await
    }

    /// Used by Realtime transport Drop: output may already have been consumed,
    /// so complete the conservative settlement rather than cancelling the hold.
    #[cfg(any(feature = "websockets", test))]
    pub(crate) fn settle_detached(
        mut self,
        actual_amount: f64,
        facts: Option<crate::core::request_ledger::SharedRequestLedgerFacts>,
    ) {
        let provider = self.provider.tracked;
        let model = self.model.tracked;
        if provider || model {
            update_billing(facts.as_ref(), |billing| {
                if provider {
                    billing.provider_settlement = Some("settlement_pending".into());
                }
                if model {
                    billing.model_settlement = Some("settlement_pending".into());
                }
                billing.settlement_updated_at = Some(chrono::Utc::now());
            });
        }
        if !self.has_distributed_lease() {
            let result = self.settle(actual_amount);
            record_settlement_result(
                facts.as_ref(),
                actual_amount,
                result.is_ok(),
                provider,
                model,
            );
            if let Err(error) = result {
                tracing::error!(?error, "detached budget settlement failed");
            }
            return;
        }
        let permit = match self.async_permit() {
            Ok(permit) => permit,
            Err(error) => {
                self.provider.settled = true;
                self.model.settled = true;
                record_settlement_result(facts.as_ref(), actual_amount, false, provider, model);
                tracing::error!(?error, "detached budget settlement could not be admitted");
                return;
            }
        };
        drop(dispatch(permit, move || {
            let result = self.settle(actual_amount);
            record_settlement_result(
                facts.as_ref(),
                actual_amount,
                result.is_ok(),
                provider,
                model,
            );
            if let Err(error) = result {
                tracing::error!(?error, "detached budget settlement failed");
            }
        }));
    }
}

#[cfg(test)]
mod deadline_tests {
    use super::*;
    use std::sync::atomic::{AtomicUsize, Ordering};

    #[tokio::test(flavor = "current_thread")]
    async fn terminal_reply_timeout_retains_worker_capacity_until_accounting_finishes() {
        let slots = Arc::new(Semaphore::new(1));
        let permit = Arc::new(AsyncBudgetPermit {
            _permit: Arc::clone(&slots).try_acquire_owned().unwrap(),
        });
        let completed = Arc::new(AtomicUsize::new(0));
        let worker_completed = Arc::clone(&completed);
        let (entered, started) = oneshot::channel();
        let (release, wait) = std::sync::mpsc::channel();
        let reply = dispatch(permit, move || {
            let _ = entered.send(());
            wait.recv_timeout(Duration::from_secs(5)).unwrap();
            worker_completed.fetch_add(1, Ordering::SeqCst);
        });
        started.await.unwrap();
        assert!(
            receive_with_timeout(reply, "settle", Duration::from_millis(25))
                .await
                .is_err()
        );
        assert_eq!(completed.load(Ordering::SeqCst), 0);
        assert_eq!(slots.available_permits(), 0);
        release.send(()).unwrap();
        tokio::time::timeout(Duration::from_secs(5), async {
            while slots.available_permits() != 1 {
                tokio::task::yield_now().await;
            }
        })
        .await
        .unwrap();
        assert_eq!(completed.load(Ordering::SeqCst), 1);
    }

    #[tokio::test(flavor = "current_thread")]
    async fn expired_admission_receiver_prevents_a_queued_reserve() {
        let (queued, reply) = oneshot::channel::<()>();
        assert!(
            receive_with_timeout(reply, "reserve", Duration::from_millis(25))
                .await
                .is_err()
        );
        assert!(
            queued.is_closed(),
            "queued admission must observe expiration"
        );
    }
    #[tokio::test]
    async fn untracked_reservation_does_not_fabricate_budget_holds() {
        let limits = UnifiedBudgetLimits::new();
        let facts = crate::core::request_ledger::SharedRequestLedgerFacts::new(
            std::sync::Mutex::new(Default::default()),
        );
        crate::core::request_ledger::scope_facts(facts.clone(), async {
            limits
                .reserve_spend_async("disabled", "disabled", 1.0)
                .await
                .unwrap()
                .settle_async(0.2)
                .await
                .unwrap();
        })
        .await;
        assert!(
            crate::core::request_ledger::snapshot_facts(&facts)
                .billing
                .is_none()
        );
    }

    #[tokio::test]
    async fn backend_failure_keeps_known_cost_and_unacknowledged_budget() {
        use crate::core::budget::{ModelLimitConfig, ProviderLimitConfig, ResetPeriod};
        let limits = UnifiedBudgetLimits::new();
        limits.providers.set_provider_limit(
            "provider",
            ProviderLimitConfig::new(10.0, ResetPeriod::Monthly),
        );
        limits
            .models
            .set_model_limit("model", ModelLimitConfig::new(10.0, ResetPeriod::Monthly));
        let facts = crate::core::request_ledger::SharedRequestLedgerFacts::new(
            std::sync::Mutex::new(Default::default()),
        );
        crate::core::request_ledger::scope_facts(facts.clone(), async {
            let mut reservation = limits
                .reserve_spend_async("provider", "model", 1.0)
                .await
                .unwrap();
            // Simulate loss of the distributed backend after obtaining its identity.
            reservation.provider.lease_id = Some("unavailable-provider-lease".into());
            reservation.model.lease_id = Some("unavailable-model-lease".into());
            reservation.provider.manager.backend =
                crate::core::budget::distributed::BudgetLeaseBackend::Unavailable;
            reservation.model.manager.backend =
                crate::core::budget::distributed::BudgetLeaseBackend::Unavailable;
            crate::core::request_ledger::record_current_settlement(
                "provider",
                "model",
                Some(1),
                Some(1),
                Some(2),
                Some(0.2),
            );
            assert!(reservation.settle_async(0.2).await.is_err());
        })
        .await;
        let recorded = crate::core::request_ledger::snapshot_facts(&facts);
        assert_eq!(recorded.cost, Some(0.2));
        let billing = recorded.billing.unwrap();
        assert_eq!(billing.unknown_reason, None);
        assert_eq!(billing.awaiting_since, None);
        assert_eq!(billing.provider_charge_amount, None);
        assert_eq!(
            billing.provider_settlement.as_deref(),
            Some("settlement_unconfirmed")
        );
        assert!(billing.requires_budget_review(0.2));
    }
}