Skip to content

fix(core): bill gemini thinking tokens when attaching model pricing - #1426

Closed
greysonlalonde wants to merge 2 commits into
NVIDIA:mainfrom
greysonlalonde:fix/core-gemini-tool-use-tokens
Closed

greysonlalonde wants to merge 2 commits into
NVIDIA:mainfrom
greysonlalonde:fix/core-gemini-tool-use-tokens

Conversation

@greysonlalonde

@greysonlalonde greysonlalonde commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Overview

Cost estimates attached through model pricing didn't account for gemini thinking tokens reported in api_specific.thoughts_tokens. This bills thinking tokens at the output rate on either pricing path.

  • I confirm this contribution is my own work, or I have the right to submit it under this project's license.
  • I searched existing issues and open pull requests, and this does not duplicate existing work.

Details

  • billed_usage folds thoughts_tokens into billed output count for attach_estimated_cost_for_provider and built-in gemini codec.
  • token sums use checked addition so an overflowing computed total/billed count leaves total_tokens or the cost estimate unset.

Where should the reviewer start?

billed_usage in crates/core/src/codec/model_pricing.rs.

Summary by CodeRabbit

  • New Features

    • Improved Gemini usage accounting to separately track thinking and tool-use prompt tokens.
    • Cost estimates now apply the correct billing treatment for Gemini grounding services, including Search and Maps.
    • Gemini response details now preserve tool-use prompt token counts.
  • Bug Fixes

    • Improved accuracy of pricing-tier selection and fallback totals.
    • Prevented invalid or overflowing token totals from producing incorrect costs.
    • Preserved Gemini usage metadata during response sanitization.
  • Documentation

    • Updated provider codec guidance with Gemini token normalization, billing rules, and threshold pricing behavior.

- attach_estimated_cost_for_provider bills gemini thoughts_tokens at the output rate through billed_usage
- checked token sums so an overflowing gemini total or billed output count leaves total_tokens or cost unset
- test: thinking-token billing and overflow cases

Signed-off-by: Greyson Lalonde <[email protected]>
- ApiSpecificResponse::GeminiGenerateContent carries tool_use_prompt_tokens from toolUsePromptTokenCount
- computed total_tokens includes tool-use prompt tokens so it matches totalTokenCount
- billed_usage folds tool_use_prompt_tokens into prompt_tokens so cost estimates bill them at the input rate
- ToolUsePromptBilling reads webSearchQueries, maps grounding chunks, and the Maps widget token so Search and Maps grounded responses bill none of them
- rate tiers select from reported prompt_tokens so folded counts never cross a threshold
- SeparateUsageCounts shared by the codec and attach_estimated_cost_for_provider so both billing paths agree
- test: total, api_specific, input-rate billing, grounding exclusion, tier selection, input overflow, and redaction pass-through

Signed-off-by: Greyson Lalonde <[email protected]>
@greysonlalonde greysonlalonde added this to the 0.11 milestone Oct 20, 2026
@greysonlalonde greysonlalonde self-assigned this Oct 20, 2026
@greysonlalonde
greysonlalonde requested review from a team as code owners October 20, 2026 21:47
@greysonlalonde greysonlalonde added the Bug issue describes bug; PR fixes bug label Oct 20, 2026
@github-actions github-actions Bot added size:L PR is large lang:rust PR changes/introduces Rust code labels Oct 20, 2026
@coderabbitai

coderabbitai Bot commented Oct 20, 2026

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Note

Currently processing new changes in this PR. This may take a few minutes, please wait...

⚙️ Run configuration
  • Configuration used: Repository: NVIDIA/NeMo-Relay/.coderabbit.yaml
  • Review profile: ASSERTIVE
  • Plan: Enterprise
  • Run ID: 22e1047b-79ac-40a9-a99e-b4d3208ac877
📥 Commits

Reviewing files that changed from the base of the PR and between 8ec6562 and 723c8c8.

📒 Files selected for processing: 8
  • crates/core/src/codec/gemini_generate_content.rs
  • crates/core/src/codec/model_pricing.rs
  • crates/core/tests/unit/codec/gemini_generate_content_tests.rs
  • crates/core/tests/unit/codec/response_tests.rs
  • crates/pii-redaction/src/trajectory.rs
  • crates/pii-redaction/tests/unit/component_tests.rs
  • crates/types/src/codec/response.rs
  • docs/integrate-into-frameworks/provider-response-codecs.mdx
+----------------------------------------------------------------+
| Pop quiz, hotshot. There's a bug in your code. What do you do? |
+----------------------------------------------------------------+
  \
   \     /)/)
        ( '')
        /  |
      o(___)
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Comment @coderabbitai help to get the list of available commands.

@greysonlalonde

Copy link
Copy Markdown
Contributor Author

Opened from wrong branch

@github-actions

Copy link
Copy Markdown

This branch was successfully deployed

No deployments
fern — 723c8c8c Deployed Oct 20, 2026 by copy-pr-bot[bot] via Preview docs #6079
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug issue describes bug; PR fixes bug lang:rust PR changes/introduces Rust code size:L PR is large

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant