Skip to content

fix(gemini): token count - #3039

Merged
icecrasher321 merged 2 commits into
stagingfrom
fix/gemini-token-count
Jan 28, 2026
Merged

fix(gemini): token count#3039
icecrasher321 merged 2 commits into
stagingfrom
fix/gemini-token-count

Conversation

@icecrasher321

Copy link
Copy Markdown
Collaborator

Summary

Token count should include thinking tokens.

Type of Change

  • Bug fix

Testing

Tested manually

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

@vercel

vercelBot commented Jan 28, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
ProjectDeploymentReviewUpdated (UTC)
docsSkippedSkippedJan 28, 2026 3:09am

Request Review

@greptile-apps

Copy link
Copy Markdown
Contributor

Greptile Overview

Greptile Summary

This PR fixes token counting for Gemini models by including thinking tokens in the output token count for accurate billing.

Main Changes:

  • Modified convertUsageMetadata in apps/sim/providers/google/utils.ts to extract thoughtsTokenCount from the Gemini API response and add it to candidatesTokenCount
  • Updated AgentInputs interface to change temperature and maxTokens from number to string types to properly handle form inputs
  • Added empty string validation in agent-handler.ts before converting temperature and maxTokens to numbers

Impact:
The thinking tokens feature in Gemini models (controlled by thinkingLevel configuration) generates internal reasoning that consumes tokens. Previously, these tokens weren't included in billing calculations, leading to underreporting of actual usage. This fix ensures thoughtsTokenCount is properly included in the output token count, which feeds into calculateCost() for accurate billing.

Confidence Score: 5/5

  • Safe to merge - straightforward bug fix with correct implementation
  • The changes are minimal, focused, and correctly implement the fix. The token counting logic properly adds thoughtsTokenCount to the output count as expected by billing. The type changes for temperature and maxTokens handle form inputs correctly with empty string validation.
  • No files require special attention

Important Files Changed

FilenameOverview
apps/sim/providers/google/utils.tsAdded thoughtsTokenCount to billing calculation, correctly includes thinking tokens in output count
apps/sim/executor/handlers/agent/types.tsChanged temperature and maxTokens types from number to string for proper form input handling
apps/sim/executor/handlers/agent/agent-handler.tsAdded empty string check before converting temperature and maxTokens to numbers, handles form empty values

Sequence Diagram

sequenceDiagram
participant User as User/Form Input
participant AgentHandler as AgentBlockHandler
participant Provider as Gemini Provider
participant GeminiAPI as Gemini API
participant Utils as Google Utils
participant Billing as Cost Calculation
User->>AgentHandler: Submit agent block with temperature & maxTokens (as strings)
AgentHandler->>AgentHandler: Check temperature != null && !== ''
AgentHandler->>AgentHandler: Check maxTokens != null && !== ''
AgentHandler->>AgentHandler: Convert strings to numbers using Number()
AgentHandler->>Provider: Send request with numeric temperature & maxTokens
Provider->>GeminiAPI: Execute model with thinkingConfig
GeminiAPI-->>Provider: Response with usageMetadata (promptTokenCount, candidatesTokenCount, thoughtsTokenCount)
Provider->>Utils: Call convertUsageMetadata(usageMetadata)
Utils->>Utils: Extract thoughtsTokenCount from usageMetadata
Utils->>Utils: Calculate candidatesTokenCount = candidatesTokenCount + thoughtsTokenCount
Utils->>Utils: Calculate totalTokenCount
Utils-->>Provider: Return GeminiUsage with updated counts
Provider->>Billing: calculateCost(promptTokenCount, candidatesTokenCount)
Billing-->>Provider: Cost for input + output tokens (including thinking)
Provider-->>AgentHandler: Return response with correct token counts and cost
Loading

@greptile-appsgreptile-appsBot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No files reviewed, no comments

Edit Code Review Agent Settings | Greptile

@icecrasher321

Copy link
Copy Markdown
CollaboratorAuthor

@cursor review

@cursorcursorBot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.

Comment threadapps/sim/providers/google/utils.ts
@icecrasher321
icecrasher321 merged commit c8ffda1 into stagingJan 28, 2026
12 checks passed
@icecrasher321
icecrasher321 deleted the fix/gemini-token-count branch January 28, 2026 04:08
royceP2 pushed a commit to arenadeveloper02/p2-sim that referenced this pull request Mar 3, 2026
* fix(gemini): token count
* fix to include tool call tokens
Sign up for freeto 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.

1 participant

@icecrasher321