Skip to content

Commit 8c2b1e1

Browse files
authored
fix(provider/anthropic): use full raw usage data in streaming responses (#12375)
## Background When streaming with the `anthropic` provider, `response.usage.raw` only contained the 4 explicitly typed fields (`input_tokens`, `output_tokens`, `cache_creation_input_tokens`, `cache_read_input_tokens`) instead of the full raw API response. The raw response from Anthropic's API includes additional fields like `service_tier`, `inference_geo`, `cache_creation`, and `server_tool_use` that were being lost. This worked correctly for `generateText` but not `streamText`. Discovered this while working on #12353: when streaming with `speed=fast`, with this PR enabled you'll see it in the response - without it you weren't even though it was already being used. ## Summary Fixed the `doStream` method to pass the full `rawUsage` JSONObject (which accumulates all fields from stream chunks) to `convertAnthropicMessagesUsage` instead of the typed `usage` object (which only has the 4 declared properties). This also fixes a secondary bug in multi-turn streaming (programmatic tool calling): the typed `usage` object was mutated in place across turns, but `raw` held a reference to this same mutable object, causing all turns to show the last turn's token values. The fix resolves this because `rawUsage` is replaced (not mutated) at each `message_start`. **Changes:** - Updated `convertAnthropicMessagesUsage` to accept `{ usage, rawUsage? }` object instead of just `usage`, using `rawUsage` for the `raw` field when provided - Updated both call sites (`doGenerate` and `doStream`) to use the new signature - Added comprehensive unit tests for the usage conversion function - Updated test snapshots to reflect the now-correct raw usage data ## Manual Verification Run this without the PR, and then again with the PR: ```bash cd examples/ai-functions pnpm tsx src/generate-text/anthropic.ts pnpm tsx src/stream-text/anthropic.ts cd ../.. ``` The `generateText` result will always include actual raw data, e.g. `service_tier`, while the `streamText` result will not. With the PR, the `streamText` result will correctly include those additional values. ## Checklist - [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) ## Related Issues N/A
1 parent de16a00 commit 8c2b1e1

File tree

6 files changed

+271
-83
lines changed

6 files changed

+271
-83
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@ai-sdk/anthropic': patch
3+
---
4+
5+
fix(provider/anthropic): include actual raw usage data for `response.usage.raw` when streaming

0 commit comments

Comments
 (0)