Skip to content

Commit 76ac43d

Browse files
chore(closes OPEN-8441): tokens not captured for Bedrock traced via the LangChain callback handler
1 parent d9f06f3 commit 76ac43d

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/openlayer/lib/integrations/langchain_callback.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"openai-chat": "OpenAI",
3030
"chat-ollama": "Ollama",
3131
"vertexai": "Google",
32+
"amazon_bedrock_converse_chat": "Bedrock",
3233
}
3334

3435

@@ -398,7 +399,8 @@ def _extract_token_info(
398399

399400
# Fallback to generation info for providers like Ollama/Google
400401
if not token_usage and response.generations:
401-
generation_info = response.generations[0][0].generation_info or {}
402+
gen = response.generations[0][0]
403+
generation_info = gen.generation_info or {}
402404

403405
# Ollama style
404406
if "prompt_eval_count" in generation_info:
@@ -417,6 +419,15 @@ def _extract_token_info(
417419
"completion_tokens": usage.get("candidates_token_count", 0),
418420
"total_tokens": usage.get("total_token_count", 0),
419421
}
422+
# AWS Bedrock / newer LangChain style - usage_metadata on the message
423+
elif hasattr(gen, "message") and hasattr(gen.message, "usage_metadata"):
424+
usage = gen.message.usage_metadata
425+
if usage:
426+
token_usage = {
427+
"prompt_tokens": usage.get("input_tokens", 0),
428+
"completion_tokens": usage.get("output_tokens", 0),
429+
"total_tokens": usage.get("total_tokens", 0),
430+
}
420431

421432
return {
422433
"prompt_tokens": token_usage.get("prompt_tokens", 0),

0 commit comments

Comments
 (0)