Skip to content

fix: explicitly merge usage fields in Accumulate using JSON presence checks#3

Merged
ibetitsmike merged 3 commits intofantasyfrom
mike/fix-usage-accumulator
Apr 8, 2026
Merged

fix: explicitly merge usage fields in Accumulate using JSON presence checks#3
ibetitsmike merged 3 commits intofantasyfrom
mike/fix-usage-accumulator

Conversation

@ibetitsmike
Copy link
Copy Markdown

This PR was authored by Mux on behalf of Mike.

When a MessageDeltaEvent arrives during streaming, Accumulate() previously only assigned acc.Usage.OutputTokens. For non-server-tool conversations, Anthropic's message_delta only includes output_tokens in the JSON payload, omitting input_tokens and cache token fields.

This patch uses Raw() != respjson.Omitted to check actual JSON field presence before updating each usage field. Fields omitted from the delta preserve their values from the initial message_start event.

Adds three test cases covering input+output accumulation, cache token preservation, and confirming omitted delta fields do not clobber message_start values.

The examples go.mod was using github.com/anthropics/anthropic-sdk-go
in its replace directive and require block, but the root module and all
example Go files use github.com/charmbracelet/anthropic-sdk-go. This
mismatch caused build failures.
@ibetitsmike ibetitsmike marked this pull request as ready for review April 8, 2026 16:38
@ibetitsmike ibetitsmike merged commit 8345653 into fantasy Apr 8, 2026
4 checks passed
@ibetitsmike ibetitsmike deleted the mike/fix-usage-accumulator branch April 8, 2026 16:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants