claude_cost/lib.rs
1//! # claude-cost
2//!
3//! Calculate Claude API call cost from a usage block.
4//!
5//! Anthropic returns four token counts on a Claude response:
6//! `input_tokens`, `output_tokens`, `cache_creation_input_tokens`, and
7//! `cache_read_input_tokens`. Each has its own price. This crate gives you
8//! a small `Pricing` table for popular Claude models on the Anthropic API
9//! and on AWS Bedrock, plus a `Usage` struct that knows how to compute
10//! cost from those four fields.
11//!
12//! Pricing is best-effort and dated; verify against
13//! <https://www.anthropic.com/pricing> and
14//! <https://aws.amazon.com/bedrock/pricing/> before using these numbers
15//! for billing.
16//!
17//! ## Quick example
18//!
19//! ```
20//! use claude_cost::{Usage, default_pricing};
21//!
22//! let pricing = default_pricing("claude-sonnet-4-5").unwrap();
23//! let usage = Usage {
24//! input_tokens: 1_000,
25//! output_tokens: 500,
26//! cache_creation_input_tokens: 0,
27//! cache_read_input_tokens: 0,
28//! };
29//! let cost = pricing.cost_for(&usage);
30//! assert!((cost - 0.0105).abs() < 1e-6); // 1k * $3/1M + 500 * $15/1M
31//! ```
32//!
33//! ## With a Bedrock model id
34//!
35//! ```
36//! use claude_cost::default_pricing;
37//! // The crate normalizes inference-profile prefixes (us./eu./apac.) and
38//! // arn:aws:bedrock:* IDs back to the underlying model name.
39//! assert!(default_pricing("us.anthropic.claude-sonnet-4-5").is_some());
40//! ```
41//!
42//! ## BYO pricing
43//!
44//! ```
45//! use claude_cost::{Pricing, Usage};
46//! let custom = Pricing {
47//! input_per_mtok: 1.25,
48//! output_per_mtok: 5.0,
49//! cache_read_per_mtok: 0.125,
50//! cache_write_per_mtok: 1.5625,
51//! };
52//! let usage = Usage::default();
53//! let _ = custom.cost_for(&usage);
54//! ```
55
56#![deny(missing_docs)]
57
58mod normalize;
59mod pricing;
60mod usage;
61
62pub use normalize::normalize_model_id;
63pub use pricing::{default_pricing, Pricing, DEFAULT_PRICING_TABLE};
64pub use usage::Usage;