Repository navigation
Conversation
LiteLLM normalizes usage.completion_tokens to OpenAI semantics, which already includes completion_tokens_details.reasoning_tokens, whereas GenerateContentResponseUsageMetadata.candidates_token_count excludes thoughts (and TokenUsage sums candidates + thoughts for output_tokens). Subtract reasoning_tokens from completion_tokens (clamped at 0) when populating candidates_token_count in both non-streaming and streaming LiteLlm responses, matching AnthropicLlm (a95b008). Fixes google#7975
rladeira
force-pushed
the
fix/litellm-reasoning-tokens
branch
from
November 3, 2026 20:45
169f0c2 to
e876735
Compare
This was referenced Nov 25, 2026
Closed
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):
2. Or, if no issue exists, describe the change:
Problem:
LiteLlmmapped LiteLLM'susage.completion_tokensdirectly toGenerateContentResponseUsageMetadata.candidates_token_countwhile also mappingusage.completion_tokens_details.reasoning_tokenstothoughts_token_countin both_model_response_to_generate_content_response(non-streaming) and theUsageMetadataChunkbranch ofLiteLlm.generate_content_async(streaming).In OpenAI/LiteLLM semantics,
completion_tokensalready includesreasoning_tokens(LiteLLM normalizes Gemini, OpenAI, Anthropic, Bedrock, xAI, and Ollama responses to this invariant). Ingoogle-genaisemantics,candidates_token_countexcludes thoughts, andTokenUsage.from_usage_metadata(google/adk/telemetry/_token_usage.py) computesoutput_tokens = candidates_token_count + thoughts_token_count. As a result, reasoning tokens were counted twice ingen_ai.usage.output_tokens(and derivedgen_ai.usage.total_tokens) on OpenTelemetry spans and logs.Solution:
Match the
AnthropicLlmprecedent (a95b008f) by computingcandidates_token_count = max(completion_tokens - reasoning_tokens, 0)in both the non-streaming and streaming paths ofLiteLlm:candidates_token_countingoogle-genaisemantics (candidate tokens excluding thoughts) and guards against negative counts withmax(..., 0)when a provider estimates reasoning tokens locally.prompt_token_count,cached_content_token_count,thoughts_token_count, andtotal_token_countunchanged.prompt_tokens=10,completion_tokens=5,total_tokens=15,reasoning_tokens=5) used inclusivecompletion_tokenssemantics (candidates_token_count5->0), and adds a parametrized regression test covering non-streaming and streaming responses (completion_tokens=25,reasoning_tokens=5->candidates_token_count == 20).Testing Plan
Unit Tests:
Added
test_reasoning_tokens_are_not_counted_twice_in_output(@pytest.mark.parametrize("stream", [False, True])) intests/unittests/models/test_litellm.pyand updated the three existing inclusive-fixture assertions.pytestsummary:Manual End-to-End (E2E) Tests:
LiteLLMClientreproduction (completion_tokens=25,reasoning_tokens=5) across non-streaming and streaming:LiteLlm(ThinkingConfig(thinking_budget=256)):Checklist
Additional context
None.