Skip to content

fix(codecs): keep Anthropic stream usage when message_delta reports null - #1778

Merged
greysonlalonde merged 2 commits into
NVIDIA:release/0.11from
Sakshitapkir03:fix/anthropic-stream-null-usage-0.11
Nov 20, 2026
Merged

greysonlalonde merged 2 commits into
NVIDIA:release/0.11from
Sakshitapkir03:fix/anthropic-stream-null-usage-0.11

Conversation

@Sakshitapkir03

@Sakshitapkir03 Sakshitapkir03 commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Overview

Skips null values when merging Anthropic message_delta usage into the message_start usage, so a streamed call keeps its input and cache token counts when the terminal delta reports them as null.

  • 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

  • crates/core/src/codec/anthropic.rs: observe_usage skips null values when extending the accumulated usage; non-null values, including 0, still replace earlier ones.
  • crates/core/tests/unit/codec/anthropic_tests.rs: add anthropic_streaming_codec_keeps_message_start_usage_when_delta_reports_null.
  • Matches Anthropic's SDK accumulator, which copies these counters from message_delta only when they are not None.
  • Fields omitted from message_delta keep the behavior from fix: preserve Anthropic usage across stream events #996.
  • No breaking changes.

Tested on macOS, on release/0.11 at cf0712e5:

  • The new test fails without the fix (left: Null, right: Number(10)) and passes with it.
  • cargo nextest run -p nemo-relay -E 'test(~anthropic)': 89 passed.
  • just test-rust: 6733 passed, 4 skipped (with ulimit -n 4096, since test(cli): raise the open-file limit for HTTP/1.1 concurrency tests #1736 is on main only). One unrelated ATOF timing test, unanswered_http_post_requests_record_flush_and_close_timeouts, was flaky and passed on retry and when run alone.
  • cargo fmt --all -- --check and cargo clippy -p nemo-relay --all-targets -- -D warnings pass.
  • Pre-commit hooks pass, except npm audit and uv audit, which I skipped; this change touches no lockfiles.

Not tested on Linux or Windows; CI covers those.

Where should the reviewer start?

observe_usage in crates/core/src/codec/anthropic.rs.

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

Closes #1775

Summary by CodeRabbit

  • Bug Fixes
    • Streaming usage totals now retain previously reported values when later updates provide null, while still applying non-null updates.

@Sakshitapkir03
Sakshitapkir03 requested a review from a team as a code owner November 20, 2026 00:30
@copy-pr-bot

copy-pr-bot Bot commented Nov 20, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@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 Nov 20, 2026
@coderabbitai

coderabbitai Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The Anthropic streaming codec now preserves accumulated usage values when an update reports null fields. A regression test checks that non-null output token updates still replace the earlier value.

Changes

Anthropic streaming usage

Layer / File(s) Summary
Usage merge and regression coverage
crates/core/src/codec/anthropic.rs, crates/core/tests/unit/codec/anthropic_tests.rs
The codec skips null fields when merging usage updates into existing usage. The regression test checks that input and cache counts remain unchanged while the output count updates.

Estimated code review effort: 2 (Simple) | ~8 minutes

Change: Bug fix · Severity of issue fixed: Medium

Merge Risk: Minimal · up to 97c8c

This change keeps input and cache token counts from the start of a streamed Anthropic response when a later update reports null, so telemetry and cost estimates stay accurate. It is small, covered by a regression test, and ready to merge.

Security architecture risk: Minimal · up to 97c8c

The change preserves previously reported token counts without expanding access or authority. No introduced or worsened risk to security, failure containment, rollback, or ownership was identified.

Pre-merge checks: Passed 3 · Inconclusive 1

Failed Failed checks: 1 inconclusive

Check name Status Explanation Resolution
Linked Issues check Inconclusive Requirements from #1775 (direct): for streamed Anthropic calls, a null field in message_delta usage must keep the earlier message_start value. Non-null values, including 0, must replace it. A regressi… Provide readable source for observe_usage in crates/core/src/codec/anthropic.rs at the reviewed head, or a diff hunk for that function, and the test body for anthropic_streaming_codec_keeps_message_start_usage_when_delta_reports_null in cra…
Passed Passed checks: 3 passed

Check name Status Explanation
Title check Passed The title uses the Conventional Commits format with the allowed type 'fix' and scope 'codecs'. It is 72 characters, which is at the limit of the 72-character maximum, and it has no trailing period. It…
Description check Passed The description follows the template. It has the Overview section with both checklist items marked, the Details section listing the changed files and test, the Where should the reviewer start section …
Out of Scope Changes check Passed The PR summary and raw summary list two changed files. These are crates/core/src/codec/anthropic.rs (observe_usage, +6/-1) and crates/core/tests/unit/codec/anthropic_tests.rs (one added regression tes…
Full details: Title check

Explanation

The title uses the Conventional Commits format with the allowed type 'fix' and scope 'codecs'. It is 72 characters, which is at the limit of the 72-character maximum, and it has no trailing period. It describes the main change: keeping Anthropic stream usage when message_delta reports null.

Full details: Description check

Explanation

The description follows the template. It has the Overview section with both checklist items marked, the Details section listing the changed files and test, the Where should the reviewer start section pointing to observe_usage, and the Related Issues section with Closes #1775. It also gives concrete testing evidence and explains the null-skipping behavior.

Full details: Out of Scope Changes check

Explanation

The PR summary and raw summary list two changed files. These are crates/core/src/codec/anthropic.rs (observe_usage, +6/-1) and crates/core/tests/unit/codec/anthropic_tests.rs (one added regression test, +42/-0). Both changes match the #1775 requirement and its regression test. The PR description reports no breaking changes and no lockfile changes. Verification is limited: the diff could not be read, so the line-level content is not confirmed. No unrelated change is shown.

Full details: Linked Issues check

Explanation

Requirements from #1775 (direct): for streamed Anthropic calls, a null field in message_delta usage must keep the earlier message_start value. Non-null values, including 0, must replace it. A regression test is also required. The PR description and the AI-generated raw summary both state that observe_usage skips null values and that the test was added. The inspection of the reviewed head did not confirm this. The diff request failed because changed blobs are missing from the checkout. The file read of crates/core/src/codec/anthropic.rs was truncated at 16000 bytes before observe_usage, so the merge logic was not read. The test file was not read. The PR text reports the test failing without the fix and passing with it, and reports the codec suite passing. Those are author claims, not verified repository evidence. Remaining uncertainty: the observe_usage filter at head, and whether the new test asserts the issue's four fields.

Resolution

Provide readable source for observe_usage in crates/core/src/codec/anthropic.rs at the reviewed head, or a diff hunk for that function, and the test body for anthropic_streaming_codec_keeps_message_start_usage_when_delta_reports_null in crates/core/tests/unit/codec/anthropic_tests.rs. Confirm that the code skips null values, that non-null values including 0 still replace earlier values, and that the test covers input_tokens, cache_creation_input_tokens, cache_read_input_tokens, and output_tokens.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 8d12ae1 and 97c8c74.

Files selected for processing: 2

  • crates/core/src/codec/anthropic.rs
  • crates/core/tests/unit/codec/anthropic_tests.rs

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: 1

  • GitHub Check: CodeRabbit
Additional context used

Path-based instructions: 2

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/anthropic_tests.rs
Review the Rust runtime for async correctness, scope isolation, middleware ordering, and event lifecycle regressions.

CodeRabbit configuration file

Files:

  • crates/core/src/codec/anthropic.rs
  • crates/core/tests/unit/codec/anthropic_tests.rs
Additional comments: 2

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

1544-1549: LGTM!

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

2198-2238: LGTM!

@greysonlalonde

Copy link
Copy Markdown
Contributor

/ok to test 97c8c74

@github-actions

Copy link
Copy Markdown

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

thank you @Sakshitapkir03 !

@greysonlalonde
greysonlalonde merged commit 58af44c into NVIDIA:release/0.11 Nov 20, 2026
97 checks passed
@greysonlalonde greysonlalonde added this to the 0.11 milestone Nov 20, 2026
@Sakshitapkir03
Sakshitapkir03 deleted the fix/anthropic-stream-null-usage-0.11 branch November 20, 2026 14:33

This branch was previously deployed

1 inactive deployment
fern — 97c8c740 Deployed Nov 20, 2026 by copy-pr-bot[bot] via Preview docs #7435
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