Skip to content

fix(ai-sdk): Make sure tokens are not double counted between parent/child spans#1379

Merged
Abhijeet Prasad (AbhiPrasad) merged 3 commits intomainfrom
abhi-ai-sdk-double-counting
Feb 19, 2026
Merged

fix(ai-sdk): Make sure tokens are not double counted between parent/child spans#1379
Abhijeet Prasad (AbhiPrasad) merged 3 commits intomainfrom
abhi-ai-sdk-double-counting

Conversation

@AbhiPrasad
Copy link
Member

@AbhiPrasad Abhijeet Prasad (AbhiPrasad) commented Feb 17, 2026

fixes https://linear.app/braintrustdata/issue/BRA-4144

supercedes #1377

Big thanks to Andrei Dorobantu (@andrewdoro) for the original PR! This is just their fix, cleaned up a bit to reduce redundant code.

When the model is wrapped by wrapAISDK, child doGenerate/doStream spans
already carry per-step token counts and estimated_cost. Previously, the
parent span (generateText, streamText, generateObject, streamObject, and
agent wrappers) also logged the same aggregated metrics, causing the
Braintrust backend to double-count both tokens and cost.

This change omits all token/cost metrics from the parent span when the
model is wrapped (i.e. child spans exist), so that metrics are only
reported once on the leaf LLM call spans.

Updated tests to assert that parent spans no longer carry token metrics
and that child doGenerate/doStream spans do.

Co-authored-by: Cursor <cursoragent@cursor.com>
@AbhiPrasad Abhijeet Prasad (AbhiPrasad) changed the title fix: fix(ai-sdk): Make sure tokens are not double counted between parent/child spans Feb 17, 2026
@AbhiPrasad Abhijeet Prasad (AbhiPrasad) merged commit 1c827b6 into main Feb 19, 2026
99 of 101 checks passed
@AbhiPrasad Abhijeet Prasad (AbhiPrasad) deleted the abhi-ai-sdk-double-counting branch February 19, 2026 16:37
Abhijeet Prasad (AbhiPrasad) added a commit that referenced this pull request Feb 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants