[Bug]: Bedrock LLM prompt_tokens leaves out cache read/write tokens
Description
When using the native Bedrock provider with prompt caching, UsageMetrics.prompt_tokens undercounts billed input. Bedrock Converse reports usage.inputTokens as the non-cached input only; cache hits and writes are separate (cacheReadInputTokens / cacheWriteInputTokens). AWS documents total input as the sum of those three, and totalTokens already includes them.
BedrockCompletion._track_token_usage_internal currently does:
self._token_usage["prompt_tokens"] += input_tokens # inputTokens only
self._token_usage["cached_prompt_tokens"] += cached_tokens # read only
# cache writes never recorded; prompt+completion != totalTokens
The Anthropic provider in this repo already folds cache_read_input_tokens + cache_creation_input_tokens into input_tokens before tracking.
Steps to Reproduce
Offline with a mocked converse response (no live AWS needed):
from unittest.mock import patch
from crewai.llm import LLM
llm = LLM(model="bedrock/anthropic.claude-3-5-sonnet-20241022-v2:0")
usage = {
"inputTokens": 10,
"outputTokens": 5,
"totalTokens": 18906,
"cacheReadInputTokens": 18891,
"cacheWriteInputTokens": 0,
}
with patch.object(llm._client, "converse") as mock_converse:
mock_converse.return_value = {
"output": {"message": {"role": "assistant", "content": [{"text": "OK"}]}},
"usage": usage,
"stopReason": "end_turn",
}
llm.call("Hello")
print(llm._token_usage)
# main: prompt_tokens=10, cached_prompt_tokens=18891, total_tokens=18906
# expected: prompt_tokens=18901 (== 10+18891), prompt+completion == total
Same gap on the first (cache-write) turn: cacheWriteInputTokens is ignored entirely (cache_creation_tokens stays 0).
Expected behavior
prompt_tokens == inputTokens + cacheRead + cacheWritecached_prompt_tokens == cacheReadcache_creation_tokens == cacheWriteprompt_tokens + completion_tokens == total_tokens(whentotalTokensis consistent)
Actual behavior
prompt_tokens= non-cachedinputTokensonly- cache writes not tracked
prompt + completion != totalon cached turns
CrewAI version
main @ 7abc196 (2026-11-06)
Additional context
HUMAN: I hit this while checking Bedrock prompt-caching metrics in crewAI: on a cache-hit turn, prompt_tokens stayed at the non-cached input count while total_tokens already included the cache read, so the parts didn't add up. Looking at the Converse usage fields and how the Anthropic path in this repo folds cache read/write into input, Bedrock wasn't doing the same. Happy to take a fix once this is confirmed.
Thanks for the offline repro and the exact expected numbers, which made it quick to check the three open fixes against real Bedrock output.
This looks like the same bug as #8626 (opened 10-26, PR #8675); #9191 and #9269 target this issue. I fed one real Converse usage pair (Claude Haiku 4.5, us-east-1, cache write then cache read) to LLM.call() with a mocked converse, on main 09ec5d9 and on each PR merged into it:
| main | #8675 | #9191 | #9269 | |
|---|---|---|---|---|
prompt_tokens (write / read) |
10 / 10 | 6636 / 6636 | 6636 / 6636 | 6636 / 6636 |
cache_creation_tokens (write) |
0 | 6626 | 6626 | 6626 |
prompt + completion == total |
❌ | ✅ | ✅ | ✅ |
UsageMetrics.from_provider_dict(usage) prompt / total |
0 / 0 | 6636 / 6641 | 0 / 0 | 0 / 0 |
| same, with open #8198 (for #8197) also merged | 10 / 6641 | conflict in usage_metrics.py |
10 / 6641 | 10 / 6641 |
test_bedrock.py + PR test files |
48 passed | 63 passed | 49 passed | 1 failed, 48 passed |
The flow aggregator reads Bedrock usage through from_provider_dict (flow/runtime/__init__.py:958, raw Converse usage from bedrock/completion.py:933). Of the three, only #8675 changes that side; with #8198 plus #9191 or #9269, _token_usage says 6636 prompt tokens and the flow side says 10. The #9269 failure is the existing test_bedrock_cached_token_tracking still expecting prompt_tokens == 100 (Bugbot flagged it too). gh pr checks shows no test run on any of the three yet.
Happy to rerun another combination if that helps.
Live usage, script, and output
Converse with us.anthropic.claude-haiku-4-5-20251001-v1:0 in us-east-1: a ~6.6k-token system block plus cachePoint, called twice (cacheDetails dropped):
first {"cacheReadInputTokens": 0, "cacheWriteInputTokens": 6626, "inputTokens": 10, "outputTokens": 5, "totalTokens": 6641}
second {"cacheReadInputTokens": 6626, "cacheWriteInputTokens": 0, "inputTokens": 10, "outputTokens": 5, "totalTokens": 6641}
Script, run with each branch checked out. m-N is main 09ec5d9 with that PR head merged (no conflicts), and m-N-8198 adds #8198 on top: #8675 16055cd, #9191 8639f05, #9269 f64d61c, #8198 9aa06bc.
import json, sys
from unittest.mock import patch
from crewai.llm import LLM
from crewai.types.usage_metrics import UsageMetrics
label, usages = sys.argv[2], json.load(open(sys.argv[1]))
for turn, usage in zip(("write", "read"), usages):
llm = LLM(model="bedrock/us.anthropic.claude-haiku-4-5-20251001-v1:0")
with patch.object(llm._client, "converse") as m:
m.return_value = {
"output": {"message": {"role": "assistant", "content": [{"text": "OK"}]}},
"usage": dict(usage),
"stopReason": "end_turn",
}
llm.call("Hello")
t = llm._token_usage
f = UsageMetrics.from_provider_dict(dict(usage))
print(
f"{label:8} {turn:5} prompt={t['prompt_tokens']} completion={t['completion_tokens']} "
f"cached={t['cached_prompt_tokens']} cache_creation={t['cache_creation_tokens']} "
f"total={t['total_tokens']} sum_ok={t['prompt_tokens'] + t['completion_tokens'] == t['total_tokens']} "
f"| from_provider_dict prompt={f.prompt_tokens if f else None} total={f.total_tokens if f else None}"
)
main write prompt=10 completion=5 cached=0 cache_creation=0 total=6641 sum_ok=False | from_provider_dict prompt=0 total=0
main read prompt=10 completion=5 cached=6626 cache_creation=0 total=6641 sum_ok=False | from_provider_dict prompt=0 total=0
m-8675 write prompt=6636 completion=5 cached=0 cache_creation=6626 total=6641 sum_ok=True | from_provider_dict prompt=6636 total=6641
m-8675 read prompt=6636 completion=5 cached=6626 cache_creation=0 total=6641 sum_ok=True | from_provider_dict prompt=6636 total=6641
m-9191 write prompt=6636 completion=5 cached=0 cache_creation=6626 total=6641 sum_ok=True | from_provider_dict prompt=0 total=0
m-9191 read prompt=6636 completion=5 cached=6626 cache_creation=0 total=6641 sum_ok=True | from_provider_dict prompt=0 total=0
m-9269 write prompt=6636 completion=5 cached=0 cache_creation=6626 total=6641 sum_ok=True | from_provider_dict prompt=0 total=0
m-9269 read prompt=6636 completion=5 cached=6626 cache_creation=0 total=6641 sum_ok=True | from_provider_dict prompt=0 total=0
m-8198 write prompt=10 completion=5 cached=0 cache_creation=0 total=6641 sum_ok=False | from_provider_dict prompt=10 total=6641
m-8198 read prompt=10 completion=5 cached=6626 cache_creation=0 total=6641 sum_ok=False | from_provider_dict prompt=10 total=6641
m-9191-8198 write prompt=6636 completion=5 cached=0 cache_creation=6626 total=6641 sum_ok=True | from_provider_dict prompt=10 total=6641
m-9191-8198 read prompt=6636 completion=5 cached=6626 cache_creation=0 total=6641 sum_ok=True | from_provider_dict prompt=10 total=6641
m-9269-8198 write prompt=6636 completion=5 cached=0 cache_creation=6626 total=6641 sum_ok=True | from_provider_dict prompt=10 total=6641
m-9269-8198 read prompt=6636 completion=5 cached=6626 cache_creation=0 total=6641 sum_ok=True | from_provider_dict prompt=10 total=6641
git merge-tree --write-tree m-8675 <#8198 head>: CONFLICT (content): Merge conflict in lib/crewai/src/crewai/types/usage_metrics.py. The other pairs merge cleanly.
Tests: pytest lib/crewai/tests/llms/bedrock/test_bedrock.py (plus test_bedrock_usage.py on m-8675). test_bedrock_raises_error_when_model_not_found is deselected because it fails on main too in an env without litellm (ImportError: ... the LiteLLM fallback package is not installed).
main 48 passed, 1 skipped
m-8675 63 passed, 1 skipped
m-9191 49 passed, 1 skipped
m-9269 FAILED lib/crewai/tests/llms/bedrock/test_bedrock.py::test_bedrock_cached_token_tracking
E assert 130 == 100
1 failed, 48 passed, 1 skipped (same on the #9269 head alone)