Skip to content

fix: preserve Gemini total token usage - #44

Merged
superdav42 merged 2 commits into
WordPress:trunkfrom
zahidul-islam-selise:fix/token-usage-prompt-count
Sep 2, 2026
Merged

superdav42 merged 2 commits into
WordPress:trunkfrom
zahidul-islam-selise:fix/token-usage-prompt-count

Conversation

@zahidul-islam-selise

Copy link
Copy Markdown
Contributor

Summary

  • use Google's authoritative totalTokenCount when parsing Gemini usage metadata
  • retain a backward-compatible fallback for older responses that omit the field
  • add regression coverage for both the authoritative value and the fallback

The previous implementation computed the total from completion and thought tokens only, so prompt tokens were silently omitted from usage reporting.

Testing

  • composer test
  • composer lint
  • git diff --check

Fixes #22

@zahidul-islam-selise
zahidul-islam-selise requested a review from a team August 21, 2026 21:46
@github-actions

github-actions Bot commented Aug 21, 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: zahidgh <zahidui@git.wordpress.org>
Co-authored-by: jeffpaul <jeffpaul@git.wordpress.org>
Co-authored-by: saarnilauri <laurisaarni@git.wordpress.org>

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

@jeffpaul
jeffpaul requested a review from superdav42 August 24, 2026 17:53
@jeffpaul jeffpaul added this to the 1.2.0 milestone Aug 24, 2026
@jeffpaul

Copy link
Copy Markdown
Member

@saarnilauri as the original issue reporter, are you able to review this to see if it resolves things properly for you?

@saarnilauri

Copy link
Copy Markdown
Contributor

@saarnilauri as the original issue reporter, are you able to review this to see if it resolves things properly for you?

@jeffpaul yes, for sure.

@saarnilauri

Copy link
Copy Markdown
Contributor

Thanks @zahidgh, this fixes #22 and I verified it rather than just reading the diff. I checked out the branch and ran composer test. I also did a mutation check: reverting only the src/ change while keeping your new tests makes both fail with exactly the failures predicted in the issue (11 is identical to 37 and 14 is identical to 40). So these are genuine regression tests. The fix itself matches the proposal in #22: prefer Google's totalTokenCount, fall back to prompt + candidates + thoughts, plus the UsageData PHPStan annotation update. Nice touch using thoughts=3 in the fallback test so the buggy formula (14) and the correct one (40) actually diverge, and the anonymous-subclass trick for reaching the protected parser is cleaner than reflection.

I also probed the live API directly (thinking off, thinking on, and a tool-use call) to sanity-check the two code paths: totalTokenCount equaled prompt + candidates + thoughts in every case, so the preferred value and the fallback agree in practice and the fallback is safe for older responses that omit the field.

@jeffpaul One thing worth flagging: issue #22 asks for the fix on both the pre-1.3.0 and 1.3.0+ paths, and the 1.3.0+ path only exists in PR #21, which is still open. Fixing trunk's single path here is correct, but #21 currently computes the total as candidates + thoughts (the same bug) and touches the same lines. Whichever PR merges second will conflict, and if #21 lands after this one without carrying the fix over, it quietly reintroduces the bug. I'll leave a cross-reference comment on #21.

Approving.

@superdav42 superdav42 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good to me. Thanks for your contribution.

@superdav42
superdav42 merged commit 4e01200 into WordPress:trunk Sep 2, 2026
14 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.

TokenUsage::$totalTokens under-reports by promptTokenCount

4 participants