> This page is for version Main (default).
> For other versions, use one of these documentation indexes:
> - Main (default): https://docs.nvidia.com/nemo/gym/main/llms.txt
> - 0.7.0: https://docs.nvidia.com/nemo/gym/v0.7.0/llms.txt
> - 0.6.0: https://docs.nvidia.com/nemo/gym/v0.6.0/llms.txt
> - 0.5.1: https://docs.nvidia.com/nemo/gym/v0.5.1/llms.txt
> - 0.5.0: https://docs.nvidia.com/nemo/gym/v0.5.0/llms.txt
> - 0.4.0: https://docs.nvidia.com/nemo/gym/v0.4.0/llms.txt
> - 0.3.0: https://docs.nvidia.com/nemo/gym/v0.3.0/llms.txt
> - 0.2.1: https://docs.nvidia.com/nemo/gym/v0.2.1/llms.txt

> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.nvidia.com/nemo/gym/llms.txt.

# Linear Model-Call Capture

> Why model-call capture grew with the square of an agent session, and the opt-in capture format and row payload placement that keep its disk linear and its memory bounded.

This note explains why [model-call capture](/nemo/gym/main/model-server/model-call-capture) grew with the square of a long agent
session, and the two opt-in settings that keep it linear on disk and bounded in memory:
`model_call_capture_format: delta` and `model_call_payloads: reference`. Defaults are unchanged.

## The problem

An agent re-sends its whole conversation on every model call. Capture stored each request in full, so a rollout's
capture file is about `0.7 × calls × last request size`. Codex also re-sends every earlier tool-output screenshot,
2-4 MB of base64 each, on every call.

A Terminal-Bench Science run (DeepSeek V4.1 Flash with Codex, 70 sessions of 8 hours) wrote 60 GB of captures in its
first hour. One file reached 25.9 GB, with single requests of 90 MB. A text-only session grows by about 80 MB per hour;
a session with screenshots by up to 26 GB per hour.

When a rollout finished, the merge parsed its whole capture into memory, payloads included, even when the row kept
none. The trajectory then copied each call's full input twice, into `ng_trajectory.model_calls[].request` and
`turns[].question`. Rows reached 27 GB, and the Gym head swung between 59 and 112 GB of RSS assembling them until the
host killed it.

## Delta capture format

With `model_call_capture_format: delta`, each line stores what the rollout's earlier calls did not already hold:

* **History.** The request's history list (`input` for Responses, `messages` for Chat and Messages) holds only the
  new items. `base_model_call_id` names an earlier line of the same file, and `shared_prefix` says how many of
  that line's items come first.
* **Inherited fields.** Request fields equal to the base call's, such as `tools` and `instructions`, are listed in
  `inherited_keys` and left `null`.
* **Blobs.** Base64 `data:` URLs of at least 1 KiB are stored once per rollout as
  `blobs/<rollout_id>/<sha256>`, next to the capture files. The line lists the JSON path of each one it replaced
  and leaves `null` there.

All of this sits under one `capture_format` key, which a full-format line never has. No value inside an exchange
can be mistaken for a reference. A line looks like this, shortened:

```json
{"model_call_id": "c7", "dialect": "responses",
 "request": {"model": "m", "instructions": null, "tools": null,
             "input": [{"type": "function_call_output", "call_id": "t7",
                        "output": [{"type": "input_image", "image_url": null}]}]},
 "response": {"...": "..."},
 "capture_format": {"version": 2, "base_model_call_id": "c6", "history_key": "input", "shared_prefix": 41,
                    "inherited_keys": ["instructions", "tools"],
                    "blobs": [[["request", "input", 0, "output", 0, "image_url"], "9f2c...e1"]]}}
```

Every other field of the exchange, such as `model_ref`, `agent_instance`, timings and the response, is stored as in
the full format.

**Choosing the base.** Policy, subagent, judge and user-simulator calls interleave in one file, in completion
order, and several processes append to it. So the base is not "the previous line". Each writer process remembers
the item digests of the last few calls it wrote for each rollout. It picks the one with the longest common prefix,
and under the file lock it reads the base line's head back to check that its `model_call_id` is still at that
offset. A capture that was deleted and recreated for a retry fails this check, and a writer that restarted
remembers no calls. In both cases, and after a context compaction (no common prefix), the line stores the whole
history. A writer never names a line written by another process.

**Crash safety.** Appends keep the existing lock, fsync and claims file. Blobs are written to a temporary file,
fsynced and renamed, and their directory and its two parents are fsynced before every line that names them, even
when every blob it names was already there: a blob left by a write whose directory fsync failed, or a directory
recreated for a retry, is durable only after fsyncs that succeed. If one fails, the line is not appended. A crash
between blob and line leaves an unreferenced blob, which is harmless. A writer killed in the middle of an append
leaves a partial last line; the next append, in either format, ends that line first, so only it is lost and no later
line extends it. A reader checks each blob against its SHA-256 name, so a damaged blob makes its line unreadable
instead of returning other content. Deleting a merged capture that has blobs waits for readers still reading it,
since a delta line read later needs its blobs; a capture without blobs is deleted at once, as before, because an open
file keeps its lines.

**Files on shared storage.** Each new image of a rollout costs one blob file: a create, an fsync and a rename, plus
three directory fsyncs (the blob directory and its parents) per line that names images. An image already written costs one stat. On the `61-0` capture below
that is 128 blob files for 211 calls, in place of 25.9 GB of repeated requests. Deleting a rollout's capture removes
its blob directory file by file.

**Writer memory.** A writer remembers the last 6 calls of up to 1,024 rollouts: 16 bytes per history item of each.
A model-server process that writes more rollouts at once forgets some while they are still active; their later
lines are stored in full, and the writer logs it once.

## Readers

`CaptureStore.read()` and `read_available()` return full exchanges from either format, line by line, so a file may
mix both. `iter_available()` yields one exchange at a time. Its `payloads=False` view drops the request history and
other large request fields, so metadata readers never hold a conversation.

Rebuilt requests of consecutive calls share the item objects of their common prefix, and each blob is read once.
Reading a whole rollout costs about its unique content. Full-format lines get the same sharing: each one's items
are compared with the call it continues and replaced by the earlier objects when equal.

A line whose base or blob cannot be read is reported unreadable, like a damaged line. It is never rebuilt
wrongly, and `capture_index` is still the line's position. Later lines that extend it cannot be rebuilt either, so
one lost blob or damaged base costs the rest of that writer's chain in the file.

## The finished-rollout merge

The merge streams the capture. Unless the row keeps request payloads, it reads the metadata view, keeping only each
call's response for the tool and turn joins. Its memory no longer grows with the conversation. A v2 trajectory
(`trajectory_schema_version: "2.0"`) links each tool call to the response that requested it, so it reads responses
even for a row that keeps no payloads; it no longer reads their requests to do so.

## Where rows keep payloads

`model_call_payloads` chooses what each rollout row copies, for `gym eval run` and `run_examples`:

| Value              | `model_calls[].request`, `turns[].question`                                    | `model_calls[].response`, turn answers |
| ------------------ | ------------------------------------------------------------------------------ | -------------------------------------- |
| `inline` (default) | Copied, as today                                                               | Copied                                 |
| `reference`        | Omitted; `payload_ref` names the capture line                                  | Copied                                 |
| `none`             | Omitted; `payload_ref` names the capture line of each model call, not of turns | Omitted                                |

`payload_ref` is `{capture_file, call_index, model_call_id}`, relative to the run's `model_call_capture_dir`. A
`none` row keeps it on model calls so the health check can read a conversation's opening from the capture;
`include_model_call_payloads=False` keeps call metadata only, with no `payload_ref`.
`nemo_gym.rollouts.resolve_model_call_payloads(trajectory, capture_dirs)` restores the omitted fields it names, and leaves a
reference whose line is unreadable or now holds another call. The turns are those Gym builds from captured calls; a
turn an agent publishes in its own trajectory keeps its question.
Responses stay inline: each holds only its own call's output, so they grow linearly, and the health checks and the
tool-request evidence read them.

The one health check input that is a request, the opening of a conversation (to tell an overflow after an answer in
the same conversation from one in another), is read from the capture as a key: a hash of the instructions, system
prompt or first message, with each image counted by the SHA-256 its blob is named by. No history is rebuilt and no
blob is read, so a health worker keeps a few keys per call whatever the requests hold. It is read only for chains
that end on an overflow. Without a readable capture, the model or turn that comparison decides is not
judged and its check is unobserved rather than flagged; what the check finds for other models and turns stands.

`delete_model_call_capture` needs `inline`. ATIF export asks for referenced rows to be resolved first, and says how.

## Compatibility

* The defaults stay `full` and `inline`: lines and rows are written as before.
* Every reader in Gym that reads through `CaptureStore` accepts both formats. `CaptureStore.copy_full()` writes a
  self-contained full-format copy; the SWE-Together judge evidence uses it. The misalignment monitor tails capture
  lines as written, so it refuses to start with `delta`.
* External readers of `*.capture.jsonl` lines need to handle `capture_format` lines before a run turns `delta` on:
  read through `CaptureStore`, or have `CaptureStore.copy_full()` write a full-format copy to read instead.
* Deleting a rollout's capture, including the collector's cleanup before a retry, also deletes its blob directory.
* `payload_ref` is a new optional field of `TrajectoryModelCall` and `TrajectoryTurn`, written only in `reference`
  mode. Older readers that forbid unknown fields reject such rows.
* A `reference` row needs the run's capture directory to restore its requests: keep the directory with the rows.

## Measurements

These are real captures from the run above. Each was converted with `scripts/replay_capture_delta.py` under a
16 GB memory cap, and every rebuilt exchange is byte-identical to its source line.

| Capture              | Calls | Full format | Delta (lines + blobs) | Ratio | Convert + verify | Peak RSS |
| -------------------- | ----- | ----------- | --------------------- | ----- | ---------------- | -------- |
| `0-0` (text)         | 167   | 87 MB       | 22.5 MB               | 3.9×  | 1.4 s            | 0.1 GB   |
| `9-0` (screenshots)  | 93    | 5.2 GB      | 101 MB (42 blobs)     | 51×   | 35 s             | 0.8 GB   |
| `4-0` (screenshots)  | 286   | 13.3 GB     | 151 MB (166 blobs)    | 88×   | 88 s             | 0.6 GB   |
| `61-0` (screenshots) | 211   | 25.9 GB     | 385 MB (128 blobs)    | 67×   | 173 s            | 2.1 GB   |

The finished-rollout enrichment (merge, trajectory and `ng_perf`, then serializing the row) was also measured,
with each measurement in its own process under the same 16 GB cap:

| Capture               | Code and payloads | Peak RSS        | Time  | Row                 |
| --------------------- | ----------------- | --------------- | ----- | ------------------- |
| `9-0`, full format    | before, inline    | 10.5 GB         | 15 s  | 5.2 GB              |
| `9-0`, full format    | before, none      | 5.6 GB          | 14 s  | 0.5 MB              |
| `9-0`, full format    | after, reference  | 0.6 GB          | 11 s  | 4.4 MB              |
| `61-0`, full format   | before, none      | killed at 16 GB | —     | —                   |
| `61-0`, full format   | after, reference  | 1.7 GB          | 58 s  | 9.3 MB              |
| `61-0`, delta format  | after, reference  | 0.3 GB          | 0.3 s | 9.3 MB              |
| `61-0`, either format | after, inline     | killed at 16 GB | —     | (26 GB of requests) |

An `inline` row holds every call's full request by definition, so for long sessions only `reference` or `none`
can keep rows bounded.

The merge alone (`scripts/replay_capture_delta.py merge`) on `9-0` peaks at 0.5 GB in `reference` or `none` on the
full format, and at 0.16 GB in `reference` on the delta format, against 5.5 GB in `inline` on either format.

In a text-only session, about 90% of the delta bytes are each call's own output. Two thirds of that is the raw
SSE text kept next to the reassembled response.