Repository navigation
Description
🔴 Required Information
Describe the Bug:
OpenAILlm and OpenAIResponsesLlm count reasoning tokens twice in output usage.
Both integrations pass the provider's output count, which already includes
reasoning, as candidates_token_count, and the reasoning count as
thoughts_token_count
(_openai_llm.py:263-282,
_openai_responses_llm.py:513-537).
telemetry/_token_usage.py defines output as candidates plus reasoning
(_token_usage.py:157-161),
so gen_ai.usage.output_tokens counts reasoning twice. With 50 output tokens
of which 38 are reasoning, telemetry records 88.
The comment in _openai_llm.py leaves the fix to the aggregator. adk-go fixed
the same bug at the provider (google/adk-go#2511), as AnthropicLlm does here
and as #7976 does for LiteLLM (#7975).
Steps to Reproduce:
- Install ADK from
main(03725e8) with theopenaiextra. - Run the snippet below. It sends a fixed OpenAI reasoning response through
each model class over a mocked transport, and reads the usage the way
telemetry does.
Expected Behavior:
candidates_token_count=12 and thoughts_token_count=38, so telemetry records
50 output tokens, as it does for AnthropicLlm.
Observed Behavior:
chat: candidates=50 thoughts=38 total=70 -> telemetry output_tokens=88
responses: candidates=50 thoughts=38 total=70 -> telemetry output_tokens=88
Environment Details:
- ADK Library Version (pip show google-adk): 2.15.1 (
mainat 03725e8) - Desktop OS: macOS
- Python Version (python -V): 3.11.18
Model Information:
- Are you using LiteLLM: No
- Which model is being used: any OpenAI reasoning model (the fixture above);
google/gemini-3.8-flashon Vertex AI's OpenAI endpoint for the shape below
🟡 Optional Information
Additional Context:
One shape must be kept. Some Chat Completions endpoints count reasoning outside
completion_tokens. Gemini on Vertex AI's OpenAI endpoint does:
google/gemini-3.8-flash returned
{"prompt_tokens": 39, "completion_tokens": 4, "completion_tokens_details": {"reasoning_tokens": 103}, "total_tokens": 146}and xAI documents the same
(usage details).
OpenAILlm maps these correctly today (candidates 4, thoughts 103, output
107), so plain subtraction would break them. adk-go tells the two apart by the
total. In Chat Completions, when total = prompt + completion + reasoning, keep
completion as candidates and reasoning as thoughts; otherwise cap reasoning at
completion and subtract it. Responses always subtracts.
I can open a PR.
Minimal Reproduction Code:
import asyncio
import httpx
from openai import AsyncOpenAI
from google.adk.integrations.openai import OpenAILlm, OpenAIResponsesLlm
from google.adk.models.llm_request import LlmRequest
from google.adk.telemetry._token_usage import TokenUsage
from google.genai import types
CHAT = {
"id": "c", "object": "chat.completion", "created": 0, "model": "m",
"choices": [{"index": 0, "finish_reason": "stop",
"message": {"role": "assistant", "content": "hi"}}],
"usage": {"prompt_tokens": 20, "completion_tokens": 50, "total_tokens": 70,
"completion_tokens_details": {"reasoning_tokens": 38}},
}
RESPONSES = {
"id": "r", "object": "response", "created_at": 0, "model": "m",
"status": "completed", "parallel_tool_calls": False,
"tool_choice": "auto", "tools": [],
"output": [{"type": "message", "id": "m1", "role": "assistant",
"status": "completed",
"content": [{"type": "output_text", "text": "hi",
"annotations": []}]}],
"usage": {"input_tokens": 20, "output_tokens": 50, "total_tokens": 70,
"input_tokens_details": {"cached_tokens": 0},
"output_tokens_details": {"reasoning_tokens": 38}},
}
def client(body):
transport = httpx.MockTransport(lambda _: httpx.Response(200, json=body))
return AsyncOpenAI(api_key="test",
http_client=httpx.AsyncClient(transport=transport))
async def run(label, llm):
req = LlmRequest(model="m", contents=[
types.Content(role="user", parts=[types.Part(text="hi")])])
async for resp in llm.generate_content_async(req, stream=False):
u = resp.usage_metadata
out = TokenUsage.from_usage_metadata(u).output_tokens
print(f"{label}: candidates={u.candidates_token_count} "
f"thoughts={u.thoughts_token_count} total={u.total_token_count} "
f"-> telemetry output_tokens={out}")
async def main():
await run("chat", OpenAILlm(model="m", client=client(CHAT)))
await run("responses", OpenAIResponsesLlm(model="m", client=client(RESPONSES)))
asyncio.run(main())How often has this issue occurred?:
- Always (100%)
Activity
Maaz-66 commentedon Nov 27, 2026
Good catch—separating
reasoning_tokenscleanly from total output usage (and keepingcached_tokensdistinct from uncached prompt tokens) is critical when calculating accurate inference costs on agent runs.If anyone on this thread is auditing raw JSONL usage logs locally to reconcile actual billable tokens vs. cached/reasoning breakdowns without sending data externally, I open-sourced a lightweight offline metadata extractor (
extract_metadata.py) that parses provider usage fields locally: https://github.com/Maaz-66/cache-bench