Open History to see the last commit
1---
2title: Harnesses
3id: sandbox-harnesses
4description: "Pick which coding agent runs inside a TanStack AI sandbox: Grok Build, Claude Code, Codex, OpenCode, or any ACP-compatible agent through acpCompatible."
5---
6
7A harness is the agent inside the sandbox. Pick Grok Build, Claude Code, Codex, OpenCode, or another CLI.
8
9A harness adapter starts that agent inside the sandbox and turns its work into `chat()` stream chunks. The [provider](./providers) decides where the agent runs. The harness decides which agent runs. You can change one without changing the other.
10
11## Pick a harness
12
131. Pick a row and install its package.
14
15| Harness | Package | Adapter | Auth |
16| --- | --- | --- | --- |
17| [Grok Build](../adapters/grok-build) | `@tanstack/ai-grok-build` | `grokBuildText` | Default `'api-key'` (`XAI_API_KEY`). `'host'` uses `grok login`. |
18| [Claude Code](../adapters/claude-code) | `@tanstack/ai-claude-code` | `claudeCodeText` | Default `'api-key'` (`ANTHROPIC_API_KEY`). `'host'` uses `claude login`. |
19| [Codex](../adapters/codex) | `@tanstack/ai-codex` | `codexText` | Default `'api-key'` (`CODEX_API_KEY`). `'host'` uses `codex login`. |
20| [OpenCode](../adapters/opencode) | `@tanstack/ai-opencode` | `opencodeText` | `OPENAI_API_KEY` from the process env. No `authMode` flag. |
21| [ACP-Compatible](../adapters/acp-compatible) | `@tanstack/ai-acp` | `acpCompatible` / `acpCompatibleText` | Default `'api-key'` uses `authMethodId`. `'host'` skips ACP `authenticate`. |
22
23<!-- ::start:tabs variant="package-manager" mode="install" -->
24
25react: @tanstack/ai-claude-code
26vue: @tanstack/ai-claude-code
27solid: @tanstack/ai-claude-code
28svelte: @tanstack/ai-claude-code
29preact: @tanstack/ai-claude-code
30angular: @tanstack/ai-claude-code
31vanilla: @tanstack/ai-claude-code
32octane: @tanstack/ai-claude-code
33
34<!-- ::end:tabs -->
35
362. Pass the adapter to `chat()`, next to `withSandbox`:
37
38```ts
39import { chat } from '@tanstack/ai'
40import { claudeCodeText } from '@tanstack/ai-claude-code'
41import {
42 createSecrets,
43 defineSandbox,
44 defineWorkspace,
45 githubRepo,
46 withSandbox,
47} from '@tanstack/ai-sandbox'
48import { dockerSandbox } from '@tanstack/ai-sandbox-docker'
49
50const sandbox = defineSandbox({
51 id: 'repo-agent',
52 provider: dockerSandbox({ image: 'node:22' }),
53 workspace: defineWorkspace({
54 source: githubRepo({ repo: 'owner/app' }),
55 setup: [
56 'npm install -g @anthropic-ai/claude-code --include=optional && claude --version',
57 ],
58 secrets: createSecrets({
59 ANTHROPIC_API_KEY: process.env.ANTHROPIC_API_KEY ?? '',
60 }),
61 }),
62})
63
64const stream = chat({
65 adapter: claudeCodeText('claude-opus-4-8'),
66 messages: [{ role: 'user', content: 'Summarize this repo.' }],
67 threadId: 'thread-1',
68 middleware: [withSandbox(sandbox)],
69})
70```
71
72The agent CLI must exist inside the sandbox. The `setup` step installs it. See [Installing an agent CLI](./workspace#installing-an-agent-cli).
73
74To change the agent, change the adapter, the secret, and the install step. The provider and the workspace source stay the same.
75
76## How it works
77
78- Every harness adapter declares `requires: [SandboxCapability]`. If no `withSandbox(...)` gives it a sandbox, `chat()` fails at the call site.
79- The adapter starts the agent CLI inside the sandbox and translates its output into AG-UI chunks.
80- The provider does not pick how the agent signs in. The default `authMode` is `'api-key'`. Set `'host'` when the machine already has a CLI login. See [Harness Auth](./auth).
81
82## Get a typed report
83
84If you want a typed object after the agent reads the repo, pass `outputSchema` on the same `chat()` call. The dedicated adapters and `acpCompatible` support it. See [Harness Agents](../structured-outputs/harnesses).
85
86## Any ACP agent (`acpCompatible`)
87
88Many coding agents speak the [Agent Client Protocol](https://agentclientprotocol.com) (ACP), for example `pi` and `gemini --acp`. See [the full list](https://agentclientprotocol.com/get-started/agents).
89
90If an agent has no dedicated package, `acpCompatible` from `@tanstack/ai-acp` builds a harness adapter for it. It is the harness version of `openaiCompatible`. Tell it how to start the agent, then use it with any provider:
91
92```ts
93import { acpCompatible } from '@tanstack/ai-acp'
94
95const pi = acpCompatible({
96 name: 'pi',
97 models: ['pi-fast', 'pi-pro'],
98 command: ({ model, harnessCwd }) => `pi --acp -m ${model} --cwd ${harnessCwd}`,
99 authMethodId: 'pi-api-key',
100})
101```
102
103The [ACP-Compatible Harness](../adapters/acp-compatible) guide has the full config: typed models, `modelOptions` for each call, WebSocket transports, permissions, and protocol coverage. To find agents, see the [ACP agents list](https://agentclientprotocol.com/get-started/agents) and the [ACP registry](https://agentclientprotocol.com/get-started/registry).
104
105## Advanced
106
107### Harness output can go to a journal
108
109`grokBuildText`, `claudeCodeText`, and `codexText` can write the agent's stdout to an append-only NDJSON file inside the sandbox (`/tmp/tanstack-runs/<runId>.ndjson`). The host then reads that file and does not hold the stdout pipe. As a result:
110
111- If the host dies, the agent gets no signal and keeps working.
112- A later reader can replay the run from byte 0.
113
114They do this only for a **durable run**. To opt in, pass `withSandbox` both `runs` and `durability`. A plain `withSandbox(sandbox)` writes no journal and streams over a pipe.
115
116You must also pass the `runId`, because the journal path comes from it. A durable run with no `runId` from the caller throws `DurableRunIdRequiredError`.
117
118```ts
119import {
120 chat,
121 chatParamsFromRequest,
122 memoryStream,
123 toServerSentEventsResponse,
124} from '@tanstack/ai'
125import { codexText } from '@tanstack/ai-codex'
126import { memoryPersistence } from '@tanstack/ai-persistence'
127import { withSandbox } from '@tanstack/ai-sandbox'
128import { sandbox } from './sandbox'
129
130// Single-process stand-ins. ./takeover has the multi-replica setup.
131const persistence = memoryPersistence()
132const { runs } = persistence.stores
133
134export async function POST(request: Request) {
135 const { messages, threadId, runId } = await chatParamsFromRequest(request)
136 const adapter = memoryStream(request)
137 const stream = chat({
138 adapter: codexText('gpt-5.3-codex'),
139 messages,
140 threadId,
141 runId, // a later reader finds the journal with this id
142 // Pass BOTH, or there is no journal.
143 middleware: [withSandbox(sandbox, { runs, durability: { adapter } })],
144 })
145 return toServerSentEventsResponse(stream, { durability: { adapter } })
146}
147```
148
149The id must be unique for each run. The journal appends, so if you use an id again, a reader stops at the exit line of the earlier run. For what you get without the opt-in, and for replay against a log that was already delivered, see [The Run Journal](./journal).
150
151`opencodeText` and `acpCompatible` harnesses do not read NDJSON from stdout. They have no journal, also when a run is durable.
152
153For the rest of the durable setup, see [Durable Runs](./durable-runs) and [Takeover & Detached Runs](./takeover). Takeover also lists the three exports that a harness adapter implements to support attach.