fix: map token usage metadata for Anthropic Claude model#1261
Open
hemasekhar-p wants to merge 1 commit into
Open
fix: map token usage metadata for Anthropic Claude model#1261hemasekhar-p wants to merge 1 commit into
hemasekhar-p wants to merge 1 commit into
Conversation
5f38b49 to
eca7395
Compare
Contributor
Author
|
@tilgalas, Could you please review this PR. |
rogerzxu
approved these changes
Jun 12, 2026
rogerzxu
left a comment
There was a problem hiding this comment.
This is the same implementation I used in my source-forked copy and confirmed it works, so LGTM
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.
Link to Issue or Description of Change
1. Link to an existing issue (if applicable):
2. Or, if no issue exists, describe the change:
Problem:
The Anthropic
Claudemodel wrapper in the ADK Java currently droping the token usage information returned by theAnthropicAPI. Because of this,LlmResponse.usageMetadata()returnsOptional.empty()for Claude-backed agents.Solution:
I have updated
convertAnthropicResponseToLlmResponseinClaude.javato extractinputTokensandoutputTokensfrom the AnthropicMessage.usage()payload. The values are mapped toGenerateContentResponseUsageMetadataTesting Plan
Unit Tests:
Added
testClaudeUsageMapping_ShouldMapTokensCorrectlytoClaudeTest.java. The test verifies that inputTokens, outputTokens, and totalTokenCount are correctly mapped from the Anthropic SDK response to the ADKUsageMetadatabuilder.Checklist