Skip to content

feat(langchain): Support cache/reasoning token metrics based on Langchain UsageMetadata#6840

Open
flippingbitss wants to merge 1 commit into
getsentry:masterfrom
flippingbitss:flippingbitss/langchain-support-cache-tokens
Open

feat(langchain): Support cache/reasoning token metrics based on Langchain UsageMetadata#6840
flippingbitss wants to merge 1 commit into
getsentry:masterfrom
flippingbitss:flippingbitss/langchain-support-cache-tokens

Conversation

@flippingbitss

Copy link
Copy Markdown

Summary

Cache read/Cache write tokens and output reasoning tokens weren't being picked up by the Langchain integration.

  • We now include these additional usage metrics and get the usages from Langchain's provider-agnostic UsageMetadata shape instead.
  • Ran the scripts/generate-test-files.sh to update Langchain version as the older latest version wasn't returning cache_write metric.

Description

As described by my initial approach here, this PR adds a few new token usage metrics cache_read/cache_creation/reasoning/audio and populates the following additional span tags.

  • gen_ai.usage.input_tokens.cached
  • gen_ai.usage.input_tokens.cache_write
  • gen_ai.usage.output_tokens.reasoning

Note: audio tokens are available and read from UsageMetadata but Sentry doesn't have any span tags for these yet, so no span tags are populated for these.

Testing

Add new asserts in Langchain v1+ only tests (test_langchain_create_agent, test_tool_execution_span) as suggested by @alexander-alderman-webb

Ran unit tests with TESTPATH=tests/integrations/langchain/test_langchain.py uv run tox -e <ENV> for following envs:

  • py3.14-langchain-base-v0.1.20
  • py3.14-langchain-base-latest

Issues

Fixes #6799
Fixes PY-2606

Related

OTel's Langchain Integration's set_chat_response_usage impl

Test Evidence

  1. py3.14-langchain-base-v0.1.20
test-base-v0.1.20
  1. py3.14-langchain-base-latest
test-base-latest

…hain UsageMetadata

Cache read/Cache write tokens and output reasoning tokens weren't being
picked up by Langchain integration. We now include those and normalize
the usages to Langchain's provider-agnostic UsageMetadata shape.

Fixes getsentryGH-6799
Fixes PY-2606
@flippingbitss
flippingbitss requested a review from a team as a code owner July 17, 2026 15:56
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.

Langchain and Langgraph don't report cached/reasoning token usage

1 participant