Open History to see the last commit
1---
2id: overview
3order: 1
4title: Sandboxes Overview
5description: "Run a coding agent (Grok Build, Claude Code, Codex, OpenCode) inside an isolated sandbox with a real filesystem and a cloned repo, and stream its work back through chat()."
6---
7
8A sandbox gives the agent a filesystem and a shell. `chat()` alone can only talk about code.
9
10A sandbox gives the agent a real computer to work in: a cloned repo, a shell, and processes. The agent runs inside it, and its edits, commands, and tool calls stream back to you like any other `chat()` run.
11
12## Try it
13
14This runs a Grok Build agent in a Docker container. You need Docker running and an `XAI_API_KEY`.
15
161. Install the packages:
17
18<!-- ::start:tabs variant="package-manager" mode="install" -->
19
20react: @tanstack/ai @tanstack/ai-grok-build @tanstack/ai-sandbox @tanstack/ai-sandbox-docker
21vue: @tanstack/ai @tanstack/ai-grok-build @tanstack/ai-sandbox @tanstack/ai-sandbox-docker
22solid: @tanstack/ai @tanstack/ai-grok-build @tanstack/ai-sandbox @tanstack/ai-sandbox-docker
23svelte: @tanstack/ai @tanstack/ai-grok-build @tanstack/ai-sandbox @tanstack/ai-sandbox-docker
24preact: @tanstack/ai @tanstack/ai-grok-build @tanstack/ai-sandbox @tanstack/ai-sandbox-docker
25angular: @tanstack/ai @tanstack/ai-grok-build @tanstack/ai-sandbox @tanstack/ai-sandbox-docker
26vanilla: @tanstack/ai @tanstack/ai-grok-build @tanstack/ai-sandbox @tanstack/ai-sandbox-docker
27octane: @tanstack/ai @tanstack/ai-grok-build @tanstack/ai-sandbox @tanstack/ai-sandbox-docker
28
29<!-- ::end:tabs -->
30
312. Save this as `agent.mts`:
32
33```ts
34import { chat } from '@tanstack/ai'
35import { GROK_CLI_INSTALL_COMMAND, grokBuildText } from '@tanstack/ai-grok-build'
36import {
37 createSecrets,
38 defineSandbox,
39 defineWorkspace,
40 githubRepo,
41 withSandbox,
42} from '@tanstack/ai-sandbox'
43import { dockerSandbox } from '@tanstack/ai-sandbox-docker'
44
45const repoSandbox = defineSandbox({
46 id: 'repo-agent',
47 provider: dockerSandbox({ image: 'node:22' }),
48 workspace: defineWorkspace({
49 source: githubRepo({ repo: 'TanStack/ai' }),
50 setup: [GROK_CLI_INSTALL_COMMAND],
51 secrets: createSecrets({
52 XAI_API_KEY: process.env.XAI_API_KEY ?? '',
53 }),
54 }),
55})
56
57const stream = chat({
58 threadId: 'overview-demo',
59 adapter: grokBuildText('grok-build'),
60 messages: [{ role: 'user', content: 'List the packages in this repo.' }],
61 middleware: [withSandbox(repoSandbox)],
62})
63
64for await (const chunk of stream) {
65 if (chunk.type === 'TEXT_MESSAGE_CONTENT') process.stdout.write(chunk.delta)
66}
67```
68
693. Run it with `npx tsx agent.mts`.
70
71The agent clones the repo into a container, reads it, and streams its answer to your terminal. Your host filesystem stays untouched.
72
73For a full walkthrough that fixes a bug and prints the diff, go to [Quick Start](./quick-start).
74
75## How it works
76
77A sandboxed run has three parts. You can change one without changing the others.
78
79| Part | What it decides | You pick it with |
80| --- | --- | --- |
81| **Provider** | Where the agent runs: your host, a container, a microVM, or a cloud VM. | A provider such as `dockerSandbox`, `sbxSandbox`, or `localProcessSandbox`. See [Providers](./providers). |
82| **Workspace** | What the agent sees: the repo, the package manager, the setup commands, and the secrets. | `defineWorkspace({ ... })`. See [Workspace](./workspace). |
83| **Harness adapter** | Which agent runs, and how its output turns into chat chunks. | `grokBuildText`, `claudeCodeText`, `codexText`, `opencodeText`, or `acpCompatible`. See [Harnesses](./harnesses). |
84
85`defineSandbox()` binds a provider and a workspace into one reusable definition. You can also add a policy, a lifecycle, and hooks. `withSandbox(definition)` is the `chat()` middleware that turns the sandbox on for a run.
86
87A run goes through three steps:
88
89```txt
90chat({ adapter: grokBuildText(), middleware: [withSandbox(repoSandbox)] })
91 |
92 |- withSandbox.setup get a sandbox: resume it, or restore a snapshot, or create and bootstrap
93 |- adapter.chatStream start `grok` INSIDE the sandbox, stream its events back as AG-UI chunks
94 '- withSandbox.onFinish snapshot or destroy, as the lifecycle says
95```
96
97Every harness adapter declares `requires: [SandboxCapability]`. If no middleware gives it a sandbox, `chat()` fails at the call site. A coding agent never runs without a place to run.
98
99The provider decides where the agent runs, not how it signs in. The default `authMode` is `'api-key'`. Set `'host'` when the machine already has a CLI login. See [Harness Auth](./auth).
100
101## When to use a sandbox
102
103Use a sandbox when the agent must act on a real codebase:
104
105- **Issue triage and bug-fix bots.** Clone the repo, reproduce the bug, find the cause, and post the findings or a draft fix.
106- **PR review.** Check out a branch, run the test and lint scripts, and comment on the results.
107- **Build and preview.** Let the agent scaffold or change an app, start the dev server, and give the user a live preview URL. See [Cloudflare](./cloudflare) and the `examples/sandbox-*` apps.
108- **Evals and benchmarks.** Run an agent against a fixture repo with a known bug, then make sure the diff is correct.
109- **Coding copilots** that must run code, edit files, and run commands.
110
111If the model only needs to read code that you already have in memory, you do not need a sandbox. A normal `chat()` with [tools](../tools/server-tools) is enough.
112
113## Pick the next piece
114
115Start here:
116
117- [Quick Start](./quick-start): fix a bug in a sandbox on your laptop.
118- [Providers](./providers): pick where the agent runs.
119- [Harnesses](./harnesses): pick which agent runs.
120- [Harness Auth](./auth): host CLI login or an API key.
121- [Workspace](./workspace): the repo, the clone depth, and the setup commands.
122
123Add these when you need them:
124
125- [Tools](./tools): give the agent your own app tools.
126- [Policy](./policy): allow, ask, or deny what the agent runs.
127- [Lifecycle & Snapshots](./lifecycle): reuse a sandbox and skip setup on the next run.
128- [Events](./events): stream file edits and tool calls to a UI.
129
130For production:
131
132- [Portable Snapshots](./portable-snapshots): keep the finished files after the sandbox is gone. Start with [Keep Files After Reload](./portable-snapshots-configure).
133- [Instance Durability](./durability): find the same sandbox from a different server.
134- [Durable Runs](./durable-runs): let a run continue after the user closes the tab.
135
136The **Sandboxes (Advanced)** group in the sidebar has the rest: the run journal, takeover, reaping, provisioning, observability, Cloudflare, and the provider details.
137
138## Example apps
139
140- [`examples/sandbox-web`](https://github.com/TanStack/ai/tree/main/examples/sandbox-web): a "build me an app" agent on Docker with durable runs. It scaffolds an app, starts the dev server, and gives back a live preview URL. The run continues after a refresh or a closed tab, and Stop cancels the agent.
141- [`examples/sandbox-cloudflare`](https://github.com/TanStack/ai/tree/main/examples/sandbox-cloudflare): the same idea at the edge. You pick the harness for each run in the UI.
142- [`examples/ts-react-chat`](https://github.com/TanStack/ai/tree/main/examples/ts-react-chat) at `/repo-report`: clone `TanStack/ai`, pick the harness and the auth mode (`host` or `api-key`), and read a typed report from `useChat().final`. See [Harness Agents](../structured-outputs/harnesses).