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
//! Pricing management API endpoints
//!
//! This module provides HTTP endpoints for managing pricing data

use crate::core::pricing_service::{LiteLLMModelInfo, PricingSnapshot};
use crate::server::state::AppState;
use crate::utils::error::gateway_error::Result;
use actix_web::{HttpResponse, web};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use tracing::{info, warn};

/// Pricing refresh request payload
#[derive(Debug, Deserialize)]
pub struct RefreshRequest {
    /// Optional: Specific source URL to refresh from
    pub source_url: Option<String>,
    /// Optional: Force refresh even if cache is still valid
    pub force: Option<bool>,
}

/// Pricing refresh response
#[derive(Debug, Serialize)]
pub struct RefreshResponse {
    /// Whether the refresh was successful
    pub success: bool,
    /// Human-readable message about the refresh operation
    pub message: String,
    /// Number of models that were updated
    pub updated_models: usize,
    /// Timestamp of the refresh operation
    pub timestamp: String,
}

/// Pricing statistics response
#[derive(Debug, Serialize)]
pub struct PricingStatsResponse {
    /// Total number of models with pricing data
    pub total_models: usize,
    /// List of available providers
    pub providers: Vec<String>,
    /// When the pricing data was last updated
    pub last_updated: String,
    /// Current cache status (fresh/stale)
    pub cache_status: String,
}

/// Refresh pricing data endpoint
/// POST /v1/pricing/refresh
pub async fn refresh_pricing(
    data: web::Data<AppState>,
    payload: web::Json<RefreshRequest>,
) -> Result<HttpResponse> {
    info!("Pricing refresh requested: {:?}", payload);

    let pricing_service = &data.pricing;

    // Check if force refresh is needed
    let needs_refresh = payload.force.unwrap_or(false) || pricing_service.needs_refresh();

    if !needs_refresh {
        return Ok(HttpResponse::Ok().json(RefreshResponse {
            success: true,
            message: "Pricing data is already up to date".to_string(),
            updated_models: 0,
            timestamp: chrono::Utc::now().to_rfc3339(),
        }));
    }

    // Perform refresh
    match pricing_service.force_refresh().await {
        Ok(_) => {
            let stats = pricing_service.get_statistics();

            info!(
                "Pricing data refreshed successfully, {} models updated",
                stats.total_models
            );

            Ok(HttpResponse::Ok().json(RefreshResponse {
                success: true,
                message: "Pricing data refreshed successfully".to_string(),
                updated_models: stats.total_models,
                timestamp: chrono::Utc::now().to_rfc3339(),
            }))
        }
        Err(e) => {
            warn!("Failed to refresh pricing data: {}", e);

            Ok(HttpResponse::InternalServerError().json(RefreshResponse {
                success: false,
                message: format!("Failed to refresh pricing data: {}", e),
                updated_models: 0,
                timestamp: chrono::Utc::now().to_rfc3339(),
            }))
        }
    }
}

/// Get pricing statistics endpoint
/// GET /v1/pricing/stats
pub async fn get_pricing_stats(data: web::Data<AppState>) -> Result<HttpResponse> {
    let pricing_service = &data.pricing;
    let stats = pricing_service.get_statistics();

    let providers = pricing_service.get_providers();
    let last_updated = chrono::DateTime::<chrono::Utc>::from(stats.last_updated);

    let cache_status = if pricing_service.needs_refresh() {
        "stale".to_string()
    } else {
        "fresh".to_string()
    };

    Ok(HttpResponse::Ok().json(PricingStatsResponse {
        total_models: stats.total_models,
        providers,
        last_updated: last_updated.to_rfc3339(),
        cache_status,
    }))
}

/// Get pricing for a specific model
/// GET /v1/pricing/model/{model_name}
pub async fn get_model_pricing(
    data: web::Data<AppState>,
    path: web::Path<String>,
) -> Result<HttpResponse> {
    let model_name = path.into_inner();
    let pricing_service = &data.pricing;
    let pricing_snapshot = pricing_service.snapshot();

    match effective_model_pricing_at(&pricing_snapshot, &model_name, Utc::now()) {
        Some(model_info) => Ok(HttpResponse::Ok().json(model_info)),
        None => Ok(HttpResponse::NotFound().json(serde_json::json!({
            "error": "Model not found",
            "model": model_name
        }))),
    }
}

