Skip to main content

cost_usd

Function cost_usd 

Source
pub fn cost_usd(
    model: &str,
    prompt_tokens: u32,
    completion_tokens: u32,
    cache_read: u32,
    cache_creation: u32,
) -> f64
Expand description

Compute USD cost for a single LLM call given the model name and token counts. Unknown models fall back to Sonnet-class pricing.

Why: (#47) We hard-code the pricing table rather than hit a live endpoint so offline/CI runs still produce comparable cost figures. Pricing is per-million-tokens as published by Anthropic and OpenRouter. What: Substring-matches the model string (e.g. “anthropic/claude-sonnet-4-5” or “claude-haiku-4”) and multiplies each token bucket by its rate. Test: cost_usd_known_model, cost_usd_unknown_defaults_to_sonnet.