Skip to content

Conversation

@icecrasher321
Copy link
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
Copy link

vercel bot commented Jan 28, 2026

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

1 Skipped Deployment
Project Deployment Review Updated (UTC)
docs Skipped Skipped Jan 28, 2026 3:09am

Request Review

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Jan 28, 2026

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

Filename Overview
apps/sim/providers/google/utils.ts Added thoughtsTokenCount to billing calculation, correctly includes thinking tokens in output count
apps/sim/executor/handlers/agent/types.ts Changed temperature and maxTokens types from number to string for proper form input handling
apps/sim/executor/handlers/agent/agent-handler.ts Added 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

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

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
Collaborator Author

@cursor review

Copy link

@cursor cursor bot left a comment

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.

@icecrasher321 icecrasher321 merged commit c8ffda1 into staging Jan 28, 2026
12 checks passed
@icecrasher321 icecrasher321 deleted the fix/gemini-token-count branch January 28, 2026 04:08
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.

2 participants