Skip to content

fix(openrouter): reconcile streamed generation cost#91073

Merged
steipete merged 1 commit into
mainfrom
fix-openrouter-generation-cost
Jun 7, 2026
Merged

fix(openrouter): reconcile streamed generation cost#91073
steipete merged 1 commit into
mainfrom
fix-openrouter-generation-cost

Conversation

@steipete

@steipete steipete commented Jun 7, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Reconciles completed OpenRouter streaming chat usage with the authoritative generation metadata total_cost before the final assistant message is emitted.
  • Keeps the streamed estimate when metadata lookup fails or returns malformed cost, and skips custom OpenRouter-compatible proxy routes.
  • Avoids generation lookup for aborted/error terminal events so cancellation stays responsive.

Fixes #68066.

Verification

  • Official OpenRouter docs checked: /api/v1/generation?id=... returns request/usage metadata including total_cost: https://openrouter.ai/docs/api/api-reference/generations/get-generation
  • Live OpenRouter API proof with stored OPENROUTER_API_KEY: streamed google/gemini-2.5-flash chat completion returned HTTP 200, gen-... response id, finish=stop, streamed cost 0.0000049; delayed generation lookup returned HTTP 200 with matching id, streamed=true, cancelled=false, total_cost=0.0000049.
  • node scripts/run-vitest.mjs extensions/openrouter/index.test.ts extensions/openrouter/provider-runtime.contract.test.ts
  • node scripts/run-vitest.mjs extensions/openrouter
  • pnpm exec oxfmt --check extensions/openrouter/stream.ts extensions/openrouter/index.test.ts
  • git diff --check
  • Autoreview clean after accepted abort-fix finding; key-layering false positive verified against src/agents/embedded-agent-runner/stream-resolution.ts and existing stream-resolution.test.ts resolved-key coverage.

Not Tested

@steipete

steipete commented Jun 7, 2026

Copy link
Copy Markdown
Contributor Author

Land-ready proof for #68066:

Behavior addressed: completed OpenRouter streaming chat completions now reconcile message.usage.cost.total with OpenRouter generation metadata before the final assistant message is emitted.

Real environment tested: yes, with stored OPENROUTER_API_KEY and OpenRouter live API. A streamed google/gemini-2.5-flash call returned HTTP 200, a gen-... response id, finish=stop, and streamed cost 0.0000049. A delayed GET https://openrouter.ai/api/v1/generation?id=<id> returned HTTP 200 with matching id, streamed=true, cancelled=false, and total_cost=0.0000049.

Exact commands run after this patch:

node scripts/run-vitest.mjs extensions/openrouter/index.test.ts extensions/openrouter/provider-runtime.contract.test.ts
node scripts/run-vitest.mjs extensions/openrouter
pnpm exec oxfmt --check extensions/openrouter/stream.ts extensions/openrouter/index.test.ts
git diff --check
/Users/steipete/Projects/agent-scripts/skills/autoreview/scripts/autoreview --mode local --prompt-file /tmp/openrouter-autoreview-evidence.md

Observed result after fix: focused OpenRouter provider tests passed, full OpenRouter extension tests passed (8 files / 79 tests), formatting and diff whitespace checks passed, and autoreview reported no accepted/actionable findings after the accepted abort-path fix.

What was not tested: I did not rerun the expensive long-context tiered-pricing repro from #68066; the live test covered the real response-id to generation-metadata contract and the patch covers the persisted final message path.

@clawsweeper

clawsweeper Bot commented Jun 7, 2026

Copy link
Copy Markdown
Contributor

ClawSweeper status: review started.

I am starting a fresh review of this pull request: fix(openrouter): reconcile streamed generation cost This is item 1/1 in the current shard. Shard 0/1.

This placeholder means the worker is alive and reading the current context. I will edit this same comment with the actual review when the claws are done clicking.

Crustacean status: shell secured, claws on keyboard, evidence pebbles being sorted.

@blacksmith-sh

blacksmith-sh Bot commented Jun 7, 2026

Copy link
Copy Markdown
Contributor

Found 2 test failures on Blacksmith runners:

Failures

Test View Logs
gateway:watch did not report ready before the idle CPU window/
gateway:watch did not report ready before the idle CPU window
View Logs
Possible duplicate dist-runtime graph regression: this can reintroduce split runtime pe
rsonalities where plugins and core observe different global state, including Telegram m
issing /voice, /phone, or /pair.
View Logs

Fix in Cursor

@steipete

steipete commented Jun 7, 2026

Copy link
Copy Markdown
Contributor Author

CI note before landing:

Relevant proof for this PR passed:

  • node scripts/run-vitest.mjs extensions/openrouter/index.test.ts extensions/openrouter/provider-runtime.contract.test.ts
  • node scripts/run-vitest.mjs extensions/openrouter
  • pnpm exec oxfmt --check extensions/openrouter/stream.ts extensions/openrouter/index.test.ts
  • git diff --check
  • live OpenRouter API proof: streamed google/gemini-2.5-flash returned a gen-... response id and streamed usage cost; delayed GET /api/v1/generation?id=... returned the matching id with streamed=true, cancelled=false, and total_cost.

CI failures inspected from run 27080990032; they are unrelated to the OpenRouter patch:

  • check-test-types: existing extensions/memory-core/doctor-contract-api.test.ts config type errors.
  • check-lint / check-additional-extension-bundled: annotations in extensions/memory-core, extensions/device-pair, scripts/test-live-media.ts, and src/cli/exec-approvals-cli.ts.
  • check-additional-runtime-topology-architecture: existing src/agents / src/plugins import cycle.
  • build-artifacts: gateway-watch did not report ready before the idle CPU window; channels/core-support-boundary tests in that job passed.

@steipete
steipete merged commit 3006b85 into main Jun 7, 2026
168 of 176 checks passed
@steipete
steipete deleted the fix-openrouter-generation-cost branch June 7, 2026 03:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Gateway writes streamed usage.cost.total for OpenRouter calls — up to 4× under actual billed amount on tier-priced models

1 participant