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

# Try several approaches from one state

> Set a sandbox up once, fork it, let each copy try something different, keep the winner.

**You want** an agent to explore: get a project to an interesting point once (dependencies installed, data loaded, a server running), then try three fixes or three prompts from exactly that point, and keep the one that works.

**Why fork fits.** [Fork](/docs/concepts/lifecycle.md#fork) copies a running sandbox's memory, files and running processes into new sandboxes. Each copy starts where the original was, so you pay for the setup once, not once per attempt. The original is frozen for about 0.1 s and then carries on.

**1. Do the expensive part once**

Create one sandbox and bring it to the state every attempt should start from. Anything you do here is in every copy, including processes still running.

**2. Fork and try one thing in each copy**

```python best_of_n.py
from anona_compute import Sandbox

# Stand-ins for what your agent would try: three ways to do the same job.
attempts = ["sort -n", "sort -rn", "sort -un"]

with Sandbox.create("base", timeout=600) as sbx:
    sbx.files.write("/home/user/data.txt", "3\n1\n2\n3\n")  # the shared starting point

    forks = sbx.fork(count=len(attempts), timeout=300)
    results = {}
    for attempt, f in zip(attempts, forks):
        if isinstance(f, Exception):  # a copy that did not fit, e.g. your plan's limit
            print(attempt, "not forked:", f)
            continue
        try:
            out = f.commands.run(f"{attempt} /home/user/data.txt").stdout
            results[attempt] = out.split()
        finally:
            f.kill()

best = min(results, key=lambda a: len(results[a]))  # your own scoring goes here
print("kept:", best, results[best])
```

Entries of the result are in request order. A copy that could not be made is an exception in its place, not a failed call: `429` when your plan's limit of sandboxes at once is reached, `503` when the platform is at capacity.

**3. Keep the winner, delete the rest**

Each fork is an ordinary sandbox, billed while it runs. Delete the losers as soon as they are scored, and keep working in the winner, or in the original, which still has its own ID and deadline.

> [!WARNING]
>
> Every fork **request** keeps a checkpoint of the source sandbox that cannot be deleted yet, capped at 32 per team. Fork at the few points worth branching from, with `count` set to the number of copies you need, not once per step. A fork of ten costs one checkpoint; ten forks of one cost ten.

From the CLI, the same branching prints one sandbox ID per line:

```bash fork_cli.sh
SANDBOX_ID=$(anona-compute create -t base --json | jq -r .sandbox_id)
anona-compute exec "$SANDBOX_ID" -- sh -c 'echo ready > /home/user/state.txt'
anona-compute fork "$SANDBOX_ID" --count 2 --timeout 5m > forks.txt
while read -r id; do anona-compute exec "$id" -- cat /home/user/state.txt; anona-compute delete "$id"; done < forks.txt
anona-compute delete "$SANDBOX_ID"
```

**Next:** [Fork](/docs/concepts/lifecycle.md#fork) for scopes and errors. Named snapshots you can keep and start from later are **coming soon**.

**Runnable example:** [`examples/python/fork_best_of_n.py`](https://github.com/anonalabs/anona-compute-sdk/blob/main/examples/python/fork_best_of_n.py) in the public SDK repository. It prints what happens at each step.


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