fix(core): use aggregate usage consistently across providers#1366
Conversation
🦋 Changeset detectedLatest commit: bb057e6 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (4)
📝 WalkthroughWalkthroughThis PR simplifies ChangesAggregate Finish Usage Consistency
Sequence Diagram(s)sequenceDiagram
participant Provider
participant streamText
participant normalizeFinishUsageStream
participant resolveFinishUsage
participant onEndHook
Provider->>streamText: stream finish-step/finish events (usage, totalUsage, providerMetadata)
streamText->>normalizeFinishUsageStream: forward stream parts
normalizeFinishUsageStream-->>streamText: yield parts unchanged
streamText->>resolveFinishUsage: totalUsage, usage
resolveFinishUsage-->>streamText: totalUsage ?? usage (provider-independent)
streamText->>onEndHook: output.usage = resolved aggregate usage
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
Deploying voltagent with
|
| Latest commit: |
bb057e6
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://019779e1.voltagent.pages.dev |
| Branch Preview URL: | https://fix-consistent-usage-totals.voltagent.pages.dev |
PR Checklist
Please check if your PR fulfills the following requirements:
Bugs / Features
What is the current behavior?
Finish usage resolution prefers aggregate
totalUsagefor most providers, but switches to last-stepusagefor Anthropic/cache cases. That makes root span usage andonEnd.output.usageprovider-dependent during tool-call runs.The stream normalizer also rewrites finish
totalUsageto the last step for Anthropic/cache streams.What is the new behavior?
resolveFinishUsagenow consistently prefers aggregatetotalUsagewhenever AI SDK provides it, falling back tousageonly when no aggregate is available.normalizeFinishUsageStreamnow preserves finishtotalUsageinstead of rewriting it by provider/cache metadata. Updated tests cover Anthropic/cache cases and assert aggregate usage is preserved.fixes #1360
Related to #1359. The dashboard/export total fallback that double-counted cached/reasoning breakdowns is handled in companion PR: https://github.com/VoltAgent/console/pull/44
Notes for reviewers
Docs were not updated because this restores consistent bug-fix behavior rather than introducing a new public API.
Summary by CodeRabbit