gemini-cost 0.1.0

Calculate Google Gemini API call cost from a usage block. Cache-aware, supports Gemini 2.5 Pro/Flash/Flash-Lite and 2.0 families, BYO pricing override. No SDK dependency.
Documentation

gemini-cost

crates.io docs.rs license: MIT OR Apache-2.0

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 gemini_cost::{Usage, default_pricing};

let pricing = default_pricing("gemini-2.5-pro").unwrap();

// promptTokenCount on the wire INCLUDES cached content tokens:
let usage = Usage::from_gemini(
    /* prompt_token_count          = */ 1000,
    /* candidates_token_count      = */  500,
    /* cached_content_token_count  = */  300,
);
let cost_usd = pricing.cost_for(&usage);

Long-prompt tier

use gemini_cost::{default_pricing, Usage, LONG_PROMPT_THRESHOLD};

let p = default_pricing("gemini-2.5-pro").unwrap();
let short = p.cost_for(&Usage { input_tokens: 100_000, output_tokens: 0, cached_input_tokens: 0 });
let long  = p.cost_for(&Usage { input_tokens: 300_000, output_tokens: 0, cached_input_tokens: 0 });
assert!(long > short * 3.0); // ~6x at 3x tokens because of the tier flip
assert_eq!(LONG_PROMPT_THRESHOLD, 200_000);

Model id normalization

Pass any Vertex resource path or dated preview snapshot; the lookup strips the qualifier back to the base alias.

use gemini_cost::default_pricing;
assert!(default_pricing("projects/p/locations/us-central1/publishers/google/models/gemini-2.5-pro").is_some());
assert!(default_pricing("gemini-2.5-pro-preview-05-06").is_some());

BYO pricing

use gemini_cost::{Pricing, Usage};
let custom = Pricing::flat(0.5, 2.0, 0.05);
let _ = custom.cost_for(&Usage::default());

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 — derive Serialize/Deserialize on Usage.
[dependencies]
gemini-cost = { version = "0.1", features = ["serde"] }

License

Licensed under either of MIT or Apache-2.0 at your option.