> This page is for version 2.1.5-pb7.
> For other versions, use one of these documentation indexes:
> - 2.1.7 (Latest) (default): https://docs.nvidia.com/nim/large-language-models/2.1.7/llms.txt
> - 2.1.6: https://docs.nvidia.com/nim/large-language-models/2.1.6/llms.txt
> - 2.1.5-pb7: https://docs.nvidia.com/nim/large-language-models/2.1.5-pb7/llms.txt
> - 2.1.5: https://docs.nvidia.com/nim/large-language-models/2.1.5/llms.txt
> - 2.1.4: https://docs.nvidia.com/nim/large-language-models/2.1.4/llms.txt
> - 2.0.13: https://docs.nvidia.com/nim/large-language-models/2.0.13/llms.txt
> - 2.0.12: https://docs.nvidia.com/nim/large-language-models/2.0.12/llms.txt
> - 2.0.11: https://docs.nvidia.com/nim/large-language-models/2.0.11/llms.txt
> - 2.0.10: https://docs.nvidia.com/nim/large-language-models/2.0.10/llms.txt
> - 2.0.4-pb6: https://docs.nvidia.com/nim/large-language-models/2.0.4-pb6/llms.txt
> - Archive: https://docs.nvidia.com/nim/large-language-models/archive/llms.txt

> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.nvidia.com/nim/large-language-models/llms.txt.

# Payload Capture

> Record NIM LLM and VLM inference requests as JSONL for replay with NVIDIA AIPerf, so you can measure speculative decoding or reproduce a production load pattern.

Payload capture is an opt-in feature that records each inference request as
JSONL suitable for replay with [NVIDIA AIPerf](https://github.com/ai-dynamo/aiperf).
It lets you capture your real traffic and replay it later, for example to measure
[speculative decoding](/nim/large-language-models/2.1.5-pb7/advanced-use-cases/speculative-decoding) acceptance and
speedup on your own workload, or to reproduce a production load pattern.

> **Warning**
>
> Payload capture records **exact request prompts**, which can contain PII, secrets, or
> other sensitive customer data. It is **disabled by default**. Enable it only when you
> intend to, write captures to an access-controlled location, and delete them when no
> longer needed.

Before you start, complete the following prerequisites:

1. Complete the credential, cache, and model-specific image setup in the [Quickstart](/nim/large-language-models/2.1.5-pb7/get-started/quickstart).
2. Provide a host directory that the container can write to, and mount it at the capture path.
3. Optional: Install [NVIDIA AIPerf](https://github.com/ai-dynamo/aiperf) if you plan to replay captures.

To capture inference payloads, complete the following steps:

1. Enable capture with `NIM_CAPTURE_ENABLE=1` and point the capture `path` at a writable, mounted directory.

   Capture is configured with two environment variables: `NIM_CAPTURE_ENABLE` is the
   toggle (off by default; unlike speculative decoding, no profile ever turns it on
   for you), and `NIM_CAPTURE_ARGS` is an optional JSON object of tuning keys.

   ```bash
   CAPTURE_DIR="$PWD/captures"
   mkdir -p "$CAPTURE_DIR"

   docker run --gpus all --shm-size=16GB \
     -u "$(id -u):0" \
     -e NGC_API_KEY \
     -e NIM_CAPTURE_ENABLE=1 \
     -e NIM_CAPTURE_ARGS='{"path": "/captures/requests.jsonl", "format": "mooncake_payload"}' \
     -v "$LOCAL_NIM_CACHE:/opt/nim/.cache" \
     -v "$CAPTURE_DIR:/captures" \
     -p 8000:8000 \
     "$NIM_LLM_IMAGE"
   ```

   The container runs as a non-root user, so mount a directory it can write to and set
   the capture `path` inside it. `-u "$(id -u):0"` runs it as you, in the root group
   (GID 0) that NIM needs, so a directory you own is writable and the capture file
   belongs to you, ready to replay from the host. NIM logs a warning at startup if the
   destination is not writable. `NIM_CAPTURE_ENABLE=1` alone (no `NIM_CAPTURE_ARGS`)
   captures to the default path in `mooncake_payload` format.

2. Optional: Set `NIM_CAPTURE_ARGS` keys to tune the destination, format, sample rate, and endpoint pattern.

   Refer to [Configuration](#configuration) for the full key list.

3. Replay the capture with AIPerf using a single `--endpoint-type` per run.

   Refer to [Output Formats](#output-formats) and [Replaying: One Endpoint Type Per Run](#replaying-one-endpoint-type-per-run).

4. Optional: Treat the capture file as sensitive, restrict access, and delete it when you finish.

   Refer to [Handling Sensitive Captures](#handling-sensitive-captures-pii).

## Configuration

`NIM_CAPTURE_ENABLE` (`0`/`1`, default `0`) is the master switch. `NIM_CAPTURE_ARGS` is
a JSON object; all keys are optional and validated (a bad value falls back to its
default; invalid JSON disables capture so prompts are never written to an unintended
location).

The following table describes the `NIM_CAPTURE_ARGS` keys:

| `NIM_CAPTURE_ARGS` Key | Default                                      | Description                                                                                                                                                                                                                                                        |
| ---------------------- | -------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `path`                 | `$NIM_WRITABLE_ROOT/captures/requests.jsonl` | Destination JSONL file (must be writable). Defaults to `/opt/nim/captures/requests.jsonl`; follows [`NIM_WRITABLE_ROOT`](/nim/large-language-models/2.1.5-pb7/reference/environment-variables#NIM_WRITABLE_ROOT) when the writable root is relocated for a read-only `/opt/nim`.                       |
| `format`               | `mooncake_payload`                           | `mooncake_payload` or `raw_payload` (refer to [Output Formats](#output-formats)).                                                                                                                                                                                  |
| `max_request_bytes`    | `1048576`                                    | Requests larger than this are forwarded normally but not recorded (a truncated payload is not replayable); a warning is logged.                                                                                                                                    |
| `sample_rate`          | `1.0`                                        | Probability a matching request is recorded. `1.0` = all; `0.1` = about 10%; `0.0` = none. Use to trim I/O under high QPS.                                                                                                                                          |
| `endpoint_pattern`     | `^/v1/(chat/completions\|completions)$`      | Regex; only matching request paths are captured.                                                                                                                                                                                                                   |
| `max_queue_bytes`      | `1073741824`                                 | In-memory ceiling (bytes) for the background writer queue. Captures are serialized and written off the request path by a writer thread; if the sink cannot keep up, records are dropped with a warning (the request is never blocked). Set >= `max_request_bytes`. |

Both formats record the **actual request payload** (the full request JSON), never
hashed or synthetic prompts, so the replay reflects your true workload (for example, coding
compared to chat traffic), not a generic corpus.

## Output Formats

For either replay command, set `MODEL_NAME` to the ID returned by
[/v1/models](/nim/large-language-models/2.1.5-pb7/reference/api-reference#find-model-name). Use the capture directory from the Docker example.

Choose `mooncake_payload` when you need inter-request timing, or `raw_payload` when you need one request JSON object per line.

### mooncake_payload (Default)

One timestamped line per request; the timestamp is milliseconds relative to the first
captured request:

```json
{"timestamp":0,"payload":{"model":"m","messages":[{"role":"user","content":"hello"}],"max_tokens":128}}
{"timestamp":540,"payload":{"model":"m","prompt":"hi","max_tokens":64}}
```

Replay (preserves inter-request timing):

```bash
aiperf profile \
  --model "$MODEL_NAME" --endpoint-type chat --streaming \
  --url localhost:8000 \
  --custom-dataset-type mooncake_trace \
  --input-file "$CAPTURE_DIR/requests.jsonl" \
  --server-metrics http://localhost:8000/v1/metrics
```

### raw_payload

One request JSON object per line:

```json
{"model":"m","messages":[{"role":"user","content":"hello"}],"max_tokens":128}
```

Replay:

```bash
aiperf profile \
  --model "$MODEL_NAME" --endpoint-type chat --streaming \
  --url localhost:8000 \
  --custom-dataset-type raw_payload \
  --input-file "$CAPTURE_DIR/requests.jsonl" \
  --server-metrics http://localhost:8000/v1/metrics
```

## Replaying: One Endpoint Type Per Run

AIPerf's payload modes send each captured payload **verbatim** to the single endpoint
named by `--endpoint-type`. A trace that mixes chat (`messages`) and completion
(`prompt`) payloads cannot be replayed in one run; the off-type lines are rejected.
The default `endpoint_pattern` captures both `/v1/chat/completions` and
`/v1/completions`, so for a single-run replay either:

* Scope capture to one endpoint, for example `"endpoint_pattern": "^/v1/chat/completions$"`.
* Split the capture by endpoint and replay each file with its matching
  `--endpoint-type` (`chat` for `messages`, `completions` for `prompt`).

AIPerf also loads a tokenizer for the served model to compute token metrics. If it
cannot be fetched (private model name or air-gapped host), pass a local tokenizer
directory with `--tokenizer <dir>` (or `--tokenizer builtin`).

## Handling Sensitive Captures (PII)

Captured payloads contain the **exact prompts**. If you are only testing performance,
you can protect any sensitive data at rest by replacing the payloads with block-hashed
values.

However, if you are testing speculative-decoding acceptance rate or acceptance length,
the original payloads should remain in place. Hashing discards the prompt content,
which severely degrades the capture's value for measuring speculative-decoding AR/AL
(acceptance depends on the actual tokens, not just token lengths or prefix structure).

> **Warning**
>
> Either way, treat the capture file as sensitive: write it to an access-controlled,
> short-lived location and delete it when finished.

NIM creates the capture file with mode `0660`, and the directory that holds it with mode
`0770` when that directory does not exist yet, so only the file's owner and group can read
captures. If the capture file already exists, NIM removes other users' access to it and
logs the change, or logs a warning when it cannot. Without `-u`, the container runs as
UID 1000 in group 0, so a capture on a host mount belongs to UID 1000: read it with `sudo`,
or run the container with `-u "$(id -u):0"` as in the example above. Rootless Docker and
Podman map container user IDs to other host IDs, so the owner differs there.

## Next Steps

After you have a capture file, replay it against speculative decoding to measure acceptance and speedup on your traffic.
For more information, refer to [Speculative Decoding](/nim/large-language-models/2.1.5-pb7/advanced-use-cases/speculative-decoding).