Give each unit of work its own sandbox, and reuse it
Decide what one sandbox is for: one user of your app, one conversation with an agent, one task, one test run. Then keep using that sandbox for as long as that unit of work lasts, instead of creating a new one for each step. A sandbox that is not in use goes to sleep with its memory, files and running programs, and costs nothing for compute while it sleeps. The next call wakes it exactly where it was, with dependencies still installed and data still loaded. Creating a fresh sandbox each time throws all of that away and makes every step pay for setup again. To find a sandbox again later, give it labels that say what it belongs to, such as{"user": "42"}, and look it
up by label. See a sandbox per user.
Set an idle timeout that fits, and a lifetime if you want one
- Lifetime: a sandbox is kept until you delete it. Give it a lifetime only when it should delete itself, such as a sandbox per conversation that may be abandoned. A lifetime counts from when you set it, and you can extend or remove it.
- Idle timeout: how long a sandbox waits with nothing happening before it sleeps, 5 minutes unless you set it. Shorter saves money; longer keeps it awake through pauses. A sandbox serving a page to people can sleep after a minute: the next visitor wakes it.
Clean up, and check that you did
Delete a sandbox when its work is done: one you do not delete is kept, and its storage counts towards your bill. Run your clean-up in afinally
block (or with with in Python), so a failure in the middle does not leave sandboxes behind. Every so often, list
sandboxes by label and delete the ones whose work has ended. Usage shows what is running and
what it costs.
Make retries safe
Networks fail, and a request can succeed even though its answer never reaches you. The SDKs retry temporary failures for you. If you call the API yourself, send anIdempotency-Key header when you create a sandbox, so
that sending the same request twice creates only one sandbox. See
errors, retries and limits.
Start from a snapshot when setup takes time
If every sandbox needs the same slow preparation, such as cloning a repository, installing dependencies or loading a model, do it once in one sandbox and save it as a snapshot. New sandboxes start from the snapshot already prepared, with its memory and running programs. For a prepared filesystem without memory, build an image from a Dockerfile instead.Run long work in the background
A quick command can run and wait. Installs, test suites, builds and agents can take minutes: start them as background commands and follow their output as it is written. They have no time limit, keep the sandbox awake while they run, and you can reattach to them by ID from anywhere. Start servers withkeep so they carry on across
sleep and wake. See run commands.
Keep secrets out of code and out of reach
Store API keys and passwords as secrets and give them by name to the one command that needs them. Never print a secret, never put one inenv_vars, and never put your account’s API key in a sandbox:
give code inside a sandbox a key for that one sandbox instead.
Limit what untrusted code can reach
For code written by a model or uploaded by a user, limit the sandbox’s network to what the code needs, such as a package index, or block it entirely. Code that cannot reach anywhere cannot send anything anywhere, whatever it found.Reconnect after a wake
Programs keep running across a sleep, but the services they talk to do not wait for them. Use your database and HTTP clients’ reconnect or health-check options, so that the first call after a wake opens a fresh connection when the old one has been closed. See connections to other services.Fan out within your limits
Many sandboxes can run at the same time, which is what makes evals, test sharding and parallel agents fast. Your plan sets how many sandboxes you can keep, awake or asleep. When you start a large batch, delete each sandbox when its task is done, start the batch in waves or through a queue, and handlesandbox_limit by waiting for earlier
tasks to finish and trying again. See parallel work and
errors, retries and limits.