get_token_usage_for_bedrock always returns 0 (reads wrong response_metadata keys for langchain-aws ChatBedrock/ChatBedrockConverse)
First, thank you for ragas — the CostCallbackHandler / TokenUsage abstraction makes per-run cost tracking really convenient, and we rely on it for Bedrock-backed evaluations.
Problem
get_token_usage_for_bedrock in src/ragas/cost.py always returns TokenUsage(input_tokens=0, output_tokens=0, model=""), so the computed cost for any Amazon Bedrock model is always 0.
The parser reads:
input_tokens = get_from_dict(g.message.response_metadata, "usage.prompt_tokens", 0)
output_tokens = get_from_dict(g.message.response_metadata, "usage.completion_tokens", 0)
model = get_from_dict(g.message.response_metadata, "model_id", "")i.e. it expects response_metadata["usage"]["prompt_tokens"] and response_metadata["model_id"].
Root cause
langchain-aws ChatBedrock / ChatBedrockConverse do not put token usage or model_id under response_metadata. Verified against the langchain-aws source:
ChatBedrock(chat_models/bedrock.py): exposes counts on the message-levelusage_metadata({"input_tokens", "output_tokens", "total_tokens"}), mirrored inadditional_kwargs["usage"]({"prompt_tokens", "completion_tokens", ...}), and setsresponse_metadata = {"model_provider": "bedrock", "model_name": self.model_id}.ChatBedrockConverse(chat_models/bedrock_converse.py): exposes counts only on the message-levelusage_metadata, and sets the model underresponse_metadata["model_name"].
So the keys response_metadata["usage"] and response_metadata["model_id"] never exist → get_from_dict falls back to 0 / "" for every Bedrock call.
The existing unit fixtures in tests/unit/test_cost.py (bedrock_llama_result, bedrock_claude_result) were constructed with response_metadata={"usage": {...}, "model_id": ...}, which matches the buggy parser but not real langchain-aws output — so the bug is invisible to the current tests.
Reproduction
Build an LLMResult in the real ChatBedrock shape and parse it:
from langchain_core.messages import AIMessage
from langchain_core.outputs import ChatGeneration, LLMResult
from ragas.cost import get_token_usage_for_bedrock
result = LLMResult(generations=[[ChatGeneration(message=AIMessage(
content="Hello, world!",
usage_metadata={"input_tokens": 10, "output_tokens": 10, "total_tokens": 20},
response_metadata={"model_provider": "bedrock",
"model_name": "us.anthropic.claude-3-5-sonnet-20240620-v1:0"},
))]], llm_output={})
print(get_token_usage_for_bedrock(result))
# current: input_tokens=0 output_tokens=0 model='' -> cost is always 0
# expected: input_tokens=10 output_tokens=10 model='us.anthropic...'Proposed fix
Read token counts from message.usage_metadata (with additional_kwargs["usage"] as a backward-compatible fallback) and the model from response_metadata["model_name"]. I have a minimal patch + corrected fixtures ready and will open a PR.
This issue was prepared with the help of an AI agent (Claude Code); a human reviewed the reproduction and root cause against the langchain-aws source before filing.
Source: vibrantlabsai/ragas