Skip to content

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

Merged
willkill07 merged 2 commits into
NVIDIA:mainfrom
greysonlalonde:fix/core-gemini-thinking-token-pricing
Oct 21, 2026
Merged

willkill07 merged 2 commits into
NVIDIA:mainfrom
greysonlalonde:fix/core-gemini-thinking-token-pricing

Conversation

@greysonlalonde

@greysonlalonde greysonlalonde commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Overview

  • 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
  • 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

  • Bug Fixes

    • Gemini cost estimates now include thinking tokens as billable output usage.
    • Cost estimates are omitted when token calculations would overflow.
    • Fallback total-token reporting now safely handles overflow and absent usage data.
  • Documentation

    • Updated Gemini response guidance to clarify how thinking tokens affect cost calculations.

- 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]>
@greysonlalonde greysonlalonde self-assigned this Oct 20, 2026
@greysonlalonde greysonlalonde added the Bug issue describes bug; PR fixes bug label Oct 20, 2026
@greysonlalonde
greysonlalonde requested review from a team as code owners October 20, 2026 21:55
@github-actions github-actions Bot added size:S PR is small 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 →

Important

Review skipped

Review was skipped as selected files did not have any reviewable changes.

⚙️ Run configuration
  • Configuration used: Repository: NVIDIA/NeMo-Relay/.coderabbit.yaml
  • Review profile: ASSERTIVE
  • Plan: Enterprise
  • Run ID: 26efa87b-d6db-4757-80fd-5509810bc2b0
📥 Commits

Reviewing files that changed from the base of the PR and between a9ac7a7 and 6a9f350.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: NVIDIA/NeMo-Relay/.coderabbit.yaml
  • Review profile: ASSERTIVE
  • Plan: Enterprise
  • Run ID: fb43ff55-a71a-4930-859e-e644a1a4c9b7
📥 Commits

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

📒 Files selected for processing: 5
  • 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
  • docs/integrate-into-frameworks/provider-response-codecs.mdx

Included review availability: This review used your included allowance. Your plan provides up to 12 included reviews per hour; 11 remain after this review.

📜 Recent review details
⏰ Context from checks skipped due to timeout: 44
  • GitHub Check: Node.js / Package (macos-arm64)
  • GitHub Check: Python / Package (windows-amd64)
  • GitHub Check: Python / Package (linux-amd64)
  • GitHub Check: Node.js / Package (linux-musl-amd64)
  • GitHub Check: Node.js / Package (windows-arm64)
  • GitHub Check: Node.js / Package (linux-musl-arm64)
  • GitHub Check: Node.js / Package (windows-amd64)
  • GitHub Check: Node.js / Test (macos-arm64)
  • GitHub Check: Python / Package (linux-musl-arm64)
  • GitHub Check: Rust / Package (macos-arm64)
  • GitHub Check: Rust / Package (linux-musl-arm64)
  • GitHub Check: Node.js / Package (linux-amd64)
  • GitHub Check: Python / Package (macos-arm64)
  • GitHub Check: Rust / Package (windows-arm64)
  • GitHub Check: Node.js / Package (linux-arm64)
  • GitHub Check: Python / Package (windows-arm64)
  • GitHub Check: Rust / Package (linux-musl-amd64)
  • GitHub Check: Rust / Package (windows-amd64)
  • GitHub Check: Rust / Package (linux-arm64)
  • GitHub Check: Python / Package (linux-musl-amd64)
  • GitHub Check: Python / Test (macos-arm64)
  • GitHub Check: Python / Package (linux-arm64)
  • GitHub Check: Python / Test (linux-amd64)
  • GitHub Check: Rust / Package (linux-amd64)
  • GitHub Check: Python / Test (linux-arm64)
  • GitHub Check: Python / Test (windows-amd64)
  • GitHub Check: Node.js / Test (linux-arm64)
  • GitHub Check: Rust / Test (linux-arm64)
  • GitHub Check: Rust / Test (windows-amd64)
  • GitHub Check: Node.js / Test (windows-amd64)
  • GitHub Check: Rust / Test (windows-arm64)
  • GitHub Check: Node.js / Test (linux-amd64)
  • GitHub Check: Node.js / Test (windows-arm64)
  • GitHub Check: Rust / Test (linux-amd64)
  • GitHub Check: Rust / Test (macos-arm64)
  • GitHub Check: Check / Run
  • GitHub Check: Node.js / Package OpenClaw plugin
  • GitHub Check: Python / Test (windows-arm64)
  • GitHub Check: Go / Test (windows-arm64)
  • GitHub Check: Go / Test (macos-arm64)
  • GitHub Check: Go / Test (linux-arm64)
  • GitHub Check: Go / Test (linux-amd64)
  • GitHub Check: Go / Test (windows-amd64)
  • GitHub Check: Preview docs
