Skip to content

[issue-6019] [P SDK] Fix cache tokens not tracked in Bedrock Claude streaming#6038

Draft
ollieagent[bot] wants to merge 1 commit intomainfrom
ollie/issue-6019-add-cache-tokens-to-usage-057507
Draft

[issue-6019] [P SDK] Fix cache tokens not tracked in Bedrock Claude streaming#6038
ollieagent[bot] wants to merge 1 commit intomainfrom
ollie/issue-6019-add-cache-tokens-to-usage-057507

Conversation

@ollieagent
Copy link
Copy Markdown
Contributor

@ollieagent ollieagent Bot commented Apr 1, 2026

Details

In ClaudeAggregator.aggregate(), the message_start chunk handler only extracted input_tokens and output_tokens from the usage dict. The cache token fields (cache_creation_input_tokens, cache_read_input_tokens) were present in the Anthropic streaming response but were never read, so anthropic_to_bedrock_usage() always received zeros for them — causing cacheWriteInputTokens and cacheReadInputTokens to be permanently 0 in the logged span usage.

The fix initializes both cache token variables before the chunk loop, extracts them from the message_start chunk's message.usage dict, and includes them in the call to anthropic_to_bedrock_usage(). The non-streaming path was already correct (it passes the full usage dict from the response body).

Change checklist

  • User facing
  • Documentation update

Issues

Testing

New unit tests in tests/unit/integrations/bedrock/test_claude_aggregator.py:

  • test_claude_aggregator__cache_tokens_in_message_start__included_in_usage — verifies that cacheWriteInputTokens and cacheReadInputTokens are populated from the message_start chunk when cache tokens are present.
  • test_claude_aggregator__no_cache_tokens__defaults_to_zero — verifies the zero-default behavior is preserved when cache tokens are absent.

To run:

cd sdks/python
python -m pytest tests/unit/integrations/bedrock/test_claude_aggregator.py -v

Documentation

No documentation updates needed.

…ing usage

Extract cache_creation_input_tokens and cache_read_input_tokens from the
message_start chunk in ClaudeAggregator.aggregate() and pass them to
anthropic_to_bedrock_usage(), so cacheWriteInputTokens and
cacheReadInputTokens are correctly tracked in streaming responses.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@github-actions github-actions Bot added python Pull requests that update Python code tests Including test files, or tests related like configuration. Python SDK labels Apr 1, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 1, 2026

Python SDK Unit Tests Results (Python 3.11)

1 tests   0 ✅  7s ⏱️
1 suites  0 💤
1 files    0 ❌  1 🔥

For more details on these errors, see this check.

Results for commit a4953bc.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 1, 2026

Python SDK Unit Tests Results (Python 3.12)

1 tests   0 ✅  7s ⏱️
1 suites  0 💤
1 files    0 ❌  1 🔥

For more details on these errors, see this check.

Results for commit a4953bc.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 1, 2026

Python SDK Unit Tests Results (Python 3.14)

1 tests   0 ✅  4s ⏱️
1 suites  0 💤
1 files    0 ❌  1 🔥

For more details on these errors, see this check.

Results for commit a4953bc.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 1, 2026

Python SDK Unit Tests Results (Python 3.13)

1 tests   0 ✅  8s ⏱️
1 suites  0 💤
1 files    0 ❌  1 🔥

For more details on these errors, see this check.

Results for commit a4953bc.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 1, 2026

Python SDK Unit Tests Results (Python 3.10)

1 tests   0 ✅  8s ⏱️
1 suites  0 💤
1 files    0 ❌  1 🔥

For more details on these errors, see this check.

Results for commit a4953bc.

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

Labels

Made by Ollie 🦉 Python SDK python Pull requests that update Python code tests Including test files, or tests related like configuration.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: bedrock claude adapter doesn't save cache tokens usage

0 participants