Skip to content

Real OpenRouter cost in ReviewResult#83

Open
dangng2004 wants to merge 1 commit into
mainfrom
wip/openrouter-actual-cost
Open

Real OpenRouter cost in ReviewResult#83
dangng2004 wants to merge 1 commit into
mainfrom
wip/openrouter-actual-cost

Conversation

@dangng2004
Copy link
Copy Markdown
Contributor

Summary

  • Reads the actual cost OpenRouter returns in `resp.usage.cost` (enabled by `extra_body.usage.include=true`) and stores it on `ReviewResult.total_cost_usd` along with `cost_source="openrouter"`.
  • New `ReviewResult.add_usage()` helper replaces 9 duplicated token-accumulation snippets across the three method modules and now also folds in cost.
  • `compute_cost` prefers the reported value when `cost_source` is set; the token-price-table estimator stays as the fallback.
  • Viz JSON now records `cost_method` (`openrouter_actual` vs `estimated`) per method so downstream tooling can tell them apart.

Non-OpenRouter providers

OpenAI, Anthropic, Gemini, Mistral are untouched — they keep the estimator path.

Status

Draft. Verified `add_usage()` accumulates tokens and cost correctly in isolation; no end-to-end run yet against OpenRouter.

Test plan

  • Run a small benchmark against OpenRouter; verify `cost_method=openrouter_actual` and that the number matches the OpenRouter dashboard
  • Run against a non-OpenRouter provider; verify estimator still fires and `cost_method=estimated`
  • Confirm older result JSONs (no `cost_source`) still load through `compute_cost`

OpenRouter returns the real (post-discount, post-routing) cost in the
usage block when asked. Plumb that through so reviews report ground-truth
spend instead of a token-count × hardcoded-price estimate.

- client.py: send extra_body.usage.include=true on OpenRouter calls;
  read resp.usage.cost (with model_extra fallback) and add it to the
  returned usage dict as cost_usd + cost_source.
- models.py: ReviewResult gains total_cost_usd and cost_source fields,
  plus an add_usage() helper that consolidates the per-call token and
  cost accumulation that was previously duplicated at every call site.
- method_local / method_progressive / method_zero_shot: 9 call sites
  switched from inline token-accumulation pairs to result.add_usage().
- evaluate.compute_cost: prefer the reported value when cost_source is
  set; estimator stays as the fallback for other providers.
- cli.py: viz JSON now records "cost_method" so downstream tooling can
  tell real numbers apart from estimates.

Non-OpenRouter providers (OpenAI, Anthropic, Gemini, Mistral) are
unaffected — they keep using the token-price-table estimate.
@dangng2004 dangng2004 marked this pull request as ready for review May 14, 2026 17:02
@dangng2004 dangng2004 changed the title WIP: real OpenRouter cost in ReviewResult Real OpenRouter cost in ReviewResult May 14, 2026
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