ta
TanStack
OSS
ta
Pipelines
Nx runs
Insights
Compare tasks
Analytics
Sign in
Toggle Sidebar
ai
ai
Pipelines
fix(ai-anthropic): report token usage when a stream stops at max_tokens (#1609) * fix(ai-anthropic): report token usage when a stream stops at max_tokens A streamed response that stops at `max_tokens` ended in a `RUN_ERROR` with no `usage`. The `max_tokens` branch in `processAnthropicStream` did not call `buildAnthropicUsage(event.usage)`, while the `tool_use` and default branches did. Anthropic bills these tokens, so code that counts `usage` undercounted every call that ended this way. Attach `usage` to that `RUN_ERROR`. Core already carries `usage` on `RUN_ERROR` to the stream and to `onChunk`. Fixes #1597 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(ai-anthropic): keep message_start token counts when message_delta omits them `processAnthropicStream` built usage from the closing `message_delta` only. The SDK types its input and cache counts as nullable, and some Anthropic-compatible servers send only `output_tokens` there. aimock is one of them, so against aimock every stream reported 0 input tokens. Keep the `message_start` usage, and take each input or cache count that the delta leaves null or out from it. Counts that the delta sends still win, because they are cumulative. This is the same merge that the SDK's MessageStream does. It applies to every stop reason, so the `max_tokens` `RUN_ERROR` from the previous commit carries the input count too. The E2E spec drives a `max_tokens` stop through aimock and needs both commits to pass. Refs #1597 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(ai-anthropic): keep message_start server tool counts, one changeset The SDK's MessageStream also keeps `server_tool_use` from `message_start` when the closing `message_delta` has it null, and `buildAnthropicUsage` reads it. Fill it the same way as the input and cache counts. The SDK carries `iterations` too, but the adapter does not read it, so it stays as the delta sends it. Fold the two changesets of this branch into one. Refs #1597 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * refactor(ai-anthropic): reuse usage builder for stream fallback --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Tom Beckenham <34339192+tombeckenham@users.noreply.github.com>
nx run-many --targets=build --exclude=examples/**,testing/**
⌘K
fix(ai-anthropic): report token usage when a stream stops at max_tokens (#1609)
Succeeded
in
9s
Started
Linux
4 CPU cores
read
access token used
Copy command
nx run-many --targets=build --exclude=examples/**,testing/**
1594
aa2a2d3
GitHub
Open on GitHub
Tasks
Resource Usage
ta
TanStack
OSS