Open History to see the last commit
1---
2title: Harness Auth
3id: sandbox-auth
4description: "Pick host login or an API key for a coding-agent harness. The same local-process sandbox can be your laptop or a CI runner."
5---
6
7Your laptop already has `grok login`, `claude login`, or `codex login`. A GitHub
8runner has no browser login. It only has an API key. Both can use
9`localProcessSandbox()`.
10
11Set `authMode` on the adapter. The sandbox type does not pick the credentials.
12The default is `'api-key'`. Most harnesses run in Docker or a cloud sandbox.
13Those have no host CLI login.
14
15- `'api-key'` (default): inject the key and use it.
16- `'host'`: use the CLI login on the machine. Do not inject an API key into
17 that process.
18
19Pass `authMode` in one of two places:
20
21- Adapter factory: `grokBuildText('composer-2.5', { authMode: 'host' })`
22- One call: `chat({ modelOptions: { authMode: 'host' } })`
23
24## Host login
25
26Use this when the machine already ran `grok login`, `claude login`, or
27`codex login`.
28
29```ts
30import { chat } from '@tanstack/ai'
31import { grokBuildText } from '@tanstack/ai-grok-build'
32import { defineSandbox, defineWorkspace, withSandbox } from '@tanstack/ai-sandbox'
33import { localProcessSandbox } from '@tanstack/ai-sandbox-local-process'
34import { messages, threadId } from './chat-context'
35
36const sandbox = defineSandbox({
37 id: 'repo-agent',
38 provider: localProcessSandbox({
39 scrubEnv: ['XAI_API_KEY', 'GROK_API_KEY'],
40 }),
41 workspace: defineWorkspace({
42 source: { type: 'local', path: '/abs/path/to/repo' },
43 }),
44})
45
46const stream = chat({
47 threadId,
48 adapter: grokBuildText('composer-2.5', { authMode: 'host' }),
49 messages,
50 middleware: [withSandbox(sandbox)],
51})
52```
53
54`scrubEnv` removes keys the host process inherited. If the CLI sees
55`XAI_API_KEY`, it can prefer that key over your login.
56
57## API key
58
59This is the default. Use it on a runner, in Docker, or on any machine that
60has no CLI login. You can omit `authMode`.
61
62```ts
63import { chat } from '@tanstack/ai'
64import { grokBuildText } from '@tanstack/ai-grok-build'
65import {
66 createSecrets,
67 defineSandbox,
68 defineWorkspace,
69 githubRepo,
70 withSandbox,
71} from '@tanstack/ai-sandbox'
72import { dockerSandbox } from '@tanstack/ai-sandbox-docker'
73import { messages, threadId } from './chat-context'
74
75const sandbox = defineSandbox({
76 id: 'repo-agent',
77 provider: dockerSandbox({ image: 'node:22' }),
78 workspace: defineWorkspace({
79 source: githubRepo({ repo: 'owner/app' }),
80 secrets: createSecrets({
81 XAI_API_KEY: process.env.XAI_API_KEY ?? '',
82 }),
83 }),
84})
85
86const stream = chat({
87 threadId,
88 adapter: grokBuildText('composer-2.5'),
89 messages,
90 middleware: [withSandbox(sandbox)],
91})
92```
93
94## Each harness
95
96| Adapter | `authMode: 'api-key'` (default) | `authMode: 'host'` |
97| --- | --- | --- |
98| [Grok Build](../adapters/grok-build) | `XAI_API_KEY` | `grok login` |
99| [Claude Code](../adapters/claude-code) | `ANTHROPIC_API_KEY` | `claude login` |
100| [Codex](../adapters/codex) | `CODEX_API_KEY` | `codex login` |
101| [ACP-Compatible](../adapters/acp-compatible) | set `authMethodId` (for Grok, `xai.api_key`) | skip ACP `authenticate` |
102
103OpenCode still reads `OPENAI_API_KEY` from the process env. It has no `authMode`
104flag.
105
106## Client and server
107
108The React chat example exposes the same choice on `/repo-report`. The client
109sends `authMode`. The server builds the adapter with that value.
110
111Client:
112
113```tsx
114import { fetchServerSentEvents, useChat } from '@tanstack/ai-react'
115
116function Report() {
117 const { sendMessage } = useChat({
118 connection: fetchServerSentEvents('/api/sandbox-repo-report'),
119 forwardedProps: { authMode: 'api-key' },
120 })
121
122 return (
123 <button type="button" onClick={() => sendMessage('Report on this repo')}>
124 Run
125 </button>
126 )
127}
128```
129
130Server:
131
132```ts
133import { chat, toServerSentEventsResponse } from '@tanstack/ai'
134import { grokBuildText } from '@tanstack/ai-grok-build'
135import { withSandbox } from '@tanstack/ai-sandbox'
136import { sandbox } from './sandbox'
137
138export async function POST(request: Request) {
139 const body: unknown = await request.json()
140 const forwarded =
141 typeof body === 'object' &&
142 body !== null &&
143 'forwardedProps' in body &&
144 typeof body.forwardedProps === 'object' &&
145 body.forwardedProps !== null
146 ? body.forwardedProps
147 : {}
148 const authMode =
149 'authMode' in forwarded && forwarded.authMode === 'host'
150 ? 'host'
151 : 'api-key'
152
153 const stream = chat({
154 adapter: grokBuildText('composer-2.5', { authMode }),
155 messages: [{ role: 'user', content: 'Report on this repo' }],
156 stream: true,
157 middleware: [withSandbox(sandbox)],
158 })
159
160 return toServerSentEventsResponse(stream)
161}
162```
163
164See [Harnesses](./harnesses) for which adapter to pick, and [Providers](./providers)
165for `scrubEnv` on local-process.