Skip to content

Commit 2432620

Browse files
committed
fix(hermes): price subscription-included sessions
calculate_hermes_cost short-circuits on any Some(cost) read from state.db, so sessions where Hermes wrote estimated_cost_usd = 0.0 (subscription_included billing, e.g. ChatGPT Plus/Pro via the codex backend) report $0 even when token usage is non-zero. Only short-circuit on strictly positive recorded cost; fall through to pricing-based calculation otherwise, matching how the codex adapter handles the same models. Closes #1112
1 parent 1e1e9b3 commit 2432620

1 file changed

Lines changed: 64 additions & 2 deletions

File tree

‎rust/crates/ccusage/src/adapter/hermes.rs‎

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,13 @@ fn to_loaded_entry(
373373
}
374374

375375
fn calculate_hermes_cost(entry: &HermesEntry, pricing: &PricingMap) -> f64 {
376-
if let Some(cost) = entry.cost_usd {
376+
// Only trust a recorded cost when it is strictly positive. Hermes writes
377+
// estimated_cost_usd = 0.0 for subscription-included billing modes (for
378+
// example, ChatGPT Plus/Pro via https://chatgpt.com/backend-api/codex),
379+
// even when the session burned non-trivial tokens. Treat that recorded
380+
// zero as "no cost recorded" and fall through to pricing-based
381+
// calculation, mirroring how the codex adapter handles the same models.
382+
if let Some(cost) = entry.cost_usd.filter(|cost| *cost > 0.0) {
377383
return cost;
378384
}
379385
let usage = TokenUsageRaw {
@@ -388,7 +394,7 @@ fn calculate_hermes_cost(entry: &HermesEntry, pricing: &PricingMap) -> f64 {
388394
CostMode::Calculate,
389395
Some(pricing),
390396
);
391-
if cost >= 0.0 && cost.is_finite() && cost > 0.0 {
397+
if cost.is_finite() && cost > 0.0 {
392398
return cost;
393399
}
394400
}
@@ -581,6 +587,62 @@ mod tests {
581587
}
582588
}
583589

590+
#[test]
591+
fn recorded_zero_cost_falls_back_to_pricing_calculation() {
592+
// Hermes writes estimated_cost_usd = 0.0 for subscription-included
593+
// billing (e.g. ChatGPT Plus/Pro via the codex backend), even when the
594+
// session consumed real tokens. The adapter should not short-circuit
595+
// on that recorded zero; it should price the tokens from the embedded
596+
// table so users still see a meaningful cost number.
597+
let pricing = PricingMap::load_embedded();
598+
let entry = HermesEntry {
599+
timestamp: crate::parse_ts_timestamp("2026-05-19T00:00:00.000Z").unwrap(),
600+
timestamp_text: "2026-05-19T00:00:00.000Z".to_string(),
601+
session_id: "subscription-included".to_string(),
602+
model: "gpt-5.5".to_string(),
603+
provider: "openai".to_string(),
604+
usage: TokenUsageRaw {
605+
input_tokens: 244_075,
606+
output_tokens: 10_019,
607+
cache_creation_input_tokens: 0,
608+
cache_read_input_tokens: 3_339_776,
609+
speed: None,
610+
},
611+
reasoning_tokens: 3_216,
612+
message_count: 72,
613+
cost_usd: Some(0.0),
614+
};
615+
616+
assert!(
617+
calculate_hermes_cost(&entry, &pricing) > 0.0,
618+
"subscription-included sessions with token usage should still be priced"
619+
);
620+
}
621+
622+
#[test]
623+
fn recorded_positive_cost_is_trusted() {
624+
let pricing = PricingMap::load_embedded();
625+
let entry = HermesEntry {
626+
timestamp: crate::parse_ts_timestamp("2026-05-19T00:00:00.000Z").unwrap(),
627+
timestamp_text: "2026-05-19T00:00:00.000Z".to_string(),
628+
session_id: "metered".to_string(),
629+
model: "gpt-5.5".to_string(),
630+
provider: "openai".to_string(),
631+
usage: TokenUsageRaw {
632+
input_tokens: 1_000,
633+
output_tokens: 100,
634+
cache_creation_input_tokens: 0,
635+
cache_read_input_tokens: 0,
636+
speed: None,
637+
},
638+
reasoning_tokens: 0,
639+
message_count: 1,
640+
cost_usd: Some(0.42),
641+
};
642+
643+
assert_eq!(calculate_hermes_cost(&entry, &pricing), 0.42);
644+
}
645+
584646
#[test]
585647
fn tries_provider_qualified_model_candidate_first() {
586648
let entry = HermesEntry {

0 commit comments

Comments
 (0)