Skip to content

fix(openrouter): record billed costs as estimated_cost - #2848

Open
BASIL K AJI (basil-k-aji-dev) wants to merge 1 commit into
braintrustdata:mainfrom
basil-k-aji-dev:fix/openrouter-estimated-cost
Open

BASIL K AJI (basil-k-aji-dev) wants to merge 1 commit into
braintrustdata:mainfrom
basil-k-aji-dev:fix/openrouter-estimated-cost

Conversation

@basil-k-aji-dev

Copy link
Copy Markdown

Summary

The OpenRouter instrumentation (openrouter-instrumentation.ts and openrouter-agent-instrumentation.ts) recorded the billed cost under a bare cost metric. Every other provider here that records a billed cost uses estimated_cost (ai-sdk-metrics.ts, flue, pi-coding-agent, eve, claude-agent-sdk, openai-batch), and ai-sdk-instrumentation.ts treats estimated_cost as a special key. OpenRouter now records it the same way, so it is picked up like the other providers.

The value is only recorded when it is a finite number, so NaN and +/-Infinity are dropped. A cost of 0 (free models) and negative values are still kept.

Behaviour change

OpenRouter spans no longer carry a cost metric. Anyone filtering or charting metrics.cost for OpenRouter spans will find the same value under estimated_cost. The cost_upstream_inference_* metrics are unchanged.

Verification

  • Unit tests in both OpenRouter test files fail on the old code with wrong-value assertions (6 per file) and pass with the change. Dropping the finite guard, reverting the rename in either file, and renaming to cost_estimated are each caught.
  • The 16 e2e snapshots (8 .json, 8 .txt) were regenerated with node scripts/run-e2e-tests.mjs --update openrouter-instrumentation openrouter-agent-instrumentation, which replays the recorded cassettes. Before regenerating, 12 snapshot tests failed on the cost key. After, 88 pass and 2 skip, twice. The diff is only each "cost" line replaced by "estimated_cost" with the same value, moved to its sorted position.
  • check:typings, lint (no new warnings) and the formatting check are clean, and the changeset validates.

Not covered

The OpenRouter parser also emits prompt_cache_write_tokens, which I left alone because #2842 touches that area.

This branch has not been deployed

No deployments
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