# How agents run

Source: https://agenta.ai/docs/self-host/agent-execution/how-agents-run

This page explains where agent code runs in a self-hosted Agenta deployment, and the pieces that
decide how a single run behaves. It is the model behind the configuration pages that follow.

## The runner service

Services does not run agent code. It sends each agent run to one runner service over an internal
URL and a shared token:

```text
Services  --(AGENTA_RUNNER_INTERNAL_URL, AGENTA_RUNNER_TOKEN)-->  Runner   run a turn
API       --(AGENTA_RUNNER_INTERNAL_URL, AGENTA_RUNNER_TOKEN)-->  Runner   Stop a turn, kill a session
```

The platform API uses the same two values for its own calls to the runner. It cancels a turn when a
user presses Stop, and it tears down a sandbox on the session `kill` verb. Both containers need
both values.

The runner owns sandbox execution. It receives a run request, starts a harness process inside a
sandbox, streams results back, and relays server-side tools. A deployment has one logical runner.
Its replicas all share the same provider configuration.

## Harness and sandbox

Two separate choices shape a run:

- **The harness** is the agent program that runs: Pi, Claude Code, or Codex.
- **The sandbox provider** is where that program runs: the local runner container, or a Daytona
  cloud sandbox.

The two are independent. One runner can serve local runs and Daytona runs at the same time, and
changing one does not change the other.

The sandbox providers are:

- **daytona** runs the harness in a Daytona cloud sandbox, isolated from the runner and from other
  runs. See [Run agents in a cloud sandbox (Daytona)](https://agenta.ai/docs/self-host/agent-execution/daytona.md).
- **local** runs the harness inside the runner container. It is the default. See
  [Run agents locally](https://agenta.ai/docs/self-host/agent-execution/run-agents-locally.md).

A deployment enables the providers it wants and picks a default with
[`AGENTA_RUNNER_ENABLED_SANDBOX_PROVIDERS`](https://agenta.ai/docs/self-host/configuration.md#sandbox-providers).

## Credentials

Two kinds of credential meet at the runner:

- **Model keys are per-run data.** Agenta resolves the model key for each run and passes it with
  the run request. It is not part of the runner's configuration.
- **Provider credentials are deployment infrastructure.** The runner's Daytona API key belongs to
  the deployment. It is configured once on the runner and never travels to a sandbox.

A run can also carry no managed key at all and authenticate the harness from a mounted personal
subscription. See [Use your own subscription](https://agenta.ai/docs/self-host/use-your-own-subscription.md).

### What the sandbox can see

A model key would normally arrive in the sandbox as an environment variable, and an MCP server's
key as a request header. Both would be readable by the agent running there, which matters because
an agent writes and runs its own code: a prompt injection that convinces it to print its
environment would print your keys.

On Daytona the runner closes that gap, and it does so by default. It stores each key as a Daytona
Secret restricted to the single hostname that key authenticates against, then puts a placeholder in
the sandbox. Daytona swaps the placeholder for the real value on requests to that host, so the
model call and the MCP call still work while the agent itself only ever holds a useless string. A
request to any other host carries the placeholder, which is what makes exfiltration fail.

This needs a Daytona API key that is allowed to manage Secrets, which is a different permission
from creating sandboxes. See
[Hiding API keys from the sandbox](https://agenta.ai/docs/self-host/configuration.md#hiding-api-keys-from-the-sandbox) for
the upgrade note and for how to switch it off if you would rather not grant that permission.

This works for a key the remote service reads over HTTPS, which covers the provider keys and the
MCP keys. It does not work for a key a provider SDK signs with locally, which today means the AWS
access keys behind Bedrock and the Google service account credentials behind Vertex AI: those
secrets never leave the sandbox, so there is no outbound request to substitute them into, and the
sandbox holds the real values.

The local sandbox is unaffected. The harness runs inside the runner container there, so its keys are
already inside your own deployment rather than a third party's.

## Persistence depends on the run

What a run keeps depends on what the run is:

| The run has | What it keeps |
|---|---|
| No session and no workflow artifact | Nothing. Its working directory is ephemeral. |
| A session | A session working directory, restored on the next turn. |
| A workflow artifact | An agent working directory tied to that artifact. |
| A resumable harness session | The harness transcript, so the session continues. |

Durable working directories are backed by the [store](https://agenta.ai/docs/self-host/infrastructure/architecture.md).
Only a run with no session and no workflow artifact is fully ephemeral.

The store is optional. A deployment that configures no `AGENTA_STORE_*` credentials runs agents
normally, with mounts disabled: every run gets an ephemeral working directory inside its sandbox,
and the table above no longer applies. See
[Enable the agent runner and durable store](https://agenta.ai/docs/self-host/upgrades/runner-and-store.md).

## Next

- [Sandbox isolation and security](https://agenta.ai/docs/self-host/agent-execution/sandbox-isolation-and-security.md) for the trust model.
- [Run agents in a cloud sandbox (Daytona)](https://agenta.ai/docs/self-host/agent-execution/daytona.md) to enable isolated cloud sandboxes.
- [Run agents locally](https://agenta.ai/docs/self-host/agent-execution/run-agents-locally.md) to configure local runs.
- [Runner and sandbox configuration](https://agenta.ai/docs/self-host/configuration.md#agent-runner) for every variable.
