[Bug]: Bedrock LLM prompt_tokens leaves out cache read/write tokens

オープン
#9,190 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

@lalith0407 がすでに取り組んでいます。

2026年11月6日 から。

  • #9191 @lalith0407 による — オープン
  • #9269 @2Zest による — オープン

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
25/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
停滞
技術スタック
python

調査の方向性

Start in the Bedrock provider at BedrockCompletion._track_token_usage_internal, named in the issue, and compare its accounting with the Anthropic provider’s cache token handling. Check the mocked Converse usage example and the existing token usage tests; done means prompt_tokens includes input, cache read, and cache write, cached_prompt_tokens and cache_creation_tokens track their respective fields, and the totals add up when consistent.

索引モデルが issue の本文から書いたものです。

説明

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 + cacheWrite
  • cached_prompt_tokens == cacheRead
  • cache_creation_tokens == cacheWrite
  • prompt_tokens + completion_tokens == total_tokens (when totalTokens is consistent)
Actual behavior
  • prompt_tokens = non-cached inputTokens only
  • cache writes not tracked
  • prompt + completion != total on 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.

主要言語
Python
スター
60.6k
フォーク
9.1k
平均マージ
1日 2時間
マージ済み PR(30日)
214

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

crewAIInc/crewAI のほかの issue

crewAIInc/crewAI の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。