Repository navigation
fix(anthropic): fall back to message_start usage when message_delta omits fields - #41743
Open
Hunter Lovell (hntrl) wants to merge 2 commits into
Open
Hunter Lovell (hntrl) wants to merge 2 commits into
Hunter Lovell (hntrl) wants to merge 2 commits into
Conversation
…n delta reports cache creation
Hunter Lovell (hntrl)
marked this pull request as ready for review
November 9, 2026 22:08
Hunter Lovell (hntrl)
requested review from
ccurme (ccurme) and
Mason Daugherty (mdrxy)
as code owners
November 9, 2026 22:08
Baihao Wang (byhow)
approved these changes
Nov 9, 2026
Baihao Wang (byhow)
left a comment
Member
There was a problem hiding this comment.
non-blocking nit, up to you
| if message_start_usage is not None: | ||
| # Some providers (e.g. Bedrock) omit input/cache fields from | ||
| # `message_delta`; fall back to `message_start` for those. | ||
| update = {k: v for k, v in event.usage if v is not None} |
Member
There was a problem hiding this comment.
is there a better native way in py? like a js map or something
Baihao Wang (byhow)
approved these changes
Nov 9, 2026
This branch has not been deployed
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.
When streaming,
ChatAnthropicbuilt usage only frommessage_delta, so any input or cache field the delta left out reported as 0. Some providers (e.g. some Bedrock deployments) put cache counts only onmessage_start, and Anthropic's docs show output-only deltas. Streams now keep themessage_startusage and fill in any field the delta leaves null; fields the delta does report (e.g. cumulative counts after server tools) still win.message_startstill emits nousage_metadata. Mirrors langchain-ai/langchainjs#12145.Release note
Streamed
usage_metadatafromChatAnthropicno longer reports 0 input/cache tokens whenmessage_deltaomits those fields.Tested with new sync and async unit tests (output-only delta, delta without cache fields, full cumulative delta);
make lintand unit tests pass.