Repository navigation
fix(ai): keep latest cumulative output tokens across message_deltas - #1100
Merged
marandaneto merged 2 commits intoOct 22, 2026
Merged
marandaneto merged 2 commits into
marandaneto merged 2 commits into
Conversation
Member
|
thanks @Harsh23Kashyap
for next PRs |
marandaneto
approved these changes
Oct 22, 2026
Server tools such as web search can emit several message_delta events in one Anthropic stream, and each delta's usage is cumulative. The stream accumulator added every delta's output_tokens together, so a stream with two deltas reporting 50 then 110 cumulative tokens captured 160. Keep the latest cumulative value, matching how the input and cache counts are already handled and how the Anthropic SDK accumulates usage.
marandaneto
force-pushed
the
fix/anthropic-stream-delta-output-tokens
branch
from
October 22, 2026 10:24
f7d2700 to
3844c36
Compare
marandaneto
enabled auto-merge (squash)
October 22, 2026 10:25
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1099.
What
A streamed Anthropic generation over-reported output tokens when its stream contained more than one message_delta. Each delta's usage is cumulative, but the accumulator merged output_tokens incrementally, so a stream whose deltas reported 50 then 110 cumulative tokens captured 160.
Fix
For message_delta events, keep the latest cumulative output_tokens instead of adding it, the same way #1074 handles the input and cache counts, and matching how the Anthropic SDK accumulates usage ('cumulative totals, so they overwrite rather than add').
Tests
New sync and async tests pin a stream with two cumulative message_deltas (pause_turn then end_turn) to the final total of 110. Full posthog/test/ai/anthropic/test_anthropic.py: 47 passed, 1 skipped.