# Run agents in a cloud sandbox (Daytona)

Source: https://agenta.ai/docs/self-host/agent-execution/daytona

Daytona runs each agent in an isolated cloud sandbox instead of the runner container. This page
sets it up on an existing deployment. For what the sandbox isolates, see
[Sandbox isolation and security](https://agenta.ai/docs/self-host/agent-execution/sandbox-isolation-and-security.md).

Every variable on this page is set on the `runner` service. Defaults and validation rules are in
the [runner reference](https://agenta.ai/docs/self-host/configuration.md#agent-runner). `AGENTA_API_URL` (step 5) is the
exception: set it in the env file that every container loads.

## Before you start

- A Daytona account and an API key that may create sandboxes **and** manage organization Secrets.
- A running deployment. See the [Quick start](https://agenta.ai/docs/self-host/quick-start.md).

## 1. Enable the provider

To serve both local and Daytona runs from one runner:

```bash
AGENTA_RUNNER_ENABLED_SANDBOX_PROVIDERS=local,daytona
AGENTA_RUNNER_DEFAULT_SANDBOX_PROVIDER=daytona
```

To run only Daytona, so no harness ever runs inside the runner container:

```bash
AGENTA_RUNNER_ENABLED_SANDBOX_PROVIDERS=daytona
AGENTA_RUNNER_DEFAULT_SANDBOX_PROVIDER=daytona
```

The default must be one of the enabled providers, or the runner fails to start.

## 2. Set the Daytona credentials

```bash
AGENTA_RUNNER_DAYTONA_API_KEY=<daytona-api-key>
AGENTA_RUNNER_DAYTONA_API_URL=https://app.daytona.io/api
AGENTA_RUNNER_DAYTONA_TARGET=eu
```

The API key is required whenever `daytona` is enabled. The URL and the target are optional.

:::warning The API key needs the Secrets permission
A Daytona run hides its model and MCP API keys from the sandbox by storing each one as a Daytona
Secret, and that is on by default. Managing Secrets is a separate permission from creating
sandboxes, so a key minted only for sandboxes fails every run that carries a model or MCP key.

Grant the key in `AGENTA_RUNNER_DAYTONA_API_KEY` permission to manage Secrets, or reissue it with
that permission. The runner never falls back to sending keys as plaintext on its own. To send them
as plain environment variables instead, set `AGENTA_RUNNER_DAYTONA_OPAQUE_SECRETS=off`. See
[Hiding API keys from the sandbox](https://agenta.ai/docs/self-host/configuration.md#hiding-api-keys-from-the-sandbox).
:::

For Helm:

```yaml
agentRunner:
  providers:
    enabled: [local, daytona]
    default: daytona
    daytona:
      apiKeySecretRef:
        name: agenta-runner-daytona
        key: api-key
      apiUrl: https://app.daytona.io/api
      target: eu
```

## 3. Build the sandbox snapshot

A [snapshot](https://www.daytona.io/docs/snapshots/) is a prebuilt sandbox image stored in your
Daytona account. Every sandbox the runner creates starts from one, so whatever the snapshot
contains is what your agents can run.

Agenta ships the build recipe rather than a prebuilt snapshot, so you build it in your own Daytona
account:

```bash
cd services/runner/images/sandbox/daytona
DAYTONA_API_KEY=<daytona-api-key> DAYTONA_TARGET=eu uv run build_snapshot.py
```

The recipe starts from the sandbox base image, which already contains Claude Code, Codex, and
OpenCode. It adds the Pi harness at the version the runner expects, the FUSE tooling that
mounts durable working directories inside the sandbox, and the everyday shell tools agents reach
for on their own: `python3` (also available as `python`), `unzip`, `zip`, `rg` (ripgrep), `fd`,
`jq`, `ps`, `file`, and `tree`. It builds a snapshot named `agenta-agent-sandbox-v8` and sets each
sandbox to 2 vCPU, 4 GB of memory, and 5 GB of disk.

The number at the end of the name is the recipe version. A release that changes the recipe
raises it, and the runner then asks for the new name. The runner ships with the harness code that
matches its own recipe version, so it must start sandboxes from a snapshot of that version.

:::warning Build the snapshot before you deploy the runner
After every upgrade, run the recipe from the new release to build the snapshot under the new name.
Do this before you deploy the new runner. A runner that starts from an older snapshot runs new
code against old harness adapters, and some agents fail or skip approval policies.
:::

To refresh a snapshot under the same name, for example after you edit the recipe yourself,
rebuild it with `--force`. Daytona keeps the snapshot you built, so a snapshot built from an older recipe
keeps its older toolset until you rebuild it. `--force` first builds a trial copy under a
temporary name, so a recipe that fails its checks leaves your working snapshot untouched. New
sandboxes cannot start while the real snapshot is rebuilt, so run it in a quiet window. If that
last step fails, the script prints the setting that points the runner at the trial copy.

The runner uses this snapshot by default, so you do not need to set anything. We recommend that
you leave `AGENTA_RUNNER_DAYTONA_SNAPSHOT` unset. The runner then always asks for the snapshot
version it was built for.

If you must pin the name, for example to point at a custom snapshot, set it:

```bash
AGENTA_RUNNER_DAYTONA_SNAPSHOT=agenta-agent-sandbox-v8
```

For Helm:

```yaml
agentRunner:
  providers:
    daytona:
      snapshot: agenta-agent-sandbox-v8
```

A pin overrides the runner's default. When you pin the name, you must update the pin on every
upgrade that raises the recipe version.

To give agents extra tools (a browser, the `gh` CLI, a language runtime) or different sandbox
resources, add them to the recipe. See
[Customize the agent runtime](https://agenta.ai/docs/self-host/agent-execution/customize-the-agent-runtime.md).

:::info
`AGENTA_RUNNER_DAYTONA_SNAPSHOT` and `AGENTA_RUNNER_DAYTONA_IMAGE` are mutually exclusive. Set one
or the other. If you set neither, the runner starts sandboxes from `agenta-agent-sandbox-v8`.
:::

## 4. Make the store reachable from the sandbox

Agents keep files across turns in a working directory that lives in the
[durable store](https://agenta.ai/docs/self-host/configuration.md#store-durable-object-store). A Daytona sandbox mounts that
directory from inside the cloud, so it reaches the store over the public internet. If the store
endpoint is not publicly reachable, the sandbox starts without the mount, working directories are
ephemeral, and the runner logs a mount-degradation warning.

**This applies to you when** `AGENTA_STORE_ENDPOINT_URL` points at a host the internet cannot
resolve: a Compose service name such as `http://seaweedfs:8333` (the default for the bundled
store), `localhost`, or a private IP. If it already points at a public S3 endpoint, as on Railway
and on Kubernetes with public ingress, skip this step.

There are three ways to fix it. Most self-hosted deployments use the first.

**Expose the bundled store through traefik.** The `gh` Compose stacks can publish the bundled
store on its own subdomain. This is off by default. Pick a hostname such as `store.example.com`
and add a DNS record that points it at the same server as your main domain. Then set these
variables in your env file (`.env.oss.gh`, or `.env.oss.gh.ssl` on the `--ssl` stack):

```bash
AGENTA_STORE_TRAEFIK_ENABLE=true
AGENTA_STORE_DOMAIN=store.example.com
AGENTA_STORE_ENDPOINT_URL=https://store.example.com
```

Recreate the stack. On the `--ssl` stack, traefik requests a Let's Encrypt certificate for the
new hostname from the same resolver it uses for your main domain. On the plain `gh` stack, traefik
serves the store on port 80 and your own proxy terminates TLS. The router matches on the hostname
and never rewrites the path, which the S3 request signature requires, so do not place the store
behind a path prefix.

To verify, run an agent on Daytona (step 6) and write a file. The runner logs
`remote mounted ... (verified alive)` for the session instead of a `mount degraded` line, and the
file is still there on the next turn.

**Point the store at an external S3 endpoint.** Use an external S3-compatible bucket (AWS S3,
Cloudflare R2, MinIO) that is already reachable from the internet, and set the store endpoint to
its URL:

```bash
AGENTA_STORE_ENDPOINT_URL=https://<your-bucket-endpoint>
```

**Tunnel the bundled store (dev only).** The dev Compose stack ships an ngrok service that
publishes the bundled store on a public URL, which the runner discovers automatically. Create an
[ngrok authtoken](https://dashboard.ngrok.com/get-started/your-authtoken), put it in your env file,
and start the stack with the `with-tunnel` profile:

```bash
# in hosting/docker-compose/oss/.env.oss.dev
NGROK_AUTHTOKEN=<your-ngrok-authtoken>
```

```bash
bash ./hosting/docker-compose/run.sh --oss --dev
```

`run.sh` enables the `with-tunnel` profile by default (`--no-tunnel` turns it off). If you drive
Compose directly, pass `--profile with-tunnel`. Confirm the tunnel is up:

```bash
docker compose logs ngrok | grep "started tunnel"
```

## 5. Make the Agenta API reachable from the sandbox

With the LLM gateway on (the default), the agent in a Daytona sandbox calls its model through the
Agenta API at `AGENTA_API_URL`. The sandbox runs in the cloud, so that address must be one the
internet reaches.

**This applies to you when** `AGENTA_API_URL` is `http://localhost/api`, a private IP, or a name
that only your network resolves. A run on `localhost`, `127.0.0.1`, `::1` or
`host.docker.internal` stops before the sandbox starts.

Set the public address in your env file and recreate the containers:

```bash
AGENTA_API_URL=https://agenta.example.com/api
```

For Helm:

```yaml
agenta:
  apiUrl: "https://agenta.example.com/api"
```

To verify, call the address from a machine outside your network:

```bash
curl -sS -o /dev/null -w '%{http_code}\n' https://agenta.example.com/api/health
```

The answer is `200`. When the runner starts a sandbox, it calls the gateway address from inside
the sandbox. If no answer comes within 10 seconds, the run stops before the agent starts.

A proxy in front of the API must not cut a long model call. See
[Network requirements for agent runs](https://agenta.ai/docs/self-host/infrastructure/networking.md#network-requirements-for-agent-runs).

## 6. Smoke test

Recreate the runner, then run an agent with the Daytona sandbox selected. The runner logs its
resolved configuration on startup:

```text
runner providers enabled=[local,daytona] default=daytona
runner daytona target=eu artifact=snapshot:agenta-agent-sandbox-v8
```

The first run creates a sandbox in your Daytona account. Later turns reuse a warm sandbox until it
stops or is deleted. See
[warm sessions and scale](https://agenta.ai/docs/self-host/configuration.md#warm-sessions-and-scale).

:::info Mounted subscription logins are local-only
A personal Pi, Claude, or ChatGPT login authenticates a harness through a directory mounted into
the runner container. Agenta does not upload that login to a Daytona sandbox, so Daytona runs use a
model API key instead. See [Use your own subscription](https://agenta.ai/docs/self-host/use-your-own-subscription.md).

A Claude token pasted in `Settings > AI providers` works on Daytona. See
[Connect a Claude subscription](https://agenta.ai/docs/guides/connect-a-claude-subscription.md).
:::

## Troubleshooting

### A run fails with `Sandbox provider 'daytona' is not enabled on this deployment`

**Cause:** `daytona` is missing from `AGENTA_RUNNER_ENABLED_SANDBOX_PROVIDERS`.

**Solution:** Add it (step 1) and recreate the runner.

### The runner fails to start with `AGENTA_RUNNER_DAYTONA_API_KEY is required`

**Cause:** `daytona` is enabled without a key.

**Solution:** Set `AGENTA_RUNNER_DAYTONA_API_KEY` (step 2).

### The runner fails to start with `AGENTA_RUNNER_DAYTONA_SNAPSHOT and AGENTA_RUNNER_DAYTONA_IMAGE are mutually exclusive`

**Cause:** Both are set.

**Solution:** Set only one of them.

### The runner fails to start with a Daytona authentication error

**Cause:** The API key is wrong, or it has no access to the target.

**Solution:** Check `AGENTA_RUNNER_DAYTONA_API_KEY` and `AGENTA_RUNNER_DAYTONA_TARGET` against your
Daytona account.

### A run fails with `Daytona refused to manage Secrets with this API key`

**Cause:** The key in `AGENTA_RUNNER_DAYTONA_API_KEY` can create sandboxes but cannot manage
Secrets. Hiding model and MCP keys from the sandbox is on by default, and it stores each key as a
Daytona Secret. The failure happens at sandbox creation, so the run produces no output, and it hits
every run that carries a model or MCP key.

**Solution:** Grant that key permission to manage Secrets in your Daytona account, or reissue it
with that permission. The runner never falls back to sending keys as plaintext. If you would rather
not grant the permission, set `AGENTA_RUNNER_DAYTONA_OPAQUE_SECRETS=off` on the runner and recreate
it; keys then reach the sandbox as plain environment variables, where the agent can read them. See
[Hiding API keys from the sandbox](https://agenta.ai/docs/self-host/configuration.md#hiding-api-keys-from-the-sandbox).

### A run fails with `The sandbox cannot reach the Agenta API at <origin>`

**Cause:** The sandbox got no answer from `AGENTA_API_URL` within 10 seconds, or the address is
`localhost`, `127.0.0.1`, `::1` or `host.docker.internal`.

**Solution:** Set `AGENTA_API_URL` to a public address (step 5) and recreate the containers.

### Working directories do not persist across turns

**Cause:** The sandbox cannot reach the store, so it mounted nothing. The runner logs a
mount-degradation warning.

**Solution:** Make the store publicly reachable (step 4).

## Next

- [Customize the agent runtime](https://agenta.ai/docs/self-host/agent-execution/customize-the-agent-runtime.md) adds tools,
  folders, or resources to the sandbox.
- [Run agents locally](https://agenta.ai/docs/self-host/agent-execution/run-agents-locally.md) runs agents in the runner
  container instead.
