# claude-cost
[](https://crates.io/crates/claude-cost)
[](https://docs.rs/claude-cost)
[](https://github.com/MukundaKatta/claude-cost/actions/workflows/ci.yml)
[](https://crates.io/crates/claude-cost)
**Calculate Claude API call cost from a usage block.**
Cache-aware (`cache_creation_input_tokens`, `cache_read_input_tokens`),
works for both Anthropic's first-party API and AWS Bedrock model IDs
(including ARNs, version suffixes, and cross-region inference profiles).
Bring-your-own pricing override. No SDK dependency.
## Install
```toml
[dependencies]
claude-cost = "0.1"
# optional: deserialize Usage from Anthropic's JSON response shape
claude-cost = { version = "0.1", features = ["serde"] }
```
## Use
```rust
use claude_cost::{Usage, default_pricing};
let pricing = default_pricing("claude-sonnet-4-5").unwrap();
let usage = Usage {
input_tokens: 423,
output_tokens: 18,
cache_creation_input_tokens: 0,
cache_read_input_tokens: 380,
};
let cost = pricing.cost_for(&usage);
// cost = 0.001653 USD
```
Works the same with Bedrock model identifiers:
```rust
use claude_cost::default_pricing;
assert!(default_pricing("anthropic.claude-sonnet-4-5-v1:0").is_some());
assert!(default_pricing("us.anthropic.claude-sonnet-4-5").is_some());
let arn = "arn:aws:bedrock:us-east-1::foundation-model/anthropic.claude-opus-4-7";
assert!(default_pricing(arn).is_some());
```
Bring your own pricing for unsupported models:
```rust
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 { input_tokens: 1_000_000, ..Usage::default() };
let cost = custom.cost_for(&usage); // = 1.25
```
## What it does NOT do
- No HTTP client. This crate doesn't talk to Anthropic or Bedrock.
- No usage tracking / aggregation. Wrap it yourself if you want a ledger.
- No prompt caching logic. Just cost math from the usage numbers Anthropic
returns.
- No tokenizer. Pass the token counts the API gave you.
## Pricing source
Built-in rates reflect anthropic.com/pricing and aws.amazon.com/bedrock/pricing
as of 2026-Q2. Verify before using these for billing — rates change.
Sibling to [`bedrock-kit`](https://github.com/MukundaKatta/bedrock-kit)
on the Python side, which embeds the same logic in a full Bedrock client.
## License
MIT OR Apache-2.0