Skip to content

fix(models): avoid double-counting LiteLLM reasoning tokens in output - #7976

Open
rladeira wants to merge 1 commit into
google:mainfrom
rladeira:fix/litellm-reasoning-tokens
Open

rladeira wants to merge 1 commit into
google:mainfrom
rladeira:fix/litellm-reasoning-tokens

Conversation

@rladeira

@rladeira rladeira commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

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:
LiteLlm mapped LiteLLM's usage.completion_tokens directly to GenerateContentResponseUsageMetadata.candidates_token_count while also mapping usage.completion_tokens_details.reasoning_tokens to thoughts_token_count in both _model_response_to_generate_content_response (non-streaming) and the UsageMetadataChunk branch of LiteLlm.generate_content_async (streaming).

In OpenAI/LiteLLM semantics, completion_tokens already includes reasoning_tokens (LiteLLM normalizes Gemini, OpenAI, Anthropic, Bedrock, xAI, and Ollama responses to this invariant). In google-genai semantics, candidates_token_count excludes thoughts, and TokenUsage.from_usage_metadata (google/adk/telemetry/_token_usage.py) computes output_tokens = candidates_token_count + thoughts_token_count. As a result, reasoning tokens were counted twice in gen_ai.usage.output_tokens (and derived gen_ai.usage.total_tokens) on OpenTelemetry spans and logs.

Solution:
Match the AnthropicLlm precedent (a95b008f) by computing candidates_token_count = max(completion_tokens - reasoning_tokens, 0) in both the non-streaming and streaming paths of LiteLlm:

  • Keeps candidates_token_count in google-genai semantics (candidate tokens excluding thoughts) and guards against negative counts with max(..., 0) when a provider estimates reasoning tokens locally.
  • Preserves prompt_token_count, cached_content_token_count, thoughts_token_count, and total_token_count unchanged.
  • Updates three existing unit test assertions whose fixtures (prompt_tokens=10, completion_tokens=5, total_tokens=15, reasoning_tokens=5) used inclusive completion_tokens semantics (candidates_token_count 5 -> 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:

  • I have added or updated unit tests for my change.
  • All unit tests pass locally.

Added test_reasoning_tokens_are_not_counted_twice_in_output (@pytest.mark.parametrize("stream", [False, True])) in tests/unittests/models/test_litellm.py and updated the three existing inclusive-fixture assertions.

pytest summary:

$ uv run pytest tests/unittests/models/
1883 passed, 31 warnings in 55.51s

$ uv run pytest tests/unittests/test_import_loading.py
29 passed in 77.97s

Manual End-to-End (E2E) Tests:

  1. Deterministic fake LiteLLMClient reproduction (completion_tokens=25, reasoning_tokens=5) across non-streaming and streaming:
# Before (main):
stream=False candidates=25 thoughts=5 total=35 gen_ai.usage.output_tokens=30 (expected 25)
stream=True  candidates=25 thoughts=5 total=35 gen_ai.usage.output_tokens=30 (expected 25)

# After (this PR):
stream=False candidates=20 thoughts=5 total=35 gen_ai.usage.output_tokens=25 (expected 25)
stream=True  candidates=20 thoughts=5 total=35 gen_ai.usage.output_tokens=25 (expected 25)
  1. Live Vertex AI Gemini thinking call via LiteLlm (ThinkingConfig(thinking_budget=256)):
# Before (main):
litellm usage: prompt=12 completion=142 total=154 reasoning=141
adk usage:     candidates=142 thoughts=141 total=154 gen_ai.usage.output_tokens=283

# After (this PR):
litellm usage: prompt=12 completion=239 total=251 reasoning=238
adk usage:     candidates=1   thoughts=238 total=251 gen_ai.usage.output_tokens=239

Checklist

  • I have read the CONTRIBUTING.md document.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.
  • I have manually tested my changes end-to-end.
  • Any dependent changes have been merged and published in downstream modules.

Additional context

None.

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

[LiteLLM] Reasoning tokens are double-counted in candidates_token_count and gen_ai.usage.output_tokens

2 participants