Repository navigation
fix(ai): use cumulative input and cache tokens from Anthropic message_delta when streaming - #5352
Merged
marandaneto merged 1 commit intoOct 21, 2026
Conversation
marandaneto
approved these changes
Oct 21, 2026
marandaneto
left a comment
Member
There was a problem hiding this comment.
The code looks good; no qualifying findings. Verdict: correct.
…_delta when streaming Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
marandaneto
force-pushed
the
fix/ai-anthropic-stream-cumulative-input-usage
branch
from
October 21, 2026 14:15
4256e04 to
5214612
Compare
marandaneto
enabled auto-merge
October 21, 2026 14:17
Open
2 tasks done
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.
Problem
@posthog/ai's Anthropic streaming wrapper only readsoutput_tokens(and the web search count) frommessage_delta. Anthropic'smessage_deltausage is cumulative: with server tools (web search, web fetch, code execution) itsinput_tokens,cache_creation_input_tokensandcache_read_input_tokensgrow past themessage_startvalues during the response. Streamed generations therefore under-report$ai_input_tokensand the cache counts.This is the same bug that was fixed for
posthog-pythonin PostHog/posthog-python#1074.Changes
In
packages/ai/src/anthropic/index.ts, themessage_deltausage now also updatesinputTokens,cacheCreationInputTokensandcacheReadInputTokens, keeping the larger of themessage_startvalue and the delta value (totals never shrink). A field the delta omits or under-reports stays at themessage_startvalue.rawUsagemerging is unchanged.A patch changeset for
@posthog/aiis included.Test strategy
packages/ai/tests/anthropic.test.ts, newPostHogAnthropic - streaming usageblock, no API key needed): one test wheremessage_deltareports larger cumulative input/cache counts (expects 120 / 30 / 70 instead of themessage_start50 / 10 / 20), and one where the delta omits or under-reports them (expects themessage_startvalues to stay). The first fails onmain; the second guards against regressions.@posthog/ai, pointed the real@anthropic-ai/sdkthroughPostHogAnthropicat a local HTTP server that streams a real SSE sequence (message_startwith input 50 / cache 10 / 20, finalmessage_deltawith input 120 / cache 30 / 70 and output 25), and read the captured event. Onmainthe event reports input 50, cache creation 10, cache read 20, output 25. With this change it reports input 120, cache creation 30, cache read 70, output 25. No API keys were used.tests/anthropic.test.tspasses (9 passed; the remaining tests in that file are skipped withoutANTHROPIC_API_KEY),oxlintreports 0 errors andoxfmt --checkis clean on the touched files.message_startmissing (previous?? 0behavior kept).Release info Sub-libraries affected
Libraries affected
Checklist
If releasing new changes
pnpm changesetto generate a changeset file🤖 Agent context
Autonomy: Fully autonomous
posthog-pythonfix, and the change, tests, smoke run and this description were produced by the agent. Agent-authored, needs human review.message_startandmessage_deltavalues rather than overwriting, since it also tolerates a delta that omits or under-reports a field.🤖 Generated with Claude Code