Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat: cost for OpenRouter image models#782

Merged
AlemTuzlak merged 2 commits into
TanStack:mainfrom
edemaine:image-cost
Jun 19, 2026
Merged

feat: cost for OpenRouter image models#782
AlemTuzlak merged 2 commits into
TanStack:mainfrom
edemaine:image-cost

Conversation

@edemaine

@edemaine edemaine commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

🎯 Changes

Fix OpenRouter image generation usage reporting so provider-reported request cost is surfaced on result.usage, matching the existing OpenRouter text adapter behavior.

OpenRouter image generation already normalized token usage via buildOpenRouterUsage(...), but did not merge in extractUsageCost(...). This meant cost and cost_details were dropped for image generation responses even when OpenRouter returned them.

Adds a regression test covering image usage with provider-reported cost and snake_case cost_details.

I also tested in my test project via pnpm link. Costs are now surfaced.

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested this code locally with pnpm run test:pr.

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).

Summary by CodeRabbit

  • New Features

    • OpenRouter image generation now surfaces provider-reported cost data in usage metrics, consistent with text generation behavior.
  • Tests

    • Added test coverage to verify image generation usage includes provider-reported cost details.

@coderabbitai

coderabbitai Bot commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: f6983544-704f-4c08-bdbb-6af653bf31ae

📥 Commits

Reviewing files that changed from the base of the PR and between cf3122d and 278c69b.

📒 Files selected for processing (1)
  • packages/ai-openrouter/src/adapters/image.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/ai-openrouter/src/adapters/image.ts

📝 Walkthrough

Walkthrough

The OpenRouter image adapter's transformResponse now imports extractUsageCost and merges its output into the returned usage object alongside buildOpenRouterUsage, surfacing provider-reported cost and cost_details. A new test verifies the mapped costDetails fields, and a changeset documents the patch.

Changes

OpenRouter image adapter cost extraction

Layer / File(s) Summary
Cost extraction in transformResponse + test coverage
packages/ai-openrouter/src/adapters/image.ts, packages/ai-openrouter/tests/image-adapter.test.ts, .changeset/openrouter-image-cost.md
extractUsageCost is imported and merged into the usage object returned by transformResponse; a new test case mocks cost and cost_details fields and asserts they map to costDetails.upstreamCost, upstreamInputCost, and upstreamOutputCost; changeset entry documents the patch release.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~5 minutes

Possibly related PRs

  • TanStack/ai#654: Introduced the shared extractUsageCost helper that this PR now applies to the image adapter's transformResponse.
  • TanStack/ai#747: Added OTel middleware that reads usage.costDetails from run results; the image adapter now populates those fields, making them available to that middleware.

Suggested reviewers

  • tombeckenham

Poem

🐰 A patch so small, a cost so clear,
The image adapter joins the gear.
extractUsageCost now merges in,
Where only token counts had been.
Upstream costs surface, hooray!
✨ The rabbit hops another way. ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main change: adding cost reporting functionality for OpenRouter image models, which is the primary objective of the PR.
Description check ✅ Passed The description comprehensively covers all required sections from the template with clear explanations of changes, motivations, testing, and checklist completion.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
packages/ai-openrouter/tests/image-adapter.test.ts (1)

114-152: ⚡ Quick win

Move this new unit test to a colocated *.test.ts file.

Line 114 adds new unit coverage in packages/ai-openrouter/tests/image-adapter.test.ts, but repo guidance requires unit tests to live alongside source. Please move this case next to packages/ai-openrouter/src/adapters/image.ts (for example, packages/ai-openrouter/src/adapters/image.test.ts).

As per coding guidelines, "Place unit tests alongside source code in *.test.ts files".

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/ai-openrouter/tests/image-adapter.test.ts` around lines 114 - 152,
The test case `surfaces provider-reported cost from OpenRouter image usage` is
currently located in the tests directory structure but should be colocated with
its source file per repository guidelines. Move this entire test case from the
tests directory to a new test file that lives alongside the image adapter source
code. The test should be placed in the same directory as the image adapter being
tested to follow the pattern of keeping unit tests adjacent to their source
implementations in dot-test files.

Source: Coding guidelines

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@packages/ai-openrouter/tests/image-adapter.test.ts`:
- Around line 114-152: The test case `surfaces provider-reported cost from
OpenRouter image usage` is currently located in the tests directory structure
but should be colocated with its source file per repository guidelines. Move
this entire test case from the tests directory to a new test file that lives
alongside the image adapter source code. The test should be placed in the same
directory as the image adapter being tested to follow the pattern of keeping
unit tests adjacent to their source implementations in dot-test files.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: b901dcb3-cf85-40f6-93b8-52a4bd2f8fd2

