Skip to content

support prompt caching token tracking in langchain#1250

Merged
Olmo Maldonado (ibolmo) merged 13 commits intomainfrom
fix/langchain-cache-tokens
Jan 24, 2026
Merged

support prompt caching token tracking in langchain#1250
Olmo Maldonado (ibolmo) merged 13 commits intomainfrom
fix/langchain-cache-tokens

Conversation

@colinbennettbrain
Copy link
Copy Markdown
Contributor

  • Extract cache_read and cache_creation from LangChain's nested input_token_details object
  • Map to Braintrust's standard metric names (prompt_cached_tokens, prompt_cache_creation_tokens)

Problem

The LangChain callback handler only extracts top-level fields from usage_metadata (input_tokens, output_tokens, total_tokens) but not the nested input_token_details containing cache metrics. This causes:

  • Cache tokens showing as 0 in the experiments UI
  • Costs calculated at full uncached rate instead of discounted cache rate

direkt and others added 3 commits January 9, 2026 12:25
Extract cache_read and cache_creation from LangChain's nested
input_token_details object and map them to Braintrust's standard
metric names (prompt_cached_tokens, prompt_cache_creation_tokens).

This enables accurate cache token tracking in the UI and correct
cost calculations for cached prompts.

Fixes Pylon #10400

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@colinbennettbrain
Copy link
Copy Markdown
Contributor Author

Added a focused unit test for cache-token extraction in langchain-py.

  • New test: test_cache_token_metrics_from_input_token_details
  • It simulates LLMResult usage_metadata.input_token_details and asserts metrics include:
    prompt_cached_tokens + prompt_cache_creation_tokens
  • Ran locally: uv run pytest src/tests/test_callbacks.py -k cache_token_metrics_from_input_token_details -q

Commit: 08205f4

Copy link
Copy Markdown
Contributor

@cpinn Caitlin Pinn (cpinn) left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change looks great to me with the added test, was what I was going to take a look into today. Usually for changes like this we also record and save a cassette to have the tests run the tests a little faster.

cc: Olmo Maldonado (@ibolmo) should we add a cassette to the change or should we merge as is and add it later?


assert "prompt_cache_creation_tokens" in first_metrics
assert first_metrics["prompt_cache_creation_tokens"] > 0
if "prompt_cached_tokens" in first_metrics:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit. do we want this if statement in the test because then the assertion may or may not run?

@ibolmo Olmo Maldonado (ibolmo) merged commit 36788d5 into main Jan 24, 2026
65 checks passed
@ibolmo Olmo Maldonado (ibolmo) deleted the fix/langchain-cache-tokens branch January 24, 2026 02:20
@ibolmo
Copy link
Copy Markdown
Collaborator

braintrust-langchain 0.2.1 and @braintrust/langchain-js 0.2.3 are both out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants