Skip to content

fix(tier3): count token usage per case correctly and keep the terminal token notes accurate - #546

Merged
rng1995 merged 16 commits into
mainfrom
naren/fix-tier3-token-usage-followups
Nov 14, 2026
Merged

rng1995 merged 16 commits into
mainfrom
naren/fix-tier3-token-usage-followups

Conversation

@rng1995

@rng1995 rng1995 commented Nov 14, 2026

Copy link
Copy Markdown
Collaborator

Summary

Follow-up to #427 (merged through #407), which added Tier 3 token usage per case, per agent and over all agents. On current main, five problems still give wrong numbers, a missing table or a note that contradicts itself: the terminal tables point at case rows that no report has, an unscored native task attempt lands in a case row of its own, an invalid cache counter on a scored attempt counts as 0, tier3 evaluate prints no token table for a run whose arms all failed, and a case that a run budget cut short reads as partial. This PR fixes each one and adds a regression test for it.

Changes

  • The terminal pointed at case rows that no report had. When the report payload has to make room, it drops the token case rows of every agent, and every report reads that payload. The validate and tier3 evaluate terminal tables still named the report files that hold the case rows and then said the rows were left out of "this report". They now say that the case rows were left out of every report to keep the reports within their size limits, and that the agent sums cover every attempt. The note that names case IDs the evaluation dataset does not have still follows.
  • An unscored native task attempt landed in a case row of its own. An attempt without a scored reward took its case from the task directory in its Harbor files. A native task can set its own [metadata] entry_id, so its unscored attempts formed a separate row (marked as an unverified case ID when the report has the dataset's case IDs), the agent note said the arms ran different cases (case_scope_mismatch), and the case row left out the attempt's tokens. The collector now records the attempt's case as entry_id in the unscored trial's failure.json. It reads the task from the trial's result.json, or from config.json for a trial that stopped before Harbor wrote a result. A Harbor run maps that task to its case with the run's trusted selector mapping. A NeMo Gym run has no such mapping, so the collector resolves the case from the arm's staged task files, the way it resolves a scored attempt, and records it only when that case is authoritative. The loader prefers the recorded case and keeps the task directory and trial name fallback for runs without it.
  • An invalid cache counter on a scored attempt counted as 0. The loader turned an invalid total_cached_tokens (negative, fractional, a string or a bool, or above 2^53 - 1) into an absent counter when it read a scored attempt's trajectory, and token usage then read the absent cache as Harbor's omitted zero. The attempt counted as measured with cached_tokens: 0, in a succeeded arm and in an arm whose scores are withheld, while the same counter on an unscored attempt left it unmeasured. The loader now names the counters a trajectory recorded with an invalid value, never the values. A report trial lists them in a new optional invalid_token_counters field next to its unchanged tokens, usage-only attempts carry the same field, and token usage leaves such an attempt unmeasured with the invalid_token_counters reason. Only the trajectory names these counters: the loader drops the internal key that carries them from each reward.json it reads, and both fields keep only prompt_tokens, completion_tokens and cached_tokens, in that order. The resources totals, which read only prompt and completion, are unchanged.
  • tier3 evaluate printed no token usage for a run whose arms all failed. The token table was printed only with the score tables, which a run without a usable arm never shows, although the report step recorded the run's token sums. It is now printed after the panel that says the evaluation did not complete, with or without --verbose.
  • A case that a run budget cut short read as partial. A case's expected attempts were pass@k's k minus the attempts skipped after a pass. Under --max-attempts-total, --max-agent-tokens or --max-cost-usd the collector plans fewer than k attempts for a case and counts the refused ones as neither skipped nor missing, so the case row was partial with missing_attempts and no change while its agent row was complete. A case now expects its pass@k record's scored, recovered and missing attempts, which leave out both skipped and refused attempts, and never fewer than it observed. A result file without those counters still gives k minus the skipped attempts.
  • docs/reports.mdx describes the recorded case with either backend, invalid_token_counters, the planned attempts a case expects and both terminal behaviors. CHANGELOG.md has one Fixed entry under Unreleased.

Known trade-offs

  • Report trials in the JSON payload gain an optional invalid_token_counters list, and an unscored trial's failure.json gains an optional entry_id. Both are additive; readers that ignore unknown fields are unaffected.
  • An unscored attempt with an invalid counter now has the reason code invalid_token_counters instead of missing_token_counters, the same code a scored attempt gets. The existing test for it asserts the new code.
  • Runs collected before this change have no entry_id in failure.json, so their unscored native task attempts keep the task directory fallback until they are collected again.
  • Without a selector mapping, as in a NeMo Gym run, the collector records the case only when the arm's staged task files or the task path resolve it. Otherwise nothing is recorded and the loader keeps its fallback, as before.

Tests

  • tests/tier3/test_token_usage.py and tests/reporting/test_tier3_token_usage_reports.py: 126 passed on this branch. With the same test files on main c19b5d7, 27 fail and 99 pass:
    • Terminal pointer: test_terminals_say_no_report_has_the_case_rows_the_payload_left_out (1), with the payload that the report step fitted for tier3 evaluate and the one validate fits.
    • Native task case: test_the_collector_records_the_case_of_an_unscored_native_task_attempt (2: from result.json and from config.json) and test_an_unscored_attempt_takes_the_case_its_failure_record_names (1).
    • Invalid cache counter: test_a_scored_attempt_whose_arm_failed_with_invalid_cached_tokens_is_not_measured (5), test_a_scored_attempt_with_invalid_cached_tokens_is_not_measured_and_resources_keep_it (5), test_a_counter_the_trajectory_recorded_invalid_is_rejected_although_tokens_leave_it_out (1), test_report_trials_and_usage_only_attempts_name_only_token_counters_in_one_order (1), the 3 invalid-cache cases of test_only_the_trajectory_names_the_invalid_token_counters_of_a_scored_attempt, and the 5 existing cases of test_unscored_attempt_with_invalid_cached_tokens_is_not_measured, which now also assert the invalid_token_counters reason (main reports missing_token_counters). The 3 valid-cache cases of the trajectory-only test pass on main and guard that a reward.json holding the internal key keeps a valid attempt measured and loads.
    • Failed run: test_tier3_evaluate_terminal_prints_the_token_usage_of_a_run_whose_arms_all_failed (1), with the failed engine result that render_agent_eval_html_report() records into.
    • Run budget: test_a_case_that_a_run_budget_cut_short_expects_only_the_attempts_it_admitted (1). On main the case row is partial with missing_attempts. The new collector case of test_an_attempt_that_never_ran_leaves_the_arm_partial passes on both and guards that an attempt that never ran is still missing.
    • test_docs_and_changelog_describe_the_token_usage_contract (1) finds the entry in the Fixed subsection of any release section and checks that no line of the token usage section in docs/reports.mdx is wider than 80 characters. Against a copy of CHANGELOG.md with ## Unreleased renamed to a numbered release under a new Unreleased section, a check of only the first section raised IndexError (new section without Fixed) or ValueError (new Fixed subsection without the entry); this check passes in both.
  • tests/test_nemo_gym_backend.py::test_an_unscored_gym_attempt_of_a_native_task_counts_its_tokens_in_its_case fails on main c19b5d7 (KeyError: 'entry_id': the failed trials' failure.json has no case) and passes on this branch. It writes NeMo Gym rollouts with write_job_from_rollouts for a native task staged with its own entry_id, one scored and one failed attempt per arm, and collects them without a selector mapping. On this branch each arm has one auth-login row of 15,200 tokens; main splits each arm into an auth-login row of 1,100 tokens and a login-task row of 14,100.
  • The test modules that import a changed source module plus tests/test_ci_workflows.py, tests/test_oss_packaging.py, tests/test_ci_change_classifier.py and tests/tier3/test_verifier_python39_syntax.py (100 modules): 6836 passed, 9 skipped, 99 failed. 89 of them fail the same way on main c19b5d7 in this environment, whose installed Harbor package predates the pinned version: most are Bedrock preflight probe tests and tests that need newer Harbor APIs, plus the Harbor keyword argument contract check and three NeMo Gym CLI probe timeout tests. The other 10 are Bedrock preflight probe, Harbor bounded-process timeout and CI helper tests that failed under load; rerun alone on this branch they pass. None is in the token usage test modules.
  • ruff check and ruff format --check pass on the changed Python files. The OSS boundary scan passes.
  • git merge-tree --write-tree of this branch and main c19b5d7 is clean, and main is an ancestor of the branch.

🤖 Generated with Claude Code

rng1995 and others added 15 commits November 12, 2026 14:52
A case's expected attempts came from pass@k's k minus the attempts
skipped after a pass. Under a run budget the collector plans fewer than
k attempts for a case and records the refused ones as neither skipped
nor missing, so the case row read as partial with missing attempts
while its agent row was complete.

Take a case's expected attempts from its pass@k counters instead:
scored plus recovered plus missing attempts, which leave out attempts
skipped after a pass and attempts the budget refused. A record without
those counters still gives k minus the skipped attempts.

Signed-off-by: Narendran Raghavan <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Narendran Raghavan <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…token usage

The loader reads a trajectory's counters into each reward's token
summary, where an invalid value becomes absent, the same as the cache
counter Harbor leaves out when it is zero. Token usage then read an
invalid cache counter of a scored attempt as 0 and counted the attempt
as measured, both in a succeeded arm and in an arm whose scores are
withheld. Only attempts without a reward were left unmeasured.

The loader now names the counters a trajectory recorded with an invalid
value, never their values. A report trial lists them in
invalid_token_counters next to its unchanged tokens, and usage-only
attempts carry the same field. Token usage counts such an attempt as
unmeasured with the invalid_token_counters reason. The resources totals,
which read only prompt and completion, are unchanged.

Signed-off-by: Narendran Raghavan <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
An attempt without a scored reward took its token usage case from the
task directory its Harbor result names. A native task may set its own
[metadata] entry_id, so its directory is not its case ID, and its
unscored attempts landed in a separate case row. The arms then looked
like they ran different cases, and a case row left out the attempt's
tokens.

The collector maps each Harbor task to its case with the run's trusted
selector mapping. It now records that case in the unscored attempt's
failure.json as entry_id, and the report loader prefers it. Runs
without the record keep the directory and trial name fallback.

Signed-off-by: Narendran Raghavan <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…opped them

When the report payload has to make room, it drops the token usage
case rows of every agent, and every report reads that payload. The
validate and tier3 evaluate terminal tables still pointed readers at
the reports for the case rows and then said the rows were left out
of "this report", which contradicts itself.

Once the payload dropped the case rows, the terminal tables now say
that they were left out of every report and that the agent sums cover
every attempt, instead of naming the report files.

Signed-off-by: Narendran Raghavan <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
tier3 evaluate printed the token usage table only with the score
tables, which a run without a usable arm never shows. A run whose
every attempt hit the agent timeout used tokens like any other, and
the report step recorded its sums, but the terminal showed only the
panel that says the evaluation did not complete.

Print the agent token sums for such a run too, after that panel and
under the same rule as the score tables: always when there is no case
summary to show.

Signed-off-by: Narendran Raghavan <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…nfig

The collector read an unscored trial's case only from its Harbor
result.json. A trial that stopped before Harbor wrote its result still
has its config.json, which names the same task, so the collector now
falls back to it. Without the record, a native task attempt whose
directory differs from its entry_id landed in a case row of its own.

Signed-off-by: Narendran Raghavan <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Add a Fixed entry for the token usage fixes that follow up #427, say in
the reports reference that a case expects only the attempts its pass@k
record planned, and reflow the invalid counter paragraph.

Signed-off-by: Narendran Raghavan <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
The Fixed changelog entry said `tier3 evaluate --verbose` prints the
agent token sums for a run whose arms all failed. A run without a usable
arm never shows a case summary, so the table prints with or without
--verbose, as docs/reports.mdx already says. The entry now names plain
`tier3 evaluate`, and its lead sentence says each attempt is counted in
the case it belongs to rather than "in its own case", which read like
the separate row the fix removes.

Rewrap the token usage bullet in docs/reports.mdx that had one line of
96 characters. The docs and changelog contract test now checks the
entry's wording and that no line of the token usage section is wider
than 80 characters.

Signed-off-by: Narendran Raghavan <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
The loader marks the token counters that a scored attempt's trajectory
recorded with an invalid value under an internal reward key. It never
cleared that key from the reward.json it loaded, so a results directory
whose reward.json held it kept the file's list whenever the trajectory
was valid. A list that mixed types made loading fail on sorting it, and
a list of names marked a valid attempt unmeasured or put any name the
file held into the report trial's invalid_token_counters.

The loader now drops the key from each loaded reward before it reads the
trajectory. Report trials and usage-only attempts keep only names of
token counters, in prompt, completion and cached order, so both list
the same counters the same way.

Signed-off-by: Narendran Raghavan <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Bring the branch up to date with main after the v0.9.0 release. Move the
token usage changelog entry from the released 0.9.0 Fixed section, where
the merge placed it, to the Fixed section under Unreleased. The docs and
changelog contract test keeps main's search for the token usage Added
entry in any release and checks the Fixed entry under Unreleased.

Signed-off-by: Narendran Raghavan <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
… map

The collector recorded the case of an unscored attempt in failure.json
only when the run passed the trusted task selector map. The NeMo Gym
backend collects without that map, so an unscored attempt of a native
task whose [metadata] entry_id differs from its directory still took
the task directory as its case: token usage split the case into two
rows and left the attempt's tokens out of the real case row.

Without the selector map, the collector now resolves the case of an
unscored trial from its result.json or config.json with the arm's
suffix and staged task entry IDs, the same way it resolves a scored
trial, and records it only when that case is authoritative. The
reader keeps its task directory fallback otherwise.

Signed-off-by: Narendran Raghavan <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
The CHANGELOG check of the token usage contract read the Fixed entry
only from the first release section. Once Unreleased becomes a numbered
release, a new Unreleased section without a Fixed subsection raises
IndexError, and one with a Fixed subsection that lacks this entry fails
the single-entry unpack. Search the Fixed subsection of every release
section instead, as the Added check in the same test already does, and
keep the content assertions on the entry it finds.

Signed-off-by: Narendran Raghavan <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Bring the branch up to date with main, which now validates token usage
case rows against the evaluation dataset's case IDs.

Token usage keeps main's dataset matching for every attempt and reads each
attempt's tokens with its invalid_token_counters. A case's expected
attempts come from main's merged pass@k record per case and still count
its scored, recovered and missing attempts. The terminal pointer says no
report has the case rows once the payload dropped them, and keeps main's
note about case IDs the dataset does not name. The changelog takes main's
file and adds the token usage entry to the Fixed section under Unreleased.
The docs and changelog test keeps both the Fixed entry check and main's
new tests.

Signed-off-by: Narendran Raghavan <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Drop the blank line at the end of the token usage test module so that
ruff format --check passes on it.

Signed-off-by: Narendran Raghavan <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>

@rng1995 rng1995 left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reviewed the per-case attribution of scored, unscored, config-only and native-task attempts (with and without a selector map), the _case_expected counts under run budgets, stop-on-pass and recovered timeouts, the handling of invalid counters in the trials, the usage-only rows and the terminal notes, the totals the terminal prints for a run whose arms all failed, and whether the docs and CHANGELOG match the code. Apart from one minor terminal pointer issue, explained inline, they match. The token usage test modules (126) and 9 related collector, NeMo Gym, display and budget modules (709 passed, 5 skipped) pass on this head.

The tier3 evaluate token table always said the case rows are in report.html
and tier3-benchmark.md. Only the Harbor runner writes the benchmark; a NeMo
Gym run writes report.html alone, and a run whose report step failed writes
neither. The pointer now follows the paths the runner recorded:
benchmark_path names both files, report_path alone names report.html, and
without either no file is named.

Signed-off-by: Narendran Raghavan <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
@rng1995
rng1995 merged commit 33b382f into main Nov 14, 2026
27 checks passed
@rng1995
rng1995 deleted the naren/fix-tier3-token-usage-followups branch November 14, 2026 17:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant