llm-cost-ops 0.1.1

Core library for cost operations on LLM deployments
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
// Budget forecasting and alerting

use chrono::{DateTime, Utc};
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};

use super::{
    engine::{ForecastEngine, ForecastRequest, ModelType},
    types::{ForecastConfig, ForecastHorizon, TimeSeriesData},
    ForecastResult,
};

/// Budget alert severity
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum AlertSeverity {
    Info,
    Warning,
    Critical,
}

/// Budget alert type
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum AlertType {
    /// Budget threshold exceeded
    ThresholdExceeded,

    /// Projected to exceed budget
    ProjectedExceedance,

    /// Unusual spending pattern
    UnusualSpending,

    /// Approaching budget limit
    ApproachingLimit,
}

/// Budget alert
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BudgetAlert {
    /// Alert type
    pub alert_type: AlertType,

    /// Severity level
    pub severity: AlertSeverity,

    /// Alert message
    pub message: String,

    /// Current spend
    pub current_spend: Decimal,

    /// Budget limit
    pub budget_limit: Decimal,

    /// Utilization percentage
    pub utilization_percent: f64,

    /// Projected spend (if applicable)
    pub projected_spend: Option<Decimal>,

    /// Timestamp when alert was generated
    pub timestamp: DateTime<Utc>,
}

/// Budget configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BudgetConfig {
    /// Total budget limit
    pub limit: Decimal,

    /// Budget period in days
    pub period_days: u64,

    /// Warning threshold (percentage of budget)
    pub warning_threshold: f64,

    /// Critical threshold (percentage of budget)
    pub critical_threshold: f64,

    /// Enable forecasting-based alerts
    pub enable_forecasting: bool,
}

impl Default for BudgetConfig {
    fn default() -> Self {
        Self {
            limit: Decimal::from(1000),
            period_days: 30,
            warning_threshold: 0.80, // 80%
            critical_threshold: 0.95, // 95%
            enable_forecasting: true,
        }
    }
}

/// Budget forecast result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BudgetForecast {
    /// Current spend
    pub current_spend: Decimal,

    /// Budget limit
    pub budget_limit: Decimal,

    /// Remaining budget
    pub remaining_budget: Decimal,

    /// Utilization percentage
    pub utilization_percent: f64,

    /// Projected end-of-period spend
    pub projected_spend: Option<Decimal>,

    /// Projected utilization at end of period
    pub projected_utilization: Option<f64>,

    /// Days remaining in period
    pub days_remaining: i64,

    /// Current daily average spend
    pub daily_average: Decimal,

    /// Projected daily spend (based on forecast)
    pub projected_daily: Option<Decimal>,

    /// Active alerts
    pub alerts: Vec<BudgetAlert>,

    /// Forecast timestamp
    pub timestamp: DateTime<Utc>,
}

/// Budget forecaster
pub struct BudgetForecaster {
    config: BudgetConfig,
    forecast_engine: ForecastEngine,
}

impl BudgetForecaster {
    /// Create a new budget forecaster
    pub fn new(config: BudgetConfig) -> Self {
        let forecast_config = ForecastConfig {
            horizon: ForecastHorizon::Days(config.period_days),
            confidence_level: 0.95,
            include_trend: true,
            detect_seasonality: true,
            min_data_points: 7,
        };

        Self {
            config,
            forecast_engine: ForecastEngine::new(forecast_config),
        }
    }

    /// Create with default configuration
    pub fn with_defaults() -> Self {
        Self::new(BudgetConfig::default())
    }

    /// Generate budget forecast
    pub fn forecast(
        &self,
        historical_data: &TimeSeriesData,
        period_start: DateTime<Utc>,
        period_end: DateTime<Utc>,
    ) -> ForecastResult<BudgetForecast> {
        // Calculate current spend
        let current_spend: Decimal = historical_data.values().iter().sum();

        // Calculate remaining budget
        let remaining_budget = self.config.limit - current_spend;
        let utilization_percent = if self.config.limit > Decimal::ZERO {
            (current_spend / self.config.limit * Decimal::from(100))
                .to_string()
                .parse::<f64>()
                .unwrap_or(0.0)
        } else {
            0.0
        };

        // Calculate days remaining
        let now = Utc::now();
        let days_remaining = (period_end - now).num_days();

        // Calculate daily average
        let period_days_elapsed = (now - period_start).num_days().max(1);
        let daily_average = current_spend / Decimal::from(period_days_elapsed);

        // Generate forecast if enabled and we have enough data
        let (projected_spend, projected_utilization, projected_daily) =
            if self.config.enable_forecasting && historical_data.len() >= 7 {
                match self.generate_projection(historical_data, days_remaining) {
                    Ok((projected, daily)) => {
                        let total_projected = current_spend + projected;
                        let proj_util = if self.config.limit > Decimal::ZERO {
                            (total_projected / self.config.limit * Decimal::from(100))
                                .to_string()
                                .parse::<f64>()
                                .unwrap_or(0.0)
                        } else {
                            0.0
                        };
                        (Some(total_projected), Some(proj_util), Some(daily))
                    }
                    Err(_) => (None, None, None),
                }
            } else {
                (None, None, None)
            };

        // Generate alerts
        let alerts = self.generate_alerts(
            current_spend,
            utilization_percent,
            projected_spend,
            projected_utilization,
        );

        Ok(BudgetForecast {
            current_spend,
            budget_limit: self.config.limit,
            remaining_budget,
            utilization_percent,
            projected_spend,
            projected_utilization,
            days_remaining,
            daily_average,
            projected_daily,
            alerts,
            timestamp: Utc::now(),
        })
    }

    /// Generate spending projection
    fn generate_projection(
        &self,
        data: &TimeSeriesData,
        days_remaining: i64,
    ) -> ForecastResult<(Decimal, Decimal)> {
        if days_remaining <= 0 {
            return Ok((Decimal::ZERO, Decimal::ZERO));
        }

        let mut forecast_config = ForecastConfig::default();
        forecast_config.horizon = ForecastHorizon::Days(days_remaining as u64);

        let request = ForecastRequest {
            data: data.clone(),
            config: forecast_config,
            preferred_model: Some(ModelType::Auto),
        };

        let forecast_result = self.forecast_engine.forecast(request)?;

        // Sum forecasted values
        let projected_total: Decimal = forecast_result
            .forecast
            .iter()
            .map(|p| p.value)
            .sum();

        // Calculate average daily projection
        let projected_daily = if days_remaining > 0 {
            projected_total / Decimal::from(days_remaining)
        } else {
            Decimal::ZERO
        };

        Ok((projected_total, projected_daily))
    }

    /// Generate budget alerts
    fn generate_alerts(
        &self,
        current_spend: Decimal,
        utilization_percent: f64,
        projected_spend: Option<Decimal>,
        projected_utilization: Option<f64>,
    ) -> Vec<BudgetAlert> {
        let mut alerts = Vec::new();
        let now = Utc::now();

        // Check current utilization
        if utilization_percent >= self.config.critical_threshold * 100.0 {
            alerts.push(BudgetAlert {
                alert_type: AlertType::ThresholdExceeded,
                severity: AlertSeverity::Critical,
                message: format!(
                    "Budget critical: {:.1}% utilized (threshold: {:.1}%)",
                    utilization_percent,
                    self.config.critical_threshold * 100.0
                ),
                current_spend,
                budget_limit: self.config.limit,
                utilization_percent,
                projected_spend,
                timestamp: now,
            });
        } else if utilization_percent >= self.config.warning_threshold * 100.0 {
            alerts.push(BudgetAlert {
                alert_type: AlertType::ApproachingLimit,
                severity: AlertSeverity::Warning,
                message: format!(
                    "Budget warning: {:.1}% utilized (threshold: {:.1}%)",
                    utilization_percent,
                    self.config.warning_threshold * 100.0
                ),
                current_spend,
                budget_limit: self.config.limit,
                utilization_percent,
                projected_spend,
                timestamp: now,
            });
        }

        // Check projected utilization
        if let Some(proj_util) = projected_utilization {
            if proj_util >= 100.0 {
                alerts.push(BudgetAlert {
                    alert_type: AlertType::ProjectedExceedance,
                    severity: AlertSeverity::Critical,
                    message: format!(
                        "Projected to exceed budget: {:.1}% utilization expected",
                        proj_util
                    ),
                    current_spend,
                    budget_limit: self.config.limit,
                    utilization_percent,
                    projected_spend,
                    timestamp: now,
                });
            } else if proj_util >= self.config.critical_threshold * 100.0 {
                alerts.push(BudgetAlert {
                    alert_type: AlertType::ProjectedExceedance,
                    severity: AlertSeverity::Warning,
                    message: format!(
                        "Projected to approach budget limit: {:.1}% utilization expected",
                        proj_util
                    ),
                    current_spend,
                    budget_limit: self.config.limit,
                    utilization_percent,
                    projected_spend,
                    timestamp: now,
                });
            }
        }

        // Check for unusual spending patterns
        if let Some(proj_spend) = projected_spend {
            // If projected spend is significantly higher than current trajectory
            let simple_projection = current_spend * Decimal::from(2); // Simple doubling
            if proj_spend > simple_projection * Decimal::new(120, 2) {
                // 20% more than simple projection
                alerts.push(BudgetAlert {
                    alert_type: AlertType::UnusualSpending,
                    severity: AlertSeverity::Warning,
                    message: "Unusual spending pattern detected: costs accelerating faster than historical average".to_string(),
                    current_spend,
                    budget_limit: self.config.limit,
                    utilization_percent,
                    projected_spend: Some(proj_spend),
                    timestamp: now,
                });
            }
        }

        alerts
    }

