trusty_code/perf/pricing.rs
1//! Cost computation, model pricing table, and small formatting helpers.
2//!
3//! Why: The pricing table + cost math is mechanically independent of the
4//! collector's lifecycle; isolating it keeps `mod.rs` focused on recording
5//! and persisting performance records.
6//! What: `cost_usd` (public), the `pricing_for`/`per_million` rate helpers, and
7//! the `filename_stamp` / `truncate_preview` formatters used by the collector.
8//! Test: `cost_usd_*` / `filename_stamp_format` / `truncate_preview_*` in
9//! `perf::tests`.
10
11/// Compute USD cost for a single LLM call given the model name and token
12/// counts. Unknown models fall back to Sonnet-class pricing.
13///
14/// Why: (#47) We hard-code the pricing table rather than hit a live endpoint
15/// so offline/CI runs still produce comparable cost figures. Pricing is
16/// per-million-tokens as published by Anthropic and OpenRouter.
17/// What: Substring-matches the model string (e.g. "anthropic/claude-sonnet-4-5"
18/// or "claude-haiku-4") and multiplies each token bucket by its rate.
19/// Test: `cost_usd_known_model`, `cost_usd_unknown_defaults_to_sonnet`.
20pub fn cost_usd(
21 model: &str,
22 prompt_tokens: u32,
23 completion_tokens: u32,
24 cache_read: u32,
25 cache_creation: u32,
26) -> f64 {
27 // Rates in USD per token (not per million).
28 let (rate_in, rate_out, rate_cache_r, rate_cache_w) = pricing_for(model);
29 let to_usd = |tokens: u32, rate: f64| tokens as f64 * rate;
30 to_usd(prompt_tokens, rate_in)
31 + to_usd(completion_tokens, rate_out)
32 + to_usd(cache_read, rate_cache_r)
33 + to_usd(cache_creation, rate_cache_w)
34}
35
36/// Returns (input, output, cache_read, cache_creation) rates per token.
37fn pricing_for(model: &str) -> (f64, f64, f64, f64) {
38 let m = model.to_ascii_lowercase();
39 // Claude Sonnet 4.x — $3 in, $15 out, $0.30 cache read, $3.75 cache write
40 if m.contains("sonnet-4") || m.contains("claude-sonnet-4") {
41 return (
42 per_million(3.0),
43 per_million(15.0),
44 per_million(0.30),
45 per_million(3.75),
46 );
47 }
48 // Claude Haiku 3/4 — $0.80 in, $4 out, $0.08 cache read, $1 cache write
49 if m.contains("haiku-3") || m.contains("haiku-4") || m.contains("claude-haiku") {
50 return (
51 per_million(0.80),
52 per_million(4.0),
53 per_million(0.08),
54 per_million(1.0),
55 );
56 }
57 // Claude Opus 4 — $15 in, $75 out (cache rates not published here, use
58 // conservative 10% / 125% of input, matching Anthropic convention).
59 if m.contains("opus-4") || m.contains("claude-opus") {
60 return (
61 per_million(15.0),
62 per_million(75.0),
63 per_million(1.50),
64 per_million(18.75),
65 );
66 }
67 // Default: Sonnet-class rates.
68 (
69 per_million(3.0),
70 per_million(15.0),
71 per_million(0.30),
72 per_million(3.75),
73 )
74}
75
76fn per_million(usd: f64) -> f64 {
77 usd / 1_000_000.0
78}
79
80/// Build the canonical filename stamp from an ISO8601 timestamp + build #.
81///
82/// Why: Deterministic filenames let tests assert exact paths and let humans
83/// sort runs chronologically with `ls`.
84/// What: Converts `2026-04-22T17:31:30Z` + build=42 to `20260422-173130-build42`.
85/// Test: `filename_stamp_format`.
86pub(super) fn filename_stamp(iso: &str, build: u64) -> String {
87 // Strip non-digit chars to keep only YYYYMMDDHHMMSS, then reinsert the dash.
88 let digits: String = iso.chars().filter(|c| c.is_ascii_digit()).collect();
89 // Expected layout: YYYYMMDDHHMMSS (14 digits). If shorter, just pad.
90 let date = digits.get(0..8).unwrap_or("00000000");
91 let time = digits.get(8..14).unwrap_or("000000");
92 format!("{date}-{time}-build{build}")
93}
94
95pub(super) fn truncate_preview(s: &str, max_chars: usize) -> String {
96 let trimmed = s.trim();
97 if trimmed.chars().count() <= max_chars {
98 return trimmed.to_string();
99 }
100 let mut out = String::with_capacity(max_chars + 3);
101 for (i, ch) in trimmed.chars().enumerate() {
102 if i >= max_chars {
103 break;
104 }
105 out.push(ch);
106 }
107 out.push_str("...");
108 out
109}