modelbridge 0.5.2

One provider-neutral Rust client for OpenAI, Anthropic, Gemini, Groq, Ollama and friends: streaming, tool calling, an agent loop, usage accounting, and subscription sign-in
Documentation
//! Token and cost accounting.
//!
//! Totals are lock-free atomics because every worker thread touches them; the
//! per-model breakdown sits behind a mutex, locked once per completed call
//! rather than per token. A poisoned lock costs the breakdown, never the
//! totals and never the request.

use std::collections::HashMap;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Mutex;

use crate::catalog;

/// What a client has spent, in tokens and in millionths of a dollar.
///
/// `cost_micros` is what the provider actually charged, when it says.
/// `est_micros` is this crate's estimate from catalog prices; `unpriced_calls`
/// counts the calls no price was known for, so an interface can say "at least"
/// instead of quietly under-reporting.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Usage {
    pub input: u64,
    pub output: u64,
    pub calls: u64,
    pub cost_micros: u64,
    pub est_micros: u64,
    pub unpriced_calls: u64,
}

impl Usage {
    pub fn total(&self) -> u64 {
        self.input + self.output
    }

    /// The best available cost figure: what was charged, else what was
    /// estimated.
    pub fn micros(&self) -> u64 {
        if self.cost_micros > 0 {
            self.cost_micros
        } else {
            self.est_micros
        }
    }

    /// The same figure in dollars, for display.
    pub fn dollars(&self) -> f64 {
        self.micros() as f64 / 1_000_000.0
    }

    /// Whether any call ran on a model with no known price, which makes
    /// [`Usage::micros`] a floor rather than a total.
    pub fn incomplete(&self) -> bool {
        self.cost_micros == 0 && self.unpriced_calls > 0
    }
}

impl std::ops::Add for Usage {
    type Output = Usage;

    fn add(self, other: Usage) -> Usage {
        Usage {
            input: self.input + other.input,
            output: self.output + other.output,
            calls: self.calls + other.calls,
            cost_micros: self.cost_micros + other.cost_micros,
            est_micros: self.est_micros + other.est_micros,
            unpriced_calls: self.unpriced_calls + other.unpriced_calls,
        }
    }
}

/// Cost in millionths of a dollar for a catalog model, and whether a price was
/// actually known.
pub(crate) fn estimate(model: &str, input: u32, output: u32) -> (u64, bool) {
    let rates = catalog::model(model)
        .and_then(|spec| spec.input.zip(spec.output))
        // Routers namespace the same model as `openai/gpt-...`; price the bare
        // id rather than pretending the rate is unknown.
        .or_else(|| {
            let (_, bare) = model.rsplit_once('/')?;
            catalog::model(bare).and_then(|spec| spec.input.zip(spec.output))
        });
    match rates {
        Some((input_rate, output_rate)) => (
            (input as f64 * input_rate as f64 + output as f64 * output_rate as f64).round() as u64,
            true,
        ),
        None => (0, false),
    }
}

#[derive(Debug, Default)]
pub(crate) struct Meter {
    input: AtomicU64,
    output: AtomicU64,
    calls: AtomicU64,
    cost: AtomicU64,
    estimate: AtomicU64,
    unpriced: AtomicU64,
    /// Keyed `provider/model`: the same model on two routers stays two rows,
    /// because they bill differently.
    by_model: Mutex<HashMap<String, Usage>>,
}

impl Meter {
    /// `key` is how the call is filed (`provider/model`); `model` is the id the
    /// price is looked up under.
    pub(crate) fn record(&self, key: &str, model: &str, input: u32, output: u32, cost: u64) {
        let (estimated, priced) = estimate(model, input, output);
        let unpriced = !priced && input + output > 0;

        self.input.fetch_add(input as u64, Ordering::Relaxed);
        self.output.fetch_add(output as u64, Ordering::Relaxed);
        self.calls.fetch_add(1, Ordering::Relaxed);
        self.cost.fetch_add(cost, Ordering::Relaxed);
        self.estimate.fetch_add(estimated, Ordering::Relaxed);
        if unpriced {
            self.unpriced.fetch_add(1, Ordering::Relaxed);
        }

        if let Ok(mut by_model) = self.by_model.lock() {
            let usage = by_model.entry(key.to_string()).or_default();
            usage.input += input as u64;
            usage.output += output as u64;
            usage.calls += 1;
            usage.cost_micros += cost;
            usage.est_micros += estimated;
            usage.unpriced_calls += u64::from(unpriced);
        }
    }

    pub(crate) fn snapshot(&self) -> Usage {
        Usage {
            input: self.input.load(Ordering::Relaxed),
            output: self.output.load(Ordering::Relaxed),
            calls: self.calls.load(Ordering::Relaxed),
            cost_micros: self.cost.load(Ordering::Relaxed),
            est_micros: self.estimate.load(Ordering::Relaxed),
            unpriced_calls: self.unpriced.load(Ordering::Relaxed),
        }
    }

    pub(crate) fn by_model(&self) -> HashMap<String, Usage> {
        self.by_model
            .lock()
            .map(|by_model| by_model.clone())
            .unwrap_or_default()
    }
}

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

    #[test]
    fn a_known_model_is_priced_per_million_tokens() {
        // claude-sonnet-5 is $3/M in and $15/M out.
        let (micros, priced) = estimate("claude-sonnet-5", 1_000_000, 0);
        assert!(priced);
        assert_eq!(micros, 3_000_000);
        assert_eq!(estimate("claude-sonnet-5", 0, 1_000_000).0, 15_000_000);
    }

    #[test]
    fn a_namespaced_model_falls_back_to_the_bare_id() {
        let (micros, priced) = estimate("somerouter/claude-sonnet-5", 1_000_000, 0);
        assert!(priced);
        assert_eq!(micros, 3_000_000);
    }

    #[test]
    fn an_unknown_model_is_counted_but_not_priced() {
        assert_eq!(estimate("released-tomorrow", 100, 100), (0, false));

        let meter = Meter::default();
        meter.record("openai/released-tomorrow", "released-tomorrow", 100, 100, 0);
        let usage = meter.snapshot();
        assert_eq!(usage.total(), 200);
        assert_eq!(usage.unpriced_calls, 1);
        assert!(usage.incomplete());
    }

    #[test]
    fn a_reported_charge_wins_over_the_estimate() {
        let meter = Meter::default();
        meter.record(
            "openai/claude-sonnet-5",
            "claude-sonnet-5",
            1_000_000,
            0,
            42,
        );
        let usage = meter.snapshot();
        assert_eq!(usage.cost_micros, 42);
        assert_eq!(usage.est_micros, 3_000_000);
        assert_eq!(usage.micros(), 42);
        assert!(!usage.incomplete());
    }

    #[test]
    fn the_breakdown_keeps_routers_apart() {
        let meter = Meter::default();
        meter.record("openai/gpt-5.6-terra", "gpt-5.6-terra", 10, 10, 0);
        meter.record("openrouter/gpt-5.6-terra", "gpt-5.6-terra", 10, 10, 0);
        let by_model = meter.by_model();
        assert_eq!(by_model.len(), 2);
        assert_eq!(meter.snapshot().calls, 2);
    }
}