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

# Run programs in the sandbox

> Run programs and keep files in a sandbox for each session: what it can reach, how to set its image and installs, and which files outlast it.

Each session gets its own sandbox: a machine with its own disk, mounted at `/workspace`. It can reach the internet but can't see your agent's environment variables, so API keys stay with your tools. What it sends counts toward your organization's monthly [network egress](/docs/operate/usage.md#network-egress). A tool runs a program there through `ctx.getSandbox()`:

```ts agent/tools/run_report.ts theme={"theme":{"light":"github-light","dark":"github-dark"}}
import { defineTool } from "eve/tools";
import { z } from "zod";

export default defineTool({
  description: "Run the monthly report script and return its output.",
  inputSchema: z.object({ month: z.string() }),
  async execute({ month }, ctx) {
    const sandbox = await ctx.getSandbox();
    const result = await sandbox.run({ command: `python3 report.py ${month}` });
    if (result.exitCode !== 0) throw new Error(result.stderr);
    return result.stdout;
  },
});
```

`run()` doesn't throw when the command fails, so check `exitCode`.

## Set it up

`agent/sandbox/sandbox.ts` sets up the sandbox. The same definition runs in Docker under `pnpm dev` and on Jaccard when deployed. The generated file, shortened:

```ts agent/sandbox/sandbox.ts theme={"theme":{"light":"github-light","dark":"github-dark"}}
const IMAGE = "buildpack-deps:trixie-curl";

async function prepare(sandbox: SandboxSession): Promise<void> {
  await step(sandbox, "curl -LsSf https://astral.sh/uv/install.sh | sh");
}

export const environment = defaultEnvironment({
  k8s: { image: IMAGE },
  docker: { image: IMAGE },
  prepare,
});
```

* **System packages go in the image.** Change `IMAGE` to an image that has them.
* **`prepare` runs once per session**, the first time it opens its sandbox. Install into `$HOME` there, as the uv line does.
* **Files under `agent/sandbox/workspace/`** are copied into `/workspace` before `prepare` runs.

## What lasts

A sandbox stops after 10 minutes without a command, or when its session ends. A command that runs longer keeps it running. Files in `/workspace` and `$HOME` stay, and the next command starts the sandbox again over them, which takes a few seconds. Anything else is gone: background processes, and packages installed with `apt-get`.

A stopped sandbox keeps its disk, so you can still [copy its files](/docs/operate/sandbox-files.md). Kept disks count against your organization's [sandbox quota](/docs/operate/usage.md). The agent's **Sandboxes** tab in the [console](/docs/operate/console.md) shows when each one was last used and when it expires.

> [!WARNING]
>
> A sandbox and its disk are deleted 30 days after it was last used. Deleting an agent deletes its sandboxes at once. Copy out any files you need first.

## Make room at your sandbox limits

Your organization has two sandbox [quotas](/docs/operate/usage.md): how many sandboxes it keeps, running or stopped, and how many run at once. A session that needs a sandbox past either one makes room instead of failing:

* **Past the running limit**, your least recently used running sandbox is stopped. Its disk stays.
* **Past the total limit**, your least recently used sandbox is deleted with its disk, a stopped one before a running one.

> [!WARNING]
>
> At the total limit, a new session deletes your least recently used sandbox and its files. Copy out what you need from old sandboxes, or ask [support@jaccard.ai](mailto:support@jaccard.ai) to raise the limit.


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