Skip to content

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

Merged
copybara-service[bot] merged 1 commit into
google:mainfrom
hemasekhar-p:claude-token-usage-fix
Jun 26, 2026
Merged

fix: map token usage metadata for Anthropic Claude model#1261
copybara-service[bot] merged 1 commit into
google:mainfrom
hemasekhar-p:claude-token-usage-fix

Conversation

@hemasekhar-p

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.

@hemasekhar-p

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

@tilgalas tilgalas left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

thanks for the PR - left some minor comments, but overall LGTM

Comment thread core/src/test/java/com/google/adk/models/ClaudeTest.java Outdated
Comment thread core/src/test/java/com/google/adk/models/ClaudeTest.java
@hemasekhar-p hemasekhar-p force-pushed the claude-token-usage-fix branch from eca7395 to f76c5f9 Compare June 25, 2026 06:04
@copybara-service copybara-service Bot merged commit a27cfee into google:main Jun 26, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

3 participants