litellm-rs 0.6.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
use super::*;

// ==================== Cost Calculation Tests ====================

#[test]
fn test_cost_calculation() {
    // Test Claude Opus pricing
    let cost = CostCalculator::calculate_cost(
        "anthropic.claude-3-opus-20240229",
        1000, // 1k input tokens
        500,  // 500 output tokens
    )
    .unwrap();

    // Expected: (1000/1000 * 0.015) + (500/1000 * 0.075) = 0.015 + 0.0375 = 0.0525
    assert!((cost - 0.0525).abs() < 0.0001);
}

#[test]
fn test_cost_calculation_claude_opus_46() {
    let cost = CostCalculator::calculate_cost("anthropic.claude-opus-4-6-v1:0", 1000, 500).unwrap();
    // Expected: (1 * 0.005) + (0.5 * 0.025) = 0.0175
    assert!((cost - 0.0175).abs() < 0.0001);
}

#[test]
fn test_cost_calculation_claude_sonnet() {
    let cost =
        CostCalculator::calculate_cost("anthropic.claude-3-sonnet-20240229", 1000, 1000).unwrap();
    // Expected: (1 * 0.003) + (1 * 0.015) = 0.018
    assert!((cost - 0.018).abs() < 0.0001);
}

#[test]
fn test_cost_calculation_claude_haiku() {
    let cost =
        CostCalculator::calculate_cost("anthropic.claude-3-haiku-20240307", 10000, 5000).unwrap();
    // Expected: (10 * 0.00025) + (5 * 0.00125) = 0.0025 + 0.00625 = 0.00875
    assert!((cost - 0.00875).abs() < 0.0001);
}

#[test]
fn test_cost_calculation_titan() {
    let cost = CostCalculator::calculate_cost("amazon.titan-text-express-v1", 5000, 2000).unwrap();
    // Expected: (5 * 0.0002) + (2 * 0.0006) = 0.001 + 0.0012 = 0.0022
    assert!((cost - 0.0022).abs() < 0.0001);
}

#[test]
fn test_cost_calculation_nova_micro() {
    let cost = CostCalculator::calculate_cost("amazon.nova-micro-v1:0", 100000, 50000).unwrap();
    // Expected: (100 * 0.000035) + (50 * 0.00014) = 0.0035 + 0.007 = 0.0105
    assert!((cost - 0.0105).abs() < 0.0001);
}

#[test]
fn test_cost_calculation_mistral() {
    let cost =
        CostCalculator::calculate_cost("mistral.mistral-large-2407-v1:0", 2000, 1000).unwrap();
    // Expected: (2 * 0.002) + (1 * 0.006) = 0.004 + 0.006 = 0.01
    assert!((cost - 0.01).abs() < 0.0001);
}

#[test]
fn test_cost_calculation_llama() {
    let cost = CostCalculator::calculate_cost("meta.llama3-70b-instruct-v1:0", 3000, 2000).unwrap();
    // Expected: (3 * 0.00265) + (2 * 0.0035) = 0.00795 + 0.007 = 0.01495
    assert!((cost - 0.01495).abs() < 0.0001);
}

#[test]
fn test_cost_calculation_cohere() {
    let cost = CostCalculator::calculate_cost("cohere.command-r-plus-v1:0", 1000, 500).unwrap();
    // Expected: (1 * 0.003) + (0.5 * 0.015) = 0.003 + 0.0075 = 0.0105
    assert!((cost - 0.0105).abs() < 0.0001);
}

#[test]
fn test_cost_calculation_ai21() {
    let cost = CostCalculator::calculate_cost("ai21.jamba-1-5-large-v1:0", 4000, 2000).unwrap();
    // Expected: (4 * 0.002) + (2 * 0.008) = 0.008 + 0.016 = 0.024
    assert!((cost - 0.024).abs() < 0.0001);
}

#[test]
fn test_cost_calculation_zero_tokens() {
    let cost = CostCalculator::calculate_cost("anthropic.claude-3-opus-20240229", 0, 0).unwrap();
    assert!((cost - 0.0).abs() < 0.0001);
}

#[test]
fn test_cost_calculation_only_input() {
    let cost = CostCalculator::calculate_cost("anthropic.claude-3-opus-20240229", 1000, 0).unwrap();
    // Expected: (1 * 0.015) + 0 = 0.015
    assert!((cost - 0.015).abs() < 0.0001);
}

#[test]
fn test_cost_calculation_only_output() {
    let cost = CostCalculator::calculate_cost("anthropic.claude-3-opus-20240229", 0, 1000).unwrap();
    // Expected: 0 + (1 * 0.075) = 0.075
    assert!((cost - 0.075).abs() < 0.0001);
}

#[test]
fn test_cost_calculation_large_tokens() {
    let cost =
        CostCalculator::calculate_cost("anthropic.claude-3-haiku-20240307", 1_000_000, 500_000)
            .unwrap();
    // Expected: (1000 * 0.00025) + (500 * 0.00125) = 0.25 + 0.625 = 0.875
    assert!((cost - 0.875).abs() < 0.001);
}

// ==================== Model Pricing Lookup Tests ====================

#[test]
fn test_model_pricing_lookup() {
    let pricing = CostCalculator::get_model_pricing("anthropic.claude-3-opus-20240229").unwrap();
    assert_eq!(pricing.input_cost_per_1k_tokens, 0.015);
    assert_eq!(pricing.output_cost_per_1k_tokens, 0.075);
    assert_eq!(pricing.currency, "USD");
}

#[test]
fn test_core_model_pricing_lookup() {
    let pricing =
        CostCalculator::get_core_model_pricing("anthropic.claude-3-opus-20240229").unwrap();
    assert_eq!(pricing.model, "anthropic.claude-3-opus-20240229");
    assert_eq!(pricing.input_cost_per_1k_tokens, 0.015);
    assert_eq!(pricing.output_cost_per_1k_tokens, 0.075);
    assert_eq!(pricing.currency, "USD");
}

#[test]
fn test_model_pricing_lookup_sonnet() {
    let pricing = CostCalculator::get_model_pricing("anthropic.claude-3-sonnet-20240229").unwrap();
    assert_eq!(pricing.input_cost_per_1k_tokens, 0.003);
    assert_eq!(pricing.output_cost_per_1k_tokens, 0.015);
}

#[test]
fn test_model_pricing_lookup_haiku() {
    let pricing = CostCalculator::get_model_pricing("anthropic.claude-3-haiku-20240307").unwrap();
    assert_eq!(pricing.input_cost_per_1k_tokens, 0.00025);
    assert_eq!(pricing.output_cost_per_1k_tokens, 0.00125);
}

#[test]
fn test_model_pricing_lookup_claude_35_sonnet() {
    let pricing =
        CostCalculator::get_model_pricing("anthropic.claude-3-5-sonnet-20241022").unwrap();
    assert_eq!(pricing.input_cost_per_1k_tokens, 0.003);
    assert_eq!(pricing.output_cost_per_1k_tokens, 0.015);
}

#[test]
fn test_model_pricing_lookup_titan() {
    let pricing = CostCalculator::get_model_pricing("amazon.titan-text-express-v1").unwrap();
    assert_eq!(pricing.input_cost_per_1k_tokens, 0.0002);
    assert_eq!(pricing.output_cost_per_1k_tokens, 0.0006);
}

#[test]
fn test_model_pricing_lookup_nova() {
    let pricing = CostCalculator::get_model_pricing("amazon.nova-pro-v1:0").unwrap();
    assert_eq!(pricing.input_cost_per_1k_tokens, 0.0008);
    assert_eq!(pricing.output_cost_per_1k_tokens, 0.0032);
}

#[test]
fn test_model_pricing_lookup_mistral() {
    let pricing = CostCalculator::get_model_pricing("mistral.mixtral-8x7b-instruct-v0:1").unwrap();
    assert_eq!(pricing.input_cost_per_1k_tokens, 0.00045);
    assert_eq!(pricing.output_cost_per_1k_tokens, 0.0007);
}

#[test]
fn test_model_pricing_lookup_llama() {
    let pricing = CostCalculator::get_model_pricing("meta.llama3-1-405b-instruct-v1:0").unwrap();
    assert_eq!(pricing.input_cost_per_1k_tokens, 0.00532);
    assert_eq!(pricing.output_cost_per_1k_tokens, 0.016);
}

#[test]
fn test_model_pricing_lookup_cohere() {
    let pricing = CostCalculator::get_model_pricing("cohere.command-r-v1:0").unwrap();
    assert_eq!(pricing.input_cost_per_1k_tokens, 0.0005);
    assert_eq!(pricing.output_cost_per_1k_tokens, 0.0015);
}

#[test]
fn test_model_pricing_lookup_ai21() {
    let pricing = CostCalculator::get_model_pricing("ai21.jamba-instruct-v1:0").unwrap();
    assert_eq!(pricing.input_cost_per_1k_tokens, 0.0005);
    assert_eq!(pricing.output_cost_per_1k_tokens, 0.0007);
}

#[test]
fn test_model_pricing_lookup_unknown() {
    let pricing = CostCalculator::get_model_pricing("unknown-model");
    assert!(pricing.is_none());
}

// ==================== Detailed Cost Breakdown Tests ====================

#[test]
fn test_detailed_cost_breakdown() {
    let breakdown =
        CostCalculator::calculate_detailed_cost("amazon.titan-text-express-v1", 2000, 1000)
            .unwrap();

    assert_eq!(breakdown.input_tokens, 2000);
    assert_eq!(breakdown.output_tokens, 1000);
    assert_eq!(breakdown.currency, "USD");
    assert!(breakdown.total_cost > 0.0);
}

#[test]
fn test_detailed_cost_breakdown_claude() {
    let breakdown =
        CostCalculator::calculate_detailed_cost("anthropic.claude-3-opus-20240229", 1000, 500)
            .unwrap();

    assert_eq!(breakdown.input_tokens, 1000);
    assert_eq!(breakdown.output_tokens, 500);
    assert!((breakdown.input_cost - 0.015).abs() < 0.0001);
    assert!((breakdown.output_cost - 0.0375).abs() < 0.0001);
    assert!((breakdown.total_cost - 0.0525).abs() < 0.0001);
    assert_eq!(breakdown.currency, "USD");
}

#[test]
fn test_detailed_cost_breakdown_zero_tokens() {
    let breakdown =
        CostCalculator::calculate_detailed_cost("anthropic.claude-3-haiku-20240307", 0, 0).unwrap();

    assert_eq!(breakdown.input_tokens, 0);
    assert_eq!(breakdown.output_tokens, 0);
    assert!((breakdown.input_cost - 0.0).abs() < 0.0001);
    assert!((breakdown.output_cost - 0.0).abs() < 0.0001);
    assert!((breakdown.total_cost - 0.0).abs() < 0.0001);
}

#[test]
fn test_detailed_cost_breakdown_unknown_model() {
    let breakdown = CostCalculator::calculate_detailed_cost("unknown-model", 1000, 500);
    assert!(breakdown.is_none());
}

#[test]
fn test_detailed_cost_sum() {
    let breakdown =
        CostCalculator::calculate_detailed_cost("mistral.mistral-large-2407-v1:0", 5000, 3000)
            .unwrap();

    // Verify total equals input + output
    let expected_total = breakdown.input_cost + breakdown.output_cost;
    assert!((breakdown.total_cost - expected_total).abs() < 0.0001);
}

// ==================== Unknown Model Tests ====================

#[test]
fn test_unknown_model() {
    let cost = CostCalculator::calculate_cost("unknown-model", 1000, 500);
    assert!(cost.is_none());
}

#[test]
fn test_empty_model_id() {
    let cost = CostCalculator::calculate_cost("", 1000, 500);
    assert!(cost.is_none());
}

#[test]
fn test_partial_model_id() {
    let cost = CostCalculator::calculate_cost("anthropic.claude", 1000, 500);
    assert!(cost.is_none());
}

// ==================== All Models List Tests ====================

#[test]
fn test_all_models_list() {
    let models = CostCalculator::get_all_models();
    assert!(!models.is_empty());
    assert!(models.contains(&"anthropic.claude-3-opus-20240229"));
    assert!(models.contains(&"amazon.titan-text-express-v1"));
}

#[test]
fn test_all_models_contains_claude() {
    let models = CostCalculator::get_all_models();
    let claude_count = models
        .iter()
        .filter(|m| m.starts_with("anthropic."))
        .count();
    assert!(claude_count >= 8);
}

#[test]
fn test_all_models_contains_titan() {
    let models = CostCalculator::get_all_models();
    let titan_count = models
        .iter()
        .filter(|m| m.starts_with("amazon.titan"))
        .count();
    assert!(titan_count >= 3);
}

#[test]
fn test_all_models_contains_nova() {
    let models = CostCalculator::get_all_models();
    let nova_count = models
        .iter()
        .filter(|m| m.starts_with("amazon.nova"))
        .count();
    assert!(nova_count >= 3);
}

#[test]
fn test_all_models_contains_mistral() {
    let models = CostCalculator::get_all_models();
    let mistral_count = models.iter().filter(|m| m.starts_with("mistral.")).count();
    assert!(mistral_count >= 5);
}

#[test]
fn test_all_models_contains_llama() {
    let models = CostCalculator::get_all_models();
    let llama_count = models
        .iter()
        .filter(|m| m.starts_with("meta.llama"))
        .count();
    assert!(llama_count >= 10);
}

#[test]
fn test_all_models_contains_cohere() {
    let models = CostCalculator::get_all_models();
    let cohere_count = models.iter().filter(|m| m.starts_with("cohere.")).count();
    assert!(cohere_count >= 4);
}

#[test]
fn test_all_models_contains_ai21() {
    let models = CostCalculator::get_all_models();
    let ai21_count = models.iter().filter(|m| m.starts_with("ai21.")).count();
    assert!(ai21_count >= 3);
}

#[test]
fn test_all_models_total_count() {
    let models = CostCalculator::get_all_models();
    // Should have at least 30 models
    assert!(models.len() >= 30);
}

// ==================== ModelPricing Struct Tests ====================

#[test]
fn test_model_pricing_debug() {
    let pricing = ModelPricing {
        model: "test-model".to_string(),
        input_cost_per_1k_tokens: 0.01,
        output_cost_per_1k_tokens: 0.02,
        ..Default::default()
    };
    let debug = format!("{:?}", pricing);
    assert!(debug.contains("ModelPricing"));
    assert!(debug.contains("0.01"));
    assert!(debug.contains("0.02"));
}

#[test]
fn test_model_pricing_clone() {
    let pricing = ModelPricing {
        model: "test-model".to_string(),
        input_cost_per_1k_tokens: 0.01,
        output_cost_per_1k_tokens: 0.02,
        ..Default::default()
    };
    let cloned = pricing.clone();
    assert_eq!(cloned.input_cost_per_1k_tokens, 0.01);
    assert_eq!(cloned.output_cost_per_1k_tokens, 0.02);
    assert_eq!(cloned.currency, "USD");
}

// ==================== CostBreakdown Struct Tests ====================

#[test]
fn test_cost_breakdown_debug() {
    let breakdown = CostBreakdown {
        input_tokens: 1000,
        output_tokens: 500,
        input_cost: 0.015,
        output_cost: 0.0375,
        total_cost: 0.0525,
        currency: "USD",
    };
    let debug = format!("{:?}", breakdown);
    assert!(debug.contains("CostBreakdown"));
    assert!(debug.contains("1000"));
    assert!(debug.contains("500"));
}

#[test]
fn test_cost_breakdown_clone() {
    let breakdown = CostBreakdown {
        input_tokens: 1000,
        output_tokens: 500,
        input_cost: 0.015,
        output_cost: 0.0375,
        total_cost: 0.0525,
        currency: "USD",
    };
    let cloned = breakdown.clone();
    assert_eq!(cloned.input_tokens, 1000);
    assert_eq!(cloned.output_tokens, 500);
    assert_eq!(cloned.total_cost, 0.0525);
}

// ==================== Legacy Model Tests ====================

#[test]
fn test_claude_v2_pricing() {
    let pricing = CostCalculator::get_model_pricing("anthropic.claude-v2").unwrap();
    assert_eq!(pricing.input_cost_per_1k_tokens, 0.008);
    assert_eq!(pricing.output_cost_per_1k_tokens, 0.024);
}

#[test]
fn test_claude_instant_pricing() {
    let pricing = CostCalculator::get_model_pricing("anthropic.claude-instant-v1").unwrap();
    assert_eq!(pricing.input_cost_per_1k_tokens, 0.00163);
    assert_eq!(pricing.output_cost_per_1k_tokens, 0.00551);
}

#[test]
fn test_llama2_pricing() {
    let pricing = CostCalculator::get_model_pricing("meta.llama2-70b-chat-v1").unwrap();
    assert_eq!(pricing.input_cost_per_1k_tokens, 0.00195);
    assert_eq!(pricing.output_cost_per_1k_tokens, 0.00256);
}