Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ toml = "1.0"
chrono = "0.4"
dirs = "6.0"
regex = "1.0"
llm-prices = "1.8"
unicode-width = "0.2"
unicode-segmentation = "1.12"
ratatui = { version = "0.30", default-features = false, features = ["crossterm", "layout-cache"] }
Expand Down
14 changes: 7 additions & 7 deletions src/metrics/projection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,12 +264,12 @@ fn cost_today(ctx: &Ctx, _: &Options) -> Option<Reading> {
pub(super) fn rewarm_cost(ctx: &Ctx) -> Option<(f64, u64)> {
let pc = ctx.input.prompt_cache.as_ref()?;
let tokens = pc.recache_tokens_if_cold.filter(|t| *t > 0)?;
let price = builtin::input_price(ctx.input.model_id()?)?;
let write = match pc.ttl.as_deref() {
Some("1h") => 2.0,
_ => 1.25,
let ttl = match pc.ttl.as_deref() {
Some("1h") => "1h",
_ => "5m",
};
Some((tokens as f64 * price * write / 1_000_000.0, tokens))
let usd = builtin::cache_write_cost(ctx.input.model_id()?, ttl, tokens)?;
Some((usd, tokens))
}

fn cold_cost(ctx: &Ctx, _: &Options) -> Option<Reading> {
Expand Down Expand Up @@ -463,9 +463,9 @@ mod tests {
// 182k tokens of Opus 4.8 at $5/M, written with the 5-minute TTL.
let r = reading("cache.cold_cost", "near-full").unwrap();
assert_eq!(field(&r, "usd"), "$1.14");
// 270k tokens of Opus 5.5 written with the 1-hour TTL.
// 270k tokens of Opus 5.5 written with the 1-hour TTL, at $8/M.
let big = reading("cache.cold_cost", "big-context").unwrap();
assert_eq!(field(&big, "usd"), "$2.70");
assert_eq!(field(&big, "usd"), "$2.16");
assert!(reading("cache.cold_cost", "third-party").is_none());
}

Expand Down
58 changes: 56 additions & 2 deletions src/models/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,46 @@ pub fn claude_name(id: &str) -> Option<String> {
})
}

/// The model's row in `llm_prices` (Anthropic's list prices, each row dated and
/// linked to the pricing page). Claude Code's context-window suffix (`[1m]`) is
/// not part of the API id, so it is dropped before the lookup.
fn list_price(id: &str) -> Option<&'static llm_prices::Price> {
let id = id.split('[').next().unwrap_or(id).trim();
llm_prices::lookup(id)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · New Claude costs use old rates

When the model ID uses a provider-qualified spelling that llm_prices::lookup does not recognize, list_price strips only the bracket suffix and passes the provider-qualified ID to llm_prices::lookup, which misses the canonical row; input_price then falls back to the family table, so newer Claude cache-write and cold-cache estimates use the old rate.

Normalize provider-qualified IDs to their canonical Claude IDs before calling llm_prices::lookup.

Prompt for AI agents
In `src/models/builtin.rs` at line 95:

**New Claude costs use old rates**

When the model ID uses a provider-qualified spelling that `llm_prices::lookup` does not recognize, `list_price` strips only the bracket suffix and passes the provider-qualified ID to `llm_prices::lookup`, which misses the canonical row; `input_price` then falls back to the family table, so newer Claude cache-write and cold-cache estimates use the old rate.

Normalize provider-qualified IDs to their canonical Claude IDs before calling `llm_prices::lookup`.

}

/// Cost in USD of writing `tokens` to the prompt cache with the given TTL
/// (`"5m"` or `"1h"`). Uses the model's own cache-write price, including its
/// long-context rate when the write crosses the threshold; for models
/// `llm_prices` does not know, 1.25× (5 minutes) or 2× (1 hour) the input price.
pub fn cache_write_cost(id: &str, ttl: &str, tokens: u64) -> Option<f64> {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Invalid TTL silently uses five minutes

When a caller passes a TTL other than "1h" or "5m", cache_write_cost treats every value other than "1h" as "5m" and returns a price, so a mistyped or unsupported TTL gives the caller a plausible but incorrect cost instead of signaling invalid input.

Represent TTL as an enum (or reject unrecognized strings) so unsupported values cannot be priced as 5-minute writes.

Prompt for AI agents
In `src/models/builtin.rs` at line 102:

**Invalid TTL silently uses five minutes**

When a caller passes a TTL other than `"1h"` or `"5m"`, `cache_write_cost` treats every value other than `"1h"` as `"5m"` and returns a price, so a mistyped or unsupported TTL gives the caller a plausible but incorrect cost instead of signaling invalid input.

Represent TTL as an enum (or reject unrecognized strings) so unsupported values cannot be priced as 5-minute writes.

if let Some(p) = list_price(id) {
let usage = if ttl == "1h" {
llm_prices::Usage {
cache_write_1h: tokens,
..Default::default()
}
} else {
llm_prices::Usage {
cache_write_5m: tokens,
..Default::default()
}
};
if let Some(c) = p.cost_usage(&usage) {
return Some(c.total);
}
}
let write = if ttl == "1h" { 2.0 } else { 1.25 };
Some(tokens as f64 * input_price(id)? * write / 1_000_000.0)
}

/// List price of uncached input tokens in USD per million, for estimates.
/// Cache writes cost 1.25× this with the 5-minute TTL and 2× with 1 hour.
/// Comes from `llm_prices` when it knows the model, else from the family and
/// version below.
pub fn input_price(id: &str) -> Option<f64> {
if let Some(price) = list_price(id).and_then(|p| p.input) {
return Some(price);
}
let m = claude_model(id)?;
let version = (m.major, m.minor.unwrap_or(0));
match m.family.as_str() {
Expand Down Expand Up @@ -133,10 +170,14 @@ mod tests {
fn prices_by_family_and_version() {
let cases = [
("claude-opus-4-8", Some(5.0)),
("claude-opus-5-5[1m]", Some(4.0)),
("claude-opus-4-1-20250805", Some(15.0)),
("claude-3-opus-20240229", Some(15.0)),
("claude-sonnet-5", Some(3.0)),
("claude-sonnet-5", Some(2.0)),
("claude-sonnet-4-5-20250929", Some(3.0)),
("claude-haiku-4-5", Some(1.0)),
("claude-haiku-5-5", Some(0.1)),
("claude-fable-5-2", Some(8.0)),
("claude-3-5-haiku-20241022", Some(0.8)),
("claude-3-haiku-20240307", Some(0.25)),
("kimi-k2-turbo-preview", None),
Expand All @@ -146,6 +187,19 @@ mod tests {
}
}

#[test]
fn prices_cache_writes_at_the_model_rate() {
// Opus 4.8: $6.25/M for 5-minute writes, $10/M for 1-hour writes.
let five = cache_write_cost("claude-opus-4-8", "5m", 1_000_000).unwrap();
assert!((five - 6.25).abs() < 1e-9, "{five}");
let hour = cache_write_cost("claude-opus-4-8", "1h", 1_000_000).unwrap();
assert!((hour - 10.0).abs() < 1e-9, "{hour}");
// Not in the list-price table: falls back to the family input price.
let old = cache_write_cost("claude-3-haiku-20240307", "5m", 1_000_000).unwrap();
assert!((old - 0.3125).abs() < 1e-9, "{old}");
assert!(cache_write_cost("kimi-k2-turbo-preview", "5m", 1_000).is_none());
}

#[test]
fn ignores_non_claude_ids() {
assert_eq!(claude_name("kimi-k2-0905-preview"), None);
Expand Down