Skip to main content

ares_store/
billing_config.rs

1use serde::{Deserialize, Serialize};
2use std::collections::HashMap;
3
4// ============= Billing Configuration =============
5
6/// Billing and estimated-cost configuration.
7#[derive(Debug, Clone, Default, Serialize, Deserialize)]
8pub struct BillingConfig {
9    /// Explicit provider/model pricing entries keyed by an operator-friendly name.
10    #[serde(default)]
11    pub model_pricing: HashMap<String, ModelPricingConfig>,
12}
13
14impl BillingConfig {
15    /// Find pricing by runtime provider/model identifiers.
16    pub fn pricing_for(
17        &self,
18        provider_name: &str,
19        model_name: &str,
20    ) -> Option<&ModelPricingConfig> {
21        let provider_key = pricing_key(provider_name);
22        let model_key = pricing_key(model_name);
23        self.model_pricing.values().find(|pricing| {
24            pricing_key(&pricing.provider) == provider_key
25                && pricing_key(&pricing.model) == model_key
26        })
27    }
28}
29
30/// Pricing for a single runtime provider/model pair.
31#[derive(Debug, Clone, Serialize, Deserialize)]
32pub struct ModelPricingConfig {
33    /// Runtime provider name, such as `openai` or `ollama-local`.
34    pub provider: String,
35    /// Runtime model identifier.
36    pub model: String,
37    /// USD per one million prompt/input tokens, if known.
38    pub input_usd_per_million_tokens: Option<f64>,
39    /// USD per one million completion/output tokens, if known.
40    pub output_usd_per_million_tokens: Option<f64>,
41    /// Currency for this estimate. Defaults to USD.
42    #[serde(default = "default_billing_currency")]
43    pub currency: String,
44}
45
46fn default_billing_currency() -> String {
47    "USD".to_string()
48}
49
50fn pricing_key(value: &str) -> String {
51    value.trim().to_ascii_lowercase()
52}