Repository navigation
Conversation
…nAI models OpenAI counts reasoning tokens inside completion_tokens and output_tokens. OpenAILlm and OpenAIResponsesLlm reported that count as candidates and the reasoning as thoughts, and telemetry sums the two, so reasoning was counted twice in gen_ai.usage.output_tokens. Subtract reasoning from the candidate count, capped at the output count, as AnthropicLlm and adk-go's openaimodel do. Chat Completions endpoints that count reasoning outside completion_tokens, such as Gemini on Vertex AI and xAI, report a total of prompt plus completion plus reasoning; their counts are already disjoint and are kept. Fixes google#8412
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.
Please ensure you have read the contribution guide before creating a pull request.
Link to Issue or Description of Change
1. Link to an existing issue (if applicable):
OpenAILlmandOpenAIResponsesLlmcount reasoning tokens twice in output usage #8412Problem:
OpenAI counts reasoning tokens inside
completion_tokens(Chat Completions)and
output_tokens(Responses).OpenAILlmandOpenAIResponsesLlmreportedthat count as
candidates_token_countand the reasoning asthoughts_token_count.telemetry/_token_usage.pysums the two into outputtokens, so reasoning was counted twice: 50 output tokens of which 38 are
reasoning became 88 in
gen_ai.usage.output_tokens.Solution:
Split at the provider, as
AnthropicLlmdoes here and adk-go'sopenaimodeldoes since google/adk-go#2511.
_openai_common.split_reasoning_tokens(output, reasoning). It subtractsreasoning from the output count and caps it there, so candidates never go
negative. Negative counts are read as zero. When either count is missing,
both are returned as they are, so a response without reasoning details maps
as before.
OpenAIResponsesLlmalways splits. Responses counts reasoning insideoutput_tokens.OpenAILlmsplits too, except for endpoints that count reasoning outsidecompletion_tokens. Gemini on Vertex AI and xAI do, and theirtotal_tokensis prompt + completion + reasoning, which OpenAI's total neveris when reasoning is reported. Those counts are already disjoint and are
kept, which is what
OpenAILlmreports for them today. adk-go uses the samerule.
telemetry/_token_usage.pyis gone.google/gemini-3.8-flash(live)qwen3:8b(live)Behavior change: for OpenAI reasoning turns,
candidates_token_countno longerincludes reasoning. Callers that read it directly as the output count get the
non-reasoning part, and can add
thoughts_token_countto recover the fullcount. In this repo that is
LoggingPlugin(logs it as output),DebugLoggingPlugin, andBigQueryAgentAnalyticsPlugin, which writes it ascompletion, so stored values for OpenAI reasoning turns drop at upgrade. Thismatches what they already get from Gemini and
AnthropicLlm.Testing Plan
Unit Tests:
I have added or updated unit tests for my change.
All unit tests pass locally.
test_openai_llm.py::test_usage_metadata_splits_reasoning_from_candidates(replaces
test_usage_metadata_maps_reasoning_tokens, which pinned thedouble count): 12 cases. Reasoning inside completion, zero, missing, all
reasoning, reasoning above completion, negative reasoning and completion, and
the outside-completion shape (Vertex numbers, reasoning below completion,
zero total, negative prompt, negative completion).
test_openai_llm.py::test_usage_metadata_reports_output_tokens_once:TokenUsage.output_tokensis 50 for the OpenAI shape and 107 for the Vertexshape.
test_openai_responses_llm.py::test_usage_metadata_splits_reasoning_from_candidates(7 cases) and
::test_usage_metadata_reports_output_tokens_once(typedResponseUsage).test_response_parsing_maps_text_reasoning_tool_calls_and_usage: thecandidate count is now 3 (output 7 minus reasoning 4), not 7.
With the source change reverted and the tests kept, 16 of the 26 usage tests
fail. Each of seven single-guard mutants (no outside-completion check, no
prompt or completion
>= 0guard, no cap, negative reasoning or outputallowed, Responses not split) fails at least one test. No new mypy errors
against
mainin the changed files, by the CI job's comparison.Manual End-to-End (E2E) Tests:
The same prompt through
OpenAILlmagainst live endpoints, blocking andstreaming, on
mainand on this branch:I made no live OpenAI call. The OpenAI rows use fixtures shaped like OpenAI's
documented usage.
Checklist
Additional context
generativelanguage.googleapis.com/v1beta/openai/)sends no reasoning count, while its
total_tokensincludes thinking.gemini-3.8-flashreturned{"completion_tokens":4,"prompt_tokens":40,"total_tokens":142},so 98 thinking tokens are missing from candidates and thoughts both before
and after this change. That needs a separate fix, here and in adk-go.
ApigeeLlmand the OCI integration mapcompletion_tokensandreasoning_tokensthe same way. I can't test their backends, so they areleft out of this change.
🤖 Generated with Claude Code