SIGN IN SIGN UP

fix(ai): sum Gateway image-generation costs across split requests (#19936)

## Background

When `generateImage` splits an image request across multiple
`doGenerate` calls, Gateway returns cost metadata for each individual
call. Core previously overwrote the earlier Gateway metadata with the
final call’s metadata, so the reported cost was too low.

## Summary

Sum Gateway’s cost fields when aggregating the results of split
image-generation calls.

## End-to-End Verification

```ts
const result = await generateImage({
  model: 'bfl/flux-kontext-pro',
  prompt:
    'A cat wearing an intricate robe while gesticulating wildly, in the style of 80s pop art',
  n: 2,
  maxImagesPerCall: 1,
});

console.log(result.providerMetadata);
```

This makes two Gateway image-generation calls. Before this change, the
result contained both generated images but
`providerMetadata.gateway.cost` reflected only the final call (`"0.04"`
in this test); afterwards it correctly reports the total (`"0.08"`).

## Checklist

- [X] All commits are signed (PRs with unsigned commits cannot be
merged)
- [X] Tests have been added / updated (for bug fixes / features)
- [ ] Documentation has been added / updated (for bug fixes / features)
- [X] A _patch_ changeset for relevant packages has been added (for bug
fixes / features - run `pnpm changeset` in the project root)
- [X] I have reviewed this pull request (self-review)

## Future Work

Ideally, Gateway-specific logic shouldn't live in `ai` at all.

A better general design is to expose a `calls` array from
`generateImage`, preserving each underlying `doGenerate` result and its
complete provider metadata. See
https://github.com/vercel/ai/issues/19938

## Related Issues

Fixes https://github.com/vercel/ai/issues/12796
N
Nick Oates committed
dd32de26de5f1bf02f6ace9788befdfa342de9ab
Parent: ed18394
Committed by GitHub <noreply@github.com> on 8/28/2026, 6:47:19 PM