> ## Documentation Index
> Fetch the complete documentation index at: https://docs.physionlabs.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Serverless RL runs

> Start, read and stop an RL run: a model loaded on GPUs for your own training loop.

An RL run is a model kept on a node of GPUs until you stop it. Your loop samples from it and trains it step by step.
It is billed by its tokens (the model's `token_prices`); starting holds credits that are returned when it stops. How to write the loop is in
[Serverless RL](https://docs.physionlabs.ai/nexcloud/guides/serverless-rl.md).

> [!WARNING]
>
> A run costs money from the moment it is ready until it is stopped, whether or not anything trains. Stop it when your
> loop ends, and when it fails.

## Estimate

`POST /v1/training/sessions/estimate` with `model` and `max_hours`.

```bash curl theme={null}
curl https://nexcloud.physionlabs.ai/v1/training/sessions/estimate \
  -H "Authorization: Bearer $NEXTRON_API_KEY" -H "Content-Type: application/json" \
  -d '{"model": "qwen3.8-27b", "max_hours": 1}'
```

```python Python theme={null}
client.rl.estimate(model="qwen3.8-27b", max_hours=1)
```

```bash CLI theme={null}
nextron-cloud rl start --model qwen3.8-27b --max-hours 1    # without --yes: the price only (exit code 3)
```

```json theme={null}
{
  "price_per_gpu_hour": 350,
  "gpus": 8,
  "price_per_hour": 2800,
  "credits": 2800,
  "balance": 4520,
  "enough": true
}
```

`credits` is what is held for `max_hours`, in US cents. You pay for the seconds the run was ready, on all its GPUs;
the rest of the hold is released.

## Start a run

`POST /v1/training/sessions`.

| Field | Default | |
| - | - | - |
| `model` | required | a model that lists `interactive` in its methods |
| `max_hours` | required | the run stops itself after this long |
| `max_context` | 16384 | 2,048 to 16,384 tokens: a prompt with its reply, and a training sequence |
| `max_grad_norm` | 1.0 | gradient clipping for every optimizer step |
| `grad_clip` | true | false turns clipping off |
| `idle_timeout` | 1800 | 60 to 14,400 seconds without a sample or a step before the run stops |
| `images` | false | true lets prompts hold images and videos |
| `video_fps`, `video_max_frames`, `video_frame_tokens` | the model's | [video settings](https://docs.physionlabs.ai/nexcloud/guides/media.md#video-settings) |
| `from_job` | the base model | a finished job or RL run of the model, or an uploaded checkpoint, to start from |
| `name`, `tags` | | your labels |

```bash curl theme={null}
curl https://nexcloud.physionlabs.ai/v1/training/sessions \
  -H "Authorization: Bearer $NEXTRON_API_KEY" -H "Content-Type: application/json" \
  -d '{"model": "qwen3.8-27b", "max_hours": 1, "name": "math rl"}'
```

```python Python theme={null}
run = client.rl.start(model="qwen3.8-27b", max_hours=1, name="math rl")
run = client.rl.wait(run.id)     # until ready: the model loads in 6-8 minutes, not billed
```

```bash CLI theme={null}
nextron-cloud rl start --model qwen3.8-27b --max-hours 1 --name "math rl" --yes
nextron-cloud rl wait rl_3fe51ec62ac54ca4
```

## The run object

`GET /v1/training/sessions/{id}` returns one run; `GET /v1/training/sessions` returns a list.

```json theme={null}
{
  "id": "rl_3fe51ec62ac54ca4",
  "state": "closed",
  "model": "qwen3.8-27b",
  "layout": "1x8-h100",
  "params": {
    "train_type": "interactive",
    "max_hours": 1,
    "max_context": 16384,
    "max_grad_norm": 1,
    "grad_clip": true,
    "idle_timeout": 600
  },
  "created": 1796184832.32,
  "ready": 1796185486.01,
  "billable_seconds": 126,
  "close_requested": true,
  "checkpoint": { "status": "ready", "step": 2, "files": 45, "bytes": 55583124184 },
  "error": null,
  "name": "math rl",
  "tags": [],
  "images": [],
  "price_per_hour": 2800,
  "gpus": 8
}
```

`state` goes `queued` → `starting` (the model loads; not billed) → `ready` → `closing` (not billed) → `closed`, or
`failed`. A failed run is not charged. `billable_seconds` is what has been billed so far.

## Stop a run

`POST /v1/training/sessions/{id}/close`. Billing stops; the model is saved if it trained at least one step, and is
then downloaded like a job's checkpoint, with the run's id in place of the job's.

```bash curl theme={null}
curl -X POST https://nexcloud.physionlabs.ai/v1/training/sessions/rl_3fe51ec62ac54ca4/close \
  -H "Authorization: Bearer $NEXTRON_API_KEY"
```

```python Python theme={null}
client.rl.stop("rl_3fe51ec62ac54ca4", wait=True)
client.jobs.download_checkpoint("rl_3fe51ec62ac54ca4", "checkpoints/rl_3fe51ec62ac54ca4")
```

```bash CLI theme={null}
nextron-cloud rl stop rl_3fe51ec62ac54ca4 --wait
nextron-cloud jobs download rl_3fe51ec62ac54ca4 checkpoints/rl_3fe51ec62ac54ca4
```

## Sample and train

A ready run serves its own calls under `/v1/training/sessions/{id}/nextron/v1/`: `sample`, `batches/{id}`, `train`,
`steps/{id}`, `info`, `heartbeat`, `images/missing` and `finish`. Drive them with the SDK's `client.rl.connect(id)`,
which also tells the run every 20 seconds that your loop is alive:

```python theme={null}
with client.rl.connect(run.id) as live:
    batch = live.sample(["What is 17 * 23?"], n=8, max_tokens=512)
    rewards = [[1.0 if "391" in reply.text else 0.0 for reply in group] for group in batch]
    result = live.train(batch, rewards, learning_rate=2e-6)
    live.finish()        # saves the model and stops the run
```

Every argument of `sample` and `train` is in [Python SDK and CLI](https://docs.physionlabs.ai/nexcloud/sdk/reference.md#serverless-rl).

## Other calls

| Call | Does |
| - | - |
| `GET /v1/training/sessions/{id}/rollouts?step=` | the best and the worst group of a training call (the latest without `step`) |
| `POST /v1/training/sessions/{id}/images` `{"dataset_id"}` | gives a run started with `images` a dataset's images and videos, named `sha256:<hex>` in prompts |

In the SDK: `client.rl.rollouts(id, step=None)`, `client.rl.metrics(id)`; on the command line
`nextron-cloud rl rollouts ID`, `nextron-cloud rl metrics ID`.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.