Repository navigation
fix(ai): take cumulative usage and web searches from the Claude Agent SDK message_delta - #1112
Merged
marandaneto merged 1 commit intoOct 23, 2026
Conversation
… SDK message_delta The tracker only read output_tokens from a message_delta. Its usage is cumulative, and with server tools (web search, ...) the input and cache counts exceed the message_start values, so streamed generations under-reported them, and the number of web searches was never recorded. Keep the larger input and cache counts, as the Anthropic wrapper does, and set $ai_web_search_count from server_tool_use.web_search_requests. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
pcbeingused333
force-pushed
the
fix/claude-agent-sdk-message-delta-usage
branch
from
October 23, 2026 06:52
3a71630 to
48feaee
Compare
marandaneto
approved these changes
Oct 23, 2026
marandaneto
left a comment
Member
There was a problem hiding this comment.
The code looks good; no qualifying findings. Verdict: correct.
marandaneto
enabled auto-merge (squash)
October 23, 2026 12:31
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.
💡 Motivation and Context
In the Claude Agent SDK integration,
_GenerationTrackertakes the input and cache token counts frommessage_startand, frommessage_delta, onlyoutput_tokens. Amessage_deltausage is cumulative: with server tools (web search, web fetch, code execution) its input and cache counts grow past themessage_startvalues, and it also carriesserver_tool_use.web_search_requests. Streamed generations therefore under-reported$ai_input_tokensand the cache counts, and$ai_web_search_countwas never set (the Anthropic wrapper in this repo sets all of them since #1074).The tracker now keeps the larger of the
message_startandmessage_deltavalues for the input, cache read and cache creation counts (a field the delta omits keeps itsmessage_startvalue), and sets$ai_web_search_countfromserver_tool_use.web_search_requestswhen it is positive.💚 How did you test it?
test_message_delta_usage_is_cumulative_with_server_toolsruns a stream whosemessage_starthas 120 input tokens and whosemessage_deltahas 5120 input, 300 output, 64 cache read, 32 cache creation and 2 web searches, and checks the captured$ai_generationproperties. It fails onmain(input 120, no cache or web search properties) and passes with the change.test_message_delta_without_input_counts_keeps_message_startchecks that a delta with onlyoutput_tokensleaves themessage_startcounts and does not add a web search count; it passes before and after.posthog/test/ai/claude_agent_sdk: 44 passed.📝 Checklist
If releasing new changes
sampo addto generate a changeset file (added.sampo/changesets/claude-agent-sdk-message-delta-usage.mdin the same format)🤖 Agent context
Autonomy: Fully autonomous