Open History to see the last commit
1---
2title: Quick Start
3id: quick-start
4order: 2
5description: "Run a Grok Build coding agent inside a Docker sandbox, let it fix a bug in a cloned repo, and print the diff. About 10 minutes."
6---
7
8This guide runs a Grok Build agent in Docker. The agent clones a repo, fixes a bug, and the script prints the `git diff`.
9
10You need:
11
12- Docker, running.
13- An `XAI_API_KEY`.
14- A public GitHub repo with a bug. The samples use `owner/buggy-app`. Replace it with your repo.
15
16## 1. 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
31- `@tanstack/ai`: the `chat()` pipeline.
32- `@tanstack/ai-grok-build`: the Grok Build harness adapter.
33- `@tanstack/ai-sandbox`: `defineSandbox`, `defineWorkspace`, and `withSandbox`.
34- `@tanstack/ai-sandbox-docker`: the Docker provider. It runs the agent in a container.
35
36## 2. Write the agent script
37
38Save this as `fix-bug.mts`:
39
40```ts
41import { chat } from '@tanstack/ai'
42import { GROK_CLI_INSTALL_COMMAND, grokBuildText } from '@tanstack/ai-grok-build'
43import {
44 createSecrets,
45 defineSandbox,
46 defineWorkspace,
47 githubRepo,
48 withSandbox,
49} from '@tanstack/ai-sandbox'
50import { dockerSandbox } from '@tanstack/ai-sandbox-docker'
51
52const repoSandbox = defineSandbox({
53 id: 'bug-fixer',
54 provider: dockerSandbox({ image: 'node:22' }),
55 workspace: defineWorkspace({
56 // Where the code comes from. A shallow clone by default.
57 source: githubRepo({ repo: 'owner/buggy-app' }),
58 packageManager: 'pnpm',
59 // These commands run once, when the sandbox is new.
60 setup: [GROK_CLI_INSTALL_COMMAND, 'corepack enable', 'pnpm install'],
61 // Put into the sandbox env. Never saved to snapshots, stores, or logs.
62 secrets: createSecrets({
63 XAI_API_KEY: process.env.XAI_API_KEY ?? '',
64 }),
65 }),
66 lifecycle: { reuse: 'thread', snapshot: 'after-setup', keepAlive: '30m' },
67})
68
69const stream = chat({
70 threadId: 'bug-fix-1',
71 adapter: grokBuildText('grok-build'),
72 messages: [
73 { role: 'user', content: 'The tests fail. Find the bug and fix it.' },
74 ],
75 middleware: [withSandbox(repoSandbox)],
76})
77
78for await (const chunk of stream) {
79 if (chunk.type === 'TEXT_MESSAGE_CONTENT') {
80 process.stdout.write(chunk.delta)
81 }
82 if (chunk.type === 'CUSTOM' && chunk.name === 'file.changed') {
83 console.log('\n--- diff ---\n')
84 console.log(chunk.value.diff)
85 }
86}
87```
88
89- `provider` is where the agent runs. Here it is a Docker container.
90- `workspace` is what the agent sees: the cloned repo, the Grok CLI, and the installed dependencies.
91- `lifecycle` keeps one sandbox per thread. It also takes a snapshot after setup, so only the first run pays for the clone and the install.
92- `threadId` names the sandbox. The next run with the same `threadId` uses the same container.
93
94## 3. Run it
95
96```bash
97XAI_API_KEY=your-key npx tsx fix-bug.mts
98```
99
100You see the agent's text as it works. When the run ends, you see the diff of every file it changed. The agent cloned the repo, found the bug, and edited the files, all inside the container.
101
102## How it works
103
104- `grokBuildText` declares that it needs a sandbox. `withSandbox(repoSandbox)` gives it one.
105- `withSandbox` resumes the sandbox for `threadId`, or creates it and runs `setup`.
106- The adapter starts `grok` inside the sandbox. Its events come back as normal `chat()` chunks.
107- When the run ends, the Grok Build adapter sends a `file.changed` event with the `git diff` of the working tree.
108
109To learn more about each piece:
110
111- [Workspace](./workspace): package manager detection, parallel setup steps, and clone depth.
112- [Events](./events): every event a harness run sends.
113- [Lifecycle & Snapshots](./lifecycle): reuse and snapshots.
114
115## No Docker? Run on your host
116
117The local-process provider runs the agent directly on your host. It gives no isolation, but it is the fastest loop for development.
118
119<!-- ::start:tabs variant="package-manager" mode="install" -->
120
121react: @tanstack/ai-sandbox-local-process
122vue: @tanstack/ai-sandbox-local-process
123solid: @tanstack/ai-sandbox-local-process
124svelte: @tanstack/ai-sandbox-local-process
125preact: @tanstack/ai-sandbox-local-process
126angular: @tanstack/ai-sandbox-local-process
127vanilla: @tanstack/ai-sandbox-local-process
128octane: @tanstack/ai-sandbox-local-process
129
130<!-- ::end:tabs -->
131
132This sample uses the `grok login` on your machine, so the `grok` CLI must be installed on your host.
133
134```ts
135import { chat } from '@tanstack/ai'
136import { grokBuildText } from '@tanstack/ai-grok-build'
137import {
138 defineSandbox,
139 defineWorkspace,
140 githubRepo,
141 withSandbox,
142} from '@tanstack/ai-sandbox'
143import { localProcessSandbox } from '@tanstack/ai-sandbox-local-process'
144
145const repoSandbox = defineSandbox({
146 id: 'bug-fixer',
147 provider: localProcessSandbox({
148 scrubEnv: ['XAI_API_KEY', 'GROK_API_KEY'],
149 }),
150 workspace: defineWorkspace({
151 source: githubRepo({ repo: 'owner/buggy-app' }),
152 setup: ['corepack enable', 'pnpm install'],
153 }),
154 lifecycle: { reuse: 'thread' },
155})
156
157const stream = chat({
158 adapter: grokBuildText('composer-2.5', { authMode: 'host' }),
159 messages: [
160 { role: 'user', content: 'The tests fail. Find the bug and fix it.' },
161 ],
162 threadId: 'thread-1',
163 middleware: [withSandbox(repoSandbox)],
164})
165
166for await (const chunk of stream) {
167 if (chunk.type === 'TEXT_MESSAGE_CONTENT') process.stdout.write(chunk.delta)
168}
169```
170
171- `authMode: 'host'` tells the adapter to use your `grok login`.
172- `scrubEnv` removes API keys from the env of each command. If those keys stay, they override the login.
173
174On a CI runner there is no browser login. There, set `authMode: 'api-key'` and pass `XAI_API_KEY` as a workspace secret. See [Harness Auth](./auth).
175
176## Want a microVM? Use Docker Sandboxes
177
178`sbxSandbox()` runs the agent in a hypervisor microVM, not in a container.
179
1801. Install Docker Sandboxes so that `sbx` is on your `PATH`:
181 - macOS: `brew trust docker/tap`, then `brew install docker/tap/sbx`.
182 - Windows: turn on HypervisorPlatform, then `winget install -h Docker.sbx`.
183 - Debian or Ubuntu: `curl -fsSL https://get.docker.com | sudo REPO_ONLY=1 sh`, then `apt-get install docker-sbx`. Add your user to the `kvm` group.
1842. Run `sbx login`.
1853. Change `repoSandbox` in `fix-bug.mts`:
186
187```ts
188import {
189 createSecrets,
190 defineSandbox,
191 defineWorkspace,
192 githubRepo,
193} from '@tanstack/ai-sandbox'
194import { sbxSandbox } from '@tanstack/ai-sandbox-docker'
195
196const repoSandbox = defineSandbox({
197 id: 'bug-fixer',
198 provider: sbxSandbox({
199 allowNetwork: ['*.npmjs.org', 'registry.npmjs.org'],
200 }),
201 workspace: defineWorkspace({
202 source: githubRepo({ repo: 'owner/buggy-app' }),
203 setup: ['pnpm install'],
204 secrets: createSecrets({
205 XAI_API_KEY: process.env.XAI_API_KEY ?? '',
206 }),
207 }),
208 lifecycle: { reuse: 'thread' },
209})
210```
211
212`allowNetwork` lets `pnpm install` reach the npm registry. A known adapter such as `grok-build` also adds its model API host.
213
214`sbxSandbox()` always clones a Git repo into the VM. It does not take snapshots. It resumes a sandbox by its name.
215
216## Run a full example app
217
218[`examples/sandbox-web`](https://github.com/TanStack/ai/tree/main/examples/sandbox-web) is a complete app. It is a "build me an app" agent (Claude Code on Docker) with runs that continue after a refresh. It scaffolds an app in the sandbox, starts the dev server, and streams back a live preview URL.
219
220[`examples/sandbox-cloudflare`](https://github.com/TanStack/ai/tree/main/examples/sandbox-cloudflare) runs a coding agent at the edge. You pick Claude Code, Codex, or Grok Build for each run in the UI.
221
222Next, pick one:
223
224- Give the agent your own server tools (database lookups, secrets): [Tools](./tools).
225- Limit what the agent can run: [Policy](./policy).
226- Watch each file the agent touches: [Events](./events).