    /// Check if budget is on track
    pub fn is_on_track(&self, forecast: &BudgetForecast) -> bool {
        if let Some(proj_util) = forecast.projected_utilization {
            proj_util <= 100.0
        } else {
            forecast.utilization_percent <= 100.0
        }
    }

    /// Get recommended budget for next period based on historical data
    pub fn recommend_budget(
        &self,
        historical_data: &TimeSeriesData,
        period_days: u64,
    ) -> ForecastResult<Decimal> {
        if historical_data.is_empty() {
            return Ok(self.config.limit); // Default to current limit
        }

        let forecast_config = ForecastConfig {
            horizon: ForecastHorizon::Days(period_days),
            confidence_level: 0.95,
            include_trend: true,
            detect_seasonality: true,
            min_data_points: 7,
        };

        let request = ForecastRequest {
            data: historical_data.clone(),
            config: forecast_config,
            preferred_model: Some(ModelType::Auto),
        };

        let forecast_result = self.forecast_engine.forecast(request)?;

        // Sum forecasted values and add 20% buffer
        let projected_total: Decimal = forecast_result
            .forecast
            .iter()
            .map(|p| p.value)
            .sum();

        let recommended = projected_total * Decimal::new(120, 2); // Add 20% buffer

        Ok(recommended.max(self.config.limit)) // At least current limit
    }
}

impl Default for BudgetForecaster {
    fn default() -> Self {
        Self::with_defaults()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use super::super::types::DataPoint;
    use chrono::Duration;
    use rust_decimal::Decimal;

    fn create_test_series(values: Vec<i32>) -> TimeSeriesData {
        let start = Utc::now() - Duration::days(values.len() as i64);
        let points: Vec<DataPoint> = values
            .into_iter()
            .enumerate()
            .map(|(i, v)| {
                DataPoint::new(start + Duration::days(i as i64), Decimal::from(v))
            })
            .collect();

        TimeSeriesData::with_auto_interval(points)
    }

    #[test]
    fn test_budget_forecaster_creation() {
        let forecaster = BudgetForecaster::with_defaults();
        assert_eq!(forecaster.config.limit, Decimal::from(1000));
        assert_eq!(forecaster.config.period_days, 30);
    }

    #[test]
    fn test_budget_forecast_basic() {
        let mut config = BudgetConfig::default();
        config.limit = Decimal::from(1000);
        config.enable_forecasting = false; // Disable for simple test

        let forecaster = BudgetForecaster::new(config);

        let data = create_test_series(vec![10, 12, 15, 13, 14, 16, 18]);
        let period_start = Utc::now() - Duration::days(7);
        let period_end = Utc::now() + Duration::days(23);

        let forecast = forecaster.forecast(&data, period_start, period_end).unwrap();

        assert_eq!(forecast.current_spend, Decimal::from(98)); // Sum of values
        assert_eq!(forecast.budget_limit, Decimal::from(1000));
        assert!(forecast.remaining_budget > Decimal::ZERO);
        assert!(forecast.utilization_percent < 100.0);
    }

    #[test]
    fn test_budget_alerts_warning() {
        let mut config = BudgetConfig::default();
        config.limit = Decimal::from(100);
        config.warning_threshold = 0.80;
        config.critical_threshold = 0.95;
        config.enable_forecasting = false;

        let forecaster = BudgetForecaster::new(config);

        // Spend 85 out of 100 (85% utilization)
        let data = create_test_series(vec![10, 15, 20, 15, 25]);
        let period_start = Utc::now() - Duration::days(5);
        let period_end = Utc::now() + Duration::days(25);

        let forecast = forecaster.forecast(&data, period_start, period_end).unwrap();

        assert!(forecast.alerts.len() > 0);
        assert!(forecast
            .alerts
            .iter()
            .any(|a| a.severity == AlertSeverity::Warning));
    }

    #[test]
    fn test_budget_alerts_critical() {
        let mut config = BudgetConfig::default();
        config.limit = Decimal::from(100);
        config.critical_threshold = 0.95;
        config.enable_forecasting = false;

        let forecaster = BudgetForecaster::new(config);

        // Spend 96 out of 100 (96% utilization)
        let data = create_test_series(vec![20, 24, 26, 26]);
        let period_start = Utc::now() - Duration::days(4);
        let period_end = Utc::now() + Duration::days(26);

        let forecast = forecaster.forecast(&data, period_start, period_end).unwrap();

        assert!(forecast.alerts.len() > 0);
        assert!(forecast
            .alerts
            .iter()
            .any(|a| a.severity == AlertSeverity::Critical));
    }

    #[test]
    fn test_is_on_track() {
        let forecaster = BudgetForecaster::with_defaults();

        let on_track_forecast = BudgetForecast {
            current_spend: Decimal::from(50),
            budget_limit: Decimal::from(100),
            remaining_budget: Decimal::from(50),
            utilization_percent: 50.0,
            projected_spend: Some(Decimal::from(80)),
            projected_utilization: Some(80.0),
            days_remaining: 15,
            daily_average: Decimal::from(3),
            projected_daily: Some(Decimal::from(2)),
            alerts: vec![],
            timestamp: Utc::now(),
        };

        assert!(forecaster.is_on_track(&on_track_forecast));

        let over_budget_forecast = BudgetForecast {
            current_spend: Decimal::from(50),
            budget_limit: Decimal::from(100),
            remaining_budget: Decimal::from(50),
            utilization_percent: 50.0,
            projected_spend: Some(Decimal::from(120)),
            projected_utilization: Some(120.0),
            days_remaining: 15,
            daily_average: Decimal::from(3),
            projected_daily: Some(Decimal::from(5)),
            alerts: vec![],
            timestamp: Utc::now(),
        };

        assert!(!forecaster.is_on_track(&over_budget_forecast));
    }

    #[test]
    fn test_daily_average_calculation() {
        let config = BudgetConfig::default();
        let forecaster = BudgetForecaster::new(config);

        let data = create_test_series(vec![10, 10, 10, 10, 10]); // 5 days, 50 total
        let period_start = Utc::now() - Duration::days(5);
        let period_end = Utc::now() + Duration::days(25);

        let forecast = forecaster.forecast(&data, period_start, period_end).unwrap();

        // Daily average should be approximately 10 (50 total / 5 days)
        assert!(forecast.daily_average >= Decimal::from(9));
        assert!(forecast.daily_average <= Decimal::from(11));
    }

    #[test]
    fn test_budget_recommendation() {
        let forecaster = BudgetForecaster::with_defaults();

        let data = create_test_series(vec![
            10, 12, 11, 13, 14, 15, 16, 17, 18, 19, 20, 21,
        ]);

        let recommended = forecaster.recommend_budget(&data, 30).unwrap();

        // Recommended budget should be at least the current limit
        assert!(recommended >= forecaster.config.limit);
    }

    #[test]
    fn test_no_alerts_when_within_budget() {
        let mut config = BudgetConfig::default();
        config.limit = Decimal::from(1000);
        config.enable_forecasting = false;

        let forecaster = BudgetForecaster::new(config);

        let data = create_test_series(vec![10, 12, 15, 13, 14]);
        let period_start = Utc::now() - Duration::days(5);
        let period_end = Utc::now() + Duration::days(25);

        let forecast = forecaster.forecast(&data, period_start, period_end).unwrap();

        // Should have no alerts when well within budget
        assert_eq!(forecast.alerts.len(), 0);
    }

    #[test]
    fn test_remaining_budget_calculation() {
        let mut config = BudgetConfig::default();
        config.limit = Decimal::from(500);
        config.enable_forecasting = false;

        let forecaster = BudgetForecaster::new(config);

        let data = create_test_series(vec![50, 60, 70]);
        let period_start = Utc::now() - Duration::days(3);
        let period_end = Utc::now() + Duration::days(27);

        let forecast = forecaster.forecast(&data, period_start, period_end).unwrap();

        assert_eq!(forecast.current_spend, Decimal::from(180));
        assert_eq!(forecast.remaining_budget, Decimal::from(320)); // 500 - 180
    }
}