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
use super::*;
use crate::core::router::deployment::publish_probe_group;
use futures::stream::{FuturesUnordered, StreamExt};
use std::collections::HashMap;
use std::collections::hash_map::Entry;
use std::future::Future;
use std::pin::Pin;
use tokio::time::Instant;

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
enum ProbeScope {
    CustomEndpoint,
    NativeDeployment {
        deployment_id: String,
        deployment_instance: usize,
    },
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct DynamicProbeKey {
    scope: ProbeScope,
    provider_name: String,
    provider_kind: String,
    policy: HealthCheckPolicy,
    timeout_secs: u64,
}

#[derive(Debug, Clone, PartialEq, Eq)]
struct ProbeMember {
    deployment_id: String,
    deployment_instance: usize,
    probe_generation: u64,
}

#[derive(Clone)]
struct ProbeTarget {
    key: DynamicProbeKey,
    provider: Provider,
    deployments: Vec<Arc<Deployment>>,
}

impl ProbeTarget {
    fn members(&self) -> Vec<ProbeMember> {
        let mut members = self
            .deployments
            .iter()
            .map(|deployment| ProbeMember {
                deployment_id: deployment.id.clone(),
                deployment_instance: Arc::as_ptr(deployment) as usize,
                probe_generation: deployment.probe_generation(),
            })
            .collect::<Vec<_>>();
        members.sort_unstable_by(|left, right| left.deployment_id.cmp(&right.deployment_id));
        members
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct ProbeRun {
    incarnation: u64,
    epoch: u64,
}

#[derive(Debug)]
struct ProbeSchedule {
    consecutive_failures: u32,
    next_run: Instant,
    running: Option<ProbeRun>,
    incarnation: u64,
    epoch: u64,
    members: Vec<ProbeMember>,
}

struct ProbeOutcome {
    key: DynamicProbeKey,
    run: ProbeRun,
    target: ProbeTarget,
    result: Result<(), ProbeFailure>,
}

type PendingProbe = Pin<Box<dyn Future<Output = ProbeOutcome> + Send>>;

pub(super) fn validate_probe_snapshot(snapshot: &RoutingSnapshot) -> Result<usize, RouterError> {
    let mut provider_policies = HashMap::new();
    let targets = current_probe_groups(snapshot);
    for target in &targets {
        if target.key.policy.endpoint.is_none() {
            let deployment = target.deployments.first().ok_or_else(|| {
                RouterError::InvalidConfiguration(format!(
                    "provider '{}' produced an empty health probe target",
                    target.key.provider_name
                ))
            })?;
            if !target
                .provider
                .has_safe_native_health_probe(&deployment.model)
            {
                return Err(RouterError::InvalidConfiguration(format!(
                    "provider '{}' deployment '{}' requires a custom health_check.endpoint; \
                     its configured model capability cannot be inferred safely for a native probe",
                    target.key.provider_name, deployment.id
                )));
            }
        }
        match provider_policies.entry(target.key.provider_name.clone()) {
            Entry::Vacant(entry) => {
                entry.insert((target.key.policy.clone(), target.key.timeout_secs));
            }
            Entry::Occupied(entry)
                if entry.get() != &(target.key.policy.clone(), target.key.timeout_secs) =>
            {
                return Err(RouterError::InvalidConfiguration(format!(
                    "provider '{}' has conflicting health probe policies",
                    target.key.provider_name
                )));
            }
            Entry::Occupied(_) => {}
        }
        build_custom_client(&target.key.policy, target.key.timeout_secs).map_err(|failure| {
            RouterError::InvalidConfiguration(format!(
                "provider '{}' health probe client failed to initialize: {failure:?}",
                target.key.provider_name
            ))
        })?;
    }
    Ok(targets.len())
}

pub(super) async fn run_probe_loop(supervisor: ProbeSupervisor) {
    let mut schedules = HashMap::new();
    let mut pending: FuturesUnordered<PendingProbe> = FuturesUnordered::new();
    let mut next_incarnation = 0;

    loop {
        let snapshot = supervisor.routing_snapshot.load_full();
        let targets = current_probe_groups(snapshot.as_ref())
            .into_iter()
            .map(|target| (target.key.clone(), target))
            .collect::<HashMap<_, _>>();
        reconcile_schedules(&targets, &mut schedules, &mut next_incarnation);
        launch_due_probes(&targets, &mut schedules, &mut pending);

        let next_run = schedules
            .values()
            .filter(|schedule| schedule.running.is_none())
            .map(|schedule| schedule.next_run)
            .min();
        let outcome = match (pending.is_empty(), next_run) {
            (false, Some(deadline)) => tokio::select! {
                outcome = pending.next() => outcome,
                () = tokio::time::sleep_until(deadline) => None,
                () = supervisor.wakeup.notified() => None,
            },
            (false, None) => tokio::select! {
                outcome = pending.next() => outcome,
                () = supervisor.wakeup.notified() => None,
            },
            (true, Some(deadline)) => {
                tokio::select! {
                    () = tokio::time::sleep_until(deadline) => {},
                    () = supervisor.wakeup.notified() => {},
                }
                None
            }
            (true, None) => {
                supervisor.wakeup.notified().await;
                None
            }
        };
        if let Some(outcome) = outcome {
            apply_probe_outcome(&supervisor, &mut schedules, outcome);
        }
    }
}

fn current_probe_groups(snapshot: &RoutingSnapshot) -> Vec<ProbeTarget> {
    let mut groups = HashMap::<DynamicProbeKey, ProbeTarget>::new();
    for deployment in snapshot.deployments.values() {
        let Some(policy) = deployment.config.health_check_policy.clone() else {
            continue;
        };
        let scope = if policy.endpoint.is_some() {
            ProbeScope::CustomEndpoint
        } else {
            // Native probes use deployment credentials and model configuration. Binding the
            // schedule to the current deployment instance prevents cloned identities or a
            // public provider-field replacement from receiving another deployment's result.
            ProbeScope::NativeDeployment {
                deployment_id: deployment.id.clone(),
                deployment_instance: Arc::as_ptr(deployment) as usize,
            }
        };
        let key = DynamicProbeKey {
            scope,
            provider_name: policy.provider_name.clone(),
            provider_kind: deployment.provider.name().to_string(),
            policy,
            timeout_secs: deployment.config.timeout_secs,
        };
        groups
            .entry(key.clone())
            .and_modify(|target| target.deployments.push(Arc::clone(deployment)))
            .or_insert_with(|| ProbeTarget {
                key,
                provider: deployment.provider.clone(),
                deployments: vec![Arc::clone(deployment)],
            });
    }
    let mut groups = groups.into_values().collect::<Vec<_>>();
    for target in &mut groups {
        target
            .deployments
            .sort_unstable_by(|left, right| left.id.cmp(&right.id));
    }
    groups
}

fn reconcile_schedules(
    targets: &HashMap<DynamicProbeKey, ProbeTarget>,
    schedules: &mut HashMap<DynamicProbeKey, ProbeSchedule>,
    next_incarnation: &mut u64,
) {
    schedules.retain(|key, _| targets.contains_key(key));
    let now = Instant::now();
    for (key, target) in targets {
        let members = target.members();
        match schedules.entry(key.clone()) {
            Entry::Vacant(entry) => {
                let incarnation = *next_incarnation;
                *next_incarnation = next_incarnation.wrapping_add(1);
                entry.insert(ProbeSchedule {
                    consecutive_failures: 0,
                    next_run: now,
                    running: None,
                    incarnation,
                    epoch: 0,
                    members,
                });
            }
            Entry::Occupied(mut entry) if entry.get().members != members => {
                let schedule = entry.get_mut();
                schedule.members = members;
                schedule.epoch = schedule.epoch.wrapping_add(1);
                schedule.consecutive_failures = 0;
                schedule.running = None;
                schedule.next_run = now;
            }
            Entry::Occupied(_) => {}
        }
    }
}

fn launch_due_probes(
    targets: &HashMap<DynamicProbeKey, ProbeTarget>,
    schedules: &mut HashMap<DynamicProbeKey, ProbeSchedule>,
    pending: &mut FuturesUnordered<PendingProbe>,
) {
    let now = Instant::now();
    for (key, schedule) in schedules {
        if schedule.running.is_some() || schedule.next_run > now {
            continue;
        }
        let Some(target) = targets.get(key).cloned() else {
            continue;
        };
        let run = ProbeRun {
            incarnation: schedule.incarnation,
            epoch: schedule.epoch,
        };
        schedule.running = Some(run);
        pending.push(Box::pin(async move {
            let result = execute_target_probe(&target).await;
            ProbeOutcome {
                key: target.key.clone(),
                run,
                target,
                result,
            }
        }));
    }
}

fn apply_probe_outcome(
    supervisor: &ProbeSupervisor,
    schedules: &mut HashMap<DynamicProbeKey, ProbeSchedule>,
    outcome: ProbeOutcome,
) {
    let Some(schedule) = schedules.get_mut(&outcome.key) else {
        return;
    };
    if schedule.running != Some(outcome.run)
        || schedule.incarnation != outcome.run.incarnation
        || schedule.epoch != outcome.run.epoch
        || schedule.members != outcome.target.members()
    {
        return;
    }
    schedule.running = None;

    let snapshot = supervisor.routing_snapshot.load();
    let target_is_current = outcome.target.deployments.iter().all(|observed| {
        snapshot
            .deployments
            .get(&observed.id)
            .is_some_and(|current| Arc::ptr_eq(current, observed))
    });
    if !target_is_current {
        schedule.next_run = Instant::now();
        return;
    }
    if matches!(
        &outcome.result,
        Err(ProbeFailure::ProviderStatus(ProviderHealthStatus::Unknown))
    ) {
        schedule.next_run =
            Instant::now() + Duration::from_secs(outcome.target.key.policy.interval_secs);
        return;
    }

    let had_failures = schedule.consecutive_failures > 0;
    let delay = match outcome.result {
        Ok(()) => apply_probe_result_to(
            &outcome.target.key.policy,
            true,
            &mut schedule.consecutive_failures,
            &outcome.target.deployments,
        ),
        Err(failure) => {
            let delay = apply_probe_result_to(
                &outcome.target.key.policy,
                false,
                &mut schedule.consecutive_failures,
                &outcome.target.deployments,
            );
            if delay.is_some() {
                log_probe_failure(
                    &outcome.target.key.policy,
                    schedule.consecutive_failures,
                    &failure,
                );
            }
            delay
        }
    };
    let Some(delay) = delay else {
        schedule.next_run = Instant::now();
        return;
    };
    if had_failures && schedule.consecutive_failures == 0 {
        tracing::info!(
            provider = %outcome.target.key.provider_name,
            "provider health probe recovered"
        );
    }
    schedule.next_run = Instant::now() + delay;
}

async fn execute_target_probe(target: &ProbeTarget) -> Result<(), ProbeFailure> {
    let custom_client = build_custom_client(&target.key.policy, target.key.timeout_secs)?;
    let model = target
        .deployments
        .first()
        .map(|deployment| deployment.model.as_str())
        .ok_or(ProbeFailure::ClientUnavailable)?;
    match tokio::time::timeout(
        Duration::from_secs(target.key.timeout_secs),
        execute_probe(
            &target.provider,
            model,
            &target.key.policy,
            custom_client.as_deref(),
        ),
    )
    .await
    {
        Ok(result) => result,
        Err(_) => Err(ProbeFailure::Timeout),
    }
}

fn build_custom_client(
    policy: &HealthCheckPolicy,
    timeout_secs: u64,
) -> Result<Option<Arc<BaseHttpClient>>, ProbeFailure> {
    policy
        .endpoint
        .as_ref()
        .map(|endpoint| {
            BaseHttpClient::new_for_provider_no_redirect(
                "health_probe",
                BaseConfig {
                    api_base: Some(endpoint.to_string()),
                    endpoint_access: policy.endpoint_access,
                    timeout: timeout_secs,
                    ..Default::default()
                },
            )
            .map(Arc::new)
            .map_err(|error| ProbeFailure::Request(error.to_string()))
        })
        .transpose()
}

pub(super) async fn execute_probe(
    provider: &Provider,
    model: &str,
    policy: &HealthCheckPolicy,
    custom_client: Option<&BaseHttpClient>,
) -> Result<(), ProbeFailure> {
    if let Some(endpoint) = &policy.endpoint {
        let client = custom_client.ok_or(ProbeFailure::ClientUnavailable)?;
        let response = client
            .get(endpoint.clone())
            .map_err(|error| ProbeFailure::Request(error.to_string()))?
            .send()
            .await
            .map_err(|error| ProbeFailure::Request(error.to_string()))?;
        let status = response.status().as_u16();
        if policy.expected_codes.contains(&status) {
            Ok(())
        } else {
            Err(ProbeFailure::UnexpectedStatus(status))
        }
    } else {
        match provider.health_check_for_model(model).await {
            ProviderHealthStatus::Healthy => Ok(()),
            status => Err(ProbeFailure::ProviderStatus(status)),
        }
    }
}

#[cfg(test)]
pub(super) fn apply_probe_result(
    policy: &HealthCheckPolicy,
    deployments: &[Arc<Deployment>],
    succeeded: bool,
    consecutive_failures: &mut u32,
) -> Duration {
    apply_probe_result_to(policy, succeeded, consecutive_failures, deployments)
        .expect("test deployments should own their probe generations")
}

fn apply_probe_result_to(
    policy: &HealthCheckPolicy,
    succeeded: bool,
    consecutive_failures: &mut u32,
    deployments: &[Arc<Deployment>],
) -> Option<Duration> {
    let previous_failures = *consecutive_failures;
    let next_failures = if succeeded {
        0
    } else {
        previous_failures.saturating_add(1)
    };
    let threshold_reached = next_failures >= policy.failure_threshold;
    let target = if succeeded {
        HealthStatus::Healthy
    } else if threshold_reached {
        HealthStatus::Unhealthy
    } else {
        HealthStatus::Degraded
    };
    if !publish_probe_group(deployments, target) {
        return None;
    }
    *consecutive_failures = next_failures;
    let delay_secs = if !succeeded && threshold_reached {
        policy.recovery_timeout_secs
    } else {
        policy.interval_secs
    };
    Some(Duration::from_secs(delay_secs))
}

#[cfg(test)]
pub(super) fn update_probe_health(deployment: &Deployment, target: HealthStatus) -> bool {
    deployment.publish_probe_health(target)
}

fn log_probe_failure(
    policy: &HealthCheckPolicy,
    consecutive_failures: u32,
    failure: &ProbeFailure,
) {
    if consecutive_failures >= policy.failure_threshold {
        tracing::error!(
            provider = %policy.provider_name,
            consecutive_failures,
            failure = ?failure,
            "provider health probe marked deployments unhealthy"
        );
    } else {
        tracing::warn!(
            provider = %policy.provider_name,
            consecutive_failures,
            failure = ?failure,
            "provider health probe failed"
        );
    }
}

#[cfg(test)]
#[path = "runtime_tests.rs"]
mod tests;