Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions integrations/langchain-py/src/braintrust_langchain/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,18 @@ def _get_metrics_from_response(response: LLMResult):
)
)

# Extract cache tokens from nested input_token_details (LangChain format)
# Maps to Braintrust's standard cache token metric names
input_token_details = usage_metadata.get("input_token_details")
if input_token_details and isinstance(input_token_details, dict):
cache_read = input_token_details.get("cache_read")
cache_creation = input_token_details.get("cache_creation")

if cache_read is not None:
metrics["prompt_cached_tokens"] = cache_read
if cache_creation is not None:
metrics["prompt_cache_creation_tokens"] = cache_creation

if not metrics or not any(metrics.values()):
llm_output: dict[str, Any] = response.llm_output or {}
metrics = llm_output.get("token_usage") or llm_output.get("estimatedTokens") or {}
Expand Down
Loading