litellm-rs 0.8.2

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
//! Single-key Lua deployment admission (cluster-safe: `KEYS[1]` only).

use super::pool::{RedisLiveConnection, RedisPool};
use crate::utils::error::gateway_error::{GatewayError, Result};
use std::collections::HashMap;
use std::sync::OnceLock;

const ADMISSION_SCRIPT: &str = r#"
local op = ARGV[1]
-- All replicas use the clock at this atomic Redis boundary.
local clock = redis.call('TIME')
local now = tonumber(clock[1]) * 1000 + math.floor(tonumber(clock[2]) / 1000)
local epoch = math.floor(tonumber(clock[1]) / 60)
local max_p = tonumber(ARGV[2]) or -1
local max_r = tonumber(ARGV[3]) or -1
local max_t = tonumber(ARGV[4]) or -1
local rpm_inc = tonumber(ARGV[5]) or 0
local tpm_inc = tonumber(ARGV[6]) or 0
local lease_id = ARGV[7]
local ttl = tonumber(ARGV[8]) or 1
local actual_tpm = tonumber(ARGV[9]) or 0

local function nums()
  local p = tonumber(redis.call('HGET', KEYS[1], 'p') or '0') or 0
  local r = tonumber(redis.call('HGET', KEYS[1], 'r') or '0') or 0
  local t = tonumber(redis.call('HGET', KEYS[1], 't') or '0') or 0
  local e = tonumber(redis.call('HGET', KEYS[1], 'e') or '0') or 0
  return p, r, t, e
end

local function save(p, r, t, e)
  if p < 0 then p = 0 end
  if r < 0 then r = 0 end
  if t < 0 then t = 0 end
  redis.call('HSET', KEYS[1], 'p', p, 'r', r, 't', t, 'e', e)
end

local function reclaim()
  local p, r, t, e = nums()
  local window_changed = epoch > e
  if window_changed then
    r = 0
    t = 0
    e = epoch
  end
  local fields = redis.call('HGETALL', KEYS[1])
  for i = 1, #fields, 2 do
    if string.sub(fields[i], 1, 2) == 'l:' then
      local pInc, rpmInc, tpmInc, lease_epoch, expiry = string.match(
        fields[i + 1], '^(%d+):(%d+):(%d+):(%-?%d+):(%d+)$'
      )
      pInc = tonumber(pInc)
      rpmInc = tonumber(rpmInc)
      tpmInc = tonumber(tpmInc)
      lease_epoch = tonumber(lease_epoch)
      expiry = tonumber(expiry)
      if pInc == nil or expiry == nil or expiry <= now then
        if pInc ~= nil and expiry ~= nil and expiry <= now then
          p = p - pInc
          if lease_epoch == e then
            r = r - rpmInc
            t = t - tpmInc
          end
        end
        redis.call('HDEL', KEYS[1], fields[i])
      elseif window_changed then
        -- Carry outstanding TPM, but charge RPM only in the arrival window.
        rpmInc = 0
        t = t + tpmInc
        redis.call(
          'HSET', KEYS[1], fields[i],
          tostring(pInc) .. ':' .. tostring(rpmInc) .. ':' .. tostring(tpmInc) .. ':' .. tostring(e) .. ':' .. tostring(expiry)
        )
      end
    end
  end
  save(p, r, t, e)
  return p, r, t, e
end

local p, r, t, e = reclaim()

if op == 'reserve' then
  if (max_p >= 0 and p + 1 > max_p)
      or (max_r >= 0 and r + rpm_inc > max_r)
      or (max_t >= 0 and t + tpm_inc > max_t) then
    return {0, p, r, t}
  end
  p = p + 1
  r = r + rpm_inc
  t = t + tpm_inc
  if ttl < 1 then ttl = 1 end
  save(p, r, t, e)
  redis.call(
    'HSET',
    KEYS[1],
    'l:' .. lease_id,
    '1:' .. tostring(rpm_inc) .. ':' .. tostring(tpm_inc) .. ':' .. tostring(e) .. ':' .. tostring(now + ttl)
  )
  return {1, p, r, t}
end

if op == 'settle' or op == 'cancel' then
  local field = 'l:' .. lease_id
  local lease = redis.call('HGET', KEYS[1], field)
  if lease then
    local pInc, rpmInc, tpmInc, lease_epoch = string.match(lease, '^(%d+):(%d+):(%d+):(%-?%d+):')
    pInc = tonumber(pInc) or 1
    rpmInc = tonumber(rpmInc) or 0
    tpmInc = tonumber(tpmInc) or 0
    lease_epoch = tonumber(lease_epoch)
    p = p - pInc
    if lease_epoch == e then
      if op == 'cancel' then
        r = r - rpmInc
        t = t - tpmInc
      else
        t = t - tpmInc + actual_tpm
      end
    end
    redis.call('HDEL', KEYS[1], field)
    save(p, r, t, e)
  end
  return {1, p, r, t}
end

return {-1, p, r, t}
"#;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct AdmissionState {
    pub allowed: bool,
    pub parallel: i64,
    pub rpm: i64,
    pub tpm: i64,
}

pub(crate) struct AdmissionReserveArgs<'a> {
    pub key: &'a str,
    pub max_parallel: i64,
    pub max_rpm: i64,
    pub max_tpm: i64,
    pub rpm_inc: i64,
    pub tpm_inc: i64,
    pub lease_id: &'a str,
    pub ttl_ms: i64,
}

fn admission_script() -> &'static redis::Script {
    static SCRIPT: OnceLock<redis::Script> = OnceLock::new();
    SCRIPT.get_or_init(|| redis::Script::new(ADMISSION_SCRIPT))
}

fn admission_runtime_connections()
-> &'static tokio::sync::Mutex<HashMap<String, RedisLiveConnection>> {
    static CACHE: OnceLock<tokio::sync::Mutex<HashMap<String, RedisLiveConnection>>> =
        OnceLock::new();
    CACHE.get_or_init(|| tokio::sync::Mutex::new(HashMap::new()))
}

async fn connection_on_current_runtime(pool: &RedisPool) -> Result<RedisLiveConnection> {
    let cache_key = format!("{}|{}", pool.config.url, pool.config.cluster);
    {
        let cache = admission_runtime_connections().lock().await;
        if let Some(conn) = cache.get(&cache_key) {
            return Ok(conn.clone());
        }
    }
    let conn = pool.open_live_connection().await?;
    let mut cache = admission_runtime_connections().lock().await;
    Ok(cache.entry(cache_key).or_insert(conn).clone())
}

fn parse_admission_state(values: Vec<i64>) -> Result<AdmissionState> {
    if values.len() != 4 {
        return Err(GatewayError::Storage(format!(
            "Unexpected Redis admission result length: {}",
            values.len()
        )));
    }
    if values[0] < 0 {
        return Err(GatewayError::Storage(
            "Redis admission script returned an error status".to_string(),
        ));
    }
    Ok(AdmissionState {
        allowed: values[0] == 1,
        parallel: values[1].max(0),
        rpm: values[2].max(0),
        tpm: values[3].max(0),
    })
}

struct AdmissionArgs<'a> {
    op: &'a str,
    max_parallel: i64,
    max_rpm: i64,
    max_tpm: i64,
    rpm_inc: i64,
    tpm_inc: i64,
    lease_id: &'a str,
    ttl_ms: i64,
    actual_tpm: i64,
}

impl RedisPool {
    pub(crate) fn admission_key(deployment_id: &str) -> String {
        format!("litellm-rs:admission:v1:{deployment_id}")
    }

    async fn invoke_admission(&self, key: &str, args: AdmissionArgs<'_>) -> Result<AdmissionState> {
        if self.noop_mode {
            return Err(GatewayError::Storage(
                "admission redis backend is unavailable".to_string(),
            ));
        }

        let _permit = self
            .semaphore
            .clone()
            .acquire_owned()
            .await
            .map_err(|_| GatewayError::Internal("Redis semaphore closed".to_string()))?;
        let mut conn = connection_on_current_runtime(self).await?;

        let values: Vec<i64> = admission_script()
            .key(key)
            .arg(args.op)
            .arg(args.max_parallel)
            .arg(args.max_rpm)
            .arg(args.max_tpm)
            .arg(args.rpm_inc)
            .arg(args.tpm_inc)
            .arg(args.lease_id)
            .arg(args.ttl_ms)
            .arg(args.actual_tpm)
            .invoke_async(&mut conn)
            .await
            .map_err(GatewayError::from)?;
        parse_admission_state(values)
    }

    pub(crate) async fn admission_reserve(
        &self,
        args: AdmissionReserveArgs<'_>,
    ) -> Result<AdmissionState> {
        self.invoke_admission(
            args.key,
            AdmissionArgs {
                op: "reserve",
                max_parallel: args.max_parallel,
                max_rpm: args.max_rpm,
                max_tpm: args.max_tpm,
                rpm_inc: args.rpm_inc,
                tpm_inc: args.tpm_inc,
                lease_id: args.lease_id,
                ttl_ms: args.ttl_ms,
                actual_tpm: 0,
            },
        )
        .await
    }

    pub(crate) async fn admission_settle(
        &self,
        key: &str,
        lease_id: &str,
        actual_tpm: i64,
    ) -> Result<AdmissionState> {
        self.admission_finish("settle", key, lease_id, actual_tpm)
            .await
    }

    pub(crate) async fn admission_cancel(
        &self,
        key: &str,
        lease_id: &str,
    ) -> Result<AdmissionState> {
        self.admission_finish("cancel", key, lease_id, 0).await
    }

    async fn admission_finish(
        &self,
        op: &str,
        key: &str,
        lease_id: &str,
        actual_tpm: i64,
    ) -> Result<AdmissionState> {
        self.invoke_admission(
            key,
            AdmissionArgs {
                op,
                max_parallel: -1,
                max_rpm: -1,
                max_tpm: -1,
                rpm_inc: 0,
                tpm_inc: 0,
                lease_id,
                ttl_ms: 0,
                actual_tpm,
            },
        )
        .await
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::models::storage::RedisConfig;

    async fn live_connection() -> Option<RedisLiveConnection> {
        let Ok(url) = std::env::var("REDIS_URL") else {
            assert!(std::env::var("CI").is_err(), "REDIS_URL is required in CI");
            return None;
        };
        let pool = RedisPool::new(&RedisConfig {
            url,
            enabled: true,
            allow_degraded: false,
            ..RedisConfig::default()
        })
        .await
        .unwrap();
        // Keep these short-lived test runtimes out of the bridge's connection cache.
        Some(pool.open_live_connection().await.unwrap())
    }

    async fn invoke_script(
        conn: &mut RedisLiveConnection,
        key: &str,
        op: &str,
        lease_id: &str,
        tokens: i64,
        max_rpm: i64,
    ) -> AdmissionState {
        let values = admission_script()
            .key(key)
            .arg(op)
            .arg(-1)
            .arg(max_rpm)
            .arg(10)
            .arg(1)
            .arg(tokens)
            .arg(lease_id)
            .arg(600_000)
            .arg(tokens)
            .invoke_async(conn)
            .await
            .unwrap();
        parse_admission_state(values).unwrap()
    }

    async fn redis_time(conn: &mut RedisLiveConnection) -> (i64, i64) {
        let (seconds, micros): (i64, i64) = redis::cmd("TIME").query_async(conn).await.unwrap();
        (seconds * 1_000 + micros / 1_000, seconds / 60)
    }

    // Age stored accounting instead of waiting for a real minute boundary.
    async fn age_window(conn: &mut RedisLiveConnection, key: &str, lease_id: &str) {
        let (_, epoch) = redis_time(conn).await;
        let field = format!("l:{lease_id}");
        let lease: String = redis::cmd("HGET")
            .arg(key)
            .arg(&field)
            .query_async(conn)
            .await
            .unwrap();
        let mut parts: Vec<String> = lease.split(':').map(str::to_owned).collect();
        parts[3] = (epoch - 1).to_string();
        redis::cmd("HSET")
            .arg(key)
            .arg("e")
            .arg(epoch - 1)
            .arg(field)
            .arg(parts.join(":"))
            .query_async::<i64>(conn)
            .await
            .unwrap();
    }

    #[tokio::test]
    async fn window_rollover_carries_live_reservations_until_finish() {
        let Some(mut conn) = live_connection().await else {
            return;
        };
        for op in ["settle", "cancel", "expire"] {
            let key = RedisPool::admission_key(&uuid::Uuid::new_v4().to_string());
            assert!(
                invoke_script(&mut conn, &key, "reserve", "old", 10, 2)
                    .await
                    .allowed
            );
            for _ in 0..2 {
                age_window(&mut conn, &key, "old").await;
                for _ in 0..2 {
                    let denied = invoke_script(&mut conn, &key, "reserve", "new", 1, 2).await;
                    assert!(!denied.allowed, "live TPM must survive rollover ({op})");
                    assert_eq!((denied.parallel, denied.rpm, denied.tpm), (1, 0, 10));
                }
            }
            if op == "expire" {
                let (_, epoch) = redis_time(&mut conn).await;
                redis::cmd("HSET")
                    .arg(&key)
                    .arg("l:old")
                    .arg(format!("1:0:10:{epoch}:0"))
                    .query_async::<i64>(&mut conn)
                    .await
                    .unwrap();
            }
            let finish_op = if op == "expire" { "cancel" } else { op };
            let finished = invoke_script(&mut conn, &key, finish_op, "old", 4, 2).await;
            let tpm = if op == "settle" { 4 } else { 0 };
            assert_eq!((finished.parallel, finished.rpm, finished.tpm), (0, 0, tpm));
            let admitted = invoke_script(&mut conn, &key, "reserve", "new", 10 - tpm, 2).await;
            assert!(admitted.allowed, "finish must release the carried estimate");
            assert_eq!((admitted.parallel, admitted.rpm, admitted.tpm), (1, 1, 10));
            let repeated = invoke_script(&mut conn, &key, finish_op, "old", 4, 2).await;
            assert_eq!(
                repeated, admitted,
                "duplicate finish must leave other leases intact"
            );
            age_window(&mut conn, &key, "new").await;
            let carried = invoke_script(&mut conn, &key, "reserve", "third", 5, 2).await;
            assert!(!carried.allowed);
            assert_eq!(
                (carried.parallel, carried.rpm, carried.tpm),
                (1, 0, 10 - tpm),
                "only outstanding TPM should carry into the next window"
            );
            redis::cmd("DEL")
                .arg(&key)
                .query_async::<i64>(&mut conn)
                .await
                .unwrap();
        }
    }

    #[tokio::test]
    async fn same_window_commands_preserve_settled_usage() {
        let Some(mut conn) = live_connection().await else {
            return;
        };
        let key = RedisPool::admission_key(&uuid::Uuid::new_v4().to_string());
        assert!(
            invoke_script(&mut conn, &key, "reserve", "old", 10, 2)
                .await
                .allowed
        );
        age_window(&mut conn, &key, "old").await;
        assert!(
            !invoke_script(&mut conn, &key, "reserve", "new", 1, 2)
                .await
                .allowed
        );
        let settled = invoke_script(&mut conn, &key, "settle", "old", 4, 2).await;
        assert_eq!((settled.parallel, settled.rpm, settled.tpm), (0, 0, 4));
        let admitted = invoke_script(&mut conn, &key, "reserve", "new", 6, 2).await;
        assert!(admitted.allowed);
        assert_eq!((admitted.parallel, admitted.rpm, admitted.tpm), (1, 1, 10));
        let denied = invoke_script(&mut conn, &key, "reserve", "third", 1, 2).await;
        assert!(
            !denied.allowed,
            "settled usage must survive another command"
        );
        assert_eq!((denied.parallel, denied.rpm, denied.tpm), (1, 1, 10));
        let cancelled = invoke_script(&mut conn, &key, "cancel", "new", 0, 2).await;
        assert_eq!(
            (cancelled.parallel, cancelled.rpm, cancelled.tpm),
            (0, 0, 4)
        );
        redis::cmd("DEL")
            .arg(&key)
            .query_async::<i64>(&mut conn)
            .await
            .unwrap();
    }

    #[tokio::test]
    async fn admission_uses_redis_time_for_windows_and_lease_expiry() {
        let Some(mut conn) = live_connection().await else {
            return;
        };
        let key = RedisPool::admission_key(&uuid::Uuid::new_v4().to_string());
        let (before, first_epoch) = redis_time(&mut conn).await;
        assert!(
            invoke_script(&mut conn, &key, "reserve", "lease", 0, 1)
                .await
                .allowed
        );
        let (epoch, lease): (i64, String) = redis::cmd("HMGET")
            .arg(&key)
            .arg(&["e", "l:lease"])
            .query_async(&mut conn)
            .await
            .unwrap();
        let (after, last_epoch) = redis_time(&mut conn).await;
        assert!(
            (first_epoch..=last_epoch).contains(&epoch),
            "admission must use the Redis window: {epoch}"
        );
        let expiry: i64 = lease.rsplit(':').next().unwrap().parse().unwrap();
        assert!((before + 600_000..=after + 600_000).contains(&expiry));
        redis::cmd("DEL")
            .arg(&key)
            .query_async::<i64>(&mut conn)
            .await
            .unwrap();
    }

    #[tokio::test]
    async fn window_rollover_does_not_charge_old_arrivals_again() {
        let Some(mut conn) = live_connection().await else {
            return;
        };
        for op in ["settle", "cancel", "expire"] {
            let key = RedisPool::admission_key(&uuid::Uuid::new_v4().to_string());
            assert!(
                invoke_script(&mut conn, &key, "reserve", "old", 0, 1)
                    .await
                    .allowed
            );
            age_window(&mut conn, &key, "old").await;
            let admitted = invoke_script(&mut conn, &key, "reserve", "new", 0, 1).await;
            assert!(
                admitted.allowed,
                "old arrivals must not consume new RPM ({op})"
            );
            assert_eq!((admitted.parallel, admitted.rpm, admitted.tpm), (2, 1, 0));
            if op == "expire" {
                let (_, epoch) = redis_time(&mut conn).await;
                redis::cmd("HSET")
                    .arg(&key)
                    .arg("l:old")
                    .arg(format!("1:0:0:{epoch}:0"))
                    .query_async::<i64>(&mut conn)
                    .await
                    .unwrap();
            }
            let finish_op = if op == "expire" { "cancel" } else { op };
            let finished = invoke_script(&mut conn, &key, finish_op, "old", 0, 1).await;
            assert_eq!((finished.parallel, finished.rpm, finished.tpm), (1, 1, 0));
            assert!(
                !invoke_script(&mut conn, &key, "reserve", "third", 0, 1)
                    .await
                    .allowed
            );
            redis::cmd("DEL")
                .arg(&key)
                .query_async::<i64>(&mut conn)
                .await
                .unwrap();
        }
    }

    #[test]
    fn parses_admission_state() {
        let allowed = parse_admission_state(vec![1, 2, 3, 4]).unwrap();
        assert!(allowed.allowed);
        assert_eq!(allowed.parallel, 2);
        assert_eq!(allowed.rpm, 3);
        assert_eq!(allowed.tpm, 4);

        let denied = parse_admission_state(vec![0, 1, 0, 0]).unwrap();
        assert!(!denied.allowed);
        assert!(parse_admission_state(vec![-1, 0, 0, 0]).is_err());
        assert!(parse_admission_state(vec![1, 0]).is_err());
    }

    #[tokio::test]
    async fn noop_redis_pool_fails_closed_on_admission_reserve() {
        let pool = RedisPool::new(&RedisConfig {
            enabled: false,
            ..RedisConfig::default()
        })
        .await
        .expect("disabled Redis should create a no-op pool");

        let err = pool
            .admission_reserve(AdmissionReserveArgs {
                key: "litellm-rs:admission:v1:noop-test",
                max_parallel: 1,
                max_rpm: -1,
                max_tpm: -1,
                rpm_inc: 0,
                tpm_inc: 0,
                lease_id: "lease",
                ttl_ms: 1_000,
            })
            .await
            .expect_err("no-op Redis must not fail-open admission");
        assert!(err.to_string().contains("unavailable"));
    }
}