util-genai: include reasoning tokens in the output token metric - #4987
Open
venkat-uk wants to merge 2 commits into
Open
util-genai: include reasoning tokens in the output token metric#4987venkat-uk wants to merge 2 commits into
venkat-uk wants to merge 2 commits into
Conversation
…the token usage metric InferenceInvocation records gen_ai.usage.output_tokens on the span as output_tokens plus thinking_tokens, but the gen_ai.client.token.usage histogram recorded output_tokens alone. The span and the metric therefore disagreed for any response carrying reasoning tokens, and no output point was recorded at all when only thinking_tokens was set. Move the sum into _get_total_output_tokens() and use it for both the span attribute and the histogram.
|
Pull request dashboard statusWaiting on the author · refreshed 2026-08-22 13:17 UTC Investigate required status check failures. Status above doesn't look right?
|
venkat-uk
force-pushed
the
fix/genai-token-metric-reasoning-tokens
branch
from
August 22, 2026 13:16
6a8caa8 to
8ff4121
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
InferenceInvocationtreats reasoning tokens as part of the output token count on thespan, but not on the token usage metric.
_get_attributes()setsgen_ai.usage.output_tokenstooutput_tokens + thinking_tokens._get_metric_token_counts()recordedoutput_tokensalone, so thegen_ai.client.token.usagehistogram and the span disagreed for any response that carriedreasoning tokens. When only
thinking_tokenswas set the histogram recorded no outputpoint at all, while the span still reported the count.
This matters for cost: providers bill reasoning tokens as output tokens, and the GenAI
semantic conventions say
gen_ai.usage.reasoning.output_tokensSHOULD be included ingen_ai.usage.output_tokens. A dashboard built on the metric under-reported spend againstthe same call the span reported correctly.
I moved the sum into
_get_total_output_tokens()and used it in both places, so the twopaths cannot drift apart again. The comment on
self.output_tokensalready stated thisintent: "Output tokens will ultimately be the sum of normal output tokens and thinking
tokens."
Only
InferenceInvocationcarriesthinking_tokens, so nothing else changes. Inputs areuntouched, and an invocation with no reasoning tokens records exactly what it recorded
before.
Fixes # (no issue)
Type of change
How Has This Been Tested?
util/opentelemetry-util-genai/tests/test_handler_metrics.py, eachasserting the span attribute and the metric point together so the two can be compared
directly:
test_stop_llm_token_metric_counts_reasoning_tokens_as_output:output_tokens=7,thinking_tokens=3. Before the change the span read 10 and the metric read 7. Bothread 10 now.
test_stop_llm_token_metric_records_reasoning_tokens_alone:thinking_tokens=4withno
output_tokens. Before the change the span read 4 andgen_ai.client.token.usagewas never emitted. Both read 4 now.
Both tests fail on
466ae4cwithout the source change.Commands run locally on Python 3.12:
Does This PR Require a Core Repo Change?
Checklist: