Open History to see the last commit
1---
2title: Code Mode Isolate Drivers
3id: code-mode-isolates
4order: 4
5description: "Pick the sandbox that runs Code Mode for your server: Node isolated-vm, QuickJS WASM, QuickJS on Bun, Cloudflare Workers, Daytona, or E2B."
6keywords:
7 - tanstack ai
8 - code mode
9 - isolate driver
10 - isolated-vm
11 - quickjs
12 - quickjs-bun
13 - bun
14 - bun:ffi
15 - cloudflare workers
16 - daytona
17 - e2b
18 - sandbox
19 - secure execution
20---
21
22Code Mode runs model-written code in a sandbox. The sandbox you can use depends on where the server runs.
23
24An isolate driver is that sandbox. All drivers have the same `IsolateDriver` shape, so you change the driver in one line and keep the rest of your code.
25
26## Pick a driver
27
28| Your server runs on | Driver | Package |
29| ------------------- | ------ | ------- |
30| Node.js | `createNodeIsolateDriver()` | `@tanstack/ai-isolate-node` |
31| Bun | `createQuickJSBunIsolateDriver()` | `@tanstack/ai-isolate-quickjs-bun` |
32| A browser, the edge, or anywhere without native addons | `createQuickJSIsolateDriver()` | `@tanstack/ai-isolate-quickjs` |
33| Any server, with code that runs on Cloudflare | `createCloudflareIsolateDriver()` | `@tanstack/ai-isolate-cloudflare` |
34| Any server, with code that runs in a full Linux sandbox | `createDaytonaIsolateDriver()` or `createE2BIsolateDriver()` | `@tanstack/ai-isolate-daytona` or `@tanstack/ai-isolate-e2b` |
35
36Then give the driver to `createCodeMode`. This example uses QuickJS:
37
38```typescript ignore
39import { createCodeMode } from "@tanstack/ai-code-mode";
40import { createQuickJSIsolateDriver } from "@tanstack/ai-isolate-quickjs";
41
42const { tools, systemPrompt } = createCodeMode({
43 driver: createQuickJSIsolateDriver(),
44 tools: [fetchWeather],
45});
46```
47
48Nothing else in your route changes.
49
50## Compare the drivers
51
52| | Node (`isolated-vm`) | QuickJS (WASM) | QuickJS Bun (`bun:ffi`) | Cloudflare Workers | Daytona | E2B |
53| -------------------- | ------------------------ | --------------------------- | ------------------------ | ------------------------------ | ---------------------------------- | --- |
54| **Best for** | Node.js servers | Browsers, edge, portability | Bun servers | Edge deployments on Cloudflare | Full remote Linux sandboxes | Remote E2B sandboxes |
55| **Speed** | Fast (V8 JIT) | Slower (interpreted) | Fast (native QuickJS) | Fast (V8 on Cloudflare edge) | Fast (native runtime, remote call) | Fast (native Node, remote call) |
56| **Native deps** | Yes (C++ addon) | None | None (TinyCC at runtime) | None | None | None |
57| **Runs in a browser** | No | Yes | No (Bun only) | N/A | No | No |
58| **Memory limit** | Yes | Yes | Yes | N/A | No | Yes |
59| **Stack size limit** | N/A | Yes | Yes | N/A | N/A | N/A |
60| **Setup** | `pnpm add` | `pnpm add` | `bun add` | Deploy a Worker first | Create or pass a Daytona sandbox | Create or pass an E2B sandbox |
61
62## Node.js driver
63
64`@tanstack/ai-isolate-node` runs the code in a V8 isolate with the [`isolated-vm`](https://github.com/laverdet/isolated-vm) native addon. It is the fastest choice on a Node.js server.
65
66<!-- ::start:tabs variant="package-manager" mode="install" -->
67
68react: @tanstack/ai-isolate-node
69vue: @tanstack/ai-isolate-node
70solid: @tanstack/ai-isolate-node
71svelte: @tanstack/ai-isolate-node
72preact: @tanstack/ai-isolate-node
73angular: @tanstack/ai-isolate-node
74vanilla: @tanstack/ai-isolate-node
75octane: @tanstack/ai-isolate-node
76
77<!-- ::end:tabs -->
78
79The package needs Node.js 22 or later. `isolated-vm` is a native C++ addon. It must be compiled for your platform.
80
81```typescript
82import { createNodeIsolateDriver } from '@tanstack/ai-isolate-node'
83
84const driver = createNodeIsolateDriver({
85 memoryLimit: 128, // MB
86 timeout: 30_000, // ms
87})
88```
89
90| Option | Type | Default | Description |
91| ------------- | --------- | ------- | ----------- |
92| `memoryLimit` | `number` | `128` | Largest heap for the V8 isolate, in MB. The execution stops if it goes over. |
93| `timeout` | `number` | `30000` | Longest wall-clock time for one execution, in ms. |
94| `skipProbe` | `boolean` | `false` | Skips the compatibility probe for `isolated-vm`. If you made sure that the addon works on your Node.js version, you can set it. |
95
96## QuickJS driver
97
98`@tanstack/ai-isolate-quickjs` runs [QuickJS](https://bellard.org/quickjs/) compiled to WebAssembly. It has no native dependencies, so it runs anywhere JavaScript runs: Node.js, browsers, Deno, Bun, and Cloudflare Workers (with no separate Worker).
99
100<!-- ::start:tabs variant="package-manager" mode="install" -->
101
102react: @tanstack/ai-isolate-quickjs
103vue: @tanstack/ai-isolate-quickjs
104solid: @tanstack/ai-isolate-quickjs
105svelte: @tanstack/ai-isolate-quickjs
106preact: @tanstack/ai-isolate-quickjs
107angular: @tanstack/ai-isolate-quickjs
108vanilla: @tanstack/ai-isolate-quickjs
109octane: @tanstack/ai-isolate-quickjs
110
111<!-- ::end:tabs -->
112
113```typescript
114import { createQuickJSIsolateDriver } from '@tanstack/ai-isolate-quickjs'
115
116const driver = createQuickJSIsolateDriver({
117 memoryLimit: 128, // MB
118 timeout: 30_000, // ms
119 maxStackSize: 524288, // bytes (512 KiB)
120 wasmLocation: '/assets/quickjs/emscripten-module.wasm',
121})
122```
123
124| Option | Type | Default | Description |
125| -------------- | -------- | -------- | ----------- |
126| `memoryLimit` | `number` | `128` | Largest heap for the QuickJS VM, in MB. |
127| `timeout` | `number` | `30000` | Longest wall-clock time for one execution, in ms. |
128| `maxStackSize` | `number` | `524288` | Largest call stack, in bytes (512 KiB). Increase it for deep recursion. Decrease it to stop runaway recursion sooner. |
129| `wasmLocation` | `string` | none | URL or path to load the QuickJS WASM binary from. If you leave it out, `quickjs-emscripten` uses its bundled binary. |
130
131To host the WASM binary yourself, for example on a CDN, serve the synchronous release binary that `@jitl/quickjs-wasmfile-release-sync/wasm` exports. Then set `wasmLocation` to its URL. For a URL on another origin, configure the host to allow cross-origin requests.
132
133QuickJS interprets JavaScript and does not JIT-compile it. Code with heavy computation runs slower than on the Node driver. Most generated scripts spend their time on `external_*` calls, so the difference is small.
134
135## QuickJS Bun driver
136
137`@tanstack/ai-isolate-quickjs-bun` runs QuickJS natively on [Bun](https://bun.sh/) through `bun:ffi`, with the [`quickjs-bun`](https://github.com/superpowerdotcom/quickjs-bun) package. Bun compiles the QuickJS C sources one time per process with its embedded TinyCC. You need no native dependencies and no build step. On Bun, it is the fastest sandbox for Code Mode.
138
139<!-- ::start:tabs variant="package-manager" mode="install" -->
140
141react: @tanstack/ai-isolate-quickjs-bun
142vue: @tanstack/ai-isolate-quickjs-bun
143solid: @tanstack/ai-isolate-quickjs-bun
144svelte: @tanstack/ai-isolate-quickjs-bun
145preact: @tanstack/ai-isolate-quickjs-bun
146angular: @tanstack/ai-isolate-quickjs-bun
147vanilla: @tanstack/ai-isolate-quickjs-bun
148octane: @tanstack/ai-isolate-quickjs-bun
149
150<!-- ::end:tabs -->
151
152The driver needs Bun 1.3.14 or later. On Node.js, it throws an error when it creates a context, so use the Node or QuickJS WASM driver there. On Windows, set the `QUICKJS_BUN_NATIVE_LIBRARY` environment variable to a prebuilt QuickJS dynamic library.
153
154```typescript
155import { createQuickJSBunIsolateDriver } from '@tanstack/ai-isolate-quickjs-bun'
156
157const driver = createQuickJSBunIsolateDriver({
158 memoryLimit: 128, // MB
159 timeout: 30_000, // ms
160 maxStackSize: 524288, // bytes (512 KiB)
161})
162```
163
164| Option | Type | Default | Description |
165| -------------- | -------- | -------- | ----------- |
166| `memoryLimit` | `number` | `128` | Largest heap for the QuickJS runtime, in MB. |
167| `timeout` | `number` | `30000` | Longest wall-clock time for one execution, in ms. |
168| `maxStackSize` | `number` | `524288` | Largest call stack, in bytes (512 KiB). Increase it for deep recursion. Decrease it to stop runaway recursion sooner. |
169| `maxToolCalls` | `number` | `1000` | Most host tool calls in one execution. It stops untrusted code that fans out, for example `Promise.all` over a huge array. If the code goes over, the sandbox throws an error that the code can catch. |
170
171On Bun, use this driver and not the WASM one. The asyncify bridge of `quickjs-emscripten` is not reliable for async tool calls under Bun.
172
173## Cloudflare Workers driver
174
175`@tanstack/ai-isolate-cloudflare` runs the code in a [Cloudflare Worker](https://workers.cloudflare.com/). Your server sends the code to the Worker over HTTP. When the code calls a tool, the Worker asks your server, and your server runs the tool. Your tools stay on your server.
176
177<!-- ::start:tabs variant="package-manager" mode="install" -->
178
179react: @tanstack/ai-isolate-cloudflare
180vue: @tanstack/ai-isolate-cloudflare
181solid: @tanstack/ai-isolate-cloudflare
182svelte: @tanstack/ai-isolate-cloudflare
183preact: @tanstack/ai-isolate-cloudflare
184angular: @tanstack/ai-isolate-cloudflare
185vanilla: @tanstack/ai-isolate-cloudflare
186octane: @tanstack/ai-isolate-cloudflare
187
188<!-- ::end:tabs -->
189
1901. Add a `wrangler.toml` to your Worker project:
191
192```toml
193# wrangler.toml
194name = "code-mode-worker"
195main = "src/worker.ts"
196compatibility_date = "2026-05-01"
197compatibility_flags = ["nodejs_compat"]
198
199[[worker_loaders]]
200binding = "LOADER"
201```
202
2032. Add the Worker entry. The package exports a ready-made Worker:
204
205```typescript
206// src/worker.ts
207export { default } from '@tanstack/ai-isolate-cloudflare/worker'
208```
209
2103. Run `wrangler dev` to test locally on `http://localhost:8787`, or run `wrangler deploy`.
211
2124. Point the driver at the Worker URL:
213
214```typescript
215import { createCloudflareIsolateDriver } from '@tanstack/ai-isolate-cloudflare'
216
217const driver = createCloudflareIsolateDriver({
218 workerUrl: 'https://my-code-mode-worker.my-account.workers.dev',
219 authorization: process.env.CODE_MODE_WORKER_SECRET,
220 timeout: 30_000,
221 maxToolRounds: 10,
222})
223```
224
225| Option | Type | Default | Description |
226| --------------- | -------- | ------- | ----------- |
227| `workerUrl` | `string` | none | Required. The full URL of the Worker. |
228| `authorization` | `string` | none | A value that the driver sends as the `Authorization` header on each request. |
229| `timeout` | `number` | `30000` | Longest wall-clock time for the full execution, tool round trips included, in ms. |
230| `maxToolRounds` | `number` | `10` | Most tool-call rounds in one execution. It stops code that calls tools in a loop. |
231
232> [!WARNING]
233> The Worker runs any code that it gets. Protect the Worker with authentication, rate limits, and an allowed-origin list before you deploy it. The ready-made Worker does not check the `Authorization` header, so check it in your own Worker or use Cloudflare Access.
234
235The `worker_loader` binding works with `wrangler dev` on the Workers Free plan. To deploy, or to run `wrangler dev --remote`, you need the Workers Paid plan.
236
237## Daytona driver
238
239`@tanstack/ai-isolate-daytona` runs the code in a [Daytona](https://www.daytona.io/) sandbox with `sandbox.process.codeRun`. Your tools stay in your server process. The sandbox gets only the wrapped code and the tool results.
240
241<!-- ::start:tabs variant="package-manager" mode="install" -->
242
243react: @tanstack/ai-isolate-daytona @daytona/sdk
244vue: @tanstack/ai-isolate-daytona @daytona/sdk
245solid: @tanstack/ai-isolate-daytona @daytona/sdk
246svelte: @tanstack/ai-isolate-daytona @daytona/sdk
247preact: @tanstack/ai-isolate-daytona @daytona/sdk
248angular: @tanstack/ai-isolate-daytona @daytona/sdk
249vanilla: @tanstack/ai-isolate-daytona @daytona/sdk
250octane: @tanstack/ai-isolate-daytona @daytona/sdk
251
252<!-- ::end:tabs -->
253
254You need `@daytona/sdk` only if your app creates the sandboxes with the official Daytona SDK.
255
256```typescript
257import { Daytona } from '@daytona/sdk'
258import { createDaytonaIsolateDriver } from '@tanstack/ai-isolate-daytona'
259
260const daytona = new Daytona()
261const sandbox = await daytona.create({ language: 'typescript' })
262
263const driver = createDaytonaIsolateDriver({
264 sandbox,
265 timeout: 30_000,
266 maxToolRounds: 10,
267})
268```
269
270| Option | Type | Default | Description |
271|--------|------|---------|-------------|
272| `sandbox` | `DaytonaSandboxLike` | none | Required. A Daytona sandbox that you own, or any object with `process.codeRun(code, params?, timeout?)`. |
273| `timeout` | `number` | `30000` | Longest wall-clock time for the full execution, replay rounds included, in ms. |
274| `maxToolRounds` | `number` | `10` | Most tool callback rounds between the sandbox and your server. It stops code that asks for tools in a loop. |
275
276The sandbox must use a runtime that can run the JavaScript that Code Mode makes. Your app owns the sandbox lifecycle, file system, network, cleanup, and secret policy. A Code Mode context does not create or delete a Daytona sandbox.
277
278## E2B driver
279
280`@tanstack/ai-isolate-e2b` runs the code as a `node` process in an [E2B](https://e2b.dev/) sandbox that your app creates. Your tools stay in your server process. The code runs one time, from start to finish.
281
282<!-- ::start:tabs variant="package-manager" mode="install" -->
283
284react: @tanstack/ai-isolate-e2b e2b
285vue: @tanstack/ai-isolate-e2b e2b
286solid: @tanstack/ai-isolate-e2b e2b
287svelte: @tanstack/ai-isolate-e2b e2b
288preact: @tanstack/ai-isolate-e2b e2b
289angular: @tanstack/ai-isolate-e2b e2b
290vanilla: @tanstack/ai-isolate-e2b e2b
291octane: @tanstack/ai-isolate-e2b e2b
292
293<!-- ::end:tabs -->
294
295Set `E2B_API_KEY` in your server environment. Then create a sandbox and give it to the driver:
296
297```typescript
298import { Sandbox } from 'e2b'
299import { createE2BIsolateDriver } from '@tanstack/ai-isolate-e2b'
300
301const sandbox = await Sandbox.create()
302
303const driver = createE2BIsolateDriver({
304 sandbox,
305 timeout: 30_000,
306})
307```
308
309| Option | Type | Default | Description |
310|--------|------|---------|-------------|
311| `sandbox` | `E2BSandboxLike` | none | Required. The E2B sandbox that runs the code, for example from `Sandbox.create()`. A sandbox from `@e2b/code-interpreter` also works. |
312| `timeout` | `number` | `30000` | Longest time for one execution, tool calls included, in ms. |
313
314The `memoryLimit` of `createCodeMode` sets the heap limit of the `node` process.
315
316Your app owns these parts:
317
318- **The sandbox lifecycle.** A context does not create, pause, or kill the sandbox.
319- **Who shares a sandbox.** All executions in one sandbox see the same files and network. Use one sandbox for each user or trusted scope.
320- **Secrets.** The driver sends no API key or server environment into the sandbox.
321
322## Advanced
323
324### How the Node driver works
325
326Each `execute_typescript` call creates a fresh V8 isolate. Your tools enter the isolate as async reference functions. When the code calls `external_myTool(...)`, the call crosses back into the host Node.js process, runs your tool, and returns the result. The driver captures the console output (`log`, `error`, `warn`, `info`) and returns it with the result. The isolate is destroyed after each call.
327
328The first call to `createNodeIsolateDriver` runs a probe in a subprocess. The probe finds a native addon that does not match your Node.js version, which can otherwise crash the process with a segfault. If the probe fails, the driver throws an error that tells you to use the QuickJS driver or a supported Node.js version.
329
330### How the QuickJS drivers work
331
332The WASM driver runs the synchronous WASM build. It bridges your async tools through QuickJS promises, so the WASM stack does not suspend. All contexts share one WASM module.
333
334The Bun driver gives each context its own native QuickJS runtime, with its own memory limit, stack size, and timeout. Contexts run independently.
335
336On both drivers, a fatal error (out of memory, stack overflow) disposes of the VM and returns a structured error. With the Bun driver, create a fresh context after a fatal error.
337
338### How the remote drivers call your tools
339
340The Cloudflare and Daytona drivers use a request and response loop. Your server runs the tools:
341
342```text
343Driver (your server) Worker or sandbox
344-------------------- -----------------
345Send: code + tool schemas ----> Run the code
346 <---- Return: needs tool X with args Y
347Run tool X locally
348Send: tool result ----> Continue
349 <---- Return: final result, or needs tool Z
350...repeat until done...
351```
352
353Each round trip adds network latency. `maxToolRounds` stops runaway scripts and caps the number of hops. The driver collects the console output from all rounds and returns it in the final result.
354
355The Daytona driver uses this loop with no parent Worker. It replays the code with the tool results that it has so far.
356
357The E2B driver keeps one `node` process alive for the full execution:
358
359```text
360Driver (your server) E2B sandbox
361-------------------- -----------
362Start one node process ---------> Run the generated code
363 <--------- Tool call request (stdout)
364Run the tool locally
365Send the result (stdin) ---------> Continue from the same point
366 <--------- Final result
367```
368
369Because the process does not replay, side effects in the sandbox happen one time, also when the code calls many tools. A file write and an HTTP request are examples of such side effects.
370
371The E2B driver kills the process group on timeout, on `dispose()`, and after each execution. This includes processes that the code started, unless they moved to their own process group (`detached: true`). If the driver cannot confirm the kill after an execution, the result log records it, and the process can still run in the sandbox. `dispose()` does not throw. The sandbox template must have `node`, `setsid`, and `timeout`. The default E2B templates have them.
372
373### The `IsolateDriver` interface
374
375All drivers implement this interface from `@tanstack/ai-code-mode`. Implement it yourself to build a custom driver, for example on Docker or a Deno subprocess:
376
377```typescript ignore
378import type { NormalizedError, ToolBinding } from '@tanstack/ai-code-mode'
379
380interface IsolateDriver {
381 createContext(config: IsolateConfig): Promise<IsolateContext>
382}
383
384interface IsolateConfig {
385 bindings: Record<string, ToolBinding>
386 timeout?: number
387 memoryLimit?: number
388}
389
390interface IsolateContext {
391 execute<T = unknown>(code: string): Promise<ExecutionResult<T>>
392 dispose(): Promise<void>
393}
394
395interface ExecutionResult<T = unknown> {
396 success: boolean
397 value?: T
398 logs?: Array<string>
399 error?: NormalizedError
400}
401```