Skip to content

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

Merged
dkotter merged 4 commits into
WordPress:developfrom
prasadkarmalkar:fix/logs__gemini_tokens_count
Jun 8, 2026
Merged

Fix: AI Request Logging: token count understated for thinking models (thoughtsTokenCount ignored)#680
dkotter merged 4 commits into
WordPress:developfrom
prasadkarmalkar:fix/logs__gemini_tokens_count

Conversation

@prasadkarmalkar

@prasadkarmalkar prasadkarmalkar commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

What?

Closes #662

Why?

Token count mismatched in AI Request Logging and in Google console logs

How?

Updated Google token extraction in Log Data Extractor to compute output tokens as:
candidatesTokenCount + thoughtsTokenCount

Use of AI Tools

Testing Instructions

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

Screenshots or screencast

After AI Logs Gemini Logs
Screenshot 2026-06-08 at 1 59 15 PM Screenshot 2026-06-08 at 2 32 15 PM

Changelog Entry

Fixed: Google gemini token counts in request logs now match provider usage totals.

Open WordPress Playground Preview

@prasadkarmalkar prasadkarmalkar marked this pull request as ready for review June 8, 2026 09:17
@github-actions

github-actions Bot commented Jun 8, 2026

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: prasadkarmalkar <prasadkarmalkar@git.wordpress.org>
Co-authored-by: dkotter <dkotter@git.wordpress.org>
Co-authored-by: riccardodicurti <riccardodicurti@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@dkotter dkotter added this to the 1.1.0 milestone Jun 8, 2026
Comment thread includes/Logging/Log_Data_Extractor.php Outdated
$usage = $response['usageMetadata'];
$input = $usage['promptTokenCount'] ?? null;
$output = $usage['candidatesTokenCount'] ?? null;
$output = ( $usage['candidatesTokenCount'] ?? 0 ) + ( $usage['thoughtsTokenCount'] ?? 0 );

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So previously we would set null here if candidatesTokenCount wasn't set. While that's an unlikely scenario, now if that isn't set, we return 0 instead. Not sure that will actually cause problems but may be best to keep original behavior here and ensure we return null if neither candidatesTokenCount or thoughtsTokenCount is set

@prasadkarmalkar prasadkarmalkar Jun 8, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to clarify, should the logic return null only if both candidatesTokenCount and thoughtsTokenCount are unset? If either one has a value, we'd keep that value instead of null?

$output = ( isset( $usage['candidatesTokenCount'] ) || isset( $usage['thoughtsTokenCount'] ) )
  ? ( $usage['candidatesTokenCount'] ?? 0 ) + ( $usage['thoughtsTokenCount'] ?? 0 )
  : null;

This preserves the original behavior of returning null when neither is set while still correctly summing whichever tokens are available when at least one is present.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that looks correct

Comment thread tests/Integration/Includes/Logging/Log_Data_ExtractorTest.php
@dkotter dkotter merged commit e3b016e into WordPress:develop Jun 8, 2026
23 of 24 checks passed
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.

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

2 participants