Skip to content

Your first coding agent

In this tutorial, a coding agent explains one of your repositories, working on a private clone of it in a throw-away sandbox. The agent here is Claude Code; every other agent sbx knows follows the same steps with its own name and key. If you have not run a sandbox yet, start with the quickstart, which needs no AI account.

Before you start

  • It costs money. The agent calls the API of its provider, billed to your API key or counted against your subscription, every run.
  • The output varies. A language model writes it: two runs give two different explanations.
  • Use a repository you may share with the provider. The agent reads the files of the clone and sends what it needs to its service. A demo repository, or a copy of yours, is the safe first choice.

Using Claude Code?

Install the fastsandbox plugin and run /fastsandbox:new-project: Claude sets the project up with you, tests included.

1. Install Docker Sandboxes

fastsandbox runs nothing by itself: it connects to the socket of sandboxd, the daemon of Docker Sandboxes, and asks it for sandboxes. Docker Sandboxes must be installed on your machine, with its daemon running for your user. Install it with the Docker documentation, then check that it answers:

sbx version

You also need:

  • Python 3.12 or newer, and uv or pip; uv python install 3.14 installs one.
  • A Claude subscription or an Anthropic API key, for Claude Code.
  • A Git repository for the agent to work on: the task clones it, and refuses a directory that is not one.
  • Internet access: the first run pulls the image of the claude kit, and the agent reaches its service through the proxy of its sandbox.

2. Install fastsandbox

The fastsandbox command runs the task. Install it as a tool, so that the repository of the agent stays as it is:

uv tool install fastsandbox

Or run it without installing anything, as uvx fastsandbox ... in place of fastsandbox ... below.

pip install fastsandbox

In an environment of your own, active in the shell that runs the commands below.

Then check that the package reaches the daemon:

fastsandbox info

It prints the socket it found, the version of sbx, and the API version of the daemon beside the range this release supports, its maximum excluded, as in (supported >=0.52.0, <0.53.0). An error here means Docker Sandboxes is not installed or its daemon is not running for your user. A daemon whose API version fastsandbox was not checked against still exits 0, with a warning on standard error, sandboxd speaks API …, which this release of fastsandbox was not audited on: check that api is in the range, since outside it connect() and every task raise UnsupportedVersionError. See Troubleshooting.

3. Write the agent task

Create agent.py at the root of the repository the agent should work on:

agent.py
from pathlib import Path
from typing import Annotated

from fastsandbox import Context, FastSandbox, WorkspaceDir
from fastsandbox.exceptions import TaskError

app = FastSandbox("quickstart")


@app.agent("claude", timeout="15m")
async def explain(ctx: Context, repo: Annotated[Path, WorkspaceDir(clone=True)]) -> str:
    """Has Claude Code explain a project, working on a private clone of it."""
    workdir = str(repo.resolve())
    run = await ctx.agent.prompt(
        "Explain what this project does and how its code is laid out, "
        "in twenty lines at most, and write the explanation to EXPLAINED.md.",
        working_dir=workdir,
    )
    if not run.ok:
        raise TaskError(f"Claude Code exited with {run.exit_code}: {run.stderr.strip()}")
    # The file is in the clone, inside the sandbox: the host is untouched.
    return await ctx.sandbox.read_text(f"{workdir}/EXPLAINED.md")
  • Line 10: @app.agent("claude") declares a task whose sandbox runs Claude Code, with a deadline of fifteen minutes.
  • Line 11: the repo input is the directory you pass on the command line. WorkspaceDir(clone=True) gives the sandbox a private Git clone of it: the agent may change and commit anything, your repository stays as it is. The directory must be a Git repository.
  • Lines 14 to 18: ctx.agent.prompt(...) runs the agent once on the prompt, unattended, and waits for it to end. The agent does not ask before it edits a file or runs a command: the sandbox and the clone are what contain it.
  • Lines 19 and 20: an agent that fails, one that cannot sign in for instance, writes nothing; the task raises TaskError with what the agent said, rather than miss the file.
  • Line 22: the task reads the file the agent wrote, in the clone, and returns it.

4. Run it

From the root of the repository:

export ANTHROPIC_API_KEY=sk-ant-...   # not needed with a Claude subscription
fastsandbox run agent.py explain --repo .

With a Claude subscription, no API key is needed. Leave ANTHROPIC_API_KEY unset: Claude Code then uses the subscription Docker Sandboxes holds, once you have signed in with it under sbx (sbx run claude, then /login). Like a key, it stays with the proxy: the sandbox never holds it.

The explanation of your project is printed: twenty lines at most, worded differently at each run. Once the daemon holds the image of the claude kit, the run takes well under a minute on a small repository; the first run also waits for the image to be pulled. Your repository is unchanged: the agent worked on a clone inside the sandbox, and the clone and the file it wrote were removed with the sandbox when the run ended.

What just happened

shape: sequence_diagram
Y: You
F: fastsandbox
S: Sandbox
Y -> F: "fastsandbox run agent.py explain --repo ."
F -> F: validate the inputs
F -> F: "forge the claude kit,\nwithout the sbx CLI"
F -> S: "create from the claude kit, with a clone\nof the repository and a placeholder of the key"
F -> S: "give the API key to the proxy,\nfor this sandbox only"
F -> S: run Claude Code on the prompt
S -> F: "EXPLAINED.md" {style.stroke-dash: 3}
F -> S: "remove the sandbox,\nthe clone and the key"
F -> Y: the explanation {style.stroke-dash: 3}

The API key of Claude Code never enters the sandbox: it goes to the proxy of that sandbox alone, which writes it into the requests Claude Code sends to Anthropic, and the agent only sees a placeholder. The agent can still make calls billed to that key, as long as the sandbox lives. Not every agent works this way: the credentials page names the exceptions.

The run removes the sandbox when the task returns, raises, times out or is cancelled, one Ctrl-C included. It cannot when the process is killed, by kill too, or the machine stops: sbx ls then shows the sandbox, named quickstart-explain- and a random suffix, and sbx rm NAME removes it. See Sandbox lifecycle.

Where the code runs

The function explain runs on the host, in your process. Claude Code, and every command it runs, run in the sandbox, with its approvals turned off. The sandbox and the clone keep your machine and your repository out of its reach; the task declares no network rule, so it reaches what the global policy of the daemon allows, and the key the proxy writes for it stays usable while it runs. See the security model.

Try another agent

The task reads a file rather than the output of the agent, so it works with any agent: change "claude" to "codex", export its key, and run again: its kit is forged on the first run.

export OPENAI_API_KEY=sk-...
fastsandbox run agent.py explain --repo .

The agents and the keys each one reads are listed on the agents page.

Next

The pages of Coding agents take each decision of an agent task in turn; the security model says what the sandbox does and does not contain.

  • Let it change your code


    Workspace, clone or copy: how the agent gets the code, and how its work comes back, checked by your tests.

    Working with repositories

  • Decide what it can reach


    Network rules for its sandbox alone, on top of the global policy, and the hosts it reached, read before the run ends.

    Restrict network access

  • Watch it work


    Its events as they come, progress for the caller, and the sandbox kept to look inside.

    Stream agent events

  • Run several agents


    Two agents on the same issue, side by side, and which patch passes the tests.

    Run agents concurrently