Repository navigation
Labels
agentsUsage: [Issues, PRs], Target: Single agentUsage: [Issues, PRs], Target: Single agentpythonUsage: [Issues, PRs], Target: PythonUsage: [Issues, PRs], Target: PythonreproducedUsage: [Issues], Target: all issues that can be reproduced by the triage workflowUsage: [Issues], Target: all issues that can be reproduced by the triage workflow
Description
Description
Same root cause as #9808 (AnthropicClient, fixed by #9859), in two more connectors that #9859 did not touch.
BedrockChatClient._parse_usagemaps ConverseinputTokenstoinput_token_count. With prompt caching, Bedrock reports cache reads and writes separately incacheReadInputTokens/cacheWriteInputTokensandinputTokenscovers only the uncached part (AWS prompt caching docs).totalTokensdoes include the cache, so on main the usage is internally inconsistent: input 12 + output 40 but total 9352.ClaudeAgentbuildsinput_token_countfrom the Claude Agent SDKResultMessage.usage["input_tokens"], which is Anthropic's uncached-only field, and computestotal_token_count = input + output, so both undercount.
input_token_count is the total input for the OpenAI and Gemini clients and is exported as gen_ai.usage.input_tokens, which the OTel GenAI semconv says should include cached tokens.
Observed Behavior (main 6fdeb2d)
Prompt = 12 uncached + 300 cache write + 9000 cache read:
[bedrock] input_token_count=12 total=9352 cache_read=9000 cache_write=300
[claude ] input_token_count=12 total=52 cache_read=9000 cache_write=300
Expected Behavior
input_token_count == 9312 for both; ClaudeAgent total_token_count == 9352. Cache fields unchanged.
Steps to Reproduce
Offline (stub Bedrock runtime, mocked ClaudeSDKClient, no keys):
repro.py
"""Offline repro (no AWS/Anthropic calls, no keys): Bedrock and ClaudeAgent report input_token_count
without cached prompt tokens. Prompt = 12 uncached + 300 cache write + 9000 cache read = 9312 tokens."""
import asyncio
from typing import Any
from unittest.mock import AsyncMock, MagicMock, patch
from agent_framework import Content, Message
from agent_framework_bedrock import BedrockChatClient
USAGE = {"inputTokens": 12, "outputTokens": 40, "totalTokens": 9352,
"cacheReadInputTokens": 9000, "cacheWriteInputTokens": 300}
class Runtime:
def converse(self, **kw: Any) -> dict[str, Any]:
return {"modelId": kw["modelId"], "usage": USAGE,
"output": {"completionReason": "end_turn",
"message": {"role": "assistant", "content": [{"text": "hi"}]}}}
async def bedrock() -> None:
client = BedrockChatClient(model="anthropic.claude-sonnet", region="us-west-2", client=Runtime()) # type: ignore[arg-type]
r = await client.get_response(messages=[Message(role="user", contents=[Content.from_text(text="hi")])])
u = r.usage_details or {}
print(f"[bedrock] input_token_count={u.get('input_token_count')} total={u.get('total_token_count')} "
f"cache_read={u.get('cache_read_input_token_count')} cache_write={u.get('cache_creation_input_token_count')}")
async def claude() -> None:
from claude_agent_sdk import AssistantMessage, ResultMessage, TextBlock
from agent_framework_claude import ClaudeAgent
async def gen():
yield AssistantMessage(content=[TextBlock(text="hi")], model="claude-sonnet")
yield ResultMessage(subtype="success", duration_ms=1, duration_api_ms=1, is_error=False, num_turns=1,
session_id="s", stop_reason="end_turn",
usage={"input_tokens": 12, "output_tokens": 40,
"cache_creation_input_tokens": 300, "cache_read_input_tokens": 9000})
m = MagicMock()
for name in ("connect", "disconnect", "query", "set_model", "set_permission_mode"):
setattr(m, name, AsyncMock())
m.receive_response = MagicMock(return_value=gen())
with patch("agent_framework_claude._agent.ClaudeSDKClient", return_value=m):
r = await ClaudeAgent().run("hello")
u = r.usage_details or {}
print(f"[claude ] input_token_count={u.get('input_token_count')} total={u.get('total_token_count')} "
f"cache_read={u.get('cache_read_input_token_count')} cache_write={u.get('cache_creation_input_token_count')}")
asyncio.run(bedrock()); asyncio.run(claude())
print("expected: input_token_count=9312 (12 + 300 + 9000) for both; claude total=9352")Notes
- Behavior change for anyone who relied on the old value (two existing unit tests assert it), so flagging it here as Python: [Bug]: AnthropicClient reports input_token_count (and gen_ai.usage.input_tokens) without cached prompt tokens #9808 did.
- Package versions: agent-framework-bedrock / agent-framework-claude from main 6fdeb2d.
Metadata
Metadata
Labels
agentsUsage: [Issues, PRs], Target: Single agentUsage: [Issues, PRs], Target: Single agentpythonUsage: [Issues, PRs], Target: PythonUsage: [Issues, PRs], Target: PythonreproducedUsage: [Issues], Target: all issues that can be reproduced by the triage workflowUsage: [Issues], Target: all issues that can be reproduced by the triage workflow
Type
Projects
- StatusShow more project fieldsDone
Activity
github-actions commentedon Nov 2, 2026
🤖 Automated triage reproduction notes (agent-authored — trust but verify)
Agent analysis
Bedrock’s bug is in
python/packages/bedrock/agent_framework_bedrock/_chat_client.py::_parse_usage, while Claude’s is inpython/packages/claude/agent_framework_claude/_agent.pyResultMessage handling around lines 981-1003. Providing nonzero uncached, cache-read, and cache-write token counts leaves cached tokens out of aggregate input usage and Claude total usage. Reproduce by running the two modified focused usage tests.python/packages/bedrock/tests/test_bedrock_client.py, python/packages/claude/tests/test_claude_agent.pykimnamu commentedon Nov 6, 2026
Thanks lalith0407 for the exact numbers and the offline repro, they made the Bedrock half quick to check against the live API.
Live Converse and ConverseStream in us-east-1 agree with the issue:
inputTokensleaves out cache reads and writes whiletotalTokensincludes them, on every model that returned cache fields. Grok 4.7, Kimi K3 and GPT-6 Luna report the same split without anycachePoint, so on main a repeated Grok prompt already showsinput_token_count=0. With the #10003 source change applied, both live client runs below giveinput + output == total.BedrockChatClient, live, 2nd callcachePoint,input_token_countget_response(stream=True)cachePoint,input_token_countinput + output == totaltotal_token_count, cache fieldsProbe scripts, full output, unit test red/green
I only checked the Bedrock half, not
ClaudeAgent, and I'm leaving the fix with #10003. Claude (via Claude Code) assisted with the probes and this comment.