Skip to content

fix: report what a call actually cost, or report nothing - #202

Merged
vicenteliu merged 1 commit into
mainfrom
fix/cost-reporting
Aug 19, 2026
Merged

fix: report what a call actually cost, or report nothing#202
vicenteliu merged 1 commit into
mainfrom
fix/cost-reporting

Conversation

@vicenteliu

Copy link
Copy Markdown
Owner

One real Consultation turn reported cost_usd: 0.0 for 6484 input / 1834 output tokens on OpenRouter. Two independent defects were behind it.

OpenRouter said every paid call was free

openai_compat.py:178 hardcoded cost_usd=0.0.

OpenRouter computes the actual charge and returns it when asked, so we ask:

kwargs["extra_body"] = {"usage": {"include": True}}

and read usage.cost. Confirmed against the live API:

{"prompt_tokens": 5, "completion_tokens": 10, "cost": 5e-06,
 "cost_details": {"upstream_inference_cost": 5e-06, ...}}

That figure is what was billed — it beats any local price table because it cannot go stale. It is an OpenRouter extension, so it goes through extra_body and is not sent to OpenAI / Gemini / Grok, which would reject it. For those, cost stays 0.0.

Anthropic used one Sonnet rate for every model

anthropic.py:191 hardcoded (in*3 + out*15)/1M, with the comment "Cost estimate for claude-sonnet-4 pricing (approximate)", applied to every Anthropic model. Haiku 4.5 ($1/$5) read ~3× high; Opus 5 ($5/$25) read low.

Replaced with a per-model list-price table in providers/pricing.py, longest-prefix matched so the playbook's dated names (claude-haiku-4-5-20251001) resolve to their family.

Unknown means nothing, not zero-dollars

A model the table does not price costs 0.0, which every surface already renders as nothing ({#if cost_usd > 0} in RunTab, ChatTab, VendorDocTab). Showing nothing beats showing a wrong number — which is the point: ADR-0023 routes between a cheap tier and a thinking tier, and cost is the number that justifies the routing.

Introductory rates are deliberately not encoded. Sonnet 5's promotional $2/$10 runs to 2026-08-31; encoding it would turn a correct number into a wrong one on a date nobody is watching. The table carries the date it was checked.

Measured against the live APIs

model before after
OpenRouter deepseek-v4-flash 0.0 $0.00001450
claude-haiku-4-5-20251001 $0.000102 $0.00003400
claude-sonnet-5 $0.000108 $0.000108 (unchanged — $3/$15 is its list price)

Verification

10 new tests in test_cost_reporting.py: the price table (per family, dated snapshots, longest-prefix, unpriced → 0.0), that OpenRouter is sent the extension and its charge is used, that the other three providers are not sent it, and that a response without a cost field reports nothing rather than crashing.

pytest -m "not slow and not requires_ollama and not requires_api_key" — 1318 passed. No protected path touched.

🤖 Generated with Claude Code

One real Consultation turn reported `cost_usd: 0.0` for 6484 input / 1834
output tokens on OpenRouter. Two independent defects were behind it.

`openai_compat.py` hardcoded `cost_usd=0.0`, so every paid OpenAI / OpenRouter /
Gemini / Grok call read as free. OpenRouter computes the actual charge and will
return it on the response when asked, so we ask: `extra_body={"usage":
{"include": True}}`, and use `usage.cost`. That figure is what was billed and
cannot go stale. It is an OpenRouter extension, so it is not sent to the others,
which would reject it; for them the cost stays 0.0.

`anthropic.py` hardcoded `(in*3 + out*15)/1M` — the comment said "claude-sonnet-4
pricing (approximate)" and it was applied to every Anthropic model. Haiku 4.5
($1/$5) read about threefold high; Opus 5 ($5/$25) read low. Replaced with a
per-model list-price table, longest-prefix matched so the playbook's dated names
(`claude-haiku-4-5-20251001`) resolve to their family.

A model the table does not price costs 0.0, which every surface already renders
as nothing (`{#if cost_usd > 0}`). Showing nothing beats showing a wrong number,
and that is the whole point: ADR-0023 routes between a cheap tier and a thinking
tier, and cost is the number that justifies the routing.

Introductory rates are deliberately not encoded. Sonnet 5's promotional
$2/$10 runs to 2026-08-31; encoding it would turn a correct number into a wrong
one on a date nobody is watching.

Measured against the live APIs before and after:

    OpenRouter deepseek-v4-flash   0.0        → $0.00001450
    claude-haiku-4-5-20251001      $0.000102  → $0.00003400
    claude-sonnet-5                $0.000108  → $0.000108   (unchanged; $3/$15)

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@vicenteliu
vicenteliu merged commit b545772 into main Aug 19, 2026
4 checks passed
@vicenteliu
vicenteliu deleted the fix/cost-reporting branch August 19, 2026 09:19
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.

1 participant