Skip to content

An open-source, faster Devin alternative

TypeScript

docs

Brian Reardon735bd9d5701d
diff --git a/docs/sandboxes/elasticity.mdx b/docs/sandboxes/elasticity.mdx
index 0dce154b..59995793 100644
--- a/docs/sandboxes/elasticity.mdx
+++ b/docs/sandboxes/elasticity.mdx
@@ -1,19 +1,36 @@
---
title: "Elasticity"
-description: "Dynamically scale sandbox memory and CPU from inside the VM"
+description: "Dynamically scale sandbox memory and CPU — manually, automatically, or via in-VM API"
---
-Sandboxes expose an internal metadata API at `http://169.254.169.254` that lets code **inside** the VM scale memory up or down on the fly. CPU scales proportionally to memory.
+A sandbox's memory and CPU can be resized at runtime. There are three ways to do it:
-This is useful for workloads with variable resource needs — scale up before a heavy compilation or ML inference, then scale back down to save resources.
+- **Inside the VM** — `POST http://169.254.169.254/v1/scale` lets code in the sandbox scale itself. Useful for workload-aware scripts (e.g. scale up before a build).
+- **From the control plane** — `POST /api/sandboxes/:id/scale` lets your application or a human operator resize externally.
+- **Automatically** — opt a sandbox into the per-sandbox autoscaler with `PUT /api/sandboxes/:id/autoscale` and the platform handles up/down based on observed memory pressure.
+
+CPU scales proportionally to memory in all three modes (1 vCPU per ~4 GB up to 16 GB / 4 vCPU). The permissible memory sizes are:
+
+| Memory | vCPU |
+| --- | --- |
+| 1 GB | 1 (best-effort) |
+| 4 GB | 1 |
+| 8 GB | 2 |
+| 16 GB | 4 |
+
+If you want a sandbox to **stay at a fixed size**, see [Locking Resources](#locking-resources) below.
+
+## In-VM Scaling
+
+Sandboxes expose an internal metadata API at `http://169.254.169.254` that lets code **inside** the VM scale itself.
<Note>
- The elasticity endpoint is only accessible from inside the sandbox. It is not exposed through the control plane API or SDKs.
+ This endpoint is only reachable from inside the sandbox. It is not exposed through the control plane API or SDKs.
</Note>
-## Scale Memory
+### Scale Memory
-Send a `POST` to `/v1/scale` with the desired memory in MB:
+Send a `POST` to `/v1/scale` with the desired memory in MB. CPU is adjusted proportionally.
```bash
curl -s -X POST http://169.254.169.254/v1/scale \
@@ -21,22 +38,13 @@ curl -s -X POST http://169.254.169.254/v1/scale \
-d '{"memoryMB": 4096}'
```
-CPU is adjusted proportionally — you only need to specify memory. The permissible sizes are:
-
-| Memory | vCPU |
-| --- | --- |
-| 1GB | 1 (best-effort) |
-| 4GB | 1 |
-| 8GB | 2 |
-| 16GB | 4 |
-
-## Check Current Limits
+### Check Current Limits
```bash
curl -s http://169.254.169.254/v1/limits
```
-## Example: Rust Compilation
+### Example: Rust Compilation
Rust builds are memory-hungry. You can alias `cargo` to scale up before compilation and scale back down after:
@@ -61,9 +69,9 @@ _cargo_scaled() {
Add this to your sandbox's `~/.bashrc` or inject it via `sandbox.exec.run` so every `cargo build`, `cargo test`, etc. automatically gets the extra resources.
-## Example: Auto-Scaling Script
+### Example: Custom Scaling Loop
-A simple shell script that monitors memory pressure and scales automatically:
+A simple shell script that monitors memory pressure and scales automatically. Useful when you want fine-grained control inside the sandbox itself; for the platform-managed equivalent see [Autoscaling](#autoscaling) below.
```bash
#!/bin/sh
@@ -108,3 +116,85 @@ while true; do
sleep 5
done
```
+
+## Manual Scaling (Control Plane)
+
+You can resize a sandbox externally from your application or operator tooling:
+
+```bash
+curl -sf -X POST "$API/api/sandboxes/$SANDBOX_ID/scale" \
+ -H "X-API-Key: $API_KEY" \
+ -H "Content-Type: application/json" \
+ -d '{"memoryMB": 4096}'
+```
+
+Response includes `autoscaleDisabled: true` if this call turned off autoscale on the sandbox (see below). The endpoint can return:
+
+- **409 `oom_floor`** — the requested size would force a guest OOM-kill because current working set exceeds it. Free memory inside the guest, then retry.
+- **403 `scaling_locked`** — the sandbox is locked (see [Locking Resources](#locking-resources)).
+
+## Autoscaling
+
+Opt a sandbox into the per-sandbox autoscaler and the platform will resize it for you based on observed memory pressure. Autoscale is **opt-in per sandbox** and **disabled by default**.
+
+### Enable
+
+```bash
+curl -sf -X PUT "$API/api/sandboxes/$SANDBOX_ID/autoscale" \
+ -H "X-API-Key: $API_KEY" \
+ -H "Content-Type: application/json" \
+ -d '{"enabled": true, "minMemoryMB": 1024, "maxMemoryMB": 4096}'
+```
+
+`minMemoryMB` and `maxMemoryMB` must be values from the tier table above. The autoscaler will keep the sandbox between these bounds, in tier steps.
+
+### Behavior
+
+- **Scale up**: when the sandbox's 1-minute average memory utilization exceeds 75 %, jump to the next tier. 60-second cooldown between up-events.
+- **Scale down**: when **all** of the 1-min, 5-min, and 15-min utilization averages stay below 25 %, drop one tier. 5-minute cooldown between down-events. Down requires at least 15 minutes of low-utilization data, so a brief idle pause won't shrink you.
+- **Manual override**: any explicit `POST /scale` call disables autoscale on that sandbox. Re-enable explicitly via `PUT /autoscale` if you want it back.
+
+### Disable
+
+```bash
+curl -sf -X PUT "$API/api/sandboxes/$SANDBOX_ID/autoscale" \
+ -H "X-API-Key: $API_KEY" \
+ -d '{"enabled": false}'
+```
+
+### Inspect
+
+```bash
+curl -sf "$API/api/sandboxes/$SANDBOX_ID/autoscale" -H "X-API-Key: $API_KEY"
+# {"sandboxID":"sb-...","enabled":true,"minMemoryMB":1024,"maxMemoryMB":4096}
+```
+
+## Locking Resources
+
+If you want a sandbox to **stay at a fixed size** — predictable billing, a benchmark, a long-running pinned workload — lock it. Locking blocks both manual scaling and autoscaling on the sandbox.
+
+```bash
+curl -sf -X PUT "$API/api/sandboxes/$SANDBOX_ID/scaling-lock" \
+ -H "X-API-Key: $API_KEY" \
+ -H "Content-Type: application/json" \
+ -d '{"locked": true}'
+```
+
+While locked:
+
+- `POST /scale` returns **403** `scaling_locked`
+- `PUT /autoscale {enabled: true}` returns **403** `scaling_locked`
+- The autoscaler skips the sandbox
+
+Locking automatically disables autoscale on the sandbox, so you have a single user-facing toggle. Unlocking does **not** auto-re-enable autoscale; turn it back on explicitly if you want it.
+
+```bash
+# Unlock
+curl -sf -X PUT "$API/api/sandboxes/$SANDBOX_ID/scaling-lock" \
+ -H "X-API-Key: $API_KEY" \
+ -d '{"locked": false}'
+
+# Inspect
+curl -sf "$API/api/sandboxes/$SANDBOX_ID/scaling-lock" -H "X-API-Key: $API_KEY"
+# {"sandboxID":"sb-...","locked":true}
+```