Python: surface cache and reasoning token counts for the Bedrock and Gemini connectors#6640
Merged
eavanvalkenburg merged 6 commits intoJun 24, 2026
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Surfaces Bedrock Converse prompt-cache token counts in the framework’s canonical usage_details so cached prompts don’t report zero cache usage, aligning Bedrock behavior with other connectors that already populate these fields.
Changes:
- Map Bedrock Converse
cacheReadInputTokens→cache_read_input_token_countandcacheWriteInputTokens→cache_creation_input_token_countinBedrockChatClient._parse_usage. - Add a Bedrock unit test asserting the cache token counts are surfaced.
- Also includes Gemini usage parsing + tests for cached/thinking token counts (note: this expands scope beyond the PR title/description).
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| python/packages/bedrock/agent_framework_bedrock/_chat_client.py | Map Bedrock Converse cache token fields into canonical UsageDetails keys. |
| python/packages/bedrock/tests/test_bedrock_client.py | Add unit test covering Bedrock cache token parsing. |
| python/packages/gemini/agent_framework_gemini/_chat_client.py | Add parsing of Gemini cached/thinking token counts into canonical usage fields. |
| python/packages/gemini/tests/test_gemini_client.py | Extend test helpers and add a test for Gemini cached/reasoning usage fields. |
eavanvalkenburg
approved these changes
Jun 22, 2026
eavanvalkenburg
left a comment
Member
There was a problem hiding this comment.
Please fix the comment from copilot
Contributor
Python Test Coverage Report •
Python Unit Test Overview
|
||||||||||||||||||||||||||||||||||||||||
e361f2d to
21ce782
Compare
21ce782 to
3daf7cc
Compare
Member
|
@he-yufeng there are still comments form copilot that need to be addressed or closed with a reason |
…e-yufeng/agent-framework into fix/bedrock-usage-cache-tokens
Matches the UsageDetails | None return annotation and the Gemini connector's behavior, so a usage payload with no recognized keys no longer propagates an empty mapping. Adds a regression test.
Contributor
Author
|
@eavanvalkenburg both Copilot comments are addressed:
Both test suites pass locally (Bedrock 9 passed, Gemini green) and ruff is clean on the changed files. |
eavanvalkenburg
approved these changes
Jun 24, 2026
rogerbarreto
approved these changes
Jun 24, 2026
This was referenced Jun 24, 2026
Closed
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.
Motivation & Context
Several connectors drop token counts that
UsageDetailsalready has canonical fields for, so cache and reasoning usage silently reads as zero for those providers. This PR fixes that for the two connectors that were missing it:cacheReadInputTokens(input tokens served from a cache) andcacheWriteInputTokens(input tokens written to a cache) when prompt caching is active, but_parse_usagedropped both.cached_content_token_countandthoughts_token_count, but_parse_usagedropped both.UsageDetailsalready definescache_read_input_token_count,cache_creation_input_token_count, andreasoning_output_token_count, and the OpenAI/Anthropic connectors already populate the cache fields, so these two connectors were the odd ones out. The change is the same smallis not Nonemapping pattern in each connector's_parse_usage.Description & Review Guide
BedrockChatClient._parse_usagemapscacheReadInputTokens -> cache_read_input_token_countandcacheWriteInputTokens -> cache_creation_input_token_count.GeminiChatClient._parse_usagemapscached_content_token_count -> cache_read_input_token_countandthoughts_token_count -> reasoning_output_token_count._parse_usagenow returnsdetails or None(matching itsUsageDetails | Noneannotation and the Gemini connector), so a usage payload with no recognized keys no longer propagates an empty mapping.UsageDetailskeys.Added focused tests in both packages (
test_parse_usage_surfaces_cache_tokens,test_parse_usage_returns_none_when_no_recognized_keys,test_get_response_usage_details_includes_cached_and_reasoning_tokens) and ran both suites locally (Bedrock: 9 passed; Gemini suite green).ruff checkandruff format --checkpass on the changed files.