gemini_cost/lib.rs
1//! # gemini-cost
2//!
3//! Calculate Google Gemini API call cost from a usage block.
4//!
5//! Gemini's `usageMetadata` block reports `promptTokenCount`,
6//! `candidatesTokenCount`, and `cachedContentTokenCount`. Some models
7//! charge a different rate when `promptTokenCount > 200_000`. This crate
8//! gives you a small `Pricing` table for popular Gemini models plus a
9//! `Usage` struct that knows how to compute cost from those fields.
10//!
11//! Pricing is best-effort and dated; verify against
12//! <https://ai.google.dev/gemini-api/docs/pricing> before using these
13//! numbers for billing.
14//!
15//! ## Quick example
16//!
17//! ```
18//! use gemini_cost::{Usage, default_pricing};
19//!
20//! let pricing = default_pricing("gemini-2.5-pro").unwrap();
21//! let usage = Usage {
22//! input_tokens: 1_000,
23//! output_tokens: 500,
24//! cached_input_tokens: 0,
25//! };
26//! let cost = pricing.cost_for(&usage);
27//! assert!(cost > 0.0);
28//! ```
29//!
30//! ## Long-prompt tiers
31//!
32//! Gemini 2.5 Pro switches to a higher rate when `input_tokens` exceeds
33//! 200,000. The pricing table carries both tiers; `cost_for` picks the
34//! right one automatically.
35//!
36//! ```
37//! use gemini_cost::{Pricing, Usage, default_pricing};
38//! let p = default_pricing("gemini-2.5-pro").unwrap();
39//! let short = p.cost_for(&Usage { input_tokens: 100_000, output_tokens: 0, cached_input_tokens: 0 });
40//! let long = p.cost_for(&Usage { input_tokens: 300_000, output_tokens: 0, cached_input_tokens: 0 });
41//! assert!(long > short * 3.0); // long-tier is more than the 3x scale alone
42//! ```
43//!
44//! ## BYO pricing
45//!
46//! ```
47//! use gemini_cost::{Pricing, Usage};
48//! let custom = Pricing::flat(0.5, 2.0, 0.05);
49//! let _ = custom.cost_for(&Usage::default());
50//! ```
51
52#![deny(missing_docs)]
53
54mod normalize;
55mod pricing;
56mod usage;
57
58pub use normalize::normalize_model_id;
59pub use pricing::{default_pricing, Pricing, LONG_PROMPT_THRESHOLD, DEFAULT_PRICING_TABLE};
60pub use usage::Usage;