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

# Best practices

> How to use Fibre well in production: one sandbox per unit of work, reused through sleep, with lifetimes, labels, safe retries, snapshots for fast starts, secrets by name, and nothing left running by accident.

Fibre sandboxes are cheap to start and cost nothing for compute while they sleep, so the right patterns are a little different
from the ones you may know from servers or containers. This page collects the practices that make an app on Fibre
fast, cheap and safe. Each one links to the page that shows it working.

## 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](https://docs.fibre.dev/use-cases/sandbox-per-user.md).

## 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.

See [sleep and wake](https://docs.fibre.dev/sandboxes/sleep-and-wake.md).

## 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 a `finally`
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](https://docs.fibre.dev/production/usage.md) 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 an **`Idempotency-Key`** header when you create a sandbox, so
that sending the same request twice creates only one sandbox. See
[errors, retries and limits](https://docs.fibre.dev/production/errors-and-limits.md).

## 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](https://docs.fibre.dev/setup/snapshots.md)**. New sandboxes start from the
snapshot already prepared, with its memory and running programs. For a prepared filesystem without memory, build an
[image](https://docs.fibre.dev/setup/images.md) 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 with `keep` so they carry on across
sleep and wake. See [run commands](https://docs.fibre.dev/sandboxes/running-commands.md).

## Keep secrets out of code and out of reach

Store API keys and passwords as [secrets](https://docs.fibre.dev/production/secrets.md) and give them **by name to the one command** that
needs them. Never print a secret, never put one in `env_vars`, and never put your account's API key in a sandbox:
give code inside a sandbox a [key for that one sandbox](https://docs.fibre.dev/get-started/api-keys.md#a-key-for-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](https://docs.fibre.dev/production/network.md) 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](https://docs.fibre.dev/sandboxes/sleep-and-wake.md#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 handle `sandbox_limit` by waiting for earlier
tasks to finish and trying again. See [parallel work](https://docs.fibre.dev/use-cases/parallel.md) and
[errors, retries and limits](https://docs.fibre.dev/production/errors-and-limits.md).

## Keep results somewhere that outlives the sandbox

A sandbox is a place to do work, not to store it. Copy out the results you need, push code to its repository, or
write output to your own storage before the sandbox is deleted. See
[keep work for longer](https://docs.fibre.dev/sandboxes/sleep-and-wake.md#keep-work-for-longer).


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