fix: report what a call actually cost, or report nothing - #202
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
One real Consultation turn reported
cost_usd: 0.0for 6484 input / 1834 output tokens on OpenRouter. Two independent defects were behind it.OpenRouter said every paid call was free
openai_compat.py:178hardcodedcost_usd=0.0.OpenRouter computes the actual charge and returns it when asked, so we ask:
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_bodyand 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:191hardcoded(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
deepseek-v4-flash0.0claude-haiku-4-5-20251001claude-sonnet-5Verification
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 acostfield 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