api_openai 0.3.0

OpenAI's API for accessing large language models (LLMs).
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
//! Cost Management Module
//!
//! This module handles cost tracking, budget management, and cost analytics
//! for enterprise `OpenAI` API usage.

use serde::{ Deserialize, Serialize };
use std::collections::HashMap;

/// Cost tracking and budget management
#[ derive( Debug, Clone, Serialize, Deserialize, PartialEq ) ]
pub struct CostTracker
{
  /// Current daily spend in USD
  pub daily_spend : f64,
  /// Current monthly spend in USD
  pub monthly_spend : f64,
  /// Budget limits configuration
  pub budget_limits : BudgetLimits,
  /// Active cost alerts
  pub cost_alerts : Vec< CostAlert >,
  /// Detailed usage breakdown
  pub usage_breakdown : UsageBreakdown,
}

/// Budget limits and alert thresholds
#[ derive( Debug, Clone, Serialize, Deserialize, PartialEq ) ]
pub struct BudgetLimits
{
  /// Daily spending limit in USD
  pub daily_limit : Option< f64 >,
  /// Monthly spending limit in USD
  pub monthly_limit : Option< f64 >,
  /// Alert threshold as percentage of limit (0.0-1.0)
  pub alert_threshold : f64,
  /// Hard limit enforcement (stop requests when exceeded)
  pub enforce_hard_limit : bool,
  /// Cost optimization settings
  pub optimization_settings : CostOptimizationSettings,
}

/// Cost alert notification
#[ derive( Debug, Clone, Serialize, Deserialize, PartialEq ) ]
pub struct CostAlert
{
  /// Alert type
  pub alert_type : AlertType,
  /// Alert severity level
  pub severity : AlertSeverity,
  /// Alert message
  pub message : String,
  /// Timestamp when alert was triggered
  pub timestamp : u64,
  /// Current spend amount that triggered alert
  pub current_spend : f64,
  /// Limit that was exceeded
  pub limit : f64,
}

/// Types of cost alerts
#[ derive( Debug, Clone, Serialize, Deserialize, PartialEq ) ]
pub enum AlertType
{
  /// Daily limit approaching
  DailyLimitApproaching,
  /// Daily limit exceeded
  DailyLimitExceeded,
  /// Monthly limit approaching
  MonthlyLimitApproaching,
  /// Monthly limit exceeded
  MonthlyLimitExceeded,
}

/// Alert severity levels
#[ derive( Debug, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord ) ]
pub enum AlertSeverity
{
  /// Informational alert
  Info,
  /// Warning alert
  Warning,
  /// Critical alert requiring attention
  Critical,
}

/// Detailed usage and cost breakdown
#[ derive( Debug, Clone, Serialize, Deserialize, PartialEq ) ]
pub struct UsageBreakdown
{
  /// Usage by time periods
  pub time_usage : Vec< TimeUsage >,
  /// Usage by token types
  pub token_usage : Vec< TokenUsage >,
  /// Usage by model types
  pub model_usage : HashMap<  String, f64  >,
  /// Usage by operation types
  pub operation_usage : HashMap<  String, f64  >,
}

/// Time-based usage tracking
#[ derive( Debug, Clone, Serialize, Deserialize, PartialEq ) ]
pub struct TimeUsage
{
  /// Time period start (Unix timestamp)
  pub start_time : u64,
  /// Time period end (Unix timestamp)
  pub end_time : u64,
  /// Total requests in this period
  pub request_count : u64,
  /// Total cost in this period
  pub cost : f64,
}

/// Token usage tracking by type
#[ derive( Debug, Clone, Serialize, Deserialize, PartialEq ) ]
pub struct TokenUsage
{
  /// Token type (input, output, etc.)
  pub token_type : String,
  /// Total tokens used
  pub count : u64,
  /// Cost per token
  pub cost_per_token : f64,
  /// Total cost for this token type
  pub total_cost : f64,
}

/// Cost optimization settings
#[ derive( Debug, Clone, Serialize, Deserialize, PartialEq ) ]
#[ allow( clippy::struct_excessive_bools ) ]
pub struct CostOptimizationSettings
{
  /// Enable automatic cost optimization
  pub enabled : bool,
  /// Prefer cheaper models when possible
  pub prefer_cheaper_models : bool,
  /// Maximum acceptable latency increase for cost savings (in milliseconds)
  pub max_latency_increase_ms : u32,
  /// Enable request batching for cost efficiency
  pub enable_request_batching : bool,
  /// Enable response caching to reduce redundant requests
  pub enable_response_caching : bool,
}

impl Default for CostOptimizationSettings
{
  #[ inline ]
  fn default() -> Self
  {
    Self
    {
      enabled : false,
      prefer_cheaper_models : false,
      max_latency_increase_ms : 1000,
      enable_request_batching : true,
      enable_response_caching : true,
    }
  }
}

