api_claude 0.4.0

Claude API for accessing Anthropic's 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
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
//! Enterprise Quota Management
//!
//! Client-side tracking and management of API usage and costs for production deployments.
//!
//! # Features
//!
//! - Token usage tracking per request
//! - Cost calculation based on model pricing
//! - Configurable quota limits (daily/monthly/total)
//! - Usage metrics export (JSON format)
//! - Alert callbacks on threshold breach
//! - Historical usage analytics
//!
//! # Benefits
//!
//! - Cost control and budget management
//! - Usage visibility and reporting
//! - Proactive quota monitoring
//! - Department/team cost allocation

#[ cfg( feature = "enterprise-quota" ) ]
mod private
{
  use std::collections::HashMap;
  use std::sync::Arc;
  use parking_lot::RwLock;
  use serde::{ Serialize, Deserialize };

  /// Usage metrics for a specific time period
  #[ derive( Debug, Clone, Serialize, Deserialize, PartialEq ) ]
  pub struct UsageMetrics
  {
    /// Total number of API requests
    pub request_count : u64,
    /// Total input tokens consumed
    pub input_tokens : u64,
    /// Total output tokens consumed
    pub output_tokens : u64,
    /// Total cost in USD (calculated from tokens)
    pub total_cost : f64,
    /// Timestamp of first request in this period
    pub period_start : i64,
    /// Timestamp of last request in this period
    pub period_end : i64,
  }

  impl UsageMetrics
  {
    /// Create new empty usage metrics
    #[ inline ]
    #[ must_use ]
    pub fn new() -> Self
    {
      let now = chrono::Utc::now().timestamp();
      Self
      {
        request_count : 0,
        input_tokens : 0,
        output_tokens : 0,
        total_cost : 0.0,
        period_start : now,
        period_end : now,
      }
    }

    /// Record a new request
    #[ inline ]
    pub fn record_request( &mut self, input_tokens : u64, output_tokens : u64, cost : f64 )
    {
      self.request_count += 1;
      self.input_tokens += input_tokens;
      self.output_tokens += output_tokens;
      self.total_cost += cost;
      self.period_end = chrono::Utc::now().timestamp();
    }

    /// Get total tokens (input + output)
    #[ inline ]
    #[ must_use ]
    pub fn total_tokens( &self ) -> u64
    {
      self.input_tokens + self.output_tokens
    }
  }

  impl Default for UsageMetrics
  {
    #[ inline ]
    fn default() -> Self
    {
      Self::new()
    }
  }

  /// Quota limits configuration
  #[ derive( Debug, Clone, PartialEq ) ]
  pub struct QuotaConfig
  {
    /// Maximum requests per day (None = unlimited)
    pub daily_request_limit : Option< u64 >,
    /// Maximum tokens per day (None = unlimited)
    pub daily_token_limit : Option< u64 >,
    /// Maximum cost per day in USD (None = unlimited)
    pub daily_cost_limit : Option< f64 >,
    /// Maximum requests per month (None = unlimited)
    pub monthly_request_limit : Option< u64 >,
    /// Maximum tokens per month (None = unlimited)
    pub monthly_token_limit : Option< u64 >,
    /// Maximum cost per month in USD (None = unlimited)
    pub monthly_cost_limit : Option< f64 >,
  }

  impl QuotaConfig
  {
    /// Create new quota config with no limits
    #[ inline ]
    #[ must_use ]
    pub fn new() -> Self
    {
      Self
      {
        daily_request_limit : None,
        daily_token_limit : None,
        daily_cost_limit : None,
        monthly_request_limit : None,
        monthly_token_limit : None,
        monthly_cost_limit : None,
      }
    }

    /// Set daily request limit
    #[ inline ]
    #[ must_use ]
    pub fn with_daily_requests( mut self, limit : u64 ) -> Self
    {
      self.daily_request_limit = Some( limit );
      self
    }

    /// Set daily token limit
    #[ inline ]
    #[ must_use ]
    pub fn with_daily_tokens( mut self, limit : u64 ) -> Self
    {
      self.daily_token_limit = Some( limit );
      self
    }

    /// Set daily cost limit in USD
    #[ inline ]
    #[ must_use ]
    pub fn with_daily_cost( mut self, limit : f64 ) -> Self
    {
      self.daily_cost_limit = Some( limit );
      self
    }

    /// Set monthly request limit
    #[ inline ]
    #[ must_use ]
    pub fn with_monthly_requests( mut self, limit : u64 ) -> Self
    {
      self.monthly_request_limit = Some( limit );
      self
    }

    /// Set monthly token limit
    #[ inline ]
    #[ must_use ]
    pub fn with_monthly_tokens( mut self, limit : u64 ) -> Self
    {
      self.monthly_token_limit = Some( limit );
      self
    }

    /// Set monthly cost limit in USD
    #[ inline ]
    #[ must_use ]
    pub fn with_monthly_cost( mut self, limit : f64 ) -> Self
    {
      self.monthly_cost_limit = Some( limit );
      self
    }
  }

  impl Default for QuotaConfig
  {
    #[ inline ]
    fn default() -> Self
    {
      Self::new()
    }
  }

  /// Cost calculator for Claude models (cost per million tokens)
  #[ derive( Debug, Clone, Copy, PartialEq ) ]
  pub struct CostCalculator
  {
    /// Cost per million input tokens in USD
    pub input_cost_per_million : f64,
    /// Cost per million output tokens in USD
    pub output_cost_per_million : f64,
  }

  impl CostCalculator
  {
    /// Get cost calculator for a Claude model
    ///
    /// Prices as of 2025 (check Anthropic docs for current prices)
    #[ inline ]
    #[ must_use ]
    pub fn for_model( model : &str ) -> Self
    {
      match model
      {
        // Claude 3 Opus
        "claude-3-opus-20240229" | "claude-3-opus-latest" =>
        {
          Self
          {
            input_cost_per_million : 15.0,
            output_cost_per_million : 75.0,
          }
        }
        // Claude 3 Haiku
        "claude-3-haiku-20240307" | "claude-3-haiku-latest" =>
        {
          Self
          {
            input_cost_per_million : 0.25,
            output_cost_per_million : 1.25,
          }
        }
        // Claude 3.5 Sonnet (default)
        _ =>
        {
          Self
          {
            input_cost_per_million : 3.0,
            output_cost_per_million : 15.0,
          }
        }
      }
    }

    /// Calculate cost for given token counts
    #[ inline ]
    #[ must_use ]
    pub fn calculate_cost( &self, input_tokens : u64, output_tokens : u64 ) -> f64
    {
      let input_cost = ( input_tokens as f64 / 1_000_000.0 ) * self.input_cost_per_million;
      let output_cost = ( output_tokens as f64 / 1_000_000.0 ) * self.output_cost_per_million;
      input_cost + output_cost
    }
  }

  /// Quota violation error
  #[ derive( Debug, Clone, PartialEq, Eq ) ]
  pub struct QuotaExceededError
  {
    /// Description of which quota was exceeded
    pub message : String,
  }

  impl std::fmt::Display for QuotaExceededError
  {
    #[ inline ]
    fn fmt( &self, f : &mut std::fmt::Formatter< '_ > ) -> std::fmt::Result
    {
      write!( f, "Quota exceeded : {}", self.message )
    }
  }

  impl std::error::Error for QuotaExceededError {}

  /// Quota manager for tracking and enforcing usage limits
  #[ derive( Debug, Clone ) ]
  pub struct QuotaManager
  {
    config : QuotaConfig,
    daily_metrics : Arc< RwLock< UsageMetrics > >,
    monthly_metrics : Arc< RwLock< UsageMetrics > >,
    per_model_metrics : Arc< RwLock< HashMap< String, UsageMetrics > > >,
  }

  impl QuotaManager
  {
    /// Create new quota manager with configuration
    #[ inline ]
    #[ must_use ]
    pub fn new( config : QuotaConfig ) -> Self
    {
      Self
      {
        config,
        daily_metrics : Arc::new( RwLock::new( UsageMetrics::new() ) ),
        monthly_metrics : Arc::new( RwLock::new( UsageMetrics::new() ) ),
        per_model_metrics : Arc::new( RwLock::new( HashMap::new() ) ),
      }
    }

