llmkit 0.1.3

Production-grade LLM client - 100+ providers, 11,000+ models. Pure Rust.
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
//! Provider pool for load balancing and failover.
//!
//! This module provides load balancing across multiple deployments of the same provider,
//! with automatic health checking and failover support.
//!
//! # Example
//!
//! ```ignore
//! use llmkit::{ProviderPool, RoutingStrategy};
//!
//! let pool = ProviderPool::builder()
//!     .add_deployment("openai-1", provider1, 1, 100)
//!     .add_deployment("openai-2", provider2, 1, 100)
//!     .strategy(RoutingStrategy::LeastLatency)
//!     .build();
//! ```

use std::collections::HashMap;
use std::pin::Pin;
use std::sync::atomic::{AtomicU32, AtomicU64, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};

use async_trait::async_trait;
use futures::Stream;
use parking_lot::RwLock;
use tokio::sync::Semaphore;

use crate::error::{Error, Result};
use crate::provider::Provider;
use crate::types::{
    BatchJob, BatchRequest, BatchResult, CompletionRequest, CompletionResponse, StreamChunk,
    TokenCountRequest, TokenCountResult,
};

/// Configuration for a single deployment (provider instance).
#[derive(Clone)]
pub struct DeploymentConfig {
    /// Unique name for this deployment
    pub name: String,
    /// The underlying provider
    pub provider: Arc<dyn Provider>,
    /// Weight for weighted routing (higher = more traffic)
    pub weight: u32,
    /// Priority for failover (lower = higher priority, used first)
    pub priority: u32,
    /// Maximum concurrent requests (None = unlimited)
    pub max_concurrent: Option<u32>,
    /// Optional region identifier
    pub region: Option<String>,
}

/// Health status of a deployment.
#[derive(Debug, Clone)]
pub struct DeploymentHealth {
    /// Whether the deployment is currently healthy
    pub healthy: bool,
    /// Time of last health check
    pub last_check: Option<Instant>,
    /// Number of consecutive failures
    pub consecutive_failures: u32,
    /// Number of consecutive successes (for recovery)
    pub consecutive_successes: u32,
    /// P50 latency in milliseconds
    pub latency_p50_ms: u64,
    /// P99 latency in milliseconds
    pub latency_p99_ms: u64,
    /// Current number of in-flight requests
    pub requests_in_flight: u32,
    /// Total requests handled
    pub total_requests: u64,
    /// Total errors
    pub total_errors: u64,
}

impl Default for DeploymentHealth {
    fn default() -> Self {
        Self {
            healthy: true,
            last_check: None,
            consecutive_failures: 0,
            consecutive_successes: 0,
            latency_p50_ms: 0,
            latency_p99_ms: 0,
            requests_in_flight: 0,
            total_requests: 0,
            total_errors: 0,
        }
    }
}

/// Routing strategy for the provider pool.
#[derive(Debug, Clone, Default)]
pub enum RoutingStrategy {
    /// Simple round-robin distribution
    #[default]
    RoundRobin,
    /// Route to the deployment with lowest latency
    LeastLatency,
    /// Route to the deployment with fewest in-flight requests
    LeastConnections,
    /// Weighted random distribution based on weights
    Weighted,
    /// Random distribution
    Random,
    /// Use primary (lowest priority), failover on error
    Primary,
}

/// Configuration for health checking.
#[derive(Debug, Clone)]
pub struct HealthCheckConfig {
    /// Interval between health checks
    pub interval: Duration,
    /// Timeout for health check requests
    pub timeout: Duration,
    /// Number of consecutive failures before marking unhealthy
    pub failure_threshold: u32,
    /// Number of consecutive successes before marking healthy again
    pub recovery_threshold: u32,
    /// Whether to enable active health checking
    pub enabled: bool,
}

impl Default for HealthCheckConfig {
    fn default() -> Self {
        Self {
            interval: Duration::from_secs(30),
            timeout: Duration::from_secs(10),
            failure_threshold: 3,
            recovery_threshold: 2,
            enabled: true,
        }
    }
}

/// Internal state for tracking routing.
struct PoolState {
    /// Round-robin counter
    rr_counter: AtomicU32,
    /// Health status per deployment
    health: RwLock<HashMap<String, DeploymentHealth>>,
    /// Concurrency limiters per deployment
    semaphores: HashMap<String, Arc<Semaphore>>,
    /// Latency samples for calculating percentiles (ring buffer)
    latency_samples: RwLock<HashMap<String, Vec<u64>>>,
}

/// Provider pool for load balancing across multiple deployments.
pub struct ProviderPool {
    /// Deployments in the pool
    deployments: Vec<DeploymentConfig>,
    /// Routing strategy
    strategy: RoutingStrategy,
    /// Health check configuration
    health_config: HealthCheckConfig,
    /// Internal state
    state: PoolState,
    /// Pool name (for logging)
    name: String,
}

impl ProviderPool {
    /// Create a new pool builder.
    pub fn builder() -> ProviderPoolBuilder {
        ProviderPoolBuilder::new()
    }

    /// Get all deployments.
    pub fn deployments(&self) -> &[DeploymentConfig] {
        &self.deployments
    }

    /// Get health status for a deployment.
    pub fn health(&self, deployment_name: &str) -> Option<DeploymentHealth> {
        self.state.health.read().get(deployment_name).cloned()
    }

    /// Get health status for all deployments.
    pub fn all_health(&self) -> HashMap<String, DeploymentHealth> {
        self.state.health.read().clone()
    }

    /// Get the number of healthy deployments.
    pub fn healthy_count(&self) -> usize {
        self.state
            .health
            .read()
            .values()
            .filter(|h| h.healthy)
            .count()
    }

    /// Select a deployment based on the routing strategy.
    fn select_deployment(&self) -> Result<&DeploymentConfig> {
        let healthy_deployments: Vec<_> = self
            .deployments
            .iter()
            .filter(|d| {
                self.state
                    .health
                    .read()
                    .get(&d.name)
                    .map(|h| h.healthy)
                    .unwrap_or(true)
            })
            .collect();

        if healthy_deployments.is_empty() {
            return Err(Error::other("No healthy deployments available"));
        }

        let selected = match &self.strategy {
            RoutingStrategy::RoundRobin => {
                let idx = self.state.rr_counter.fetch_add(1, Ordering::Relaxed) as usize;
                healthy_deployments[idx % healthy_deployments.len()]
            }
            RoutingStrategy::LeastLatency => {
                let health = self.state.health.read();
                healthy_deployments
                    .iter()
                    .min_by_key(|d| {
                        health
                            .get(&d.name)
                            .map(|h| h.latency_p50_ms)
                            .unwrap_or(u64::MAX)
                    })
                    .copied()
                    .unwrap_or(healthy_deployments[0])
            }
            RoutingStrategy::LeastConnections => {
                let health = self.state.health.read();
                healthy_deployments
                    .iter()
                    .min_by_key(|d| {
                        health
                            .get(&d.name)
                            .map(|h| h.requests_in_flight)
                            .unwrap_or(0)
                    })
                    .copied()
                    .unwrap_or(healthy_deployments[0])
            }
            RoutingStrategy::Weighted => {
                let total_weight: u32 = healthy_deployments.iter().map(|d| d.weight).sum();
                if total_weight == 0 {
                    healthy_deployments[0]
                } else {
                    let random = rand_u32() % total_weight;
                    let mut cumulative = 0;
                    healthy_deployments
                        .iter()
                        .find(|d| {
                            cumulative += d.weight;
                            cumulative > random
                        })
                        .copied()
                        .unwrap_or(healthy_deployments[0])
                }
            }
            RoutingStrategy::Random => {
                let idx = rand_u32() as usize % healthy_deployments.len();
                healthy_deployments[idx]
            }
            RoutingStrategy::Primary => {
                // Sort by priority (lowest first) and pick the first healthy one
                let mut sorted = healthy_deployments.clone();
                sorted.sort_by_key(|d| d.priority);
                sorted[0]
            }
        };

        Ok(selected)
    }

    /// Record a successful request.
    fn record_success(&self, deployment_name: &str, latency_ms: u64) {
        let mut health = self.state.health.write();
        if let Some(h) = health.get_mut(deployment_name) {
            h.total_requests += 1;
            h.consecutive_successes += 1;
            h.consecutive_failures = 0;

            // Recovery check
            if !h.healthy && h.consecutive_successes >= self.health_config.recovery_threshold {
                h.healthy = true;
                tracing::info!(deployment = deployment_name, "Deployment recovered");
            }
        }

        // Update latency samples
        let mut samples = self.state.latency_samples.write();
        let entry = samples.entry(deployment_name.to_string()).or_default();
        if entry.len() >= 100 {
            entry.remove(0);
        }
        entry.push(latency_ms);

        // Update percentiles
        if let Some(h) = health.get_mut(deployment_name) {
            if let Some(samples) = samples.get(deployment_name) {
                let mut sorted = samples.clone();
                sorted.sort_unstable();
                if !sorted.is_empty() {
                    h.latency_p50_ms = sorted[sorted.len() / 2];
                    h.latency_p99_ms = sorted[(sorted.len() * 99) / 100];
                }
            }
        }
    }

    /// Record a failed request.
    fn record_failure(&self, deployment_name: &str, _error: &Error) {
        let mut health = self.state.health.write();
        if let Some(h) = health.get_mut(deployment_name) {
            h.total_requests += 1;
            h.total_errors += 1;
            h.consecutive_failures += 1;
            h.consecutive_successes = 0;

            // Mark unhealthy if threshold exceeded
            if h.healthy && h.consecutive_failures >= self.health_config.failure_threshold {
                h.healthy = false;
                tracing::warn!(
                    deployment = deployment_name,
                    failures = h.consecutive_failures,
                    "Deployment marked unhealthy"
                );
            }
        }
    }

    /// Increment in-flight counter.
    fn inc_in_flight(&self, deployment_name: &str) {
        let mut health = self.state.health.write();
        if let Some(h) = health.get_mut(deployment_name) {
            h.requests_in_flight += 1;
        }
    }

    /// Decrement in-flight counter.
    fn dec_in_flight(&self, deployment_name: &str) {
        let mut health = self.state.health.write();
        if let Some(h) = health.get_mut(deployment_name) {
            h.requests_in_flight = h.requests_in_flight.saturating_sub(1);
        }
    }
}

// Simple random number generator (avoid extra dependency)
fn rand_u32() -> u32 {
    static COUNTER: AtomicU64 = AtomicU64::new(0);
    let seed = COUNTER.fetch_add(1, Ordering::Relaxed);
    let time = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap_or_default()
        .as_nanos() as u64;
    ((seed.wrapping_mul(6364136223846793005).wrapping_add(time)) >> 32) as u32
}

#[async_trait]
impl Provider for ProviderPool {
    fn name(&self) -> &str {
        &self.name
    }

    async fn complete(&self, request: CompletionRequest) -> Result<CompletionResponse> {
        let deployment = self.select_deployment()?;
        let deployment_name = deployment.name.clone();

        // Acquire semaphore permit if max_concurrent is set
        let _permit = if let Some(sem) = self.state.semaphores.get(&deployment_name) {
            Some(
                sem.acquire()
                    .await
                    .map_err(|_| Error::other("Semaphore closed"))?,
            )
        } else {
            None
        };

        self.inc_in_flight(&deployment_name);
        let start = Instant::now();

        let result = deployment.provider.complete(request).await;

        let latency_ms = start.elapsed().as_millis() as u64;
        self.dec_in_flight(&deployment_name);

        match &result {
            Ok(_) => self.record_success(&deployment_name, latency_ms),
            Err(e) => self.record_failure(&deployment_name, e),
        }

        result
    }

    async fn complete_stream(
        &self,
        request: CompletionRequest,
    ) -> Result<Pin<Box<dyn Stream<Item = Result<StreamChunk>> + Send>>> {
        let deployment = self.select_deployment()?;
        let deployment_name = deployment.name.clone();

        self.inc_in_flight(&deployment_name);
        let start = Instant::now();

        let result = deployment.provider.complete_stream(request).await;

        let latency_ms = start.elapsed().as_millis() as u64;
        self.dec_in_flight(&deployment_name);

        match &result {
            Ok(_) => self.record_success(&deployment_name, latency_ms),
            Err(e) => self.record_failure(&deployment_name, e),
        }

        result
    }

    fn supports_tools(&self) -> bool {
        self.deployments
            .first()
            .map(|d| d.provider.supports_tools())
            .unwrap_or(false)
    }

    fn supports_vision(&self) -> bool {
        self.deployments
            .first()
            .map(|d| d.provider.supports_vision())
            .unwrap_or(false)
    }

    fn supports_streaming(&self) -> bool {
        self.deployments
            .first()
            .map(|d| d.provider.supports_streaming())
            .unwrap_or(true)
    }

    fn supports_token_counting(&self) -> bool {
        self.deployments
            .first()
            .map(|d| d.provider.supports_token_counting())
            .unwrap_or(false)
    }

    async fn count_tokens(&self, request: TokenCountRequest) -> Result<TokenCountResult> {
        let deployment = self.select_deployment()?;
        deployment.provider.count_tokens(request).await
    }

    fn supports_batch(&self) -> bool {
        self.deployments
            .first()
            .map(|d| d.provider.supports_batch())
            .unwrap_or(false)
    }

    async fn create_batch(&self, requests: Vec<BatchRequest>) -> Result<BatchJob> {
        let deployment = self.select_deployment()?;
        deployment.provider.create_batch(requests).await
    }

    async fn get_batch(&self, batch_id: &str) -> Result<BatchJob> {
        // Try all deployments since we don't know which one created the batch
        for deployment in &self.deployments {
            if let Ok(job) = deployment.provider.get_batch(batch_id).await {
                return Ok(job);
            }
        }
        Err(Error::other(format!("Batch not found: {}", batch_id)))
    }

    async fn get_batch_results(&self, batch_id: &str) -> Result<Vec<BatchResult>> {
        for deployment in &self.deployments {
            if let Ok(results) = deployment.provider.get_batch_results(batch_id).await {
                return Ok(results);
            }
        }
        Err(Error::other(format!("Batch not found: {}", batch_id)))
    }

    async fn cancel_batch(&self, batch_id: &str) -> Result<BatchJob> {
        for deployment in &self.deployments {
            if let Ok(job) = deployment.provider.cancel_batch(batch_id).await {
                return Ok(job);
            }
        }
        Err(Error::other(format!("Batch not found: {}", batch_id)))
    }

    async fn list_batches(&self, limit: Option<u32>) -> Result<Vec<BatchJob>> {
        // Aggregate batches from all deployments
        let mut all_batches = Vec::new();
        for deployment in &self.deployments {
            if let Ok(batches) = deployment.provider.list_batches(limit).await {
                all_batches.extend(batches);
            }
        }
        Ok(all_batches)
    }
}

/// Builder for creating a provider pool.
pub struct ProviderPoolBuilder {
    deployments: Vec<DeploymentConfig>,
    strategy: RoutingStrategy,
    health_config: HealthCheckConfig,
    name: String,
}

impl Default for ProviderPoolBuilder {
    fn default() -> Self {
        Self::new()
    }
}

impl ProviderPoolBuilder {
    /// Create a new pool builder.
    pub fn new() -> Self {
        Self {
            deployments: Vec::new(),
            strategy: RoutingStrategy::default(),
            health_config: HealthCheckConfig::default(),
            name: "pool".to_string(),
        }
    }

    /// Set the pool name.
    pub fn name(mut self, name: impl Into<String>) -> Self {
        self.name = name.into();
        self
    }

    /// Add a deployment to the pool.
    pub fn add_deployment(
        mut self,
        name: impl Into<String>,
        provider: impl Provider + 'static,
        weight: u32,
        priority: u32,
    ) -> Self {
        self.deployments.push(DeploymentConfig {
            name: name.into(),
            provider: Arc::new(provider),
            weight,
            priority,
            max_concurrent: None,
            region: None,
        });
        self
    }

