Expand description
USD cost estimation for token usage (E3, v0.22.1 §S6).
LLM providers bill per token, at rates that differ by model and by input-vs-output.
This module turns a (prompt_tokens, completion_tokens, model) triple into a USD
estimate, so traces and cost dashboards can answer “what did this agent run cost?”
without hardcoding floats in every caller.
Rates are a static snapshot of the listed model prices (per 1M tokens) and are
deliberately approximate — list price, not the effective price after tiered volume
discounts or other negotiated terms. Unknown models yield None rather than a guess
(an invented rate is worse than no rate).
§Design
price_for matches by model-name substring, longest/most-specific first, so
claude-3-5-sonnet resolves to its own rate instead of the claude-3-sonnet catch.
The table lives as an ordered slice so a single find returns the right entry.
Structs§
- Model
Price - USD price of a model, per 1M tokens.
Functions§
- estimate_
cost_ usd - Estimates the USD cost of a call from its token counts and model name.
- price_
for - Looks up the list price for a model name by most-specific substring match.