Skip to main content

Crate cost_meter

Crate cost_meter 

Source
Expand description

§cost-meter

Aggregate LLM API cost across providers, models, and time windows.

Provider-agnostic: you compute the per-call cost with whatever pricing crate you like (claude-cost, openai-cost, gemini-cost, bedrock-cost, or BYO) and feed (provider, model, tokens, cost) into the meter. The meter keeps running totals broken down by provider and model, and exposes them as a sorted snapshot.

§Example

use cost_meter::{Meter, Call};

let mut meter = Meter::new();
meter.record(Call {
    provider: "anthropic",
    model: "claude-sonnet-4-5",
    input_tokens: 1_000,
    output_tokens: 500,
    cost_usd: 0.0105,
});
meter.record(Call {
    provider: "openai",
    model: "gpt-5",
    input_tokens: 2_000,
    output_tokens: 800,
    cost_usd: 0.0105,
});

let snap = meter.snapshot();
assert_eq!(snap.total_calls, 2);
assert!((snap.total_cost_usd - 0.021).abs() < 1e-9);

// Per-provider breakdown is sorted by cost desc.
let by_provider = meter.by_provider();
assert_eq!(by_provider.len(), 2);

Structs§

Bucket
Aggregated counters for a single (provider, model) pair.
Call
A single LLM call to record against the meter.
Meter
Cost aggregator. Cheap to construct; cheap to record.
Snapshot
Top-level snapshot of running totals.