use std::collections::HashMap;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Mutex;
use crate::catalog;
#[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
}
pub fn micros(&self) -> u64 {
if self.cost_micros > 0 {
self.cost_micros
} else {
self.est_micros
}
}
pub fn dollars(&self) -> f64 {
self.micros() as f64 / 1_000_000.0
}
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,
}
}
}
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))
.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,
by_model: Mutex<HashMap<String, Usage>>,
}
impl Meter {
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() {
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);
}
}