Skip to main content

Crate claude_cost

Crate claude_cost 

Source
Expand description

§claude-cost

Calculate Claude API call cost from a usage block.

Anthropic returns four token counts on a Claude response: input_tokens, output_tokens, cache_creation_input_tokens, and cache_read_input_tokens. Each has its own price. This crate gives you a small Pricing table for popular Claude models on the Anthropic API and on AWS Bedrock, plus a Usage struct that knows how to compute cost from those four fields.

Pricing is best-effort and dated; verify against https://www.anthropic.com/pricing and https://aws.amazon.com/bedrock/pricing/ before using these numbers for billing.

§Quick example

use claude_cost::{Usage, default_pricing};

let pricing = default_pricing("claude-sonnet-4-5").unwrap();
let usage = Usage {
    input_tokens: 1_000,
    output_tokens: 500,
    cache_creation_input_tokens: 0,
    cache_read_input_tokens: 0,
};
let cost = pricing.cost_for(&usage);
assert!((cost - 0.0105).abs() < 1e-6); // 1k * $3/1M + 500 * $15/1M

§With a Bedrock model id

use claude_cost::default_pricing;
// The crate normalizes inference-profile prefixes (us./eu./apac.) and
// arn:aws:bedrock:* IDs back to the underlying model name.
assert!(default_pricing("us.anthropic.claude-sonnet-4-5").is_some());

§BYO pricing

use claude_cost::{Pricing, Usage};
let custom = Pricing {
    input_per_mtok: 1.25,
    output_per_mtok: 5.0,
    cache_read_per_mtok: 0.125,
    cache_write_per_mtok: 1.5625,
};
let usage = Usage::default();
let _ = custom.cost_for(&usage);

Structs§

Pricing
Per-model rates, USD per 1M tokens.
Usage
Four-field token usage as returned by Anthropic API and Bedrock Converse.

Constants§

DEFAULT_PRICING_TABLE
Built-in pricing table. Source: anthropic.com/pricing and aws.amazon.com/bedrock/pricing as of 2026-Q2. VERIFY before billing.

Functions§

default_pricing
Look up pricing for a model id. Accepts Anthropic API names, Bedrock model ids (with or without version suffix), inference-profile prefixes, and ARNs. Returns None for unknown models.
normalize_model_id
Strip ARN, inference-profile, version, and anthropic. prefixes.