Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix: map token usage metadata for Anthropic Claude model#1261

Open
hemasekhar-p wants to merge 1 commit into
google:main from
hemasekhar-p:claude-token-usage-fix
Open

fix: map token usage metadata for Anthropic Claude model #1261
hemasekhar-p wants to merge 1 commit into
google:main from
hemasekhar-p:claude-token-usage-fix

Conversation

@hemasekhar-p

@hemasekhar-p hemasekhar-p commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

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 Claude model wrapper in the ADK Java currently droping the token usage information returned by the Anthropic API. Because of this, LlmResponse.usageMetadata() returns Optional.empty() for Claude-backed agents.

Solution:
I have updated convertAnthropicResponseToLlmResponse in Claude.java to extract inputTokens and outputTokens from the Anthropic Message.usage() payload. The values are mapped to GenerateContentResponseUsageMetadata

Testing Plan

Unit Tests:

  • I have added or updated unit tests for my change.
  • All unit tests pass locally.

Added testClaudeUsageMapping_ShouldMapTokensCorrectly to ClaudeTest.java. The test verifies that inputTokens, outputTokens, and totalTokenCount are correctly mapped from the Anthropic SDK response to the ADK UsageMetadata builder.

Checklist

  • I have read the CONTRIBUTING.md document.
  • My pull request contains a single commit.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.
  • I have manually tested my changes end-to-end.

Copy link
Copy Markdown
Contributor Author

@tilgalas, Could you please review this PR.

@rogerzxu rogerzxu left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the same implementation I used in my source-forked copy and confirmed it works, so LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

1 more reviewer

@rogerzxu rogerzxu rogerzxu approved these changes

Reviewers whose approvals may not affect merge requirements

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Claude (Anthropic) model wrapper drops token usage — Event.usageMetadata() is always empty

AltStyle によって変換されたページ (->オリジナル) /