gemini-cost
Calculate Google Gemini API call cost from a usage block. Cache-aware, two-tier pricing for >200k-token prompts, supports the Gemini 2.5 and 2.0 families. Bring your own pricing override. Zero runtime dependencies.
Why
Gemini's billing has a wrinkle: 2.5 Pro charges roughly 2x when the prompt exceeds 200k tokens. Every usage logger I've seen gets this wrong or doesn't model it at all. This crate models the two tiers and picks the right one automatically.
Usage
use ;
let pricing = default_pricing.unwrap;
// promptTokenCount on the wire INCLUDES cached content tokens:
let usage = from_gemini;
let cost_usd = pricing.cost_for;
Long-prompt tier
use ;
let p = default_pricing.unwrap;
let short = p.cost_for;
let long = p.cost_for;
assert!; // ~6x at 3x tokens because of the tier flip
assert_eq!;
Model id normalization
Pass any Vertex resource path or dated preview snapshot; the lookup strips the qualifier back to the base alias.
use default_pricing;
assert!;
assert!;
BYO pricing
use ;
let custom = flat;
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://ai.google.dev/gemini-api/docs/pricing before billing.
Features
serde— deriveSerialize/DeserializeonUsage.
[]
= { = "0.1", = ["serde"] }
License
Licensed under either of MIT or Apache-2.0 at your option.