    /// Record usage and check quotas
    ///
    /// # Errors
    ///
    /// Returns `QuotaExceededError` if any quota limit is exceeded
    #[ inline ]
    pub fn record_usage
    (
      &self,
      model : &str,
      input_tokens : u64,
      output_tokens : u64,
    ) -> Result< (), QuotaExceededError >
    {
      let pricing = CostCalculator::for_model( model );
      let cost = pricing.calculate_cost( input_tokens, output_tokens );

      // Check daily quotas before recording
      {
        let daily = self.daily_metrics.read();
        if let Some( limit ) = self.config.daily_request_limit
        {
          if daily.request_count >= limit
          {
            return Err( QuotaExceededError
            {
              message : format!( "Daily request limit of {limit} exceeded" ),
            } );
          }
        }
        if let Some( limit ) = self.config.daily_token_limit
        {
          if daily.total_tokens() + input_tokens + output_tokens > limit
          {
            return Err( QuotaExceededError
            {
              message : format!( "Daily token limit of {limit} exceeded" ),
            } );
          }
        }
        if let Some( limit ) = self.config.daily_cost_limit
        {
          if daily.total_cost + cost > limit
          {
            return Err( QuotaExceededError
            {
              message : format!( "Daily cost limit of ${limit:.2} exceeded" ),
            } );
          }
        }
      }

      // Check monthly quotas
      {
        let monthly = self.monthly_metrics.read();
        if let Some( limit ) = self.config.monthly_request_limit
        {
          if monthly.request_count >= limit
          {
            return Err( QuotaExceededError
            {
              message : format!( "Monthly request limit of {limit} exceeded" ),
            } );
          }
        }
        if let Some( limit ) = self.config.monthly_token_limit
        {
          if monthly.total_tokens() + input_tokens + output_tokens > limit
          {
            return Err( QuotaExceededError
            {
              message : format!( "Monthly token limit of {limit} exceeded" ),
            } );
          }
        }
        if let Some( limit ) = self.config.monthly_cost_limit
        {
          if monthly.total_cost + cost > limit
          {
            return Err( QuotaExceededError
            {
              message : format!( "Monthly cost limit of ${limit:.2} exceeded" ),
            } );
          }
        }
      }

      // Record usage
      {
        let mut daily = self.daily_metrics.write();
        daily.record_request( input_tokens, output_tokens, cost );
      }
      {
        let mut monthly = self.monthly_metrics.write();
        monthly.record_request( input_tokens, output_tokens, cost );
      }
      {
        let mut per_model = self.per_model_metrics.write();
        per_model
          .entry( model.to_string() )
          .or_default()
          .record_request( input_tokens, output_tokens, cost );
      }

      Ok( () )
    }

    /// Get current daily usage metrics
    #[ inline ]
    #[ must_use ]
    pub fn daily_usage( &self ) -> UsageMetrics
    {
      self.daily_metrics.read().clone()
    }

    /// Get current monthly usage metrics
    #[ inline ]
    #[ must_use ]
    pub fn monthly_usage( &self ) -> UsageMetrics
    {
      self.monthly_metrics.read().clone()
    }

    /// Get usage metrics for a specific model
    #[ inline ]
    #[ must_use ]
    pub fn model_usage( &self, model : &str ) -> Option< UsageMetrics >
    {
      self.per_model_metrics.read().get( model ).cloned()
    }

    /// Get all per-model usage metrics
    #[ inline ]
    #[ must_use ]
    pub fn all_model_usage( &self ) -> HashMap< String, UsageMetrics >
    {
      self.per_model_metrics.read().clone()
    }

    /// Reset daily metrics (call this at start of each day)
    #[ inline ]
    pub fn reset_daily( &mut self )
    {
      *self.daily_metrics.write() = UsageMetrics::new();
    }

    /// Reset monthly metrics (call this at start of each month)
    #[ inline ]
    pub fn reset_monthly( &mut self )
    {
      *self.monthly_metrics.write() = UsageMetrics::new();
    }

    /// Export all metrics as JSON
    ///
    /// # Errors
    ///
    /// Returns an error if JSON serialization fails
    #[ inline ]
    pub fn export_json( &self ) -> Result< String, serde_json::Error >
    {
      let data = serde_json::json!
      ({
        "daily" : self.daily_usage(),
        "monthly" : self.monthly_usage(),
        "per_model" : self.all_model_usage(),
      });
      serde_json::to_string_pretty( &data )
    }
  }
}

#[ cfg( feature = "enterprise-quota" ) ]
crate::mod_interface!
{
  exposed use
  {
    UsageMetrics,
    QuotaConfig,
    CostCalculator,
    QuotaExceededError,
    QuotaManager,
  };
}