Skip to content

fix(core): bill uncached input tokens in model pricing - #1500

Merged
willkill07 merged 1 commit into
NVIDIA:mainfrom
greysonlalonde:fix/core-uncached-input-pricing
Oct 25, 2026
Merged

willkill07 merged 1 commit into
NVIDIA:mainfrom
greysonlalonde:fix/core-uncached-input-pricing

Conversation

@greysonlalonde

@greysonlalonde greysonlalonde commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Overview

Providers report cached input in different ways. This can cause a cost estimate to count tokens twice or miss some tokens.

This change makes cached-input pricing consistent. Relay bills each token group once. It does not estimate a cost when the reported counts conflict.

  • 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

  • Bill uncached input at the normal input rate.
  • Bill cache reads and writes at their own rates.
  • Count all input tokens when selecting a pricing tier.
  • Support providers that report either uncached input or total input.
  • Skip the cost estimate when token counts conflict.
  • Keep token counts consistent in cost estimates and optimization summaries.
  • Add tests and documentation for these cases.

For example, a request can have 1,000 uncached input tokens and 200 cached input tokens. Relay bills each group at its own rate and uses 1,200 tokens to select the pricing tier.

Where should the reviewer start?

Start with the cached-input pricing behavior. Check that Relay bills each token group once and uses the full input count to select a pricing tier. Then check that conflicting counts do not produce an estimate.

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

  • Relates to: none

Summary by CodeRabbit

  • Bug Fixes
    • Improved cost estimates for usage that reports uncached input, cache reads and writes, or separately billed tool-use tokens.
    • Pricing tiers now account for cache tokens when determining applicable rates.
    • Inconsistent token counts may result in an unavailable estimate rather than an unreliable charge.
    • Usage reporting better preserves related prompt, cache, and uncached-input counts when combining usage data.
  • Documentation
    • Clarified how uncached input, cache accounting, and token-based pricing tiers affect estimates.

@greysonlalonde greysonlalonde self-assigned this Oct 25, 2026
@coderabbitai

coderabbitai Bot commented Oct 25, 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
Commits

Reviewing files that changed from the base of the PR and between 532c353 and c312f75.

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
Commits

Reviewing files that changed from the base of the PR and between 4ce3a87 and 532c353.

Files selected for processing: 10
  • crates/core/src/api/optimization.rs
  • crates/core/src/codec/model_pricing.rs
  • crates/core/src/observability/mod.rs
  • crates/core/tests/unit/codec/parity_tests.rs
  • crates/core/tests/unit/codec/response_tests.rs
  • crates/core/tests/unit/observability/exporter_parity_tests.rs
  • crates/core/tests/unit/observability/mod_tests.rs
  • crates/core/tests/unit/optimization_tests.rs
  • docs/configure-plugins/model-pricing.mdx
  • 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: Python / Package (linux-musl-arm64)
  • GitHub Check: Rust / Package (linux-musl-arm64)
  • GitHub Check: Node.js / Package (windows-amd64)
  • GitHub Check: Go / Test (macos-arm64)
  • GitHub Check: Rust / Package (windows-arm64)
  • GitHub Check: Rust / Package (linux-arm64)
  • GitHub Check: Rust / Package (windows-amd64)
  • GitHub Check: Node.js / Test (windows-amd64)
  • GitHub Check: Rust / Package (macos-arm64)
  • GitHub Check: Rust / Package (linux-musl-amd64)
  • GitHub Check: Node.js / Package (macos-arm64)
  • GitHub Check: Rust / Package (linux-amd64)
  • GitHub Check: Node.js / Package (linux-musl-amd64)
  • GitHub Check: Node.js / Test (linux-arm64)
  • GitHub Check: Node.js / Test (macos-arm64)
  • GitHub Check: Go / Test (linux-arm64)
  • GitHub Check: Go / Test (windows-amd64)
  • GitHub Check: Node.js / Test (linux-amd64)
  • GitHub Check: Node.js / Package (linux-arm64)
  • GitHub Check: Python / Package (linux-amd64)
  • GitHub Check: Python / Package (windows-amd64)
  • GitHub Check: Node.js / Package (linux-amd64)
  • GitHub Check: Python / Package (linux-musl-amd64)
  • GitHub Check: Go / Test (windows-arm64)
  • GitHub Check: Node.js / Package (linux-musl-arm64)
  • GitHub Check: Node.js / Test (windows-arm64)
  • GitHub Check: Node.js / Package (windows-arm64)
  • GitHub Check: Python / Package (macos-arm64)
  • GitHub Check: Python / Package (linux-arm64)
  • GitHub Check: Python / Package (windows-arm64)
  • GitHub Check: Python / Test (macos-arm64)
  • GitHub Check: Python / Test (linux-amd64)
  • GitHub Check: Rust / Test (windows-arm64)
  • GitHub Check: Python / Test (windows-amd64)
  • GitHub Check: Python / Test (windows-arm64)
  • GitHub Check: Rust / Test (macos-arm64)
  • GitHub Check: Python / Test (linux-arm64)
  • GitHub Check: Go / Test (linux-amd64)
  • GitHub Check: Rust / Test (linux-amd64)
  • GitHub Check: Rust / Test (linux-arm64)
  • GitHub Check: Rust / Test (windows-amd64)
  • GitHub Check: Node.js / Package OpenClaw plugin
  • GitHub Check: Check / Run
  • GitHub Check: Preview docs
Additional context used
Code guidelines: 4
.agents/skills/contribute-docs/SKILL.md — configured
.agents/skills/add-binding-feature/SKILL.md — configured
.agents/skills/add-middleware/SKILL.md — configured
.agents/skills/review-doc-style/SKILL.md — configured
Path-based instructions: 8
Review documentation for technical accuracy against the current API, command correctness, and consistency across language bindings.

CodeRabbit configuration file

Files:

  • docs/configure-plugins/model-pricing.mdx
  • 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/parity_tests.rs
  • crates/core/tests/unit/observability/exporter_parity_tests.rs
  • crates/core/tests/unit/observability/mod_tests.rs
  • crates/core/tests/unit/codec/response_tests.rs
  • crates/core/tests/unit/optimization_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/parity_tests.rs
  • crates/core/tests/unit/observability/exporter_parity_tests.rs
  • crates/core/tests/unit/observability/mod_tests.rs
  • crates/core/src/observability/mod.rs
  • crates/core/src/api/optimization.rs
  • crates/core/tests/unit/codec/response_tests.rs
  • crates/core/tests/unit/optimization_tests.rs
  • crates/core/src/codec/model_pricing.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/configure-plugins/model-pricing.mdx
  • docs/integrate-into-frameworks/provider-response-codecs.mdx
Source excerpt: **Core Rust** Implement the behavior first in `crates/core/src/api/` and related core modules such as `crates/core/src/api/runtime/`, `crates/core/src/codec/`, or `crates/core/src/json.rs`.

CodeRabbit inference engine (.agents/skills/add-binding-feature/SKILL.md)

Files:

  • crates/core/src/api/optimization.rs
Source excerpt: [ ] Core function with doc comment in `crates/core/src/api/`

CodeRabbit inference engine (.agents/skills/add-binding-feature/SKILL.md)

Files:

  • crates/core/src/api/optimization.rs
Source excerpt: Add registration and deregistration APIs in `crates/core/src/api/`.

CodeRabbit inference engine (.agents/skills/add-middleware/SKILL.md)

Files:

  • crates/core/src/api/optimization.rs
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/configure-plugins/model-pricing.mdx
  • docs/integrate-into-frameworks/provider-response-codecs.mdx
Learnings: 1
Common learnings
Learnt from: CR
Repo: NVIDIA/NeMo-Relay PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-10-19T23:46:28.573Z
Learning: Source excerpt:
# NeMo Relay Repository Guidance

## Repository Conventions

- Use signed-off commits and repository branch naming only when the user asks
  for commit or branch work. For PR work, read the current repository template.
Additional comments: 10
crates/core/src/observability/mod.rs: 1

867-895: LGTM!

crates/core/tests/unit/observability/mod_tests.rs: 1

4-5: LGTM!

Also applies to: 15-40

crates/core/src/codec/model_pricing.rs: 1

272-273: LGTM!

Also applies to: 312-327, 332-332, 340-343, 383-402, 520-520, 573-573, 577-577, 580-580, 870-870, 879-918, 931-931, 942-954, 960-963, 971-981

crates/core/tests/unit/codec/response_tests.rs: 1

137-138: LGTM!

Also applies to: 146-147, 686-830, 1683-1758

crates/core/tests/unit/codec/parity_tests.rs: 1

642-643: LGTM!

Also applies to: 647-647

crates/core/tests/unit/observability/exporter_parity_tests.rs: 1

380-380: LGTM!

Also applies to: 387-388

docs/configure-plugins/model-pricing.mdx: 1

67-80: LGTM!

docs/integrate-into-frameworks/provider-response-codecs.mdx: 1

126-126: LGTM!

Also applies to: 180-184, 376-376, 388-388

crates/core/src/api/optimization.rs: 1

13-13: LGTM!

Also applies to: 582-585, 589-589, 632-632, 683-748

crates/core/tests/unit/optimization_tests.rs: 1

17-18: LGTM!

Also applies to: 807-1052, 1269-1269, 1286-1293

Walkthrough

Relay now uses uncached_input_tokens in cost estimation and optimization baseline reconstruction. Usage merging preserves related prompt and cache fields, while pricing validates split counts and includes cache tokens in rate-tier selection.

Changes

Uncached Input Usage Accounting

Layer / File(s) Summary
Merge split usage fields
crates/core/src/observability/mod.rs, crates/core/tests/unit/observability/mod_tests.rs
When usage includes uncached_input_tokens, merge_usage takes prompt and cache fields from that same usage. Tests cover both input orders.
Price uncached input and select tiers
crates/core/src/codec/model_pricing.rs, crates/core/tests/unit/codec/response_tests.rs, crates/core/tests/unit/codec/parity_tests.rs, crates/core/tests/unit/observability/exporter_parity_tests.rs, docs/configure-plugins/model-pricing.mdx, docs/integrate-into-frameworks/provider-response-codecs.mdx
Cost estimation uses uncached input when reported and checks its consistency with prompt and cache counts. Cache reads and writes count toward rate-tier selection. Gemini tool-use input tokens are included in billing. Tests and documentation cover these behaviors and provider usage mappings.
Reconstruct optimization baseline usage
crates/core/src/api/optimization.rs, crates/core/tests/unit/optimization_tests.rs
Baseline derivation uses saved prompt and cache tokens with the observed and API-specific input splits. Tests cover inconsistent or undetermined splits, missing prompt counts, and overflow.

Priority: Normal

Estimated code review effort: 4 (Complex) | ~45 minutes

Change: Bug fix

Merge Risk: Minimal · up to 532c3

No actionable pricing or usage-accounting issue remains identified; the change is ready for normal merge checks.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 2.13% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 47 functions across 8 files. (2 skipped: 2… 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, gives a concise imperative summary, stays under 72 characters, and has no trailing period.
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.
Description check Passed The pull request description includes all required sections, completed contribution and duplication checks, detailed implementation scope, reviewer guidance, and a valid Relates to entry indicating th…
Full details: Description check

Explanation

The pull request description includes all required sections, completed contribution and duplication checks, detailed implementation scope, reviewer guidance, and a valid Relates to entry indicating that no issue is linked.

Full details: Docstring Coverage

Explanation

Docstring coverage is 2.13% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 47 functions across 8 files. (2 skipped: 2 unsupported.)

Resolution

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

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.

@github-actions github-actions Bot added size:L PR is large Bug issue describes bug; PR fixes bug lang:rust PR changes/introduces Rust code labels Oct 25, 2026
@github-actions

Copy link
Copy Markdown

- bill `uncached_input_tokens` at the input rate when usage reports it
- leave usage unpriced when its input counts don't match
- count cache reads and writes when picking a rate tier
- keep uncached input in optimization baseline usage
- take merged input counts from the source with `uncached_input_tokens`
- test: split usage pricing, tier selection, baseline uncached input, merged usage

Signed-off-by: Greyson Lalonde <[email protected]>
@greysonlalonde
greysonlalonde force-pushed the fix/core-uncached-input-pricing branch from 532c353 to c312f75 Compare October 25, 2026 19:09
@greysonlalonde
greysonlalonde marked this pull request as ready for review October 25, 2026 21:13
@greysonlalonde
greysonlalonde requested review from a team as code owners October 25, 2026 21:13
@willkill07
willkill07 merged commit 1cb799a into NVIDIA:main Oct 25, 2026
99 of 100 checks passed
@willkill07 willkill07 added this to the 0.11 milestone Oct 25, 2026

This branch was previously deployed

1 inactive deployment
fern — c312f75d Deployed Oct 25, 2026 by willkill07 via Clean up docs preview #6539
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.

2 participants