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

# Sandboxes

> Run your agent's code in a sandbox whose files need no machine. A micro-VM attaches only while a command runs, and only those seconds bill.

A floo sandbox is a set of files plus a machine that comes and goes. Reading, writing and listing files is answered from the sandbox's workspace with no machine and no disk attached. A micro-VM boots on the sandbox's disk when a command runs and stops after it is idle. Compute bills per second, only while a machine is attached.

The Python SDK takes the same calls as Modal's `Sandbox`, so code written for Modal runs with one changed import.

## Create a sandbox

Install the SDK and set a floo API key. The SDK needs Python 3.10 or later.

```bash theme={null}
pip install floo-sandbox
export FLOO_API_KEY=<your floo API key>
```

```python theme={null}
import floo_sandbox as modal

app = modal.App.lookup("agent-demo", create_if_missing=True)
sb = modal.Sandbox.create(app=app, timeout=600)
fs = sb.filesystem

# File work: no machine exists yet.
fs.write_text("def add(a, b):\n    return a + b\n", "/work/calc.py")
fs.write_text("from calc import add\nassert add(2, 3) == 5\nprint('ok')\n", "/work/test_calc.py")
print([f.name for f in fs.list_files("/work")])

# A command: a machine attaches, runs it, and is released when idle.
test = sb.exec("python3", "test_calc.py", workdir="/work")
print(test.stdout.read(), test.wait())

sb.terminate()
```

`Sandbox.create` boots nothing. The first `exec` boots a machine and answers in under half a second.

[Sandbox performance and state](https://getfloo.com/docs/guides/sandbox-performance.md) has the measured times, p50 and p95, and what survives a stop or a pause.

## Which calls need a machine

| Call | Machine |
| - | - |
| `filesystem.read_text`, `read_bytes`, `write_text`, `write_bytes`, `list_files`, `make_directory`, `remove` | No |
| `exec` of `cat`, `ls`, `head`, `tail`, `wc -l`, `test`, `grep` for a literal word, `find` by name or type | No |
| Any other `exec` | Yes |
| Reading inside `node_modules`, `.git`, `.venv`, `site-packages`, a system directory, or a file over 8 MiB | Yes |

A machine stays attached for `keep_warm` seconds after its last command, 30 by default, then stops. File calls do not keep it attached. What the command changed on disk is in the workspace before the machine stops, so the next file call sees it.

Set the window per sandbox, from 0 to 86,400 seconds:

```python theme={null}
sb = modal.Sandbox.create(app=app, experimental_options={"keep_warm": 120})
```

## Keep processes across idle time

A persistent sandbox pauses with its memory and processes when it is idle, and runs again where it was on the next call.

```python theme={null}
sb = modal.Sandbox.create(app=app, _experimental_persistent=True)
sb.exec("sh", "-c", "python3 server.py &")
sb._experimental_pause()      # or let it pause by itself once idle
# Resumes in under a second; server.py is still running.
sb.exec("python3", "-c", "import urllib.request as u; print(u.urlopen('http://localhost:8000').status)")
```

A persistent sandbox lives until it is terminated, 24 hours at most.

## Size a sandbox

A sandbox is small, medium or large. One you do not size is small.

| Size | vCPUs | Memory | Disk |
| - | - | - | - |
| Small | 1 | 512 MiB | 4 GiB |
| Medium | 2 | 2 GiB | 8 GiB |
| Large | 4 | 4 GiB | 16 GiB |

`cpu` and `memory` take a number or a `(request, limit)` pair, as on Modal, and you get the smallest size that has both. `cpu=2` is a medium, and so is `memory=1024`. More than a large raises `InvalidError`.

```python theme={null}
sb = modal.Sandbox.create(app=app, cpu=2, memory=2048)  # medium
```

A quarter of a size's vCPUs is held for its machine, and it uses all of them when the host has them free. You pay for the CPU a machine uses, second by second, and never for less than what is held. The disk is part of the size and has no charge of its own. A sandbox you resize keeps the disk it was made with.

## Run an agent as a worker

A sandbox created with a command runs it from the start and finishes when it exits. Use one to run the agent loop itself on floo.

```python theme={null}
worker = modal.Sandbox.create(
    "python", "agent.py",
    app=app,
    timeout=None,
    experimental_options={"restart": "always"},
)
```

Inside a worker, `Sandbox.create` needs no API key and makes a child sandbox on the same host, 16 at once. A child takes `cpu` and `memory` as any sandbox does. A worker whose programs are idle pauses, and runs again on its next call or its stdin.

## Network and secrets

A sandbox reaches the public internet and nothing of floo's or another sandbox's. Narrow it with `block_network=True`, `outbound_cidr_allowlist=[...]` or `outbound_domain_allowlist=[...]`.

`modal.Secret.from_name("NAME")` gives a sandbox one of its app's secret variables. Your program never holds the value: it holds a placeholder, and floo puts the value in its place as a request leaves the sandbox.

The value is sent only as an `Authorization`, `X-Api-Key`, `Api-Key`, `X-Goog-Api-Key`, `X-Auth-Token` or `Private-Token` request header, alone or after one scheme word such as `Bearer`. A placeholder anywhere else (a URL, a body, another header) reaches the host unchanged, and that host answers 401.

## Pricing

| What | Rate |
| - | - |
| Machine attached | $0.045 per vCPU-hour plus $0.015 per GiB-hour, per second. CPU is what the machine used, and what is held for it at least |
| Small machine or worker, 0.25 vCPU held and 512 MiB | $0.019 per hour attached and idle, up to $0.053 per hour at a full vCPU |
| Medium, 0.5 vCPU held and 2 GiB | $0.053 per hour attached and idle, up to $0.12 per hour at 2 full vCPUs |
| Large, 1 vCPU held and 4 GiB | $0.105 per hour attached and idle, up to $0.24 per hour at 4 full vCPUs |
| Paused sandboxes and workspace files | The Storage rate |

File calls that need no machine bill no compute. Sandboxes bill to your credits on one **Sandboxes** line, at the same rates on every plan.

## Differences from Modal

* Sign in with `FLOO_API_KEY`, not a Modal token id and secret.
* An option of `Sandbox.create` that floo does not take is refused by name. It is never ignored.
* `exec` takes no PTY.
* A pause keeps processes as well as files.
* GPUs are not available.
* Tunnels (`encrypted_ports`) are not available yet.
* Sandboxes run in one US region.

## Upgrade the SDK

When a release changes how the SDK talks to floo, floo keeps serving the release before it. An SDK older than that is refused on every call, with a message that says to upgrade. Install the latest release the same way you installed the SDK, and run the program again.

## When floo updates a sandbox host

floo updates sandbox hosts in place, without ending your sandboxes. A sandbox keeps its id, files, env, secrets and time left. An update pauses the sandboxes on a host for a few seconds. A call made meanwhile waits, and the SDK makes it again by itself. If a host stays away for more than five minutes, the call raises `ResourceExhaustedError`.

What happens to what was running:

* A sandbox where a command is running, a sandbox with no disk, and a worker with its children run on where they were, commands included. Open network connections close, so reconnect.
* A sandbox that mounts volumes cannot run on. Its commands started with `exec` end, and your program is told why. Run them again.
* These end, each with its reason: a worker, a child, or a sandbox with no disk that mounts volumes; a child whose worker ended; and a sandbox that mounts volumes whose own command does not restart.
* A few updates change the machines sandboxes run on. A sandbox that would have run on then ends instead, unless it has a disk and its command restarts. That command runs again from the start and says so.

## Limits

* A sandbox's `timeout` is 300 seconds unless set, 24 hours at most. A worker alone may set `timeout=None`.
* When every machine on a host is busy, a call that needs one waits in line, up to 5 minutes, then raises `ResourceExhaustedError`. `Sandbox.create` waits the same way when a host holds all the sandboxes it can.
* An organization runs 32 sandboxes at once. Past that, `Sandbox.create` raises `ResourceExhaustedError` until one finishes or is terminated.
* The workspace holds 64 MiB of file content per sandbox. Larger content lives on the sandbox's disk and needs a machine to read.
* A sandbox's disk holds what its commands write, beside its image: 4 GiB for a small, 8 GiB for a medium and 16 GiB for a large. A write past that fails with "No space left on device".


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