Repository navigation
Python: count Gemini thinking tokens in the usage of Vertex AI completions - #14716
Open
alexcastgonz (pcbeingused333) wants to merge 1 commit into
Open
alexcastgonz (pcbeingused333) wants to merge 1 commit into
alexcastgonz (pcbeingused333) wants to merge 1 commit into
Conversation
alexcastgonz (pcbeingused333)
deployed
to
github-app-auth
November 3, 2026 11:54 — with
GitHub Actions
Active
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are recorded for the Vertex AI completion-token accounting change.
No open issues 🎉
What changed in this PR
This PR corrects under-reported completion usage for thinking Gemini models in the Python Vertex AI connector, extending the approach from the Google AI fix in #14671.
Changes:
- Adds a shared helper that sums candidate and thinking token counts.
- Uses the helper in chat and text response metadata, including streaming paths.
- Adds tests for token-count combinations, absent metadata, and both services’ usage reporting.
| File | Description |
|---|---|
python/tests/unit/connectors/ai/google/vertex_ai/services/test_vertex_ai_utils.py |
Adds helper and service-level token accounting tests. |
python/semantic_kernel/connectors/ai/google/vertex_ai/services/vertex_ai_text_completion.py |
Includes thinking tokens in text completion usage. |
python/semantic_kernel/connectors/ai/google/vertex_ai/services/vertex_ai_chat_completion.py |
Includes thinking tokens in chat completion usage. |
python/semantic_kernel/connectors/ai/google/vertex_ai/services/utils.py |
Adds the shared completion-token counting helper. |
Review effort: Balanced
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation and Context
With a thinking Gemini model through the Vertex AI connector, the
usagein the response metadata under-reports the completion tokens. Vertex AI reports thinking tokens inusage_metadata.thoughts_token_count, separately fromcandidates_token_count, and bills them as output tokens, butVertexAIChatCompletionandVertexAITextCompletiontookcompletion_tokensfromcandidates_token_countalone. Telemetry and cost tracking that read this usage missed every thinking token. This is the Vertex counterpart of the Google AI fix in #14671.Description
A new
get_completion_token_counthelper invertex_ai/services/utils.pyreturnscandidates_token_count + thoughts_token_count(either may be missing, and it isNoneonly when both are). Both Vertex AI completion services use it in_get_metadata_from_response, which streaming also goes through.Tests in
test_vertex_ai_utils.pycover the helper (both counts, one zero, none, no usage metadata) and check that the chat and text services report 50 completion tokens for 10 candidate and 40 thinking tokens.test_vertex_ai_utils.pypasses (17 tests; the chat service test fails without the fix) and ruff check and format are clean on the touched paths. A live Vertex AI call was not made.Contribution Checklist
ruff checkandruff format --checkpass on the changed files; mypy not run)This change was prepared with the help of an AI assistant (Claude Code), and the author is responsible for the change.
🤖 Generated with Claude Code