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

# Resizable templates

> Build a template whose sandboxes can start, resume and reboot with more CPUs or memory than it boots with.

A template normally fixes the CPUs and memory of every sandbox created from it. A **resizable** template sets a maximum next to each, and every sandbox chooses its own size up to that maximum:

* **When it is created:** `Sandbox.create()` takes `cpuCount` and `memoryMB` in JavaScript or `cpu_count` and `memory_mb` in Python.
* **When it resumes or reboots after a pause:** `Sandbox.connect()` takes them inside `onResume` in JavaScript or `on_resume` in Python.

A running sandbox is never resized. Sizes are chosen only when a sandbox starts.

> [!NOTE]
>
> Resizable templates are currently in private beta.
> If you'd like access, please reach out to us at [support@e2b.dev](mailto:support@e2b.dev).
>
> CPU resizing on create needs the E2B SDK 2.64.0 or later. Resizable memory, and choosing sizes on `connect()`, need SDK 2.66.0 or later.

## Build a resizable template

Set `maxCpuCount` and `maxMemoryMB` in JavaScript or `max_cpu_count` and `max_memory_mb` in Python next to `cpuCount` and `memoryMB`. The `cpuCount` and `memoryMB` of the template are what a sandbox starts with when it doesn't ask for anything else.

```typescript JavaScript & TypeScript theme={"theme":"css-variables"}
import { Template } from 'e2b'

await Template.build(template, 'my-template', {
  cpuCount: 2,
  maxCpuCount: 8,
  memoryMB: 2048,
  maxMemoryMB: 8192,
})
```

```python Python theme={"theme":"css-variables"}
from e2b import Template

Template.build(
    template,
    "my-template",
    cpu_count=2,
    max_cpu_count=8,
    memory_mb=2048,
    max_memory_mb=8192,
)
```

The same options work with `Template.buildInBackground()` in JavaScript or `Template.build_in_background()` in Python. You can make a template resizable for CPUs only, memory only, or both. Leave a maximum out and that resource stays fixed, as before.

| | CPUs | Memory |
| - | - | - |
| Maximum | `maxCpuCount`: at least `cpuCount`, and an even number (or `1`) | `maxMemoryMB`: at least `memoryMB`, and `memoryMB` plus a multiple of 128 MiB |
| Sizes a sandbox can choose | Any count from `1` to the maximum | `memoryMB` and up, in steps of 128 MiB, to the maximum |
| Limit | Your project's CPU limit | By default, growth of up to three times `memoryMB` (for example, a maximum of up to 8192 MiB for a template with 2048 MiB), and your project's memory limit. If you need a higher maximum, contact [support@e2b.dev](mailto:support@e2b.dev). |

## Choose the size when you create a sandbox

Pass `cpuCount` and `memoryMB` in JavaScript or `cpu_count` and `memory_mb` in Python to `Sandbox.create()`. Leave them out to use the template's own `cpuCount` and `memoryMB`.

```typescript JavaScript & TypeScript theme={"theme":"css-variables"}
import { Sandbox } from 'e2b'

const sandbox = await Sandbox.create('my-template', {
  cpuCount: 4,
  memoryMB: 4096,
})
```

```python Python theme={"theme":"css-variables"}
from e2b import Sandbox

sandbox = Sandbox.create("my-template", cpu_count=4, memory_mb=4096)
```

On a template that isn't resizable, only its own `cpuCount` and `memoryMB` are accepted.

## Choose the size when you resume or reboot

When you [connect](https://docs.e2b.dev/sandbox/connect.md) to a paused sandbox, pass an `onResume` object in JavaScript or an `on_resume` dict in Python. It sets how the sandbox comes back and the size it comes back with:

* `action`: `'restore'` (the default) restores the memory snapshot; `'reboot'` reboots the sandbox from disk, like [reboot on resume](https://docs.e2b.dev/sandbox/resume-without-memory.md).
* `memoryMB`: memory in MiB, from the size the sandbox was paused with up to the template's `maxMemoryMB`, in steps of 128 MiB. A sandbox's memory can only grow, so a size below the paused size is refused.
* `cpuCount`: from `1` to the template's `maxCpuCount`, up or down from the count it was paused with.

```typescript JavaScript & TypeScript theme={"theme":"css-variables"}
import { Sandbox } from 'e2b'

// Restore the memory snapshot with more memory and CPUs
const sandbox = await Sandbox.connect(sandboxId, {
  onResume: { memoryMB: 6144, cpuCount: 8 },
})

// Reboot from disk with more memory
const rebooted = await Sandbox.connect(sandboxId, {
  onResume: { action: 'reboot', memoryMB: 8192 },
})
```

```python Python theme={"theme":"css-variables"}
from e2b import Sandbox

# Restore the memory snapshot with more memory and CPUs
sandbox = Sandbox.connect(sandbox_id, on_resume={"memory_mb": 6144, "cpu_count": 8})

# Reboot from disk with more memory
rebooted = Sandbox.connect(
    sandbox_id, on_resume={"action": "reboot", "memory_mb": 8192}
)
```

> [!WARNING]
>
> The sizes apply only when the call resumes a paused sandbox. If the sandbox is already running, or another request is already resuming it (for example [auto-resume](https://docs.e2b.dev/sandbox/auto-resume.md) on inbound traffic), it keeps its size and the values you passed are ignored, with no error. Read the size back to see what the sandbox runs with.

## What changes inside the sandbox

### CPUs

The change is asynchronous. The sandbox brings CPUs online, or takes them offline, one at a time as it resumes, and `connect()` can return before that has finished. Until it has, `nproc` inside the sandbox can show a count in between. `getInfo()` in JavaScript or `get_info()` in Python reports the count you asked for from the start, not the count that is online at that moment.

If your code depends on the CPU count, for example to size a worker pool, check it inside the sandbox before you use it:

```typescript JavaScript & TypeScript theme={"theme":"css-variables"}
import { Sandbox } from 'e2b'

const sandbox = await Sandbox.connect(sandboxId, { onResume: { cpuCount: 8 } })

// Poll until the sandbox shows the CPUs it was asked for.
// The time limit is your own choice; the change has no fixed duration.
const deadline = Date.now() + timeoutMs
while (Date.now() < deadline) {
  const { stdout } = await sandbox.commands.run('nproc')
  if (stdout.trim() === '8') break
  await new Promise((resolve) => setTimeout(resolve, 100))
}
```

```python Python theme={"theme":"css-variables"}
import time

from e2b import Sandbox

sandbox = Sandbox.connect(sandbox_id, on_resume={"cpu_count": 8})

# Poll until the sandbox shows the CPUs it was asked for.
# The time limit is your own choice; the change has no fixed duration.
deadline = time.monotonic() + timeout_seconds
while time.monotonic() < deadline:
    if sandbox.commands.run("nproc").stdout.strip() == "8":
        break
    time.sleep(0.1)
```

When you lower the count, the highest-numbered CPUs are the ones removed: a sandbox resumed with 2 CPUs has CPUs 0 and 1.

> [!WARNING]
>
> A process pinned to specific CPUs, with `taskset` for example, loses that pin when its CPUs are removed: Linux moves it to the CPUs that remain, and it keeps running. If you rely on pinning, pin the process again after you raise the count.

### Memory

Adding memory is asynchronous too. The sandbox adds it as it starts, and `create()` or `connect()` can return before all of it is in place. Processes can use the memory as it arrives. `getInfo()` in JavaScript or `get_info()` in Python reports the size you asked for from the start; to see what the sandbox has at that moment, read it inside the sandbox, with `free -m` for example.

> [!NOTE]
>
> Memory growth needs a recent version of the agent that runs inside the sandbox. If a template built a while ago refuses a larger `memoryMB`, rebuild the template.

## Snapshots

A [snapshot](https://docs.e2b.dev/sandbox/snapshots.md) keeps the size of the sandbox it was taken from, and so does a [filesystem-only snapshot](https://docs.e2b.dev/sandbox/filesystem-only-snapshots.md). A sandbox created from one starts with that size unless you choose another. It can grow up to the template's maximums, and it can't have less memory than the snapshot has.

## Read the sizes back

`getInfo()` in JavaScript or `get_info()` in Python on a sandbox returns the CPUs and memory it runs with. For a template, `Template.list()`, `Template.getInfo()` and `Template.listBuilds()` in JavaScript or `Template.list()`, `Template.get_info()` and `Template.list_builds()` in Python return the maximums next to the template's CPUs and memory; both maximums are unset for a template that isn't resizable.

```typescript JavaScript & TypeScript theme={"theme":"css-variables"}
const info = await sandbox.getInfo()
console.log(info.cpuCount, info.memoryMB)

const template = await Template.getInfo('my-template')
console.log(template.defaultBuild?.maxCpuCount, template.defaultBuild?.maxMemoryMB)
```

```python Python theme={"theme":"css-variables"}
info = sandbox.get_info()
print(info.cpu_count, info.memory_mb)

template = Template.get_info("my-template")
build = template.default_build
if build:
    print(build.max_cpu_count, build.max_memory_mb)
```

`e2b template list` shows the maximums in its `MAX VCPUS` and `MAX RAM MIB` columns.

## Billing

A sandbox is billed based on the size you request for it. The CPUs and memory chosen at a create, resume or reboot apply until the sandbox next pauses or stops. See [Billing](https://docs.e2b.dev/billing.md#usage-based-pricing).

## Related

* [Build](https://docs.e2b.dev/template/build.md) — build options for a template.
* [Connect to a sandbox](https://docs.e2b.dev/sandbox/connect.md) — resume a paused sandbox explicitly.
* [Reboot on resume](https://docs.e2b.dev/sandbox/resume-without-memory.md) — resume from disk alone.
* [Sandbox persistence](https://docs.e2b.dev/sandbox/persistence.md) — pause and resume.
