agents icon indicating copy to clipboard operation
agents copied to clipboard

Google LLM plugin leaves Gemini thinking tokens out of completion_tokens

Open pcbeingused33 opened this issue 1 month ago • 1 comments

Bug Description

For Gemini thinking models (2.5, 3), usageMetadata reports the answer tokens (candidatesTokenCount) and the thinking tokens (thoughtsTokenCount) separately. Both are billed as output, and totalTokenCount includes both.

livekit-plugins-google's LLM (livekit/plugins/google/llm.py, the CompletionUsage built from usage_metadata) sets completion_tokens = candidates_token_count and doesn't set reasoning_tokens. CompletionUsage documents reasoning_tokens as "already counted in completion_tokens". So with thinking on:

  • completion_tokens under-reports output,
  • reasoning_tokens is 0,
  • prompt_tokens + completion_tokens != total_tokens.

Usage metrics and any cost computed from completion_tokens come out low.

#7326 adds reasoning tokens for the Gemini Live RealtimeModel (realtime_api.py). This is the same gap in the non-realtime LLM.

Expected Behavior

completion_tokens = candidates_token_count + thoughts_token_count and reasoning_tokens = thoughts_token_count, matching how CompletionUsage defines them and how the OpenAI-compatible path reports completion_tokens_details.reasoning_tokens.

Reproduction Steps

google.LLM(model="gemini-2.5-flash", api_key=..., http_options=types.HttpOptions(base_url=<local server>)) against a server that streams one chunk with:

"usageMetadata": {"promptTokenCount": 10, "candidatesTokenCount": 5, "thoughtsTokenCount": 40, "totalTokenCount": 55}

The usage chunk is completion_tokens=5 reasoning_tokens=0 total_tokens=55 (expected completion_tokens=45 reasoning_tokens=40).

Package Versions

livekit-agents and livekit-plugins-google from main (f548149).

This issue was drafted with Claude Code; the reproduction above was run on main to confirm the output.

pcbeingused33 avatar Oct 17 '26 03:10 pcbeingused33

Hi, I'm Himanshu, exploring a first contribution. I saw the fix and tests in #7891. Would independent validation of usage chunks with missing/zero thoughts_token_count be useful, or is there another small coverage gap around this issue? I'd like to help without opening a competing fix; I haven't run the reproduction yet.

himanshumehta10 avatar Oct 24 '26 03:10 himanshumehta10