Skip to content

fix(package/ai): fix Gateway image cost reporting bug#10478

Closed
R-Taneja wants to merge 4 commits intomainfrom
fix-cost-reporting-generate-image
Closed

fix(package/ai): fix Gateway image cost reporting bug#10478
R-Taneja wants to merge 4 commits intomainfrom
fix-cost-reporting-generate-image

Conversation

@R-Taneja
Copy link
Copy Markdown
Contributor

Background

I discovered a bug in the cost reporting for images generated via Gateway. If n > 1, the cost in the provider metadata would be reported as if n = 1 (but the correct amount would be debited). This is because the cost fields in the provider metadata were being merged without being summed across requests (since maxImagesPerCall = 1)

Summary

Merged with summation

Manual Verification

Ran Gateway requests and cost is now reported accurately

Checklist

  • Tests have been added / updated (for bug fixes / features)
  • Documentation has been added / updated (for bug fixes / features)
  • A patch changeset for relevant packages has been added (for bug fixes / features - run pnpm changeset in the project root)
  • I have reviewed this pull request (self-review)

const currentEntry = providerMetadata[providerName];
if (currentEntry != null && typeof currentEntry === 'object') {
providerMetadata[providerName] = {
const merged = {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Since this is gateway-specific logic, ideally there would be a way to solve this on the Gateway server or provider side. Earlier in the flow, summing things before we get into this code.

Since this is just reporting, and I get that I think the issue is this ai-core code is the only place we have all of the data come together, can we report the gateway provider metadata for each chunk/image-response within that chunk/response and leave it up to the caller to sum or aggregate or look across it if they choose to do so?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The whole if (providerName === 'gateway') {} block is already gateway specific and needs to be refactored.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed that the whole block needs to be refactored. As a temporary solution to ship image models asap, 2 paths I see are:

  1. Go with my code in an earlier commit that sums the cost and sets the cost field to the true total cost
  2. Go with the current code in the PR that adds an allCalls array to the provider metadata containing the metadata for each request, and let the user sum the costs if they want. The only real issue with this is the metadata output gets quite long, and can be somewhat confusing because the data outside the allCalls array is just for the last request and shows the cost of one image (same issue), so if a user doesn’t read into the allCalls array they won’t realize what the actual cost is.

I was leaning towards option 1, but I realized that other fields other than cost are also being overwritten with the last request's metadata. This could be an issue as we'd only return fields like modelAttempts, generationId, resolvedProvider, etc for the last request if n > 1. So maybe option 2 is the best because it returns metadata from all calls. I'm just concerned about the duplicative-ness of the last call's metadata being outside allCalls (in the top level providerMetadata object) and also in the last position of the allCalls array.

@shaper
Copy link
Copy Markdown
Collaborator

shaper commented Nov 23, 2025

Can we arrange things so that on ai side n is always 1 -- didn't we do something similar for embeddings?

@R-Taneja R-Taneja closed this Nov 23, 2025
auto-merge was automatically disabled November 23, 2025 23:29

Pull request was closed

@gr2m gr2m added ai/provider related to a provider package. Must be assigned together with at least one `provider/*` label provider/gateway Issues related to the @ai-sdk/gateway provider and removed ai/gateway labels Mar 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai/provider related to a provider package. Must be assigned together with at least one `provider/*` label provider/gateway Issues related to the @ai-sdk/gateway provider

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants