Skip to content

fix(capture): record usage for streamed chat without a usage chunk - #5061

Merged
cmunley1 merged 1 commit into
mainfrom
cmunley1/capture-streamed-usage
Nov 6, 2026
Merged

cmunley1 merged 1 commit into
mainfrom
cmunley1/capture-streamed-usage

Conversation

@cmunley1

Copy link
Copy Markdown
Contributor

What does this PR do?

Model-call capture now records token usage for streamed Chat Completions calls whose client does not set stream_options.include_usage.

Gym answers a streaming chat request by calling the backend non-streaming and replaying the answer as SSE. A client that asks for no usage chunk gets none, which is correct, but capture rebuilds its record from that same stream, so the record had no token counts. The OpenClaw gateway streams this way. In a SWE-Atlas QA run through NeMo Fabric's OpenClaw adapter, capture recorded 24 and 96 calls with null tokens.

The dispatch now keeps the backend's usage on request.state, and capture fills it in when the rebuilt response has none. The bytes sent to the client are unchanged.

Validation

  • New test test_capture_records_usage_the_client_did_not_ask_for: a streamed request with and without include_usage through the real dispatch and capture middleware. The include_usage: false case fails on main.
  • pytest tests/unit_tests/test_chat_completions_streaming.py tests/unit_tests/test_base_responses_api_model.py tests/unit_tests/test_rollout_correlation.py: 147 passed.
  • Full tests/unit_tests: the same failures with and without this change, all in environment-dependent tests on macOS (OpenSandbox, telemetry extras, launchers). None touch capture.
  • ruff format and ruff check on the changed files: passed.

Compatibility and benchmark impact

Capture records only. Streamed calls that already carry a usage chunk and non-streamed calls are unchanged.

Checklist

  • I have read the contributing guidelines.
  • The change is focused.
  • Tests added and pass locally.
  • Documentation: module docstring in nemo_gym/chat_streaming.py updated.
  • Pre-commit checks pass locally.
  • New source files include the required Apache-2.0 SPDX header (no new files).
  • All commits have DCO sign-off.

@copy-pr-bot

copy-pr-bot Bot commented Oct 25, 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.

@cmunley1

Copy link
Copy Markdown
Contributor Author

/ok to test 0f3067c

@cmunley1

Copy link
Copy Markdown
Contributor Author

/review

@nemo-automation-bot
nemo-automation-bot Bot requested a review from a team October 25, 2026 02:35
@svcnvidia-nemo-ci svcnvidia-nemo-ci added area:model Model servers, inference providers, and model adapters bug Something isn't working needs-review PR is ready for code review and waiting on a reviewer labels Oct 25, 2026

@svcnvidia-nemo-ci svcnvidia-nemo-ci 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.

SHIP

LGTM — no reliability concerns.

Reviewed all three changed files and traced immediate/delayed streaming dispatch through capture storage and downstream token accounting. The fallback preserves existing usage and leaves client SSE unchanged. The new regression test exercises real dispatch and capture for both usage settings.

Static review only; tests were not executed.

Note: formal approval is not available for this request, so this review is posted as a comment only.

Review generated by codex. This was a full review. Request a full review with /review reset=true.

@cmunley1

Copy link
Copy Markdown
Contributor Author

/ok to test 25eb296

@cmunley1

cmunley1 commented Nov 5, 2026

Copy link
Copy Markdown
Contributor Author

/review

@svcnvidia-nemo-ci svcnvidia-nemo-ci 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.

SHIP

LGTM — no concerns found.

This follow-up checked changes since the previous clean review. Both production files are unchanged. The expanded regression test adds delayed responses, verifies that a keepalive came first, and retains client-usage and captured-token assertions for both usage settings.

I traced delayed dispatch through capture finalization and storage. Capture reads the request-state usage after the streamed handler completes. One delegated report was unavailable, so I checked its capture-storage topic directly.

Static review only; tests were not executed.

Review generated by codex. This was a findings-only review. Request a full review with /review reset=true.

… no usage chunk

A chat client that does not set stream_options.include_usage gets no usage chunk, so the
model-call record reconstructed from the stream had no token counts. The dispatch now hands
the backend's usage to capture, as the Responses SSE path already does.

Signed-off-by: Christian Munley <[email protected]>
@cmunley1

cmunley1 commented Nov 5, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test 2d7a980

@cmunley1
cmunley1 added this pull request to the merge queue Nov 6, 2026
@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA-NeMo/Gym/actions/runs/42264665410

Merged via the queue into main with commit cc6fd5b Nov 6, 2026
106 of 109 checks passed
@cmunley1
cmunley1 deleted the cmunley1/capture-streamed-usage branch November 6, 2026 18:01

This branch was previously deployed

1 inactive deployment
public — 2d7a9803 Deployed Nov 5, 2026 by copy-pr-bot[bot] via release / finalize / notify #10456
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:model Model servers, inference providers, and model adapters bug Something isn't working needs-review PR is ready for code review and waiting on a reviewer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants