Model-Call Capture
Model-call capture records requests, responses, token usage, tool calls, reasoning, latency, and errors at a Gym model server. Capture does not modify model responses, agent responses, or rollout rewards.
gym eval submit enables capture by default under each benchmark’s job directory at model-calls/.
Set driver.benchmarks.<name>.run.observability_enabled: false to opt out, or set
driver.benchmarks.<name>.run.model_call_capture_dir to an absolute path to customize the directory.
Other entry points, including local evaluations and training, remain opt-in.
Run an evaluation with capture
This example extends the quickstart MCQA evaluation. It assumes that your
model credentials are already configured in env.yaml.
In the first terminal, choose an absolute capture directory and start the servers with capture enabled:
In a second terminal, activate the same environment and pass the same capture settings to rollout collection:
Inspect the attachment on the first rollout and list the raw capture files:
The rollout record is the usual starting point for analysis. Use its ng_model_call_capture field
for normalized calls and aggregate metrics; use the matching file in CaptureStore when you need the
original request and response payloads.
What capture contains
Buffered SSE with external token staging
With token_id_capture.external_staging: true and parent_resolution: fingerprint, captured requests can use stream: true
on Chat Completions, Responses, and Anthropic Messages.
Gym requests a complete, non-streaming answer from the inference worker. The worker stages
training tokens before returning commit coordinates. Gym removes internal token fields,
converts the answer to the client-visible API format, and serializes the SSE events before
recording a successful capture and replaying the answer. Capture fingerprints preserve
namespaced tool identity across the backend and client representations. Conversion or
serialization failures leave no successful capture record, and capture failures are recorded
before a terminal response.failed event is sent, including when observability is disabled.
No content is delivered during generation, so buffered SSE does not reduce time to first content.
Worker streaming with external capture remains unsupported. Training reconstruction reads the external staged tokens, not the SSE text.
The staging ledger’s wire models (nemo_gym/models/token_capture/staging/records.py) forbid unknown
fields, so the model server that stages a call and the framework that rebuilds the rollout from
the staged rows must run the same Gym version. A framework whose Gym predates a column the model
server writes, such as source_capture_key or finish_reason, rejects every manifest row as
invalid_manifest_row and masks the rollout.
Source-history continuity and context compaction
A compatible training framework can own exact-token prefix reuse and training-row reconstruction.
Set token_id_capture.parent_resolution: source_history to match source history and effective render
options. Gym sends a root when they do not match or no unique parent can be identified. The default
fingerprint mode continues to match assistant content against captured calls.
For a candidate parent, the inference worker verifies the converted-message boundary and any
required null/empty tool-content equivalence before restoring exact tokens. The harness sends its chosen
message history, including any compaction. It does not send tokens, segment IDs, or a parent header.
Source-history candidates carry an explicit source-match proof. Deploy compatible Gym and RL worker revisions together: a missing proof is an error, not permission to assume an exact match. Direct roots retain replay evidence and framework safety checks so later calls can continue them.
token_id_capture.allow_system_suffix defaults to false and requires source_history with
external_staging_backend: vllm_worker. Dynamo staging supports source-history capture with this
setting off; system-suffix admission is not enabled for that backend. Enable it only with a worker that
checks that the full tokenized prompt starts with the separately rendered historical prefix
before installing captured tokens. This permits appended system messages such as CLI budget
updates. If the template moves the update ahead of the historical prefix, the worker must
capture a new root (worker_rooted). Workers that only check message boundaries must leave
this setting disabled.
Staging admissions, snapshots, commit coordinates, manifests and receipts identify the current capture
attempt with capture_key. A carried-over CallRecord.source_capture_key identifies the earlier
attempt that staged that call; when absent, the receipt’s own capture key applies. Logical rollout IDs,
staging-key strings and binary digest encoding are unchanged by these field names.
The staging wire schema is version 3. Gym and every external-staging worker must upgrade together, including deployments using ordinary capture with fingerprint parent resolution. Version 2 admissions, staged rows, receipts and checkpoints are incompatible and require a separate migration; this change does not provide one. Finish or retire existing attempts before upgrading, and do not resume their persisted data with the new schema.
Source-history capture rows include output fingerprints, a bounded completion status and minimal original
text for output validation. Ordinary capture keeps its existing fingerprints and finish reason without
these additional selection/validation fields. The status is completed, incomplete, failed or
unknown; cancellation and abort mean failed, while missing or unrecognized completion information
means unknown. NeMo RL masks a rollout containing a selected failed or unknown completion.
Source-history roots record root_reason: first_call (no committed history), no_prefix_match,
non_user_suffix (an otherwise matching prefix followed by a non-observation item),
render_options_changed, worker_rooted (the worker declined a source-compatible candidate),
ambiguous_candidate (several equally long prefixes match without unique identity),
or model_server_changed (a continuation whose only match was served by another model server for the
same agent instance).
These are diagnostic observations, not proof that the harness performed compaction. With fingerprint
parent resolution, a root records model_server_changed when that is why it is a root; other ordinary
roots and continuations leave the field unset. An ambiguous source match starts a new root with no parent
or staging chain; it does not choose which earlier generation the harness accepted. The trainer must
still validate training selection independently and reject ambiguous returned-history attribution.
The framework must configure the worker, shared capture ledger and control bearer token as well as these run-wide settings:
This requires a framework worker implementing Gym’s capture protocol; stock vLLM alone is insufficient.
For NeMo RL’s engines behind a Dynamo frontend, set external_staging_backend: dynamo_worker instead; see
Token capture through Dynamo.
Keep the usual per-agent capture opt-in or all_agents: true. rebuild_response: false preserves
ordinary rollout output for framework reconstruction instead of Gym’s single-chain rebuilding.
Prompt truncation and assistant prefill are unsupported in this mode. Incompatible server settings
are rejected at startup; unsupported options in request metadata.extra_body return HTTP 422 before
capture intent is recorded, so a corrected request can reuse the attempt.
The policy checkpoint gate supports source-history capture. It publishes the staged call’s ledger record immediately before forwarding the first answer chunk. Restore carries captured history into the new attempt without inheriting the previous attempt’s closed-admission state.
Independent calls may overlap. Each registers an intent before inference. Failed or pending capture
does not block later calls; the manifest retains those outcomes for the trainer. After the harness returns, the
framework calls the bearer-protected POST /training-token-capture/control/rollouts/{capture_key}/close-admission
then drains already-admitted calls through the manifest endpoint. Closure refuses new generation but
allows admitted calls to finish. Missing acknowledgements remain pending; failure is not permission
to delete data that a worker may still be writing. NeMo RL masks an incomplete attempt.
Closed admission returns HTTP 409 with a reason before generation.
The same refusal applies to a ledger missing source replay evidence, or a selected storage chain
with a cycle or missing ancestor. The existing ledger is preserved for diagnosis.
Use a fresh attempt ID when restarting a rollout.
Some SDKs automatically retry HTTP 409. External harness clients should disable those automatic
retries for generation requests; a retry cannot reopen closed admission or repair an unusable ledger.
Framework-owned capture forwards recognized context-length refusals as HTTP 400, even when
propagate_context_overflow_errors is false. The harness decides whether to compact and retry.
To keep that retry usable, the worker must acknowledge the refused call before generation or
staging using the internal x-ng-capture-refused response header. A ledger implementing
refuse_call settles that intent and retains its audit row, excluding it from the training
manifest. This header is between the worker and Gym; harnesses neither send nor receive it.
An error without a matching acknowledgement still leaves capture incomplete.
A Dynamo frontend forwards only an error’s status and message, so there the worker’s acknowledgement ends the
error message, and the model server moves it to this header.
The model server also exposes POST /ng-rollout/{rollout_id}/training-token-capture/context/measure.
This is a rollout-scoped model route, not a bearer-protected control endpoint.
Call it with a non-streaming Responses request containing
explicit history. It returns {"prompt_token_count": N} using the same candidate and rendering
path and rollout-based backend routing as generation, without registering an intent, writing capture
records, or creating ledger files for an unused rollout. This is a count of
that request, not a reservation against concurrent changes. Idempotent worker tokenization retains
ordinary retries. Captured generation retries only failed connection setup, within the configured
connection limit (three attempts by default); it never resends after uncertain delivery or an HTTP
error response. Permanent authentication and quota errors still stop further calls to that endpoint.
Framework callers should use run_examples(retry_requests=False) to avoid automatically replaying
an entire /run after transport failure. This does not prevent a harness from explicitly making a
new call after a definite, successfully captured response.
Rollout-record attachment
rollout_collection attaches available model-call data to each rollout. This representative example
includes a gap to show the complete shape. The gaps field is omitted when there are no gaps.
metrics.num_calls is the number of captured calls. Token and latency totals include only values
reported by the provider; a null value means unknown, not zero. For example, Anthropic does not
report reasoning-token usage, so tokens_reasoning is null for that dialect.
Each call’s tokens_in is the whole prompt size. When the provider reports prompt caching, the call
also carries cached_tokens (tokens read from the cache) and cache_creation_tokens (tokens written
to the cache); both are part of tokens_in. Gym reads cache writes from OpenAI’s
input_tokens_details.cache_write_tokens (Responses) or prompt_tokens_details.cache_write_tokens
(Chat Completions), and from Anthropic’s cache_creation_input_tokens.
After successful ng_trajectory projection, attached calls
omit request and response payloads; those remain in CaptureStore. If projection fails, payloads
remain attached and gaps includes trajectory_projection_failed. LabBench rows with the
multimodal_history_redacted gap omit payload copies from ng_trajectory.
A capture write started by a cancelled call runs detached, so a rollout’s outstanding writes are
waited for before its record is snapshotted. If one has not landed by the drain timeout, gaps
includes model_call_capture_pending. The model server claims each call in
<rollout_id>.capture.claims before forwarding it and releases the claim once its record is
written. The snapshot reads the capture while holding the claims file’s lock, so no call claims or
releases during the read, and deletes the claims file only when no claim in it is outstanding. If a
call was still in flight at the snapshot or arrived after it, the claims file stays next to its
capture file until the same rollout ID is dispatched again. A run that deletes merged captures
deletes each one only after its record is written or returned, and keeps a capture file that gained
a row after the snapshot. A claims file whose lock stays held past a short wait counts as having a
claim outstanding.
Claims rely on one file lock that every writer of the claims file shares. With node-local locks,
such as a Lustre mount with localflock, a model server on another host that shares the capture
directory can lose claims. Gym’s own model server runs on the collector’s node, so the default
layout is safe. When the model server and the collector share a node, a node-local
model_call_capture_dir avoids the shared filesystem altogether.
The attachment is additive: it does not replace or rewrite the existing response, reward,
NeMoGymResponse, token-id, or log-prob fields. Aggregate-metrics requests exclude it, and W&B
rollout tables omit ng_trajectory and model-call request and response payloads.
Raw CaptureStore record
Each rollout has an append-only file named <rollout_id>.capture.jsonl. Each line contains one raw
model-server exchange, including its request and response.
The following synthetic record shows the exact persisted field shape. It is formatted across lines
for readability but stored as one JSONL line.
A call whose URL names an agent instance also carries agent_instance and agent_kind. The raw record and
the training-token records (ledger rows, manifest records and failures, receipts) leave both fields out for
an unnamed agent, so a single-agent record keeps its earlier shape. ModelCallRecord always has both and
sets them to null. Read an absent field and null as the same thing: the agent is unnamed.
A call that a Gym server made while handling a rollout, such as a judge call from a resources server’s
/verify, also carries caller_server: the top-level config name of the server that made it, read from its
NEMO_GYM_CONFIG_PATH. The field is
absent when the caller is not a Gym server. It is in the raw record only, not in ModelCallRecord.
Writes are flushed and fsynced. Capture failures are logged without failing model requests. Resume
attempts use an -a<n> suffix so their data does not mix with an earlier attempt. Before dispatch,
the collector clears any existing capture for the exact rollout-attempt id.
Read raw records programmatically when you need more than the rollout attachment:
ModelCallRecord includes the server-generated model_call_id, protocol response_id, typed
model_ref, agent_instance and agent_kind, UTC started_at and completed_at timestamps, durable call_index, API dialect, token
and cache usage, status, finish reason, latency, error details, tool calls, reasoning, and captured
payloads. call_index and timestamps do not establish causal ordering between concurrent calls.
Streaming Responses, Chat Completions, and Anthropic Messages are reassembled for capture on a
best-effort basis. If reassembly fails, request, status, correlation, and latency data remain
available. A successful-status stream that closes without its dialect’s terminal event retains any
partial reconstruction and is marked stream_truncated.
Configure capture
The CLI workflow above sets these top-level fields in Gym’s global config:
You can put the same settings in a YAML config instead:
Record only some calls
A long run that records every call can fill its capture directory. Three more top-level fields choose which calls the capture store records. Their defaults record every call.
Use these settings for training runs that need a subset of calls, such as judge calls. Server selection and sampling omit calls without marking a rollout incomplete. When the free-space floor skips a selected call, Gym attempts to mark that rollout’s capture incomplete.
Keep full capture for evaluation runs. Rollout-record attachment, rollout health checks, and the agent-timeout retry decision on resume rely on the recorded calls. If a skipped call failed on the model server, resume can keep the timed-out result instead of retrying the rollout.
Trajectory schema selection
The default trajectory schema is "1.0". To retain qualified v2 tool timing, add:
observability_enabled and model_call_capture_dir remain top-level settings as shown above.
Direct callers can use run_examples(..., trajectory_schema_version="2.0"); an explicit argument
wins over the configured version. Schema selection also applies with enrich_results=False:
the default projects raw results to v1, while selecting v2 preserves producer evidence without
upgrading legacy evidence or reading captures. Direct HTTP /run replies remain raw producer
evidence; schema negotiation happens at collection or export.
Model servers and rollout_collection must resolve model_call_capture_dir to the same location.
For separate pods or nodes, mount that absolute path in every producer and the collector. Multiple
model servers can append to one rollout file when the shared filesystem supports POSIX advisory file
locking.
Rollout ids contain task, rollout, and retry-attempt indices, but not an evaluation-run id. Use a separate capture directory for concurrent collection jobs whose indices can overlap.
If you build a custom agent
Capture requires a caller-supplied rollout correlation id. Gym’s built-in compatible agents handle this routing; custom agent authors must preserve it on model calls.
Use /ng-rollout/<rollout_id> as the model-server URL prefix. SDKs append /v1/responses,
/v1/chat/completions, or /v1/messages, and the model server removes the rollout prefix before
routing. An unprefixed call is forwarded normally but is not captured. Calls sent directly to an
external provider instead of through a Gym model server are also not captured.
Agents based on SimpleResponsesAPIAgent can add the prefix with these helpers:
The prefixed self-call route (/ng-rollout/{rollout_id}/v1/responses) is registered on every agent by
default.
Clients with a fixed base URL
A long-lived client, such as one a resources server calls during verification, may not be able to change its
base URL for each rollout. nemo_gym.server_runtime.with_rollout_prefix rewrites each request URL instead: it
inserts /ng-rollout/<rollout> after the matching base URL, using the current rollout, so the call reaches the
model server exactly like a prefixed one, and every Gym server layer that reads the prefix (capture, policy
checkpointing, session affinity) sees it.
- Only URLs under one of the base URLs you list are changed. They are compared by scheme, host (case-insensitive), effective port and whole path segments, the way HTTP clients normalize them.
- A URL that already carries a rollout prefix below the base is left as it is.
- With no current rollout the URL is unchanged, a debug message is logged, and the model server forwards the call without capture.
- The current rollout is a
ContextVar. A synchronous client run throughasyncio.to_threadorcontextvars.copy_context().runsees it; one run throughloop.run_in_executoror a thread pool does not.
rollout_prefix_hook and async_rollout_prefix_hook wrap it as httpx request event hooks. The OpenAI SDK
accepts such a client through http_client.
With observability or checkpointing on, Gym sets the current rollout for every request it sends to a
resources server for that rollout, including /verify. Clients created once and shared across concurrent
rollouts therefore name each request’s own rollout. The rollout is the capture key, including an -a<N>
attempt suffix when the run retries.
Add agent-side evidence
Model-call capture is one input to Gym’s broader rollout-evidence pipeline. Supported harnesses can
also emit ng_agent_observations; rollout_collection exact-joins the available sources into
ng_trajectory and derives ng_perf when sufficient evidence exists.
See Rollout evidence for the full pipeline, exact correlation rules, payload retention, agent observations, gaps, and performance summaries. Agent-side coverage varies by producer and execution path; the harness conformance results show measured harness checks.
Framework-owned media staging
Training frameworks using nemo_gym.models.token_capture.staging can hand a framework’s
processed media (for example an inference worker’s pixel tensors) to
RolloutTokenCapture.complete_call or complete_call_from_response through the
keyword-only attachments mapping. Gym forwards the mapping unchanged to
StagingSink.stage(record, attachments=...) so the media lands in the same durable
write as the token record, and staged coordinates are returned only after that
combined write succeeds. Attachments are opaque to Gym: they never enter
StagedCallRecord, extras, or any digest, so digest, extras_digest, and
CommitCoords are identical with or without them.
The keyword is passed only when the caller supplies a mapping. A legacy stage(record)
sink therefore keeps working for text-only calls; if it is given attachments it raises
TypeError, which the capture boundary reports as capture_failed. Gym never retries a
write with the attachments dropped. A sink that accepts the keyword must store every
supplied attachment before returning success or reject the call. Validation of the
attached tensors, their storage layout, and read-back belong to the framework.
VLLMCaptureAdapter preserves optional routed_experts extras and passes through
media_spans, small metadata that the RL vLLM worker uses to remap image/video
placeholders when reusing a captured prefix. vLLM uses expanded token IDs and does not
emit a separate compact token delta.
This replaces both the experimental tensor-attachment sink extension and the
digest-covered media summary. Deploy the paired RL changes with this adapter; rows and
checkpoints written in either older format need a separate migration. Supported
processors and modalities are enforced by the framework’s inference worker.
Retire capture ledgers
With external staging, the model server keeps a capture ledger for each rollout: one <rollout_id>.lineage.jsonl file in the lineage store’s directory, recording the model calls the rollout made without their token arrays.
The training framework reads it once, through the manifest route, when the rollout ends.
Gym can’t tell when the framework is done with a rollout, so the framework removes each ledger itself, using the same two operations the complete-record token store has: retire and delete.
Retire a rollout the framework is done with
Retiring deletes the rollout’s ledger and leaves an empty <rollout_id>.lineage.retired file in its place.
That file acts as a fence: if a model call for the rollout finishes later, Gym discards its row instead of starting a new ledger.
Late calls are more common than they sound, because an agent the framework gave up on can keep running, and a client retry can run a finished rollout a second time.
TokenSource.drop leaves the same kind of fence in the complete-record token store.
After a rollout is retired, its manifest route returns HTTP 410 instead of an empty manifest, so a framework that reads it too late finds out rather than building an empty receipt.
A call that starts after its rollout was retired is not admitted for capture, so it stages nothing. A call that was already in flight, however, may have staged its tokens before Gym discarded its row, and no manifest names those tokens. Gym logs a warning with the call’s staging key, and the framework that staged the tokens has to remove them, for example by sweeping staged rows that no receipt references.
Retire every rollout the framework is done with, whether it finished or was abandoned. A finished rollout can be retired once its manifest has been read and its receipt built, unless a crash before the receipt is saved would make the framework rerun it under the same ID; in that case, wait until the receipt is saved. An abandoned rollout should be retired as soon as the framework gives up on it.
Delete fences once nothing can still write
Deleting removes a rollout’s ledger together with its fence, which is only safe once no call from that rollout can still arrive.
In practice that means the end of the run, or a point where every Gym server sharing the lineage directory has restarted.
Until then, the fences accumulate at one empty file per retired rollout.
Also delete a rollout ID before reusing it, so that the new run starts from an empty ledger, as TokenCaptureStore.delete does for the complete-record store.
Send the requests
Send each request to a model server whose lineage directory holds the rollout’s ledger, either the server that served the rollout or any other server configured with the same directory.
A model server only sees the ledgers in its own lineage directory and reports any other rollout as absent.
The body field was earlier named rollout_ids, which the routes still accept for one release.
Each request takes at most 4096 capture keys, and RolloutControlClient.retire and RolloutControlClient.delete split longer lists into batches for you.
The reply lists the rollouts whose ledger was removed under removed and the rest under absent.
Because repeating either operation changes nothing, a failed batch can simply be sent again.
Neither operation touches staged token data. The framework that staged the data removes it, and it must keep any staged data that a checkpoint it may restore still refers to.
Limitations
The model-server boundary observes model HTTP requests and responses. It can record tool calls, reasoning, and tool results present in those payloads, but it does not observe tool execution, environment events, context compaction, semantic turns, or subagent structure. Those require instrumentation at the agent, tool, environment, or rollout layer.