Skip to content

fix: accept scalar provider costs across response codecs - #1576

Merged
willkill07 merged 2 commits into
NVIDIA:release/0.11from
willkill07:fix/1573-scalar-usage-cost
Oct 29, 2026
Merged

willkill07 merged 2 commits into
NVIDIA:release/0.11from
willkill07:fix/1573-scalar-usage-cost

Conversation

@willkill07

@willkill07 willkill07 commented Oct 29, 2026 •

Copy link
Copy Markdown
Member

Overview

Preserve response annotations when OpenAI Responses or Anthropic Messages upstreams return a numeric usage.cost, as OpenRouter does. Numeric costs are recorded as provider-reported USD totals.

  • 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

  • Share scalar and detailed cost decoding across Chat Completions, Responses, and Messages.
  • Preserve cost_usd precedence and detailed cost currency/component handling.
  • Ignore unrecognized usage.cost values so token usage, response text, and finish reasons remain available.
  • Add regression coverage across all three codecs and update the cost field documentation.

Validation:

  • just test-rust: 6,599 passed, 4 skipped before updating the branch base to the latest release/0.11.
  • Focused provider cost regression tests passed again on the latest release base.
  • Formatting, Clippy, workspace checks, documentation links, Python audit, and npm audit passed on the latest release base.
  • The cargo-deny hook failed while parsing the local advisory database (failed to find toml block); only that hook was skipped when committing.
  • just test-python-google-adk: 566 passed, including annotation retention for unrecognized cost and the separate invalid-usage failure path. Other binding suites were not rerun locally.

Where should the reviewer start?

Start with crates/core/src/codec/response.rs for shared cost decoding, then crates/core/tests/unit/codec/response_tests.rs for numeric, detailed, malformed, and precedence cases.

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

Summary by CodeRabbit

  • New Features
    • Cost reporting now supports both scalar and structured usage.cost values for OpenAI Chat, OpenAI Responses, and Anthropic Messages. When available, cost_usd takes precedence over usage.cost.
    • Unrecognized cost values no longer prevent token usage or other response details from being retained. Configured pricing can still provide an estimate when no usable reported cost is available.
  • Documentation
    • Updated provider integration guidance to describe supported cost formats and precedence.

@willkill07
willkill07 requested review from a team as code owners October 29, 2026 20:02
@coderabbitai

coderabbitai Bot commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Walkthrough

The response codecs now accept scalar and structured usage.cost values through a shared decoder. Unrecognized cost values no longer prevent the codecs from retaining other response annotations. Tests and documentation cover the supported cost formats and precedence.

Changes

Provider-reported response costs

Layer / File(s) Summary
Shared provider cost decoding
crates/core/src/codec/response.rs
A shared raw cost type accepts scalar values, detailed objects, and unrecognized shapes. Provider cost handling maps scalar values to the total and retains existing detailed-cost handling.
Codec integration and validation
crates/core/src/codec/anthropic.rs, crates/core/src/codec/openai_chat.rs, crates/core/src/codec/openai_responses.rs, crates/core/tests/unit/codec/response_tests.rs, docs/integrate-into-frameworks/provider-response-codecs.mdx, python/tests/integrations/google_adk_tests/test_model_backends.py
All three codecs use the shared type. Tests cover scalar, structured, and unrecognized costs, including retention of other annotations and cost_usd behavior. Documentation describes accepted cost fields and precedence.

Priority: Normal

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

Change: Bug fix · Severity of issue fixed: Medium

Merge Risk: Low · up to b4e33

Some responses may show a labeled estimate rather than no cost. This is a limited reporting discrepancy and does not block merging.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat… 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 fix, provides a concise imperative summary, is 56 characters long, and has no trailing period.
Description check Passed The description includes the required Overview, Details, reviewer-start, and Related Issues sections. It documents the changes, validation results, known cargo-deny limitation, and linked issues.
Linked Issues check Passed Issue #1573 requires scalar and detailed usage.cost support for Responses and Messages. It also requires retention of token usage and response annotations when cost is unrecognized. The shared `RawP…
Out of Scope Changes check Passed The changes stay within issue #1573. The shared decoder refactor supports the required behavior across the three response codecs. The codec tests, integration test updates, and provider cost documenta…
Full details: Linked Issues check

Explanation

Issue #1573 requires scalar and detailed usage.cost support for Responses and Messages. It also requires retention of token usage and response annotations when cost is unrecognized. The shared RawProviderUsageCost type now supports scalar, detailed, and unknown values. Responses, Messages, and Chat Completions use the shared decoder. provider_reported_cost preserves cost_usd precedence and detailed cost handling. Regression tests cover the three codecs, malformed values, precedence, token usage, response annotations, and finish reasons.

Full details: Out of Scope Changes check

Explanation

The changes stay within issue #1573. The shared decoder refactor supports the required behavior across the three response codecs. The codec tests, integration test updates, and provider cost documentation validate or explain that behavior. No unrelated product behavior or unrelated files appear in the reviewed changes.

Full details: Docstring Coverage

Explanation

Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 4 functions across 6 files.

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.

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

lgtm

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

Copy link
Copy Markdown

@greysonlalonde

Copy link
Copy Markdown
Contributor

lgtm

I take it back ❌

Need to update tests with old contract

@github-actions github-actions Bot added the lang:python PR changes/introduces Python code label Oct 29, 2026
@willkill07 willkill07 self-assigned this Oct 29, 2026
@willkill07 willkill07 added this to the 0.11 milestone Oct 29, 2026
@willkill07
willkill07 merged commit b2e5551 into NVIDIA:release/0.11 Oct 29, 2026
60 of 61 checks passed
@willkill07
willkill07 deleted the fix/1573-scalar-usage-cost branch October 30, 2026 14:46

This branch was previously deployed

1 inactive deployment
fern — b4e3367e Deployed Oct 29, 2026 by willkill07 via Clean up docs preview #6807
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:python PR changes/introduces Python code 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