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

> ## Agent Instructions
> To build and serve your own model, start at /deploy/development/quickstart and /deploy/development/overview. Authenticate with reactor auth login, scaffold with reactor init, then run reactor deploy production from the workspace. Deploy builds remotely by default, creates a missing workspace project and its production environment, and waits for rollout. No local Docker or model.version bump is needed. Projects own images and weight sets; environments select the image, weights, GPU, instance count, and variables. Create staging explicitly with reactor environment create staging before deploying to it. Use reactor scale for capacity and reactor rollback for an earlier revision’s image and weights. See /deploy/platform/projects and /deploy/platform/deploy. Client SDK modelName remains account-qualified and selects production by default. Model code imports reactor_runtime; Python client code imports reactor_sdk. Local testing is optional; /deploy/development/local-testing covers reactor run, which needs Docker.
> Reactor hosts multiple models, each with its own connect slug (modelName) and command/event schema. The video model catalog — slug, typed SDK package, and links to its schema — is at /model-api-reference/overview. Robotics policy documentation starts at /robotics/overview; The launch policies are Cosmos Nano DROID (/robotics/cosmos/nano-policy-droid/), DreamZero DROID (/robotics/dreamzero/droid/), and FLUX Action DROID (/robotics/flux-action/droid/), organized by model family and embodiment. Some models expose one slug per experience (e.g. HappyOyster); always take the slug from the model's own pages, never guess it.
> Fastest path to a working app: `npx create-reactor-app my-app --model=<slug>` scaffolds a complete app with secure auth wired up. Typed TypeScript SDKs are published as @reactor-models/<model>; Python uses the base reactor-sdk package.
> Auth: exchange an API key (rk_...) for a JWT via POST https://api.reactor.inc/tokens from your server. Never put the API key in client-side code.
> Append .md to any docs URL for clean Markdown. Search these docs via the MCP server at https://docs.reactor.inc/mcp.

# Batch requests

> Step several sessions together: the model server runs up to max_batch requests in one forward pass.

On [Serve several sessions](https://docs.reactor.inc/deploy/development/reactor-app/concurrent-sessions.md), the sessions
share one model server and take turns. If your model can run several requests in one forward pass,
the model server can step the sessions together. One forward pass then serves up to `max_batch`
sessions, in about the time of one.

> [!NOTE]
>
> **Batching helps only when one request leaves the GPU partly idle.** If one request already
> keeps the GPU busy, a batch takes as long as its requests one after another. The GPU also needs
> memory for the activations of a full batch. Measure your step time first. See
> [Choose max\_batch](#choose-max_batch).

## How a batch forms

The model server follows these rules:

* A session has at most one request waiting or running.
* When the model server is free and a request waits, it waits up to 5 ms for others.
* It stops waiting early when `max_batch` requests wait, or when every live session has a request
  waiting. A session is live when it opened, reset, sent a request, or got a result in the last 2
  seconds, so a session runtime with no client stops holding batches back.
* It then calls your `step()` once, with up to `max_batch` requests, oldest first.
* A request that arrives while a step runs joins the next step.

![Animation. Four session runtimes each send one request to the model server, at slightly different times. When all four are waiting, the model server runs one forward pass on the GPU for all four requests. Then each session gets its own frame back.](https://mintcdn.com/reactortechnologiesinc/nGA8jDns1abXjgnY/diagrams/concurrent-sessions-batch.svg?fit=max\&auto=format\&n=nGA8jDns1abXjgnY\&q=85\&s=d52f769b3c0b9b07accae8a8672f02da)

One turn of the model server, slowed down. Each session's request arrives at its own moment. When every session has a request waiting, one forward pass runs, and each session gets its own frame back.

The short wait lets requests that arrive a moment apart step together. To change it, set
`runtime.model_server.batch_window` in seconds.

## What your step() must accept

With batching on, the model server keeps this contract with your `step()`:

* **A batch groups requests by arrival.** It holds whichever sessions' requests are waiting, oldest
  first, not a fixed set of sessions in slot order.
* **A batch holds any number of requests from 1 to `max_batch`.** So the batch dimension of your
  tensors changes from step to step. A model that needs a fixed shape, for example for CUDA graphs,
  pads the batch itself.
* **Each request belongs to one session.** Its `slot` and `generation` say which, so state for each
  session stays keyed by slot, as on the previous page.
* **Return one output per request, in the order of the requests.**

## Turn batching on

Start from the Lava example on the previous page, with its 4 sessions. Its `step()` already meets
the contract: it stacks every request in the list into one tensor, runs one forward pass for any
list length, and returns one frame per request. So the code does not change. Only `reactor.yaml`
changes, from a `max_batch` of 1 (the default) to 4.

Set `max_batch` under `runtime.model_server`, and remove the `sessions` block. With no
`sessions.count`, each instance serves `max_batch` sessions, so it still serves 4.

```yaml reactor.yaml theme={"theme":{"light":"github-light","dark":"github-dark-high-contrast"}}
$schema: reactor/v2

model:
  name: your-org/lava

runtime:
  import: lava:Lava
  model_server:
    import: lava_model:LavaModel
    max_batch: 4  # up to 4 requests in one forward pass; also 4 sessions

build:
  runtime_version: "3.12.0"
  cuda_version: "12.9.1"
```

Your class reads the value as `self.max_batch` before `load()` runs, so the Lava warm-up renders a
full batch of 4. A warm-up of a full batch also shows at once whether the GPU has the memory for it.

Deploy it, connect 4 clients at the same time, and read the logs. The environment keeps the GPU of
its first deploy, and the deploy sends the 4 sessions that `max_batch` sets:

```sh theme={"theme":{"light":"github-light","dark":"github-dark-high-contrast"}}
reactor deploy production
reactor logs production --follow
```

The `mean batch size` lines from `LavaModel.step()` now show how full the batches are. With 4 live
clients, they move from 1.00 toward 4.

## Choose max\_batch

Every session needs a frame in each frame period. At 24 frames per second, the period is about 42
ms.

A request can arrive just after a step starts. It then waits for that step to end, then up to 5
ms, then for its own step. So keep two step times plus 5 ms under the frame period.

1. Measure how long one `step()` takes for 1, 2, 4, and 8 requests.
2. Find the largest batch that meets the rule above.
3. Set `runtime.model_server.max_batch` to that number.
4. Deploy, connect that many clients, and read the logs.

For example, say one step takes 10 ms for 1 request, 12 ms for 4, and 16 ms for 8:

| Setting | Worst wait for a frame | Fits in 42 ms? |
| - | - | - |
| 4 sessions, no batching | 4 steps × 10 ms = 40 ms | barely |
| 4 sessions, `max_batch: 4` | 2 × 12 ms + 5 ms = 29 ms | yes |
| 8 sessions, `max_batch: 8` | 2 × 16 ms + 5 ms = 37 ms | yes |

Batching turns 4 sessions that barely fit into 8 sessions that fit.

To serve more sessions than `max_batch`, set `model.resources.sessions.count` higher. A request then
waits for the running step and for every full batch ahead of it. The running step holds up to
`max_batch` sessions, which cannot send, so with up to twice `max_batch` sessions the rule above
still holds: two step times plus 5 ms. Each further `max_batch` sessions adds one step time. With
12 sessions and a `max_batch` of 4, keep three step times plus 5 ms under the frame period.

## Finish a request on your own threads

Some models run a request across their own threads, for example in stages over several GPUs. Such
a model can return a `concurrent.futures.Future` from `step()` in place of a result, and complete it
later. The session stays busy until the future is done, while the next batch steps. The
`ModelServer.step()` docstring in `reactor_runtime.model_server` gives the rules.

## Next

> [card] [Serve several sessions](https://docs.reactor.inc/deploy/development/reactor-app/concurrent-sessions.md)
>
> The model server, the session runtimes, failures, and state for each session.

> [card] [reactor.yaml reference](https://docs.reactor.inc/deploy/platform/reactor-yaml.md#runtimemodel_server)
>
> Every key of `runtime.model_server` and `model.resources.sessions`.


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