Skip to content

fix(hermes): don't trust a $0 estimated cost — calculate from tokens - #1213

Merged
iamtoruk merged 2 commits into
getagentseal:mainfrom
MiguelMachado-dev:fix/hermes-recorded-cost-fallback
Sep 1, 2026
Merged

fix(hermes): don't trust a $0 estimated cost — calculate from tokens#1213
iamtoruk merged 2 commits into
getagentseal:mainfrom
MiguelMachado-dev:fix/hermes-recorded-cost-fallback

Conversation

@MiguelMachado-dev

Copy link
Copy Markdown
Contributor

Problem

Every Hermes session on a flat subscription (Codex plan, z.ai, Kimi) reports $0.00 in CodeBurn despite real token usage.

Root cause: Hermes writes estimated_cost_usd = 0.0 — not NULL — when its cost_status is 'unknown' (no pricing data) or 'included' (flat subscription, per Hermes's usage_pricing.py: "subscription-included; no provider invoice for usage"). resolveHermesCost treats any non-null estimate as a recorded amount, so the placeholder $0 wins over the token-based calculation forever.

I verified against a live ~/.hermes/state.db: 17 sessions with full token counts, all estimated_cost_usd = 0.0 with cost_status 'included'/'unknown' and cost_source = 'none' — no measurement happened.

Fix

Only trust positive recorded estimates; $0 falls through to calculateCost. Deliberately not keyed off cost_status: the > 0 check also covers DBs from before the column existed, and matches the columns CodeBurn already reads.

Semantics preserved:

  • actual_cost_usd non-null still wins — including explicit $0 (a real invoice amount is fact)
  • genuinely free models still land on $0 — litellm prices them at 0, so the fallback calculation agrees

The ledger's cost-delta path makes existing sessions self-heal: on the next scan, calculated cost > lastSeen $0 emits a cost-only correction observation.

Tests

  • New tests/hermes-cost-fallback.test.ts: zero-estimate → calculated, positive estimate → trusted, zero actual → $0
  • Existing hermes suites pass (12 tests total)

Hermes writes estimated_cost_usd = 0.0 (not NULL) when cost_status is
'unknown' (no pricing data) or 'included' (flat subscription, e.g. Codex
plan). resolveHermesCost treated any non-null estimate as a recorded
amount, so every subscription-backed Hermes session reported $0.00 with
real token counts underneath.

Only trust positive estimates now; $0 falls through to the token-based
calculateCost. An explicit $0 actual_cost_usd (a real invoice amount)
still wins, and genuinely free models still land on $0 since litellm
prices them at 0.
@MiguelMachado-dev

Copy link
Copy Markdown
Contributor Author

The test failures are unrelated to this PR — pre-existing date-dependent bug in tests/cli-budget.test.ts (seeds at today 12:00 UTC, which is in the future before noon UTC, so the overview shows no usage). All four of these PRs fail on the identical test. Reproduced and fixed in #1216; a re-run after that merges (or after 12:00 UTC) goes green.

…re-cost

The cost rule changed, so sessions whose state.db fingerprint has not
moved must be re-parsed or they keep serving the stale placeholder zero.
@iamtoruk

iamtoruk commented Sep 1, 2026

Copy link
Copy Markdown
Member

Reviewed in depth and merging. The fix is right, and the objection that a literal zero might be a genuine free-model zero does not hold in practice: free, local and flat-rate models calculate to zero anyway, API-equivalent pricing for subscription turns is the same convention every other provider uses, and docs/providers/hermes.md has said since the provider landed that only a positive recorded cost is trusted. Your new fixture test is genuine red/green (fails on main with expected 0 to be greater than 0). One thing patched on top: PROVIDER_PARSE_VERSIONS.hermes in src/session-cache.ts was not bumped, so sessions whose state.db fingerprint has not changed would have kept serving the cached zero; bumped it so they re-cost. tsc clean, hermes and session-cache suites green.

@iamtoruk
iamtoruk merged commit eae2b95 into getagentseal:main Sep 1, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants