Repository navigation
Description
Use cases, pain points, and background
Two workstreams need the same thing: step-level metrics and traces of the model call inside every step, with each record tagged by its turn and trial, attributable to the model server that produced it, available without changing the agent harness for both training and evaluation.
Terms used here follow the Gym glossary: a trial is a single attempt at completing a task; a turn is one round-trip interaction boundary between the agent and the external world within a trial; a step is the micro-level execution cycle inside the agent's harness (model generates → tool call executes → observation received). The trajectory is the sequence of records produced during a trial. This epic defines what each step contributes to the trajectory for the model-call portion of that step.
- Rollout. Today, blackbox agentic harnesses (OpenHands, SWE-Agent, Claude Code) make inferences through their own SDKs that route around Gym's model-server layer entirely. There is no per-turn trajectory record (only the final response is written), no per-inference token attribution (totals are summed at the trial level, with no breakdown by turn, retry, or model server), inconsistent turn counters across agents, reasoning traces flattened into response text rather than structured fields, no cache hit/miss surfacing in the rollout output, and a generic exception-catching retry layer with limited failure classification.
- Evaluation reporting. Evaluation runs need the same granularity for different reasons: per-trial cost accounting (token totals summed across inferences), reproducibility and audit (full per-inference trajectory ordered by turn), multi-turn benchmark analysis (turns per trial, inferences per turn), consistency checks (cache hit/miss per inference), and reliability stats (normalized retry/error classification per inference). Nemo Evaluator (NEL) already produces this granularity, via its adapter system.
The two use cases collapse to one technical need: a single metrics-and-traces contract that defines what each step contributes to the trajectory for the model-call portion, regardless of whether the caller is a rollout loop or an evaluation run. NEL already produces this granularity. This epic is framed around the granularity of output Gym needs
Ref: #1386
Description
Define and deliver a step-level metrics-and-traces contract for the model-call portion of every step: the metrics (token totals, cache hit/miss, retry/error classification) and traces (request/response payload, structured reasoning content) captured per step, tagged with trial, turn, and step indices so records aggregate cleanly, for every model server Gym supports, opt-in per server and off by default. The same contract serves both rollout and evaluation reporting.
Design
The contract is what every step contributes to the trajectory, regardless of caller or agent harness:
- Token accounting. Prompt, completion, reasoning, and total tokens (e.g. tokens_in, tokens_out, tokens_reasoning) for the model call in each step. Aggregable to per-turn and per-trial totals.
- Model-call record. Full request (model prompt + tools) and response payload of the model call in each step, including any structured tool_calls (name, args, return), ordered into the per-trial trajectory by turn and step index, sufficient to replay or audit a trial.
- Trial, turn, and step indices. Each step tagged with its trial, its turn within the trial, and its step_index within the turn. Enables turn counts per trial and step counts per turn.
- Reasoning trace, structured. Where the model emits separate reasoning content, captured as a structured field on the step record not flattened into response text.
- Cache visibility. Hit/miss signal per step when caching is active on the model call.
- Retry/error classification. Normalized
error_categoryand retry stats per step, distinguishing client-side from upstream — replacing generic exception catching. - Latency. Step-level timing for the model call (e.g.
latency_total_ms,latency_ttft_ms).
Properties of the layer that produces the contract:
- Lives inside each model server. No standalone proxy process to deploy or operate separately from the model.
- Opt-in per model server, off by default. A server that doesn't opt in behaves exactly as it does today.
- Harness-agnostic. Works for any agent harness Gym integrates with; the harness is not modified.
- Distinct from
nemo_gym/sandbox/integrations/policy_proxy.py(GRPO logprob trace recorder).
| Scope | Fields | Use |
|---|---|---|
Run identity (_aggregate_metrics.json) |
run_id (unique per invocation), config_hash (sha256 over model / temperature / max_tokens / top_p / seed / system_prompt / benchmark / benchmark_version / num_repeats / max_problems / shuffle_seed), sdk_version, timestamp, gym_commit, benchmark_version (from BenchmarkConfig.version) |
Cross-run compare; cache key; model-registry sync; reproducibility; cross-version benchmark comparison |
Run shape (_aggregate_metrics.json) |
n_results, num_repeats, num_tasks, num_samples_in_parallel, total_walltime_s |
Sample-size sanity; cost reasoning; parallelism audit |
Run aggregate (_aggregate_metrics.json, already in Gym) |
key_metrics.* (headline), agent_metrics.* (full), group_level_metrics (per-task) + new: CIs (bootstrap, clustered), per-category breakdown (scoring_details_breakdown) |
Headline score; per-benchmark CI; gate input |
| Per-rollout record (JSONL; existing) | task_index, rollout_index, prompt, response, reward, scoring_details (per-benchmark verifier output: error_category, judge votes, etc.) + new: tokens_in, tokens_out, tokens_reasoning, latency_total_ms, latency_ttf_ms, num_turns |
Per-task pass/fail; flip analysis (Feature #7); token/cost accounting |
| Per-step trajectory (agentic; JSONL or sidecar) | step_index, request (model prompt + tools), response, tool_calls (name, args, return), sandbox_state (cwd, env, file changes if requested), snapshot_ref (if snapshot() taken), latency_ms |
Trajectory-aware scoring (PinchBench); workspace inspection; turn-level debugging |
Run config (_aggregate_metrics.json) |
Resolved config_paths tree (after _inherit_from merge) |
Reproducibility; cache lookup; debugging "what actually ran" |
Out of scope
- A 1:1 port of NEL's interceptor catalog. This epic is the granularity contract; hooks beyond the contract can follow as separate work.
- A standalone proxy process for Gym.
- GRPO logprob token-trace recording — owned by policy_proxy.py, different concept.
- Changes to the agent harness side — harnesses stay blackbox. The tool-execution and observation portions of each step are captured by the harness, not by this contract.
- Performance optimization beyond what's needed to meet the contract.
Acceptance Criteria
- Equivalence demonstrated against NEL's adapter output (
ArtifactCollector/StepRecord/ModelResponsepluslog_tokens/response_stats/turn_counter/request_logging interceptors) across a representative corpus of real model calls; required for internal evaluation reporting. - Granularity contract published, naming every field captured per step and every per-turn / per-trial aggregate derivable from it. Field names align with existing OpenAI shapes where applicable.
- Token totals (
tokens_in/tokens_out/tokens_reasoning/total) produced for the model call in every step; per-turn and per-trial sums derivable. - Model-call record (
request,response,tool_calls) produced for every step, ordered by turn and step index into a complete per-trial trajectory. - Trial, turn, and step_index produced on every step record.
- Latency captured per step (
latency_total_ms, andlatency_ttft_mswhere applicable). - Reasoning trace captured as a structured field when the model emits separate reasoning content (not flattened into response text).
- Cache hit/miss signal produced per step when caching is active.
- Normalized retry/error classification (
error_category,retry stats) produced per step, replacing generic exception catching. - At least one model server (e.g.
local_vllm_model_proxy) producing the full contract output end-to-end, opt-in via config, off by default. - Per-step record published as a typed Pydantic schema with JSONSchema available via model_json_schema().
- Contract output verified to satisfy both rollout and evaluation-reporting consumers.
- Contributor docs published: the granularity contract, the trial / turn / step terms as used here, how to opt a model server in, what each field means.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
feat(observability): typed StepRecord contract + off-by-default (#1483)
feat(observability): error_category capture + per-rollout aggregates (#…
ritaneves commentedon Jun 25, 2026
Status: not for 0.4.0 release.
Token level stats design note will be shared by EOW. Later on, there will be a wider design review for adapters/interceptors.
refactor(trajectory): make assembler eval-only; cede RL token-id traj…
8 remaining items
feat(observability): in-model-server per-rollout trajectory capture (#…
feat(observability): in-model-server per-rollout trajectory capture (#…
feat(observability): in-model-server per-rollout trajectory capture (#…
feat(observability): in-model-server per-rollout trajectory capture (#…
feat(observability): in-model-server per-rollout trajectory capture (#…
feat(observability): in-model-server per-rollout trajectory capture (#…
feat(observability): in-model-server per-rollout trajectory capture (#…
feat(observability): in-model-server per-rollout trajectory capture (#…
feat(observability): in-model-server per-rollout trajectory capture (#…
feat(observability): in-model-server per-rollout trajectory capture (#…