Skip to content

AI Request Logging: token count understated for thinking models (thoughtsTokenCount ignored) #662

@riccardodicurti

Description

@riccardodicurti

Description

Description

The AI Request Logs show a lower token count than Google AI Studio
for requests made with thinking models (e.g. gemini-2.5-flash,
gemini-3-flash-preview).

Image Image

Expected behavior

Token count in the logs matches Google AI Studio.

Actual behavior

The logs show fewer tokens. In the example above: 309 in WordPress vs 338 input + ~570 output in Google AI Studio.

Root cause

Log_Data_Extractor::extract_token_usage() ignores thoughtsTokenCount (internal reasoning tokens) from Google's usageMetadata. The SDK itself already handles this correctly in GoogleTextGenerationModel.php:584:

($usage['candidatesTokenCount'] ?? 0) + ($usage['thoughtsTokenCount'] ?? 0)

But Log_Data_Extractor.php:239–243 does not:

$input  = $usage['promptTokenCount'] ?? null;
$output = $usage['candidatesTokenCount'] ?? null; // ← thoughtsTokenCount ignored

Affected file

includes/Logging/Log_Data_Extractor.php

Step-by-step reproduction instructions

Steps to reproduce

  1. Enable the AI Request Logging experiment
  2. Configure the Google provider with a thinking model (e.g. gemini-3-flash-preview)
  3. Trigger a text generation (e.g. via the Title Generation experiment)
  4. Compare the token count in Tools → AI Request Logs with the usage in Google AI Studio → Usage

Screenshots, screen recording, code snippet

No response

Environment info

No response

Please confirm that you have searched existing issues in the repo.

  • Yes

Please confirm that you have tested with all plugins deactivated except the AI plugin.

  • Yes

Please confirm which theme type you used for testing.

  • Block
  • Classic
  • Hybrid (e.g. classic with theme.json)
  • Not sure

Metadata

Metadata

Assignees

No one assigned

    Labels

    [Type] BugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    Status
    Done

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions