Repository navigation
fix(agent): sum token usage across every model call in four loop agents - #4753
Conversation
proof_refinement_agent and three conversational_tool_use generation agents (domain, policy_tool, scenario) returned a rollout response whose usage came from one model call: the last correction turn, the last domain phase, the final tools completion, or the one completion chosen as the scenario artifact. reward_profile copies response.usage into the rollout token fields, so those under-counted every multi-call rollout. Sum every policy-model call's usage with accumulate_response_usage, as #4716 does for aviary_agent and toolsandbox_agent. Chat agents convert each completion's usage with a new chat_completion_usage_to_response_usage helper, which chat_completion_to_response now uses too. Scenario generation also counts completions whose text failed to parse; policy_tool_generation leaves its judge calls out, as rollout usage covers the policy model. proof_refinement_agent correction turns also copied max_tokens, which Responses requests don't have, so the caller's max_output_tokens cap was dropped on every turn after the first. Copy max_output_tokens instead. Follow-up to NVBug 6900411. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: qiaochuz <[email protected]>
|
/ok to test 4ca8744 |
yaoyu-33
left a comment
There was a problem hiding this comment.
Thanks for this, @qiaochuz-nv. I reviewed the diff at 4ca8744c9c with a team of reviewers (core library, agent harness, bug hunt, tests) plus an independent second pass, and had every finding challenged before posting.
Verdict: no blocking issues, no inline findings.
What I checked:
chat_completion_usage_to_response_usageis behavior-identical to the block it replaced inchat_completion_to_response, with annotations and a docstring.- Each of the four agents counts every policy-model call exactly once. That includes parse failures, rejected attempts, and every exit path of the proof-refinement loop. Judge calls in
policy_tool_generationstay excluded. - Nothing is shared or mutated:
accumulate_response_usagedeep-copies, and the summed usage is set on a response the agent owns. Accumulation is serial inscenario_generationandpolicy_tool_generation, so there is no race. - A parse-failed scenario completion now carries usage but can never be picked as the artifact, because selection still requires parsed scenarios.
max_output_tokensis the correct key: the Responses params model has nomax_tokensand forbids extras, and the new test asserts it on the correction request.- The new tests fail when the usage assignment is removed (checked per agent), and the PR's tests pass on this head.
Optional follow-ups, none blocking:
- A test for a turn that reports no
usage(the code handles it). - A test for a rejected-then-accepted attempt in
policy_tool_generation, to pin that earlier attempts' policy calls stay counted. NeMoGymResponseUsage.model_validate(turn_usage)inproof_refinement_agentis new strict validation on raw model JSON. A malformed usage block would now fail the rollout. The model servers already validate upstream, so I think this is fine as is.
PR hygiene: title, body, validation list, rollout evidence and compatibility notes all match the AGENTS.md bar. CI has not run yet (/ok to test is posted), and the PR is currently BLOCKED pending required checks and review.
yaoyu-33
left a comment
There was a problem hiding this comment.
Approving after the team review above: no blocking issues at the reviewed head.
|
🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA-NeMo/Gym/actions/runs/39829650997 |
What does this PR do?
Makes four more multi-call agents report the token usage of the whole rollout. Each of them returned
response.usagefrom a single model call, so per-rolloutinput_tokens/output_tokens/total_tokens, and everytoken aggregate built from them, were under-counted:
response.usagebefore this PRproof_refinement_agent(math_formal_lean multi-turn)max_correction_turns: 2)conversational_tool_use_domain_generationfollowup_count: 1)conversational_tool_use_policy_tool_generationconversational_tool_use_scenario_generationrequest_count)It also fixes a second bug in
proof_refinement_agentthat the live smoke surfaced: correction turns dropped thecaller's output cap.
Follow-up to #4716 (same defect in
aviary_agentandtoolsandbox_agent), NVBug6900411. No separate issue: the 6900411 retro's sweep of loop agents
found these agents, and the review of this PR found
policy_tool_generation.Cause
proof_refinement_agent.run()returns the last/verifyresult.math_formal_leanechoes the request body, soresponseis the last turn's model response.domain_generationreassignsfinal_responseeach phase and converts only it.policy_tool_generationconverts onlyfinal_completion.scenario_generationconverts onlylast_artifact_completion.None of them sums usage.
nemo_gym/reward_profile.pycopiesresponse.usageinto each rollout row. Thedocumented contract (
rollout-evidence.mdx) is the sum over model responses that finished.Output cap: since #563, correction turns copy
["temperature", "max_tokens", "top_p"]fromresponses_create_params. Responses requests have nomax_tokens(the params model forbids extra fields), somax_output_tokenswas dropped on every turn after the first.Fix
chat_completion_usage_to_response_usageinnemo_gym/responses_converter.py, extractedunchanged from
chat_completion_to_response, which now calls it.accumulate_response_usage(as fix(aviary): sum token usage across every model call in an episode #4716 does) and sets the sum on theresponse it returns. The response text and other fields still come from the same call as before.
stays on its failure outcome. It is never picked as the artifact, because selection still requires parsed scenarios.
role == "policy"calls only. Judge calls go to an auxiliary model, and Gymkeeps those out of rollout usage (rollout-health "Binding Policy-Model Calls";
switchyard.mdx). A rejectedattempt's policy calls are counted, since their tokens were spent.
max_output_tokensinstead ofmax_tokens.sum. Scenario generation also has a test where every completion fails to parse. The proof refinement test checks
that a correction turn keeps
max_output_tokens/temperature/top_p, plus a direct test for the helper. Oneexisting scenario test pinned the old value (5/3/8, one of 19 completions); it now expects 95/57/152.
Before the fix
This branch's tests run against
main's (d3205cd89) agent code:Live, with model-call capture on (inference provider,
Nemotron-3-Nano-30B-A3B, shipped config, one example roweach).
response.usageis one captured call:After the fix
Gym
d80b6a404(this branch, then rebased without conflicts onto4ced0d735as4ca8744c9; the unit counts belowwere rerun on
4ca8744c9), same commands:Validation
gym env test +entrypoint=responses_api_agents/<agent> +should_validate_data=truefor all four: 4, 17, 29, and 24passed. Every new assertion fails on
main.tests/unit_tests/test_responses_converter.py+test_openai_utils.py: 460 passed. Fulltests/unit_testson the Mac: 36 failed with the fix vs 37 on a pristine
maintree; the fix's failures are a subset ofmain's(pre-existing sandbox/telemetry failures on this host).
pre-commit run --all-files: passed.mainand this branch. No registered QA case coversthese agents. Proof refinement ran the shipped
math_formal_lean_multi_turnconfig without a Lean sandbox: thesandbox client turns the connection error into a failed compile, so every turn asks for a correction. Rewards in
that run are meaningless; only the agent loop and its usage were checked.
scenario_generationran withrequest_count=3andscenarios_per_request=2, andpolicy_tool_generationwithmax_retrieslowered, to boundcost.
Compatibility
follow-up phase, all policy calls including rejected attempts, and all returned scenario completions. Earlier
numbers under-count and are not comparable. Judge calls in policy/tool generation remain excluded.
proof_refinement_agentcorrection turns now honormax_output_tokens. Before, they generated uncapped, up to45k tokens in one smoke turn against a 1024 cap. That changes generation length and cost for any run that sets a
cap.
chat_completion_usage_to_response_usageis a new public helper;chat_completion_to_responseoutput is unchanged.reasoning/instructions/top_logprobs; no shipped caller sets them. Left as is.🤖 Generated with Claude Code