fn effective_model_pricing_at(
    pricing_snapshot: &PricingSnapshot,
    model_name: &str,
    pricing_time: DateTime<Utc>,
) -> Option<LiteLLMModelInfo> {
    let raw = pricing_snapshot.get_model_info(model_name)?;
    pricing_snapshot
        .get_model_info_for_provider_at(&raw.litellm_provider, model_name, pricing_time)
        .map(|(_, model_info)| model_info)
}

/// Calculate cost for a completion
/// POST /v1/pricing/calculate
#[derive(Debug, Deserialize)]
pub struct CostCalculationRequest {
    /// Model name to calculate cost for
    pub model: String,
    /// Optional provider name for provider-aware pricing
    pub provider: Option<String>,
    /// Number of input tokens
    pub input_tokens: u32,
    /// Number of output tokens
    pub output_tokens: u32,
    /// Optional prompt text for character-based pricing
    pub prompt: Option<String>,
    /// Optional completion text for character-based pricing
    pub completion: Option<String>,
    /// Optional duration in seconds for time-based pricing
    pub duration_seconds: Option<f64>,
}

/// Calculate the cost for a specific model usage
pub async fn calculate_cost(
    data: web::Data<AppState>,
    payload: web::Json<CostCalculationRequest>,
) -> Result<HttpResponse> {
    let pricing_service = &data.pricing;

    let result = if let Some(provider) = payload.provider.as_deref() {
        if pricing_service.needs_refresh()
            && let Err(e) = pricing_service.refresh_pricing_data().await
        {
            warn!("Failed to refresh pricing data: {}", e);
        }
        pricing_service.calculate_loaded_completion_cost_for_provider(
            provider,
            &payload.model,
            payload.input_tokens,
            payload.output_tokens,
            payload.prompt.as_deref(),
            payload.completion.as_deref(),
            payload.duration_seconds,
        )
    } else {
        pricing_service
            .calculate_completion_cost(
                &payload.model,
                payload.input_tokens,
                payload.output_tokens,
                payload.prompt.as_deref(),
                payload.completion.as_deref(),
                payload.duration_seconds,
            )
            .await
    };

    match result {
        Ok(cost_result) => Ok(HttpResponse::Ok().json(cost_result)),
        Err(e) => Ok(HttpResponse::BadRequest().json(serde_json::json!({
            "error": "Cost calculation failed",
            "message": e.to_string()
        }))),
    }
}

/// Configure pricing endpoints
pub fn configure_pricing_routes(cfg: &mut web::ServiceConfig) {
    cfg.service(
        web::scope("/v1/pricing")
            .route("/refresh", web::post().to(refresh_pricing))
            .route("/stats", web::get().to(get_pricing_stats))
            .route("/model/{model_name}", web::get().to(get_model_pricing))
            .route("/calculate", web::post().to(calculate_cost)),
    );
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::pricing_service::{LiteLLMModelInfo, PricingService};
    use crate::server::HttpServer as GatewayHttpServer;
    use actix_web::http::StatusCode;
    use actix_web::{App, test};
    use chrono::{TimeZone, Utc};
    use serde_json::{Value, json};
    use std::collections::HashMap;

    fn runtime_model_info(provider: &str) -> LiteLLMModelInfo {
        LiteLLMModelInfo {
            max_tokens: Some(8192),
            max_input_tokens: Some(8192),
            max_output_tokens: Some(2048),
            input_cost_per_token: Some(0.00001),
            output_cost_per_token: Some(0.00003),
            input_cost_per_character: None,
            output_cost_per_character: None,
            cost_per_second: None,
            litellm_provider: provider.to_string(),
            mode: "chat".to_string(),
            supports_function_calling: Some(true),
            supports_vision: Some(false),
            supports_streaming: Some(true),
            supports_parallel_function_calling: Some(true),
            supports_system_message: Some(true),
            extra: HashMap::new(),
        }
    }

    async fn build_pricing_route_state() -> AppState {
        let mut config = crate::server::valid_test_config();
        config.gateway.storage.database.enabled = false;
        config.gateway.storage.redis.enabled = false;

        let server = match GatewayHttpServer::new(&config).await {
            Ok(server) => server,
            Err(error) => {
                panic!("gateway server should initialize for pricing route tests: {error}")
            }
        };
        server.state().clone()
    }

    #[tokio::test]
    async fn model_pricing_route_resolves_the_effective_rate_at_request_time() {
        let pricing =
            PricingService::with_embedded_default().expect("embedded pricing should initialize");
        let after_promotion = Utc.with_ymd_and_hms(2027, 1, 1, 0, 0, 0).unwrap();

        let snapshot = pricing.snapshot();
        let model_info = effective_model_pricing_at(&snapshot, "gemini-3.7-flash", after_promotion)
            .expect("Gemini 3.7 pricing should resolve");

        assert_eq!(model_info.input_cost_per_token, Some(1.5e-6));
        assert_eq!(model_info.output_cost_per_token, Some(7.5e-6));
        assert_eq!(
            model_info.extra["cache_read_input_token_cost"],
            serde_json::json!(1.5e-7)
        );
    }

    #[tokio::test]
    async fn model_pricing_resolution_stays_on_one_snapshot_generation() {
        let pricing =
            PricingService::with_embedded_default().expect("embedded pricing should initialize");
        let snapshot = pricing.snapshot();
        pricing.add_custom_model(
            "gemini-3.7-flash".to_string(),
            runtime_model_info("replacement_provider"),
        );

        let model_info = effective_model_pricing_at(
            &snapshot,
            "gemini-3.7-flash",
            Utc.with_ymd_and_hms(2026, 8, 30, 0, 0, 0).unwrap(),
        )
        .expect("the pinned generation should retain Gemini pricing");

        assert_eq!(model_info.litellm_provider, "vertex_ai-language-models");
        assert_eq!(model_info.input_cost_per_token, Some(0.75e-6));
    }

    #[tokio::test]
    async fn calculate_cost_route_uses_provider_aware_runtime_pricing() {
        let state = build_pricing_route_state().await;
        state.pricing.add_custom_model(
            "runtime-route-priced-model".to_string(),
            runtime_model_info("runtime_provider"),
        );
        let app = test::init_service(
            App::new()
                .app_data(web::Data::new(state))
                .configure(configure_pricing_routes),
        )
        .await;

        let request = test::TestRequest::post()
            .uri("/v1/pricing/calculate")
            .set_json(json!({
                "provider": "runtime_provider",
                "model": "runtime-route-priced-model",
                "input_tokens": 1000,
                "output_tokens": 500
            }))
            .to_request();
        let response = test::call_service(&app, request).await;

        assert_eq!(response.status(), StatusCode::OK);
        let body: Value = test::read_body_json(response).await;
        assert_eq!(body["model"], "runtime-route-priced-model");
        assert_eq!(body["provider"], "runtime_provider");
        let total_cost = match body["total_cost"].as_f64() {
            Some(total_cost) => total_cost,
            None => panic!("pricing response should include numeric total_cost: {body}"),
        };
        assert!((total_cost - 0.025).abs() < f64::EPSILON);
    }

    #[tokio::test]
    async fn calculate_cost_route_resolves_xai_openai_like_prefix() {
        let state = build_pricing_route_state().await;
        let app = test::init_service(
            App::new()
                .app_data(web::Data::new(state))
                .configure(configure_pricing_routes),
        )
        .await;

        let request = test::TestRequest::post()
            .uri("/v1/pricing/calculate")
            .set_json(json!({
                "provider": "openai_like",
                "model": "xai/grok-4.3",
                "input_tokens": 1000,
                "output_tokens": 500
            }))
            .to_request();
        let response = test::call_service(&app, request).await;

        assert_eq!(response.status(), StatusCode::OK);
        let body: Value = test::read_body_json(response).await;
        assert_eq!(body["model"], "xai/grok-4.3");
        assert_eq!(body["provider"], "xai");
        let total_cost = match body["total_cost"].as_f64() {
            Some(total_cost) => total_cost,
            None => panic!("pricing response should include numeric total_cost: {body}"),
        };
        assert!((total_cost - 0.0025).abs() < f64::EPSILON);
    }

    #[tokio::test]
    async fn calculate_cost_route_applies_provider_aware_tiered_pricing() {
        let state = build_pricing_route_state().await;
        let app = test::init_service(
            App::new()
                .app_data(web::Data::new(state))
                .configure(configure_pricing_routes),
        )
        .await;

        let request = test::TestRequest::post()
            .uri("/v1/pricing/calculate")
            .set_json(json!({
                "provider": "azure",
                "model": "gpt-5.5",
                "input_tokens": 300000,
                "output_tokens": 1000
            }))
            .to_request();
        let response = test::call_service(&app, request).await;

        assert_eq!(response.status(), StatusCode::OK);
        let body: Value = test::read_body_json(response).await;
        assert_eq!(body["model"], "azure/gpt-5.5");
        assert_eq!(body["provider"], "azure");
        let total_cost = match body["total_cost"].as_f64() {
            Some(total_cost) => total_cost,
            None => panic!("pricing response should include numeric total_cost: {body}"),
        };
        assert!((total_cost - 3.045).abs() < 1e-12);
    }
}