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

@flippingbitss
flippingbitss requested a review from a team as a code owner July 17, 2026 15:56

@alexander-alderman-webb alexander-alderman-webb left a comment

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.

Re-generating the test matrix has pulled in some test failures.
Could you revert changes to tox.ini, scripts/populate_tox/releases.jsonl, and scripts/populate_tox/package_dependencies.jsonl?

@flippingbitss
flippingbitss force-pushed the flippingbitss/langchain-support-cache-tokens branch 2 times, most recently from 62ab173 to 88ef6da Compare August 6, 2026 03:38
Comment thread sentry_sdk/integrations/langchain.py Outdated
@flippingbitss
flippingbitss force-pushed the flippingbitss/langchain-support-cache-tokens branch from 88ef6da to 12997a4 Compare August 6, 2026 04:40

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Want reviews to match your repository better? Bugbot Learning can learn team-specific rules from PR activity. A team admin can enable Learning in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 12997a4. Configure here.

):
token_usage = _get_token_usage(response)
if token_usage:
tokens = _extract_tokens(token_usage)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Token counts inflated for multiple choices

Medium Severity

_record_token_usage now always prefers summing usage from response.generations before considering response-level llm_output usage. Providers typically attach the same request-level usage_metadata to every choice, so calls with n &gt; 1 can report multiplied input, output, total, cache, and reasoning tokens. Previously, present llm_output token usage was used once and avoided that inflation.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 12997a4. Configure here.

@alexander-alderman-webb

Copy link
Copy Markdown
Contributor

Going to review this now.

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

2 participants