Skip to content

(Openinference Migration: Langchain) - Add support for cache and reasoning token counts (PR #1)#272

Open
rads-1996 wants to merge 11 commits into
open-telemetry:mainfrom
rads-1996:pr-token-counts
Open

(Openinference Migration: Langchain) - Add support for cache and reasoning token counts (PR #1)#272
rads-1996 wants to merge 11 commits into
open-telemetry:mainfrom
rads-1996:pr-token-counts

Conversation

@rads-1996

@rads-1996 rads-1996 commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

Description

  • PR 1 — Token counts. Sets cache_creation_input_tokens, cache_read_input_tokens, and thinking_tokens from LangChain usage_metadata.
  • PR 2 — Legacy function_call. Parses the pre-tools OpenAI additional_kwargs.function_call shape into a ToolCallRequest on both the input path (_ai_message_parts) and the output path (on_llm_end), matching the modern tool_calls handling.
  • PR 3 — Multimodal images. Converts OpenAI image_url (URL or data: base64) and Anthropic image/source blocks into Uri (URL) or Blob (base64 decoded to raw bytes). Base64 decoding matches the sibling genai-anthropic extract_base64_blob behavior so the same image yields identical Blob bytes across instrumentations.

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How has this been tested?

Please describe the tests that you ran to verify your changes. Provide
instructions so we can reproduce. List any relevant details for your test
configuration.

  • uv run tox -e py312-test-instrumentation-genai-langchain -- -q
  • uv run tox -e py312-test-instrumentation-genai-langchain-conformance -- -q
  • uv run --python 3.12 tox -e lint-instrumentation-genai-langchain

Checklist

See CONTRIBUTING.md
for the style guide, changelog guidance, and more.

  • Followed the style guidelines of this project
  • Changelog updated if the change requires an entry
  • Unit tests added
  • Documentation updated

@rads-1996
rads-1996 force-pushed the pr-token-counts branch 2 times, most recently from fb0a489 to 1e5b733 Compare July 16, 2026 22:11
@rads-1996
rads-1996 marked this pull request as ready for review July 16, 2026 22:16
@rads-1996
rads-1996 requested a review from a team as a code owner July 16, 2026 22:16
Copilot AI review requested due to automatic review settings July 16, 2026 22:16

Copilot AI 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.

Pull request overview

Adds support for LangChain-provided cache token counts and reasoning (“thinking”) token counts, ensuring util-genai’s token metrics treat reasoning tokens as part of overall output token usage.

Changes:

  • Update util-genai inference token metric accounting to include thinking_tokens in OUTPUT token metrics.
  • Extract cache/reasoning token breakdowns from LangChain usage_metadata and apply them to the inference invocation (including adjusting output_tokens so util-genai can re-sum totals correctly).
  • Add unit tests covering token breakdown extraction and on_llm_end behavior for cache/reasoning and ignored audio tokens.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
util/opentelemetry-util-genai/src/opentelemetry/util/genai/_inference_invocation.py Includes thinking_tokens in OUTPUT token metrics to match span attribute behavior.
instrumentation/opentelemetry-instrumentation-genai-langchain/src/opentelemetry/instrumentation/genai/langchain/callback_handler.py Parses LangChain usage metadata into cache/reasoning fields and adjusts output token handling.
instrumentation/opentelemetry-instrumentation-genai-langchain/src/opentelemetry/instrumentation/genai/langchain/utils.py Adds helper to extract cache/reasoning token details from LangChain metadata.
instrumentation/opentelemetry-instrumentation-genai-langchain/tests/test_callback_handler.py Adds tests for new token breakdown behavior and helper extraction.
instrumentation/opentelemetry-instrumentation-genai-langchain/.changelog/272.added Adds a towncrier fragment for the new feature.

@opentelemetry-pr-dashboard

opentelemetry-pr-dashboard Bot commented Jul 16, 2026

Copy link
Copy Markdown

Pull request dashboard status

  • Waiting on: Reviewers
  • Next step: Review the latest changes.

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.

2 participants