litellm-rs 0.4.16

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
//! Provider-specific thinking implementations
//!
//! This module contains thinking/reasoning implementations for each supported provider:
//! - OpenAI (o1, o3, o4 series)
//! - Anthropic (Claude extended thinking)
//! - DeepSeek (R1/Reasoner)
//! - Gemini (Flash Thinking / Deep Think)
//! - OpenRouter (passthrough to underlying providers)

use serde_json::Value;

use crate::core::providers::unified_provider::ProviderError;
use crate::core::types::thinking::{
    ThinkingCapabilities, ThinkingConfig, ThinkingContent, ThinkingEffort, ThinkingUsage,
};

/// OpenAI-specific thinking implementation
pub mod openai_thinking {
    use super::*;

    /// OpenAI thinking models (o1, o3, o4 series)
    const OPENAI_THINKING_MODELS: &[&str] = &[
        "o1",
        "o1-preview",
        "o1-mini",
        "o3",
        "o3-mini",
        "o4",
        "o4-mini",
    ];

    /// Check if an OpenAI model supports thinking
    pub fn supports_thinking(model: &str) -> bool {
        let model_lower = model.to_lowercase();
        OPENAI_THINKING_MODELS
            .iter()
            .any(|m| model_lower.starts_with(m) || model_lower.contains(&format!("/{}", m)))
    }

    /// Get thinking capabilities for OpenAI models
    pub fn capabilities(model: &str) -> ThinkingCapabilities {
        if supports_thinking(model) {
            ThinkingCapabilities {
                supports_thinking: true,
                supports_streaming_thinking: false, // OpenAI doesn't stream reasoning
                max_thinking_tokens: Some(20_000),
                supported_efforts: vec![
                    ThinkingEffort::Low,
                    ThinkingEffort::Medium,
                    ThinkingEffort::High,
                ],
                thinking_models: OPENAI_THINKING_MODELS
                    .iter()
                    .map(|s| s.to_string())
                    .collect(),
                can_return_thinking: true,
                thinking_always_on: false,
            }
        } else {
            ThinkingCapabilities::unsupported()
        }
    }

    /// Transform thinking config for OpenAI
    pub fn transform_config(config: &ThinkingConfig, _model: &str) -> Result<Value, ProviderError> {
        let mut params = serde_json::Map::new();

        if let Some(budget) = config.budget_tokens {
            // OpenAI max is 20,000
            let capped = budget.min(20_000);
            params.insert("max_reasoning_tokens".into(), capped.into());
        }

        if config.include_thinking {
            params.insert("include_reasoning".into(), true.into());
        }

        // Map effort to reasoning_effort
        if let Some(effort) = &config.effort {
            let effort_str = match effort {
                ThinkingEffort::Low => "low",
                ThinkingEffort::Medium => "medium",
                ThinkingEffort::High => "high",
            };
            params.insert("reasoning_effort".into(), effort_str.into());
        }

        Ok(Value::Object(params))
    }

    /// Extract thinking from OpenAI response
    pub fn extract_thinking(response: &Value) -> Option<ThinkingContent> {
        response
            .pointer("/choices/0/message/reasoning")
            .and_then(|v| v.as_str())
            .map(|text| ThinkingContent::Text {
                text: text.to_string(),
                signature: None,
            })
    }

    /// Extract thinking usage from OpenAI response
    pub fn extract_usage(response: &Value) -> Option<ThinkingUsage> {
        response
            .pointer("/usage/reasoning_tokens")
            .map(|tokens| ThinkingUsage {
                thinking_tokens: tokens.as_u64().map(|t| t as u32),
                budget_tokens: None,
                thinking_cost: None,
                provider: Some("openai".to_string()),
            })
    }
}

/// Anthropic-specific thinking implementation
pub mod anthropic_thinking {
    use super::*;

    /// Anthropic models with thinking support
    const ANTHROPIC_THINKING_MODELS: &[&str] = &[
        "claude-3-opus",
        "claude-3-sonnet",
        "claude-3-haiku",
        "claude-3-5-sonnet",
        "claude-3-5-opus",
        "claude-4",
    ];

    /// Check if an Anthropic model supports thinking
    pub fn supports_thinking(model: &str) -> bool {
        let model_lower = model.to_lowercase();
        ANTHROPIC_THINKING_MODELS
            .iter()
            .any(|m| model_lower.contains(m))
    }

    /// Get thinking capabilities for Anthropic models
    pub fn capabilities(model: &str) -> ThinkingCapabilities {
        if supports_thinking(model) {
            ThinkingCapabilities {
                supports_thinking: true,
                supports_streaming_thinking: true, // Anthropic supports streaming thinking
                max_thinking_tokens: Some(100_000), // Anthropic allows larger budgets
                supported_efforts: vec![ThinkingEffort::Medium, ThinkingEffort::High],
                thinking_models: ANTHROPIC_THINKING_MODELS
                    .iter()
                    .map(|s| s.to_string())
                    .collect(),
                can_return_thinking: true,
                thinking_always_on: false,
            }
        } else {
            ThinkingCapabilities::unsupported()
        }
    }

    /// Transform thinking config for Anthropic
    pub fn transform_config(config: &ThinkingConfig, _model: &str) -> Result<Value, ProviderError> {
        let mut params = serde_json::Map::new();

        if config.enabled {
            let mut thinking = serde_json::Map::new();
            thinking.insert("type".into(), "enabled".into());

            if let Some(budget) = config.budget_tokens {
                thinking.insert("budget_tokens".into(), budget.into());
            }

            params.insert("thinking".into(), Value::Object(thinking));
        }

        Ok(Value::Object(params))
    }

    /// Extract thinking from Anthropic response
    pub fn extract_thinking(response: &Value) -> Option<ThinkingContent> {
        response
            .pointer("/content")
            .and_then(|v| v.as_array())
            .and_then(|blocks| {
                blocks.iter().find_map(|block| {
                    if block.get("type")?.as_str()? == "thinking" {
                        Some(ThinkingContent::Block {
                            thinking: block.get("thinking")?.as_str()?.to_string(),
                            block_type: Some("thinking".to_string()),
                        })
                    } else {
                        None
                    }
                })
            })
    }

    /// Extract thinking usage from Anthropic response
    pub fn extract_usage(response: &Value) -> Option<ThinkingUsage> {
        let thinking_tokens = response
            .pointer("/usage/thinking_tokens")
            .and_then(|v| v.as_u64())
            .map(|t| t as u32);

        if thinking_tokens.is_some() {
            Some(ThinkingUsage {
                thinking_tokens,
                budget_tokens: response
                    .pointer("/usage/thinking_budget_tokens")
                    .and_then(|v| v.as_u64())
                    .map(|t| t as u32),
                thinking_cost: None,
                provider: Some("anthropic".to_string()),
            })
        } else {
            None
        }
    }
}

/// DeepSeek-specific thinking implementation
pub mod deepseek_thinking {
    use super::*;

    /// DeepSeek thinking models
    const DEEPSEEK_THINKING_MODELS: &[&str] = &["deepseek-r1", "deepseek-reasoner", "r1"];

    /// Check if a DeepSeek model supports thinking
    pub fn supports_thinking(model: &str) -> bool {
        let model_lower = model.to_lowercase();
        DEEPSEEK_THINKING_MODELS
            .iter()
            .any(|m| model_lower.contains(m))
    }

    /// Get thinking capabilities for DeepSeek models
    pub fn capabilities(model: &str) -> ThinkingCapabilities {
        if supports_thinking(model) {
            ThinkingCapabilities {
                supports_thinking: true,
                supports_streaming_thinking: true,
                max_thinking_tokens: None, // No documented limit
                supported_efforts: vec![
                    ThinkingEffort::Low,
                    ThinkingEffort::Medium,
                    ThinkingEffort::High,
                ],
                thinking_models: DEEPSEEK_THINKING_MODELS
                    .iter()
                    .map(|s| s.to_string())
                    .collect(),
                can_return_thinking: true,
                thinking_always_on: true, // DeepSeek R1 always thinks
            }
        } else {
            ThinkingCapabilities::unsupported()
        }
    }

    /// Transform thinking config for DeepSeek
    pub fn transform_config(config: &ThinkingConfig, _model: &str) -> Result<Value, ProviderError> {
        let mut params = serde_json::Map::new();

        // DeepSeek uses reasoning_effort
        if let Some(effort) = &config.effort {
            let effort_str = match effort {
                ThinkingEffort::Low => "low",
                ThinkingEffort::Medium => "medium",
                ThinkingEffort::High => "high",
            };
            params.insert("reasoning_effort".into(), effort_str.into());
        }

        Ok(Value::Object(params))
    }

    /// Extract thinking from DeepSeek response
    pub fn extract_thinking(response: &Value) -> Option<ThinkingContent> {
        response
            .pointer("/choices/0/message/reasoning_content")
            .and_then(|v| v.as_str())
            .map(|text| ThinkingContent::Text {
                text: text.to_string(),
                signature: None,
            })
    }

    /// Extract thinking usage from DeepSeek response
    pub fn extract_usage(response: &Value) -> Option<ThinkingUsage> {
        response
            .pointer("/usage/reasoning_tokens")
            .map(|tokens| ThinkingUsage {
                thinking_tokens: tokens.as_u64().map(|t| t as u32),
                budget_tokens: None,
                thinking_cost: None,
                provider: Some("deepseek".to_string()),
            })
    }
}

/// Gemini-specific thinking implementation
pub mod gemini_thinking {
    use super::*;

    /// Gemini thinking models
    const GEMINI_THINKING_MODELS: &[&str] = &[
        "gemini-2.0-flash-thinking",
        "gemini-3.0-deep-think",
        "gemini-thinking",
    ];

    /// Check if a Gemini model supports thinking
    pub fn supports_thinking(model: &str) -> bool {
        let model_lower = model.to_lowercase();
        GEMINI_THINKING_MODELS
            .iter()
            .any(|m| model_lower.contains(m))
            || model_lower.contains("thinking")
            || model_lower.contains("deep-think")
    }

    /// Get thinking capabilities for Gemini models
    pub fn capabilities(model: &str) -> ThinkingCapabilities {
        if supports_thinking(model) {
            ThinkingCapabilities {
                supports_thinking: true,
                supports_streaming_thinking: true,
                max_thinking_tokens: Some(32_000),
                supported_efforts: vec![ThinkingEffort::Medium, ThinkingEffort::High],
                thinking_models: GEMINI_THINKING_MODELS
                    .iter()
                    .map(|s| s.to_string())
                    .collect(),
                can_return_thinking: true,
                thinking_always_on: false,
            }
        } else {
            ThinkingCapabilities::unsupported()
        }
    }

    /// Transform thinking config for Gemini
    pub fn transform_config(config: &ThinkingConfig, _model: &str) -> Result<Value, ProviderError> {
        let mut params = serde_json::Map::new();

        if config.enabled {
            params.insert("enableThinking".into(), true.into());

            if let Some(budget) = config.budget_tokens {
                params.insert("thinkingBudget".into(), budget.into());
            }
        }

        Ok(Value::Object(params))
    }

    /// Extract thinking from Gemini response
    pub fn extract_thinking(response: &Value) -> Option<ThinkingContent> {
        // Try thoughts field first
        response
            .pointer("/candidates/0/content/thoughts")
            .and_then(|v| v.as_str())
            .map(|text| ThinkingContent::Text {
                text: text.to_string(),
                signature: None,
            })
            // Also try thinking field
            .or_else(|| {
                response
                    .pointer("/candidates/0/content/thinking")
                    .and_then(|v| v.as_str())
                    .map(|text| ThinkingContent::Text {
                        text: text.to_string(),
                        signature: None,
                    })
            })
    }

    /// Extract thinking usage from Gemini response
    pub fn extract_usage(response: &Value) -> Option<ThinkingUsage> {
        response
            .pointer("/usageMetadata/thinkingTokenCount")
            .map(|tokens| ThinkingUsage {
                thinking_tokens: tokens.as_u64().map(|t| t as u32),
                budget_tokens: None,
                thinking_cost: None,
                provider: Some("gemini".to_string()),
            })
    }
}

/// OpenRouter passthrough implementation
///
/// OpenRouter routes to multiple providers, so we detect the underlying provider
/// and use the appropriate thinking extraction.
pub mod openrouter_thinking {
    use super::*;

    /// Check if a model supports thinking through OpenRouter
    pub fn supports_thinking(model: &str) -> bool {
        let model_lower = model.to_lowercase();

        // Check for OpenAI reasoning models
        if model_lower.contains("o1") || model_lower.contains("o3") || model_lower.contains("o4") {
            return true;
        }

        // Check for Anthropic models
        if model_lower.contains("claude") {
            return true;
        }

        // Check for DeepSeek reasoning models
        if model_lower.contains("deepseek-r1") || model_lower.contains("reasoner") {
            return true;
        }

        // Check for Gemini thinking models
        if model_lower.contains("gemini") && model_lower.contains("thinking") {
            return true;
        }

        false
    }

    /// Detect the underlying provider from model name
    pub fn detect_provider(model: &str) -> &'static str {
        let model_lower = model.to_lowercase();

        if model_lower.contains("openai")
            || model_lower.starts_with("o1")
            || model_lower.starts_with("o3")
            || model_lower.starts_with("o4")
        {
            "openai"
        } else if model_lower.contains("anthropic") || model_lower.contains("claude") {
            "anthropic"
        } else if model_lower.contains("deepseek") {
            "deepseek"
        } else if model_lower.contains("gemini") || model_lower.contains("google") {
            "gemini"
        } else {
            "unknown"
        }
    }

    /// Get thinking capabilities for OpenRouter models
    pub fn capabilities(model: &str) -> ThinkingCapabilities {
        match detect_provider(model) {
            "openai" => openai_thinking::capabilities(model),
            "anthropic" => anthropic_thinking::capabilities(model),
            "deepseek" => deepseek_thinking::capabilities(model),
            "gemini" => gemini_thinking::capabilities(model),
            _ => ThinkingCapabilities::unsupported(),
        }
    }

    /// Transform thinking config for OpenRouter
    ///
    /// For models with a recognized provider prefix (openai/, anthropic/, etc.) the
    /// provider-specific format is used so OpenRouter can forward it correctly.
    /// For all other models the OpenRouter-native `reasoning.effort` object is emitted,
    /// which OpenRouter accepts universally across its model catalog.
    pub fn transform_config(config: &ThinkingConfig, model: &str) -> Result<Value, ProviderError> {
        match detect_provider(model) {
            "openai" => openai_thinking::transform_config(config, model),
            "anthropic" => anthropic_thinking::transform_config(config, model),
            "deepseek" => deepseek_thinking::transform_config(config, model),
            "gemini" => gemini_thinking::transform_config(config, model),
            _ => {
                // Use OpenRouter's native reasoning object for generic/unknown models.
                // `effort` and `max_tokens` are alternative controls on OpenRouter;
                // sending both causes validation failures on stricter backends.
                let mut params = serde_json::Map::new();
                if let Some(effort) = &config.effort {
                    let effort_str = match effort {
                        ThinkingEffort::Low => "low",
                        ThinkingEffort::Medium => "medium",
                        ThinkingEffort::High => "high",
                    };
                    let mut reasoning = serde_json::Map::new();
                    reasoning.insert("effort".into(), effort_str.into());
                    // Do not set max_tokens when effort is present; they are mutually exclusive.
                    params.insert("reasoning".into(), Value::Object(reasoning));
                } else if let Some(budget) = config.budget_tokens {
                    let mut reasoning = serde_json::Map::new();
                    reasoning.insert("max_tokens".into(), budget.into());
                    params.insert("reasoning".into(), Value::Object(reasoning));
                }
                Ok(Value::Object(params))
            }
        }
    }

    /// Extract thinking from OpenRouter response
    ///
    /// Tries multiple extraction patterns since the response format
    /// depends on the underlying provider.
    pub fn extract_thinking(response: &Value) -> Option<ThinkingContent> {
        // Try OpenAI style
        if let Some(thinking) = openai_thinking::extract_thinking(response) {
            return Some(thinking);
        }

        // Try DeepSeek style
        if let Some(thinking) = deepseek_thinking::extract_thinking(response) {
            return Some(thinking);
        }

        // Try Anthropic style
        if let Some(thinking) = anthropic_thinking::extract_thinking(response) {
            return Some(thinking);
        }

        // Try Gemini style
        if let Some(thinking) = gemini_thinking::extract_thinking(response) {
            return Some(thinking);
        }

        None
    }

    /// Extract thinking usage from OpenRouter response
    pub fn extract_usage(response: &Value) -> Option<ThinkingUsage> {
        // Try OpenAI style
        if let Some(mut usage) = openai_thinking::extract_usage(response) {
            usage.provider = Some("openrouter".to_string());
            return Some(usage);
        }

        // Try DeepSeek style
        if let Some(mut usage) = deepseek_thinking::extract_usage(response) {
            usage.provider = Some("openrouter".to_string());
            return Some(usage);
        }

        // Try Anthropic style
        if let Some(mut usage) = anthropic_thinking::extract_usage(response) {
            usage.provider = Some("openrouter".to_string());
            return Some(usage);
        }

        // Try Gemini style
        if let Some(mut usage) = gemini_thinking::extract_usage(response) {
            usage.provider = Some("openrouter".to_string());
            return Some(usage);
        }

        None
    }
}