openai-cost
Calculate OpenAI API call cost from a usage block. Cache-aware (cached input tokens), supports GPT-5, GPT-4.1, o3, o4 model families. Bring your own pricing override. Zero runtime dependencies.
Why
Every usage logger ships its own copy of this. They get the cache math wrong, or they hard-code yesterday's pricing, or they break when OpenAI adds a new model. This crate is the small, dated, tested version you can reuse and override where you need to.
Usage
use ;
let pricing = default_pricing.unwrap;
// From a Chat Completions response (prompt_tokens INCLUDES cached):
let usage = from_chat_completions;
let cost_usd = pricing.cost_for;
Or from the Responses API (already split):
use Usage;
let usage = from_responses_api;
Dated snapshots
Pass any dated snapshot id; the lookup strips the -YYYY-MM-DD suffix
back to the base alias.
use default_pricing;
assert!;
assert!;
BYO pricing
use ;
let custom = Pricing ;
let _ = custom.cost_for;
Pricing notes
All rates are USD per 1,000,000 tokens. Pricing is dated as of 2026-Q2. Verify against https://openai.com/api/pricing/ before billing.
Features
serde— deriveSerialize/DeserializeonUsageso you can parse a raw API response directly.
[]
= { = "0.1", = ["serde"] }
License
Licensed under either of MIT or Apache-2.0 at your option.