You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Streamed Anthropic generations stack output tokens when a stream emits more than one message_delta #1099
Stream an Anthropic response whose stream emits more than one message_delta event. This happens with server tools such as web search or code execution, which pause and resume the turn inside one stream.
Each message_delta carries cumulative usage, but the accumulator in posthog/ai/anthropic/_anthropic_stream.py merges output_tokens incrementally, so successive deltas stack.
Minimal repro:
from types import SimpleNamespace as NS
from posthog.ai.anthropic._anthropic_stream import _AnthropicStreamAccumulator
110, the final cumulative total. The Anthropic SDK accumulates stream usage by overwriting on each message_delta ('Usage counts on a message_delta are cumulative totals, so they overwrite rather than add', anthropic-sdk-python lib/streaming/_messages.py). #1074 fixed the same stacking for input and cache counts; output_tokens still stacks.
Actual Result
160 (50 + 110 stacked). Streamed generations over-report output tokens whenever a stream contains more than one message_delta.
Activity