📥 Commits

Reviewing files that changed from the base of the PR and between 8fa6cc5 and cf3122d.

📒 Files selected for processing (3)
  • .changeset/openrouter-image-cost.md
  • packages/ai-openrouter/src/adapters/image.ts
  • packages/ai-openrouter/tests/image-adapter.test.ts

@nx-cloud

nx-cloud Bot commented Jun 19, 2026

Copy link
Copy Markdown

View your CI Pipeline Execution ↗ for commit cf3122d

Command Status Duration Result
nx run-many --targets=build --exclude=examples/... ✅ Succeeded 6s View ↗

☁️ Nx Cloud last updated this comment at 2026-06-19 12:41:24 UTC

@pkg-pr-new

pkg-pr-new Bot commented Jun 19, 2026

Copy link
Copy Markdown

Open in StackBlitz

@tanstack/ai

npm i https://pkg.pr.new/@tanstack/ai@782

@tanstack/ai-angular

npm i https://pkg.pr.new/@tanstack/ai-angular@782

@tanstack/ai-anthropic

npm i https://pkg.pr.new/@tanstack/ai-anthropic@782

@tanstack/ai-client

npm i https://pkg.pr.new/@tanstack/ai-client@782

@tanstack/ai-code-mode

npm i https://pkg.pr.new/@tanstack/ai-code-mode@782

@tanstack/ai-code-mode-skills

npm i https://pkg.pr.new/@tanstack/ai-code-mode-skills@782

@tanstack/ai-devtools-core

npm i https://pkg.pr.new/@tanstack/ai-devtools-core@782

@tanstack/ai-elevenlabs

npm i https://pkg.pr.new/@tanstack/ai-elevenlabs@782

@tanstack/ai-event-client

npm i https://pkg.pr.new/@tanstack/ai-event-client@782

@tanstack/ai-fal

npm i https://pkg.pr.new/@tanstack/ai-fal@782

@tanstack/ai-gemini

npm i https://pkg.pr.new/@tanstack/ai-gemini@782

@tanstack/ai-grok

npm i https://pkg.pr.new/@tanstack/ai-grok@782

@tanstack/ai-groq

npm i https://pkg.pr.new/@tanstack/ai-groq@782

@tanstack/ai-isolate-cloudflare

npm i https://pkg.pr.new/@tanstack/ai-isolate-cloudflare@782

@tanstack/ai-isolate-node

npm i https://pkg.pr.new/@tanstack/ai-isolate-node@782

@tanstack/ai-isolate-quickjs

npm i https://pkg.pr.new/@tanstack/ai-isolate-quickjs@782

@tanstack/ai-mcp

npm i https://pkg.pr.new/@tanstack/ai-mcp@782

@tanstack/ai-ollama

npm i https://pkg.pr.new/@tanstack/ai-ollama@782

@tanstack/ai-openai

npm i https://pkg.pr.new/@tanstack/ai-openai@782

@tanstack/ai-openrouter

npm i https://pkg.pr.new/@tanstack/ai-openrouter@782

@tanstack/ai-preact

npm i https://pkg.pr.new/@tanstack/ai-preact@782

@tanstack/ai-react

npm i https://pkg.pr.new/@tanstack/ai-react@782

@tanstack/ai-react-ui

npm i https://pkg.pr.new/@tanstack/ai-react-ui@782

@tanstack/ai-solid

npm i https://pkg.pr.new/@tanstack/ai-solid@782

@tanstack/ai-solid-ui

npm i https://pkg.pr.new/@tanstack/ai-solid-ui@782

@tanstack/ai-svelte

npm i https://pkg.pr.new/@tanstack/ai-svelte@782

@tanstack/ai-utils

npm i https://pkg.pr.new/@tanstack/ai-utils@782

@tanstack/ai-vue

npm i https://pkg.pr.new/@tanstack/ai-vue@782

@tanstack/ai-vue-ui

npm i https://pkg.pr.new/@tanstack/ai-vue-ui@782

@tanstack/openai-base

npm i https://pkg.pr.new/@tanstack/openai-base@782

@tanstack/preact-ai-devtools

npm i https://pkg.pr.new/@tanstack/preact-ai-devtools@782

@tanstack/react-ai-devtools

npm i https://pkg.pr.new/@tanstack/react-ai-devtools@782

@tanstack/solid-ai-devtools

npm i https://pkg.pr.new/@tanstack/solid-ai-devtools@782

commit: 278c69b

@AlemTuzlak AlemTuzlak merged commit 1047e40 into TanStack:main Jun 19, 2026
10 checks passed
@github-actions github-actions Bot mentioned this pull request Jun 19, 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.

2 participants