# Scaling and cold starts

> Every scaling setting of an autoscaled service with its default, how Legion adds and removes replicas, scale to zero and cold starts, warm replicas, the hardware choice, waiting for GPUs and how changes roll out.

Source: https://legion.ambient.xyz/docs/renting/services/scaling

After this page you know what every scaling setting does, what it costs to turn it, and what a caller sees when the service grows, shrinks or sleeps.

This page is about an autoscaled service. A service on a rental serves one rental that you manage and none of these settings apply: see [From one rental to autoscaling](https://legion.ambient.xyz/docs/renting/services/overview#from-one-rental-to-autoscaling) to turn autoscaling on, or `legion services autoscale on`.

## How Legion decides

Legion's autoscaler looks at the traffic the router sees, never at anything a replica reports about itself, so a host cannot fake load to get rented. Every few seconds it compares two signals with your settings:

- **Requests in flight per replica** against your **target**. When the load divided by the target needs more replicas than you have, Legion adds one.
- **How long requests wait.** When requests wait for a free slot longer than the scale-up threshold, Legion adds one, and when the wait is over three times the threshold it adds two at once.

It never goes above your **maximum replicas** or the **spend cap**, and it never starts a replica above your **price ceiling**.

## The settings

These are the values a service gets when you leave a field alone. The create form shows the ones it will use before you create.

| Setting | Default | What it does |
| --- | --- | --- |
| **Minimum replicas** | 1 | Replicas that always run. At 0 the service scales to zero when idle. |
| **Maximum replicas** | 2 | Legion never runs more than this, including a surge replica during a roll. |
| **Scale to zero** | Off | When on, the service can drop to no replicas after it has been idle for the idle window. It is opt-in because the first request after idle is a cold start. |
| **Idle window** | 15 minutes | With scale to zero on, how long the service must go without a request before it scales to zero. |
| **Scale-down delay** | 10 minutes | How long the load must stay below capacity before Legion removes a replica. It stops a short lull from shrinking the service just before the next burst. |
| **Target requests per replica** | From the model, else 8 | Legion adds a replica when requests in progress per replica go above this. |
| **Scale up when requests wait longer than** | 4 seconds | The queue wait that adds a replica even when the target is not reached. |
| **Maximum requests per replica** | 32 | Requests in flight per replica at most. More wait in the queue. |
| **Request timeout** | 600 seconds (at most 3600) | How long one request may run before Legion ends it, streaming included. |
| **Warm replicas** | 0 (1 when scale to zero is on) | Replicas kept stopped, with their disk, for a fast restart. See [Warm replicas](#warm-replicas). |
| **Price ceiling** | You set it | The most one replica may cost per hour (the whole replica, not per GPU), fees included. |
| **Monthly spend cap** | You set it | Required. See [Service billing](https://legion.ambient.xyz/docs/renting/services/billing). |

Change them on the service's **Settings** tab, with `legion services update` (for example `--min`, `--max`, `--scale-to-zero`, `--target-concurrency`), or with `updateService` (`PATCH /v1/services/{service_id}`). `GET /v1/services/defaults` returns the defaults the platform uses, and the app and the CLI read them from there. Changes apply to the next scaling decision. Running replicas are not restarted.

The minimum can't be above the maximum, and the maximum can't be above your organization's limit: 10 replicas per service and 20 across the organization (`409 replica_limit_exceeded`). Ask [support](https://legion.ambient.xyz/docs/account/support) to raise it. A refused value answers `400 invalid_scaling`, and `409 revision_mismatch` means someone changed the service after you read it: read it again and retry.

## Scaling up

Legion adds at most one replica every 2 minutes, or two when requests wait three times longer than the threshold. Starting from zero it adds one at once. Replacing a replica that failed is not limited. A replica is added from a warm one first, which is faster, and otherwise by renting a new one on the best host.

While a replica starts, it is **Starting** on the Replicas tab and the service's status is **Scaling**. A replica takes requests only when the router's health check passes.

## Scaling down

Legion removes one replica at a time, and only after the scale-down delay. It picks, in order, one that is not ready, a spot one before an on-demand one, the most expensive, then the newest. A replica **drains** first: it gets no new requests and finishes the ones it has, for up to 2 minutes. Then it stops. If you have warm replicas free, it is kept; otherwise it is deleted.

## Scale to zero and cold starts

With scale to zero on, an idle service holds no replicas and costs nothing. The status reads **Idle**. The first request wakes it: Legion rents a host, starts the model and answers when a replica is ready. That wait is a **cold start**. It covers finding a host, starting the container and loading the weights, so it depends on the model. The service's page shows the median of recent cold starts.

What a caller sees depends on the request:

- Without anything extra, it gets `503 service_starting` at once, with a `Retry-After` of the usual cold start.
- With `X-Legion-Wait: <seconds>` (at most 300), Legion holds the request until a replica is ready or the wait ends. See [Call a service](https://legion.ambient.xyz/docs/renting/services/call#when-no-replica-is-ready).

If cold starts matter, keep one replica running (minimum 1) or use a [warm replica](#warm-replicas).

## Warm replicas

A **warm replica** is a stopped replica that Legion keeps on its host, with its disk and the model's cached files. When the service needs a replica, Legion restarts the warm one first. That skips the host search and the download, so it is much faster than a cold start.

A warm replica does not bill for GPU time. It bills for the disk it keeps, like any [stopped rental](https://legion.ambient.xyz/docs/renting/stopping-and-backups). The cost is small but not zero, and it shows on the service's cost card as storage. Legion deletes a warm replica that has stayed stopped for 60 hours. The host can also be rented to someone else while it is stopped, in which case the restart fails and Legion falls back to a new rental.

The number of warm replicas can't be above the number you allow to be kept. With scale to zero on, the default is 1, so the first request after idle restarts a warm replica instead of starting from nothing.

## How Legion chooses hardware

Legion picks the hardware, so you don't have to know which GPUs a model fits on. To start a replica it:

1. Looks at every offer that your placement and spot or on-demand choice allow, and every GPU count the model can use on it. The counts follow the model: it can only split across 1, 2, 4 or 8 GPUs, at least the number it needs, and never more GPUs than needed. A larger count is chosen only when it is cheaper in total or the smaller one doesn't fit. Legion sets the model server to use all of a replica's GPUs.
2. Keeps the configurations that can really run the model: enough GPU memory, a supported driver and architecture. It is the same check that **Compatible hardware** uses when you rent.
3. Drops those over your price ceiling, and any GPU model you didn't tick under **Only these GPUs**, if you chose that instead of **Any GPU that fits**.
4. Rents the cheapest total price per hour. A tie goes to the configuration with fewer GPUs, then to the host with the higher Legion score.
5. Among hosts that tie, prefers one that holds no other replica of this service, so one host failing doesn't take every replica, and never puts more than half of a service's replicas on one provider once it has four or more.

A host that failed a replica of your service in the last 24 hours is skipped.

**Replicas can differ.** One replica may be a single RTX 4090 and the next two L40S, whatever was cheapest and fit when each started. The router balances on requests in flight, and each replica's share of requests scales with its GPU memory compared with the smallest configuration that fits, so a bigger replica takes more.

**It drifts to cheaper hardware.** Replacing a replica runs the same choice again, so a service moves to cheaper hardware over time, without you doing anything. The replacement starts before the old one drains, as in [Making changes without dropping requests](#making-changes-without-dropping-requests).

**You can see why.** The **Replicas** tab shows each replica's GPU model, count and price per GPU-hour, and whether it is on the current settings: during a rolling change, replicas not replaced yet are marked. The **Activity** feed says why Legion chose it, for example "cheapest fitting offer: 1x RTX 4090 at 0.42/h; 2 offers did not fit, for example 1x RTX 3090: 24 GB of GPU memory needed" (the `placement` of a `scale_up` event). The create form and the estimate show the likely hardware before you start.

## Change the model, hardware or price ceiling

You change a service in place, on its **Settings** tab under **Model and hardware**, or with `legion services update chat --gpu H100 --gpu L40S --max-price 3`. The service keeps its name, URL and keys, so there is no need to create a new one.

- **Price ceiling.** Edit **Never pay more than** and save. A new ceiling applies at once: running replicas are not restarted, the next replica Legion starts uses it, and spot replicas' bids follow it. When the service reads **Waiting for GPUs**, its Overview has **Raise price ceiling**, which opens this field.
- **Model and hardware.** Choose **Change model or hardware** to pick another model, other GPUs, spot or on-demand, or where replicas run. The estimated cost shows what the change costs before you save. Saving replaces the replicas one at a time, as in [Making changes without dropping requests](#making-changes-without-dropping-requests). A new model starts with its own default settings.

## Waiting for GPUs

When no host can run a replica at your price ceiling, the service's status reads **Waiting for GPUs** (the API reports the status `waiting_for_gpus`). It is a market condition, not an error. Legion tries again every 30 seconds. While it waits, requests that need a replica wait up to the queue limit and then fail with a 503. To fix it, raise the price ceiling, remove GPU models from your **Only these GPUs** list, switch from spot to on-demand, or set an [offer alert](https://legion.ambient.xyz/docs/renting/offer-alerts) to hear when hosts appear.

After three replicas in a row fail to start, the service reads `start_failures` and Legion backs off, 5 minutes at first and up to 30, until one replica becomes ready.

## When a replica fails or a host goes away

Legion replaces a replica without waiting for the old one to finish draining:

| What happened | What Legion does |
| --- | --- |
| The replica is not ready 20 minutes after it started (5 for a warm restart) | Deletes it, skips that host for 24 hours, starts another |
| The model server reports an error | Same |
| More than half its requests fail over 30 seconds | Stops sending it traffic, then replaces it |
| The host goes offline or the container exits for 2 minutes | Deletes it and starts another |
| A spot replica gets the 5-minute notice | Drains it and starts a replacement at once |

Replacements are not limited by the one-replica-per-2-minutes rule. Billing starts when a replica is running, so a replica that never started costs nothing.

## Making changes without dropping requests

Replacing replicas, for a failure or for a change of model, image or hardware, rolls one at a time, with one surge replica: the new replica starts and becomes ready before the old one drains, so requests are not dropped. The surge replica counts against your maximum and your spend cap, so a service at its maximum rolls only when there is room.

Legion never deletes or shrinks an idle service on its own. If a service sits unused, Legion can email you, but it leaves the decision to you.

## Pause

**Pause** scales the service to zero and turns callers away with `503 service_paused`, until you resume it. The URL and keys stay. Replicas drain for up to 30 seconds. No GPU time bills while it is paused. Resume puts the service back to its minimum, restarting warm replicas first. A capped service can't be resumed until you raise the cap. See [Billing](https://legion.ambient.xyz/docs/renting/services/billing#the-monthly-spend-cap).