🧰 Additional context used
📚 Code guidelines: 5
.agents/skills/contribute-docs/SKILL.md — configured
.agents/skills/review-doc-style/SKILL.md — configured
CONTRIBUTING.md — configured
.agents/skills/prepare-code-freeze/SKILL.md — configured
.agents/skills/draft-release-notes/SKILL.md — configured
📓 Path-based instructions: 9
Review documentation for technical accuracy against the current API, command correctness, and consistency across language bindings.

⚙️ CodeRabbit configuration file

Files:

  • docs/integrate-into-frameworks/provider-response-codecs.mdx
Tests should cover the behavior promised by the changed API surface, including error paths and cross-request isolation where relevant.

⚙️ CodeRabbit configuration file

Files:

  • crates/core/tests/unit/codec/response_tests.rs
  • crates/core/tests/unit/codec/gemini_generate_content_tests.rs
Review the Rust runtime for async correctness, scope isolation, middleware ordering, and event lifecycle regressions.

⚙️ CodeRabbit configuration file

Files:

  • crates/core/tests/unit/codec/response_tests.rs
  • 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
Source excerpt: In MDX files, top-of-file comments must use JSX comment delimiters: `{/*` to open and `*/}` to close.

📄 CodeRabbit inference engine (.agents/skills/contribute-docs/SKILL.md)

Files:

  • docs/integrate-into-frameworks/provider-response-codecs.mdx
Source excerpt: Verify MDX files use JSX delimiters for top-of-file SPDX comments.

📄 CodeRabbit inference engine (.agents/skills/review-doc-style/SKILL.md)

Files:

  • docs/integrate-into-frameworks/provider-response-codecs.mdx
Applicability reference: AGENTS.md: # NeMo Relay Repository Guidance See `CONTRIBUTING.md` and `docs/contribute/testing-and-docs.mdx` for contributor details, and `.agents/skills/README.md` for the maintainer skill boundar...

📄 CodeRabbit inference engine (CONTRIBUTING.md)

Files:

  • crates/core/tests/unit/codec/response_tests.rs
  • 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
Source excerpt: Search documentation source for references to the old version and update current-version install commands, package examples, and configuration examples to `` where appropriate: Review matches before changing th...

📄 CodeRabbit inference engine (.agents/skills/prepare-code-freeze/SKILL.md)

Files:

  • docs/integrate-into-frameworks/provider-response-codecs.mdx
Applicability reference: AGENTS.md: # NeMo Relay Repository Guidance See `CONTRIBUTING.md` and `docs/contribute/testing-and-docs.mdx` for contributor details, and `.agents/skills/README.md` for the maintainer skill boundary.

📄 CodeRabbit inference engine (CONTRIBUTING.md)

Files:

  • crates/core/tests/unit/codec/response_tests.rs
  • 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
Source excerpt: Preserve MDX front matter and the JSX SPDX comment.

📄 CodeRabbit inference engine (.agents/skills/draft-release-notes/SKILL.md)

Files:

  • docs/integrate-into-frameworks/provider-response-codecs.mdx

Walkthrough

Gemini thought tokens now contribute to output-token cost estimates without changing normalized completion tokens. Checked arithmetic prevents cost estimates or fallback totals when token additions overflow.

Changes

Gemini Billing

Layer / File(s) Summary
Build billable usage
crates/core/src/codec/model_pricing.rs, crates/core/tests/unit/codec/response_tests.rs, docs/integrate-into-frameworks/provider-response-codecs.mdx
Provider pricing adds Gemini thoughts_tokens to billable completion usage with checked arithmetic. Response tests cover billing and overflow. The documentation describes the pricing rule.
Apply billing to Gemini usage
crates/core/src/codec/gemini_generate_content.rs, crates/core/tests/unit/codec/gemini_generate_content_tests.rs
The Gemini codec uses billable usage for cost estimates and checked addition for fallback totals. Tests cover thought-only billing and overflow cases.

Priority: ⬇️ Low

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Merge Risk: ⚪ Minimal · up to a9ac7

Gemini thinking tokens are included in estimated output cost without duplicate billing. No identified issue blocks merging after normal checks.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 68.75% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 16 functions across 4 files. (1 skipped: … Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Title check Passed The title follows Conventional Commits format, uses the allowed lowercase type and scope, states the main change, stays under 72 characters, and has no trailing period.
Description check Passed The description covers the overview, required confirmations, implementation details, reviewer starting point, and test scope. It omits the Related Issues section, but the description is otherwise comp…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Description check

Explanation

The description covers the overview, required confirmations, implementation details, reviewer starting point, and test scope. It omits the Related Issues section, but the description is otherwise complete and relevant.

Full details: Docstring Coverage

Explanation

Docstring coverage is 68.75% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 16 functions across 4 files. (1 skipped: 1 unsupported.)

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

✨ Finishing Touches: 💡 1
🧪 Generate unit tests (beta)
  • Create a new PR
🛠️ Fix failing CI checks: 💡
  • Commit to this branch
  • 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 greysonlalonde added this to the 0.11 milestone Oct 20, 2026
@github-actions

Copy link
Copy Markdown

@willkill07 willkill07 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.

I really like the logic improvements to model_pricing.rs

@willkill07
willkill07 merged commit 7f2de5c into NVIDIA:main Oct 21, 2026
96 checks passed
willkill07 pushed a commit that referenced this pull request Oct 21, 2026
#### Overview

<!-- Describe your pull request here -->

Fixes gemini usage accounting to include tool-use prompt tokens and
their associated input costs.

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

#### Details

<!-- Describe the changes made in this PR. -->

- added `tool_use_prompt_tokens` to
`ApiSpecificResponse::GeminiGenerateContent`, parsed from
`toolUsePromptTokenCount`. rust code constructing this variant must
provide this field.
- included tool-use prompt tokens in `total_tokens`, consistent with
google `totalTokenCount`.
- added input-rate costs for tool-use prompt tokens, excludes them when
grounding metadata shows google search or google maps, as those are
per-request, google doesn't document retrieved context as input tokens.
- select prompt-token pricing tier using the reported prompt count
before adding tool-use prompt tokens.
- added tests for token totals, `api_specific`, input-rate billing,
grounding exclusions, pricing tiers, input overflow, and redaction
pass-through.
- documented `tool_use_prompt_tokens` and billing in
`provider-response-codecs.mdx`.

#### Where should the reviewer start?

<!-- Point to the most important file, test, or design decision. -->

`billed_usage` and `ToolUsePromptBilling` in
`crates/core/src/codec/model_pricing.rs`

#### Related Issues: (use one of the action keywords Closes / Fixes /
Resolves / Relates to)

- Relates to #1427 


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
<!-- coderabbit-ai-generated:v1 -->

## Summary by CodeRabbit

## Summary by CodeRabbit

- **New Features**
  - Gemini response details now include tool-use prompt tokens.
- Gemini billing accounts for thinking tokens, tool usage, grounding,
URL context, and code execution.
- Pricing tiers use the original prompt-token count for more accurate
estimates.

- **Bug Fixes**
  - Improved fallback token totals and cost calculations.
- Costs are omitted when usage exceeds supported billing limits or
billing evidence is inconclusive.

- **Documentation**
  - Updated guidance on Gemini token accounting and billing rules.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Signed-off-by: Greyson Lalonde <[email protected]>

This branch was previously deployed

1 inactive deployment
fern — 6a9f3500 Deployed Oct 21, 2026 by willkill07 via Clean up docs preview #6107
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:S PR is small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants