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
//! Adaptive smart router with ML-based provider selection.
//!
//! This module provides an intelligent request router that learns from historical provider
//! performance metrics and makes real-time routing decisions optimized for latency, cost,
//! or reliability. It uses exponential weighted moving average (EWMA) for online learning
//! and adapts to changing network conditions.
//!
//! # Features
//!
//! - **EWMA-based latency prediction**: Adapts to changing provider performance
//! - **Cost optimization**: Automatic provider switching for cost efficiency
//! - **Reliability optimization**: Route around failing providers
//! - **Fallback chain**: Cascade through providers on failures
//! - **Online learning**: No training required, learns from live traffic
//! - **Sub-millisecond routing**: <1ms overhead per request

use std::collections::HashMap;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use std::time::SystemTime;

use tokio::sync::RwLock;

use crate::error::{Error, Result};
use crate::types::CompletionRequest;

/// Optimization strategy for routing decisions
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum Optimization {
    /// Minimize latency (choose fastest provider)
    #[default]
    Latency,
    /// Minimize cost (choose cheapest provider)
    Cost,
    /// Maximize reliability (choose most stable provider)
    Reliability,
}

/// Provider performance metrics
#[derive(Debug, Clone)]
pub struct ProviderMetrics {
    /// Exponential weighted moving average latency (milliseconds)
    pub ewma_latency_ms: f64,
    /// Error rate (0.0 to 1.0)
    pub error_rate: f64,
    /// Cost per 1000 tokens (normalized)
    pub cost_per_1k_tokens: f64,
    /// Total requests processed
    pub request_count: u64,
    /// Total errors
    pub error_count: u64,
    /// Last update timestamp
    pub last_updated: SystemTime,
}

impl Default for ProviderMetrics {
    fn default() -> Self {
        Self {
            ewma_latency_ms: 100.0, // Start with 100ms estimate
            error_rate: 0.0,
            cost_per_1k_tokens: 0.01, // Default cost estimate
            request_count: 0,
            error_count: 0,
            last_updated: SystemTime::now(),
        }
    }
}

/// Router provider configuration
#[derive(Debug, Clone)]
pub struct RouterProviderConfig {
    /// Provider name
    pub name: String,
    /// Cost per 1000 tokens
    pub cost_per_1k_tokens: f64,
    /// Reliability weight (0.0 to 1.0)
    pub reliability_weight: f64,
}

/// Smart routing decision
#[derive(Debug, Clone)]
pub struct RoutingDecision {
    /// Selected provider name
    pub provider: String,
    /// Predicted latency (ms)
    pub predicted_latency_ms: f64,
    /// Predicted cost
    pub predicted_cost: f64,
    /// Fallback providers in priority order
    pub fallback_chain: Vec<String>,
}

/// Adaptive smart router with ML-based provider selection
pub struct SmartRouter {
    /// Provider configurations
    providers: HashMap<String, RouterProviderConfig>,
    /// Historical metrics per provider
    metrics: Arc<RwLock<HashMap<String, ProviderMetrics>>>,
    /// Optimization strategy
    optimization: Optimization,
    /// EWMA decay factor (0.0 to 1.0, typically 0.1-0.3)
    ewma_alpha: f64,
    /// Request counter for global statistics
    request_counter: Arc<AtomicU64>,
}

impl SmartRouter {
    /// Create a new smart router builder
    pub fn builder() -> SmartRouterBuilder {
        SmartRouterBuilder::default()
    }

    /// Make a routing decision for a request
    pub async fn route(&self, request: &CompletionRequest) -> Result<RoutingDecision> {
        let metrics = self.metrics.read().await;

        // Filter available providers
        let available: Vec<_> = self
            .providers
            .iter()
            .filter(|(name, _)| metrics.contains_key(*name))
            .collect();

        if available.is_empty() {
            return Err(Error::Configuration(
                "No providers available for routing".to_string(),
            ));
        }

        // Calculate routing scores
        let scores: Vec<(String, f64)> = available
            .iter()
            .map(|(name, config)| {
                let metric = &metrics[*name];
                let score = self.calculate_score(metric, config);
                ((*name).clone(), score)
            })
            .collect();

        // Sort by score (higher is better)
        let mut sorted = scores;
        sorted.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));

        // Build decision with fallback chain
        let primary = sorted[0].0.clone();
        let fallback_chain: Vec<String> = sorted
            .iter()
            .skip(1)
            .take(2)
            .map(|(name, _)| name.clone())
            .collect();

        let primary_metric = &metrics[&primary];
        let primary_config = &self.providers[&primary];

        Ok(RoutingDecision {
            provider: primary,
            predicted_latency_ms: primary_metric.ewma_latency_ms,
            predicted_cost: self.estimate_request_cost(request, primary_config),
            fallback_chain,
        })
    }

    /// Record request result for learning
    pub async fn record_request(
        &self,
        provider: &str,
        latency_ms: f64,
        success: bool,
        _tokens: u32,
    ) -> Result<()> {
        let mut metrics = self.metrics.write().await;

        let metric = metrics.entry(provider.to_string()).or_insert_with(|| {
            // Initialize from provider config if available
            let mut m = ProviderMetrics::default();
            if let Some(config) = self.providers.get(provider) {
                m.cost_per_1k_tokens = config.cost_per_1k_tokens;
            }
            m
        });

        // Update EWMA latency
        metric.ewma_latency_ms =
            (1.0 - self.ewma_alpha) * metric.ewma_latency_ms + self.ewma_alpha * latency_ms;

        // Update request count
        metric.request_count += 1;

        // Update error rate
        if !success {
            metric.error_count += 1;
        }
        metric.error_rate = metric.error_count as f64 / metric.request_count as f64;

        metric.last_updated = SystemTime::now();

        self.request_counter.fetch_add(1, Ordering::Relaxed);

        Ok(())
    }

    /// Get current metrics for a provider
    pub async fn get_metrics(&self, provider: &str) -> Option<ProviderMetrics> {
        self.metrics.read().await.get(provider).cloned()
    }

    /// Get routing statistics
    pub async fn stats(&self) -> RouterStats {
        let metrics = self.metrics.read().await;
        let total_requests = self.request_counter.load(Ordering::Relaxed);

        let avg_latency = if metrics.is_empty() {
            0.0
        } else {
            metrics.values().map(|m| m.ewma_latency_ms).sum::<f64>() / metrics.len() as f64
        };

        RouterStats {
            total_requests,
            provider_count: metrics.len(),
            average_latency_ms: avg_latency,
            optimization_strategy: self.optimization,
        }
    }

    /// Calculate routing score based on optimization strategy
    fn calculate_score(&self, metric: &ProviderMetrics, _config: &RouterProviderConfig) -> f64 {
        // Normalize metrics to 0.0-1.0 range
        let latency_score = 1.0 / (1.0 + metric.ewma_latency_ms / 100.0);
        let reliability_score = 1.0 - metric.error_rate;
        let cost_score = 1.0 / (1.0 + metric.cost_per_1k_tokens * 100.0);

        match self.optimization {
            Optimization::Latency => {
                // Latency-optimized: 70% latency, 30% reliability
                latency_score * 0.7 + reliability_score * 0.3
            }
            Optimization::Cost => {
                // Cost-optimized: 60% cost, 40% reliability
                cost_score * 0.6 + reliability_score * 0.4
            }
            Optimization::Reliability => {
                // Reliability-optimized: equal weight with latency fallback
                reliability_score * 0.7 + latency_score * 0.3
            }
        }
    }

    /// Estimate cost for a request
    fn estimate_request_cost(
        &self,
        _request: &CompletionRequest,
        config: &RouterProviderConfig,
    ) -> f64 {
        // Simplified: assume ~100 tokens per request
        (config.cost_per_1k_tokens / 1000.0) * 100.0
    }
}

/// Router statistics
#[derive(Debug, Clone, Copy)]
pub struct RouterStats {
    /// Total requests routed
    pub total_requests: u64,
    /// Number of tracked providers
    pub provider_count: usize,
    /// Average latency across all providers
    pub average_latency_ms: f64,
    /// Current optimization strategy
    pub optimization_strategy: Optimization,
}

/// Builder for SmartRouter
#[derive(Default)]
pub struct SmartRouterBuilder {
    providers: HashMap<String, RouterProviderConfig>,
    optimization: Optimization,
    ewma_alpha: f64,
}

impl SmartRouterBuilder {
    /// Add a provider to the router
    pub fn add_provider(mut self, config: RouterProviderConfig) -> Self {
        self.providers.insert(config.name.clone(), config);
        self
    }

    /// Add multiple providers
    pub fn with_providers(mut self, configs: Vec<RouterProviderConfig>) -> Self {
        for config in configs {
            self.providers.insert(config.name.clone(), config);
        }
        self
    }

    /// Set optimization strategy
    pub fn optimize_for(mut self, optimization: Optimization) -> Self {
        self.optimization = optimization;
        self
    }

    /// Set EWMA decay factor (0.1-0.3 recommended)
    pub fn with_ewma_alpha(mut self, alpha: f64) -> Self {
        self.ewma_alpha = alpha.clamp(0.01, 1.0);
        self
    }

    /// Build the router
    pub fn build(self) -> SmartRouter {
        SmartRouter {
            providers: self.providers,
            metrics: Arc::new(RwLock::new(HashMap::new())),
            optimization: self.optimization,
            ewma_alpha: if self.ewma_alpha > 0.0 {
                self.ewma_alpha
            } else {
                0.1
            },
            request_counter: Arc::new(AtomicU64::new(0)),
        }
    }
}

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

    fn create_test_router() -> SmartRouter {
        let providers = vec![
            RouterProviderConfig {
                name: "openai".to_string(),
                cost_per_1k_tokens: 0.01,
                reliability_weight: 0.9,
            },
            RouterProviderConfig {
                name: "anthropic".to_string(),
                cost_per_1k_tokens: 0.008,
                reliability_weight: 0.95,
            },
            RouterProviderConfig {
                name: "groq".to_string(),
                cost_per_1k_tokens: 0.0001,
                reliability_weight: 0.85,
            },
        ];

        SmartRouter::builder()
            .with_providers(providers)
            .optimize_for(Optimization::Latency)
            .with_ewma_alpha(0.2)
            .build()
    }

    #[test]
    fn test_router_builder() {
        let router = create_test_router();
        assert_eq!(router.providers.len(), 3);
        assert_eq!(router.optimization, Optimization::Latency);
    }

    #[tokio::test]
    async fn test_route_decision() {
        let router = create_test_router();
        let request = CompletionRequest::new("openai/gpt-4", vec![Message::user("test")]);

        // Record some metrics
        router
            .record_request("openai", 50.0, true, 100)
            .await
            .unwrap();
        router
            .record_request("anthropic", 30.0, true, 100)
            .await
            .unwrap();
        router
            .record_request("groq", 20.0, true, 100)
            .await
            .unwrap();

        let decision = router.route(&request).await.unwrap();
        assert_eq!(decision.provider, "groq"); // Fastest
        assert!(decision.fallback_chain.len() <= 2);
    }

    #[tokio::test]
    async fn test_cost_optimization() {
        let providers = vec![
            RouterProviderConfig {
                name: "expensive".to_string(),
                cost_per_1k_tokens: 0.1,
                reliability_weight: 0.95,
            },
            RouterProviderConfig {
                name: "cheap".to_string(),
                cost_per_1k_tokens: 0.001,
                reliability_weight: 0.90,
            },
        ];

        let router = SmartRouter::builder()
            .with_providers(providers)
            .optimize_for(Optimization::Cost)
            .build();

        let request = CompletionRequest::new("openai/gpt-4", vec![Message::user("test")]);

        router
            .record_request("expensive", 100.0, true, 100)
            .await
            .unwrap();
        router
            .record_request("cheap", 150.0, true, 100)
            .await
            .unwrap();

        let decision = router.route(&request).await.unwrap();
        assert_eq!(decision.provider, "cheap"); // Lower cost despite higher latency
    }

    #[tokio::test]
    async fn test_reliability_optimization() {
        let providers = vec![
            RouterProviderConfig {
                name: "stable".to_string(),
                cost_per_1k_tokens: 0.01,
                reliability_weight: 1.0,
            },
            RouterProviderConfig {
                name: "flaky".to_string(),
                cost_per_1k_tokens: 0.01,
                reliability_weight: 0.1,
            },
        ];

        let router = SmartRouter::builder()
            .with_providers(providers)
            .optimize_for(Optimization::Reliability)
            .build();

        let request = CompletionRequest::new("openai/gpt-4", vec![Message::user("test")]);

        // Record metrics showing flakiness
        for i in 0..5 {
            router
                .record_request("stable", 50.0, true, 100)
                .await
                .unwrap();
            let success = i % 5 != 0; // 80% success rate
            router
                .record_request("flaky", 40.0, success, 100)
                .await
                .unwrap();
        }

        let decision = router.route(&request).await.unwrap();
        assert_eq!(decision.provider, "stable"); // More reliable
    }

    #[tokio::test]
    async fn test_ewma_learning() {
        let router = create_test_router();

        // Initial: simulate provider "openai" with latency update
        // Formula: ewma = (1 - alpha) * previous + alpha * new
        // With alpha=0.2: ewma = 0.8 * 100 + 0.2 * 100 = 100
        // The default starts at 100.0, so first record stays ~100
        router
            .record_request("openai", 80.0, true, 100)
            .await
            .unwrap();

        let m1 = router.get_metrics("openai").await.unwrap();
        // After first update with 80ms: 0.8 * 100 + 0.2 * 80 = 96
        assert!(m1.ewma_latency_ms < 100.0);

        // Subsequent: faster latency
        router
            .record_request("openai", 50.0, true, 100)
            .await
            .unwrap();

        let m2 = router.get_metrics("openai").await.unwrap();
        assert!(m2.ewma_latency_ms < m1.ewma_latency_ms);
        assert_eq!(m2.request_count, 2);
    }

    #[tokio::test]
    async fn test_router_stats() {
        let router = create_test_router();

        router
            .record_request("openai", 50.0, true, 100)
            .await
            .unwrap();
        router
            .record_request("anthropic", 60.0, true, 100)
            .await
            .unwrap();

        let stats = router.stats().await;
        assert_eq!(stats.total_requests, 2);
        assert_eq!(stats.optimization_strategy, Optimization::Latency);
    }

    #[tokio::test]
    async fn test_fallback_chain() {
        let router = create_test_router();
        let request = CompletionRequest::new("openai/gpt-4", vec![Message::user("test")]);

        router
            .record_request("openai", 100.0, true, 100)
            .await
            .unwrap();
        router
            .record_request("anthropic", 50.0, true, 100)
            .await
            .unwrap();
        router
            .record_request("groq", 30.0, true, 100)
            .await
            .unwrap();

        let decision = router.route(&request).await.unwrap();
        assert_eq!(decision.fallback_chain.len(), 2);
        assert!(!decision.fallback_chain.contains(&decision.provider));
    }
}