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 sandbox, fix a bug in a cloned repo, and stream the diff, in minutes."
6---
7
8You have an app that already calls `chat()`, and you have an `XAI_API_KEY` (or
9you've logged in on grok.com). By the end of this guide a Grok Build agent will
10clone a repo into a Docker sandbox, fix a bug, and stream the resulting `git diff`
11back to you.
12
13If you only want concepts first, read the [Overview](./overview). Otherwise, start
14here.
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 core `chat()` pipeline.
32- `@tanstack/ai-grok-build`: the Grok Build **harness adapter**.
33- `@tanstack/ai-sandbox`: `defineSandbox`, `defineWorkspace`, `withSandbox`.
34- `@tanstack/ai-sandbox-docker`: the Docker **provider** that runs the agent in a
35 container.
36
37You'll also need Docker running locally, and the **`grok` CLI available in your
38sandbox image** (the Grok Build harness spawns it inside the sandbox). No Docker?
39See [the local-process alternative](#no-docker-run-on-your-host) below. For a
40microVM instead of a container, see [Docker Sandboxes](#docker-sandboxes-microvm-sbx).
41
42## 2. Define the sandbox
43
44A sandbox bundles three things: a **provider** (the isolation primitive; here
45Docker), a **workspace** (what the agent sees; here a cloned git repo plus a
46setup step), and a **lifecycle** (when to reuse, snapshot, and tear it down).
47
48```ts
49import {
50 createSecrets,
51 defineSandbox,
52 defineWorkspace,
53 githubRepo,
54} from '@tanstack/ai-sandbox'
55import { dockerSandbox } from '@tanstack/ai-sandbox-docker'
56
57export const repoSandbox = defineSandbox({
58 id: 'bug-fixer',
59 provider: dockerSandbox({ image: 'node:22' }),
60 workspace: defineWorkspace({
61 // Where the working tree comes from (shallow clone by default).
62 source: githubRepo({ repo: 'owner/buggy-app' }),
63 packageManager: 'pnpm',
64 // Commands that run once during bootstrap.
65 setup: ['corepack enable', 'pnpm install'],
66 // Injected into the sandbox env at create/resume, never persisted to
67 // snapshots, the sandbox store, or the event log.
68 secrets: createSecrets({
69 XAI_API_KEY: process.env.XAI_API_KEY ?? '',
70 }),
71 }),
72 lifecycle: { reuse: 'thread', snapshot: 'after-setup', keepAlive: '30m' },
73})
74```
75
76`snapshot: 'after-setup'` (the default when the provider supports snapshots) means
77the next run resumes from a post-`pnpm install` snapshot instead of re-cloning and
78re-installing, so only the first run pays the cold-start cost.
79
80For everything `defineWorkspace()` can describe, package manager auto-detection,
81parallel setup groups, clone depth, see [Workspace](./workspace).
82
83## 3. Call `chat()` with the harness adapter
84
85The Grok Build adapter declares that it `requires` a sandbox capability.
86`withSandbox(...)` is the middleware that **provides** it: it resumes-or-creates
87the sandbox, bootstraps the workspace, and tears it down per the lifecycle.
88
89```ts
90import { chat } from '@tanstack/ai'
91import { grokBuildText } from '@tanstack/ai-grok-build'
92import { withSandbox } from '@tanstack/ai-sandbox'
93import { messages, threadId } from './chat-context'
94import { repoSandbox } from './sandbox'
95
96const stream = chat({
97 threadId,
98 adapter: grokBuildText('grok-build'),
99 messages,
100 middleware: [withSandbox(repoSandbox)],
101})
102```
103
104Here `messages` is your conversation (e.g. a user turn asking the agent to fix the
105bug), and `threadId` keys the sandbox so the same thread reuses the same container.
106Spawning `grok` happens **inside** the sandbox; its events stream back as normal
107`chat()` chunks.
108
109## 4. Stream the result and read the diff
110
111Harness runs emit standard AG-UI chunks (text, tool calls, reasoning) plus a
112namespaced `CUSTOM` event. When the run finishes, the Grok Build adapter emits a
113`file.changed` event carrying the working-tree `git diff`:
114
115```ts
116import { stream } from './my-run'
117
118for await (const chunk of stream) {
119 if (chunk.type === 'TEXT_MESSAGE_CONTENT') {
120 process.stdout.write(chunk.delta)
121 }
122
123 if (chunk.type === 'CUSTOM' && chunk.name === 'file.changed') {
124 const value = chunk.value
125 if (value !== null && typeof value === 'object' && 'diff' in value) {
126 console.log('\n--- diff ---\n')
127 console.log(value.diff)
128 }
129 }
130}
131```
132
133That `diff` is point B: the agent cloned the repo, found the bug, edited the files,
134and you printed the change it made, all without the agent touching your host
135filesystem.
136
137## No Docker? Run on your host
138
139Swap the provider for the local-process one to skip Docker entirely. It runs the
140agent directly on your host (no isolation), which makes for the fastest dev loop:
141
142<!-- ::start:tabs variant="package-manager" mode="install" -->
143
144react: @tanstack/ai-sandbox-local-process
145vue: @tanstack/ai-sandbox-local-process
146solid: @tanstack/ai-sandbox-local-process
147svelte: @tanstack/ai-sandbox-local-process
148preact: @tanstack/ai-sandbox-local-process
149angular: @tanstack/ai-sandbox-local-process
150vanilla: @tanstack/ai-sandbox-local-process
151octane: @tanstack/ai-sandbox-local-process
152
153<!-- ::end:tabs -->
154
155```ts
156import { chat } from '@tanstack/ai'
157import { grokBuildText } from '@tanstack/ai-grok-build'
158import {
159 defineSandbox,
160 defineWorkspace,
161 githubRepo,
162 withSandbox,
163} from '@tanstack/ai-sandbox'
164import { localProcessSandbox } from '@tanstack/ai-sandbox-local-process'
165import { messages } from './chat-context'
166
167export const repoSandbox = defineSandbox({
168 id: 'bug-fixer',
169 provider: localProcessSandbox({
170 scrubEnv: ['XAI_API_KEY', 'GROK_API_KEY'],
171 }),
172 workspace: defineWorkspace({
173 source: githubRepo({ repo: 'owner/buggy-app' }),
174 setup: ['corepack enable', 'pnpm install'],
175 }),
176 lifecycle: { reuse: 'thread' },
177})
178
179const stream = chat({
180 adapter: grokBuildText('composer-2.5', { authMode: 'host' }),
181 messages,
182 middleware: [withSandbox(repoSandbox)],
183})
184```
185
186Set `authMode: 'host'` so the adapter uses `grok login`. `scrubEnv` removes
187keys the host process inherited. Those keys would override the login.
188
189A local-process run can also be a CI runner. That machine has no browser
190login. Set `authMode: 'api-key'`. Then inject `XAI_API_KEY` as a workspace
191secret. The sandbox type does not pick this. See [Harness Auth](./auth).
192
193## Docker Sandboxes microVM (sbx)
194
195If you want a hypervisor microVM, use `sbxSandbox()`. This is not a Docker container.
196
1971. Install Docker Sandboxes so `sbx` is on `PATH`.
198 - macOS: `brew trust docker/tap`, then `brew install docker/tap/sbx`
199 - Windows: enable HypervisorPlatform, then `winget install -h Docker.sbx`
200 - 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.
2012. Run `sbx login`.
2023. Swap the provider. Pass `allowNetwork` so `pnpm install` can reach the npm registry. A known adapter such as `grok-build` also adds its model API host.
203
204```ts
205import {
206 createSecrets,
207 defineSandbox,
208 defineWorkspace,
209 githubRepo,
210} from '@tanstack/ai-sandbox'
211import { sbxSandbox } from '@tanstack/ai-sandbox-docker'
212
213export const repoSandbox = defineSandbox({
214 id: 'bug-fixer',
215 provider: sbxSandbox({
216 allowNetwork: ['*.npmjs.org', 'registry.npmjs.org'],
217 }),
218 workspace: defineWorkspace({
219 source: githubRepo({ repo: 'owner/buggy-app' }),
220 setup: ['pnpm install'],
221 secrets: createSecrets({
222 XAI_API_KEY: process.env.XAI_API_KEY ?? '',
223 }),
224 }),
225 lifecycle: { reuse: 'thread' },
226})
227```
228
229`sbxSandbox()` always clones a Git repo into the VM. It does not snapshot. Resume is by sandbox name.
230
231## Run the working example
232
233A complete, runnable app ships at
234[`examples/sandbox-web`](https://github.com/TanStack/ai/tree/main/examples/sandbox-web).
235That app is a "build me an app" agent (Claude Code on a Docker sandbox) with
236durable, refresh-surviving runs. It scaffolds an app in the sandbox, runs the
237dev server, and streams back a live preview URL. For a coding agent running at
238the edge, with the harness (Claude Code, Codex, Grok Build) picked per run
239from the UI, see
240[`examples/sandbox-cloudflare`](https://github.com/TanStack/ai/tree/main/examples/sandbox-cloudflare).
241
242From here:
243
244- Give the agent your own server-side tools (DB lookups, secrets), see [Tools](./tools).
245- Lock down what the agent is allowed to run, see [Policy](./policy).
246- Watch every file the agent touches as it works, see [Events](./events).