    /// Add a deployment with full configuration.
    pub fn add_deployment_config(mut self, config: DeploymentConfig) -> Self {
        self.deployments.push(config);
        self
    }

    /// Set the routing strategy.
    pub fn strategy(mut self, strategy: RoutingStrategy) -> Self {
        self.strategy = strategy;
        self
    }

    /// Set the health check configuration.
    pub fn health_check(mut self, config: HealthCheckConfig) -> Self {
        self.health_config = config;
        self
    }

    /// Disable health checking.
    pub fn disable_health_check(mut self) -> Self {
        self.health_config.enabled = false;
        self
    }

    /// Build the provider pool.
    pub fn build(self) -> Result<ProviderPool> {
        if self.deployments.is_empty() {
            return Err(Error::config("Pool must have at least one deployment"));
        }

        // Initialize health status and semaphores
        let mut health_map = HashMap::new();
        let mut semaphores = HashMap::new();

        for deployment in &self.deployments {
            health_map.insert(deployment.name.clone(), DeploymentHealth::default());

            if let Some(max_concurrent) = deployment.max_concurrent {
                semaphores.insert(
                    deployment.name.clone(),
                    Arc::new(Semaphore::new(max_concurrent as usize)),
                );
            }
        }

        Ok(ProviderPool {
            deployments: self.deployments,
            strategy: self.strategy,
            health_config: self.health_config,
            state: PoolState {
                rr_counter: AtomicU32::new(0),
                health: RwLock::new(health_map),
                semaphores,
                latency_samples: RwLock::new(HashMap::new()),
            },
            name: self.name,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    // Mock provider for testing
    struct MockProvider {
        name: String,
    }

    #[async_trait]
    impl Provider for MockProvider {
        fn name(&self) -> &str {
            &self.name
        }

        async fn complete(&self, _request: CompletionRequest) -> Result<CompletionResponse> {
            Ok(CompletionResponse {
                id: "test".to_string(),
                model: "test".to_string(),
                content: vec![],
                stop_reason: crate::types::StopReason::EndTurn,
                usage: crate::types::Usage {
                    input_tokens: 0,
                    output_tokens: 0,
                    cache_creation_input_tokens: 0,
                    cache_read_input_tokens: 0,
                },
            })
        }

        async fn complete_stream(
            &self,
            _request: CompletionRequest,
        ) -> Result<Pin<Box<dyn Stream<Item = Result<StreamChunk>> + Send>>> {
            Err(Error::not_supported("streaming"))
        }
    }

    #[test]
    fn test_pool_builder() {
        let pool = ProviderPool::builder()
            .name("test-pool")
            .add_deployment(
                "d1",
                MockProvider {
                    name: "mock1".into(),
                },
                1,
                100,
            )
            .add_deployment(
                "d2",
                MockProvider {
                    name: "mock2".into(),
                },
                2,
                200,
            )
            .strategy(RoutingStrategy::Weighted)
            .build()
            .unwrap();

        assert_eq!(pool.deployments.len(), 2);
        assert_eq!(pool.healthy_count(), 2);
    }

    #[test]
    fn test_empty_pool_error() {
        let result = ProviderPool::builder().build();
        assert!(result.is_err());
    }

    #[test]
    fn test_health_tracking() {
        let pool = ProviderPool::builder()
            .add_deployment(
                "d1",
                MockProvider {
                    name: "mock".into(),
                },
                1,
                1,
            )
            .build()
            .unwrap();

        // Initially healthy
        let health = pool.health("d1").unwrap();
        assert!(health.healthy);
        assert_eq!(health.consecutive_failures, 0);

        // Record failures
        for _ in 0..3 {
            pool.record_failure("d1", &Error::Timeout);
        }

        let health = pool.health("d1").unwrap();
        assert!(!health.healthy);
        assert_eq!(health.consecutive_failures, 3);

        // Record successes for recovery
        for _ in 0..2 {
            pool.record_success("d1", 100);
        }

        let health = pool.health("d1").unwrap();
        assert!(health.healthy);
    }
}