impl Default for BudgetLimits
{
  #[ inline ]
  fn default() -> Self
  {
    Self
    {
      daily_limit : None,
      monthly_limit : None,
      alert_threshold : 0.8, // Alert at 80% of limit
      enforce_hard_limit : false,
      optimization_settings : CostOptimizationSettings::default(),
    }
  }
}

impl Default for UsageBreakdown
{
  #[ inline ]
  fn default() -> Self
  {
    Self
    {
      time_usage : Vec::new(),
      token_usage : Vec::new(),
      model_usage : HashMap::new(),
      operation_usage : HashMap::new(),
    }
  }
}

impl Default for CostTracker
{
  #[ inline ]
  fn default() -> Self
  {
    Self
    {
      daily_spend : 0.0,
      monthly_spend : 0.0,
      budget_limits : BudgetLimits::default(),
      cost_alerts : Vec::new(),
      usage_breakdown : UsageBreakdown::default(),
    }
  }
}

impl CostTracker
{
  /// Create new cost tracker with default settings
  #[ must_use ]
  #[ inline ]
  pub fn new() -> Self
  {
    Self::default()
  }

  /// Create cost tracker with custom budget limits
  #[ must_use ]
  #[ inline ]
  pub fn with_limits( daily_limit : Option< f64 >, monthly_limit : Option< f64 > ) -> Self
  {
    Self
    {
      budget_limits : BudgetLimits
      {
        daily_limit,
        monthly_limit,
        ..BudgetLimits::default()
      },
      ..Self::default()
    }
  }

  /// Update spending and check for alerts
  ///
  /// # Panics
  ///
  /// Panics if the system time is before the Unix epoch when creating alert timestamps.
  #[ inline ]
  pub fn update_spending( &mut self, daily_delta : f64, monthly_delta : f64 ) -> Vec< CostAlert >
  {
    self.daily_spend += daily_delta;
    self.monthly_spend += monthly_delta;

    let mut new_alerts = Vec::new();

    // Check daily limits
    if let Some( daily_limit ) = self.budget_limits.daily_limit
    {
      let threshold = daily_limit * self.budget_limits.alert_threshold;

      if self.daily_spend >= daily_limit
      {
        new_alerts.push( CostAlert
        {
          alert_type : AlertType::DailyLimitExceeded,
          severity : AlertSeverity::Critical,
          message : format!( "Daily spending limit exceeded : ${:.2}", self.daily_spend ),
          timestamp : std::time::SystemTime::now()
            .duration_since( std::time::UNIX_EPOCH )
            .unwrap()
            .as_secs(),
          current_spend : self.daily_spend,
          limit : daily_limit,
        } );
      }
      else if self.daily_spend >= threshold
      {
        new_alerts.push( CostAlert
        {
          alert_type : AlertType::DailyLimitApproaching,
          severity : AlertSeverity::Warning,
          message : format!( "Daily spending approaching limit : ${:.2}", self.daily_spend ),
          timestamp : std::time::SystemTime::now()
            .duration_since( std::time::UNIX_EPOCH )
            .unwrap()
            .as_secs(),
          current_spend : self.daily_spend,
          limit : daily_limit,
        } );
      }
    }

    // Check monthly limits
    if let Some( monthly_limit ) = self.budget_limits.monthly_limit
    {
      let threshold = monthly_limit * self.budget_limits.alert_threshold;

      if self.monthly_spend >= monthly_limit
      {
        new_alerts.push( CostAlert
        {
          alert_type : AlertType::MonthlyLimitExceeded,
          severity : AlertSeverity::Critical,
          message : format!( "Monthly spending limit exceeded : ${:.2}", self.monthly_spend ),
          timestamp : std::time::SystemTime::now()
            .duration_since( std::time::UNIX_EPOCH )
            .unwrap()
            .as_secs(),
          current_spend : self.monthly_spend,
          limit : monthly_limit,
        } );
      }
      else if self.monthly_spend >= threshold
      {
        new_alerts.push( CostAlert
        {
          alert_type : AlertType::MonthlyLimitApproaching,
          severity : AlertSeverity::Warning,
          message : format!( "Monthly spending approaching limit : ${:.2}", self.monthly_spend ),
          timestamp : std::time::SystemTime::now()
            .duration_since( std::time::UNIX_EPOCH )
            .unwrap()
            .as_secs(),
          current_spend : self.monthly_spend,
          limit : monthly_limit,
        } );
      }
    }

    self.cost_alerts.extend( new_alerts.clone() );
    new_alerts
  }

  /// Reset daily spending (typically called at day boundary)
  #[ inline ]
  pub fn reset_daily_spending( &mut self )
  {
    self.daily_spend = 0.0;
    // Remove daily alerts
    self.cost_alerts.retain( | alert |
      !matches!( alert.alert_type, AlertType::DailyLimitApproaching | AlertType::DailyLimitExceeded )
    );
  }

  /// Reset monthly spending (typically called at month boundary)
  #[ inline ]
  pub fn reset_monthly_spending( &mut self )
  {
    self.monthly_spend = 0.0;
    // Remove monthly alerts
    self.cost_alerts.retain( | alert |
      !matches!( alert.alert_type, AlertType::MonthlyLimitApproaching | AlertType::MonthlyLimitExceeded )
    );
  }

  /// Get current cost efficiency ratio
  #[ must_use ]
  #[ inline ]
  pub fn get_cost_efficiency_ratio( &self ) -> f64
  {
    if self.usage_breakdown.time_usage.is_empty()
    {
      return 1.0;
    }

    let total_requests : u64 = self.usage_breakdown.time_usage.iter()
      .map( | usage | usage.request_count )
      .sum();

    if total_requests == 0
    {
      return 1.0;
    }

    // Calculate cost per request
    let cost_per_request = ( self.daily_spend + self.monthly_spend ) / total_requests as f64;

    // Industry baseline cost per request (example value)
    let baseline_cost_per_request = 0.01;

    baseline_cost_per_request / cost_per_request.max( 0.001 )
  }
}

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

  #[ test ]
  fn test_cost_tracker_creation()
  {
    let tracker = CostTracker::new();
    assert!( tracker.daily_spend.abs() < f64::EPSILON, "Expected daily_spend to be approximately 0.0, got {}", tracker.daily_spend );
    assert!( tracker.monthly_spend.abs() < f64::EPSILON, "Expected monthly_spend to be approximately 0.0, got {}", tracker.monthly_spend );
    assert!( tracker.cost_alerts.is_empty() );
  }

  #[ test ]
  fn test_cost_tracker_with_limits()
  {
    let tracker = CostTracker::with_limits( Some( 100.0 ), Some( 1000.0 ) );
    assert_eq!( tracker.budget_limits.daily_limit, Some( 100.0 ) );
    assert_eq!( tracker.budget_limits.monthly_limit, Some( 1000.0 ) );
  }

  #[ test ]
  fn test_spending_alerts()
  {
    let mut tracker = CostTracker::with_limits( Some( 100.0 ), Some( 1000.0 ) );

    // Test warning alert - 85% of daily limit should trigger daily warning
    // but 85.0 on monthly limit of 1000.0 (8.5%) should not trigger monthly warning
    let alerts = tracker.update_spending( 85.0, 85.0 );
    assert_eq!( alerts.len(), 1 ); // Only daily warning
    assert!( alerts.iter().any( | a | matches!( a.alert_type, AlertType::DailyLimitApproaching ) ) );

    // Test both alerts by triggering monthly as well
    let mut tracker2 = CostTracker::with_limits( Some( 100.0 ), Some( 1000.0 ) );
    let alerts2 = tracker2.update_spending( 85.0, 850.0 ); // 85% of monthly limit too
    assert_eq!( alerts2.len(), 2 ); // Both daily and monthly warnings
    assert!( alerts2.iter().any( | a | matches!( a.alert_type, AlertType::DailyLimitApproaching ) ) );
    assert!( alerts2.iter().any( | a | matches!( a.alert_type, AlertType::MonthlyLimitApproaching ) ) );

    // Test critical alert
    let alerts = tracker.update_spending( 20.0, 20.0 );
    assert!( alerts.iter().any( | a | matches!( a.alert_type, AlertType::DailyLimitExceeded ) ) );
  }

  #[ test ]
  fn test_spending_reset()
  {
    let mut tracker = CostTracker::with_limits( Some( 100.0 ), Some( 1000.0 ) );
    tracker.update_spending( 150.0, 150.0 ); // Trigger alerts

    assert!( !tracker.cost_alerts.is_empty() );

    tracker.reset_daily_spending();
    assert!( tracker.daily_spend.abs() < f64::EPSILON, "Expected daily_spend to be approximately 0.0 after reset, got {}", tracker.daily_spend );

    // Daily alerts should be removed
    assert!( !tracker.cost_alerts.iter().any( | alert |
      matches!( alert.alert_type, AlertType::DailyLimitApproaching | AlertType::DailyLimitExceeded )
    ) );
  }

  #[ test ]
  fn test_cost_efficiency_ratio()
  {
    let mut tracker = CostTracker::new();
    tracker.daily_spend = 10.0;
    tracker.usage_breakdown.time_usage.push( TimeUsage
    {
      start_time : 0,
      end_time : 100,
      request_count : 100,
      cost : 10.0,
    } );

    let efficiency = tracker.get_cost_efficiency_ratio();
    assert!( efficiency > 0.0 );
  }
}