AI SDK code execution tool
@slicevm/ai-sdk adds a code execution tool to the AI SDK (v7). The model writes JavaScript or TypeScript, the tool runs it in a Slice sandbox and returns the result, console output and any files the code wrote. Each call gets a fresh isolate with no network, file system, environment or subprocess access unless you grant it, and stops at hard fuel, memory and time limits.
The same tool runs code on your machine during development and on Slice Cloud in production.
Install
Section titled “Install”npm install @slicevm/ai-sdk ainpm install --save-dev @slicevm/cli # only needed to run locallyNode.js 22 or later, like the AI SDK itself.
Use it
Section titled “Use it”import { generateText, isStepCount } from "ai";import { executeCode } from "@slicevm/ai-sdk";
const { text } = await generateText({ model: "anthropic/claude-sonnet-5.5", tools: { executeCode: executeCode() }, stopWhen: isStepCount(5), prompt: "What is the 50th Fibonacci number? Compute it.",});
console.log(text);executeCode() works with generateText, streamText and ToolLoopAgent like any other tool.
Local or Slice Cloud
Section titled “Local or Slice Cloud”| Setting | Where the code runs |
|---|---|
| No API key | Locally, through the slice binary from @slicevm/cli |
SLICE_API_KEY set, or executeCode({ apiKey }) |
Slice Cloud (https://api.slicevm.com) |
executeCode({ local: true }) |
Locally, even with a key set |
Create a key with eval access in the console. The Free plan is enough to try it.
What the model controls
Section titled “What the model controls”The model sends only code. Limits and grants come from your app, so a model cannot give itself network access or a bigger budget:
executeCode({ timeoutMs: 5_000, memoryKb: 16_384, allowNet: ["api.github.com"], // the description tells the model which hosts it can reach auditEgress: [process.env.GITHUB_TOKEN!], // never sent out, in any encoding});| Option | Default | |
|---|---|---|
fuel, memoryKb, timeoutMs |
the plan maximum on Slice Cloud; 25,000,000 fuel and 4,096 KB locally | Limits per run (plans) |
allowNet |
none | Hosts the code may reach |
auditEgress |
none | Secrets that must never leave the sandbox |
artifacts |
true |
Return files written under /artifacts; false refuses that path too |
deterministicSeed |
none | Seeds Math.random and the clock |
client |
created on first use | A client from createSlice() in @slicevm/sdk |
description |
built in | Replaces the tool description. The built-in one (0.15.3) tells the model how results are returned, that each call starts clean, that Node built-ins and Web APIs are available but npm packages are not, which hosts it may reach, that only /artifacts is writable, and that refusals throw coded errors such as ERR_SLICE_NET_DENIED |
imagesToModel |
true |
Send image files to the model as images |
maxOutputChars |
20000 |
Longest console output sent to the model |
What comes back
Section titled “What comes back”The tool result has ok, result (the top-level return or last expression), stdout, error, deniedCapabilities, artifacts and, on Slice Cloud, runId.
- The model gets the run as JSON without file bytes, plus PNG, JPEG, GIF and WebP files as images, so it can check a chart it drew.
- Your app gets everything, including each file’s bytes (Base64) in
steps[i].toolResults, ready to save or show. - A sandbox limit (fuel, memory, time, a refused capability) is not an exception: the model sees
ok: falseanderror, and can fix its code and try again. - An API error (invalid key, quota) throws
SliceApiErrorand reaches the model as a tool error.
const { steps } = await generateText({ model: "anthropic/claude-sonnet-5.5", tools: { executeCode: executeCode() }, stopWhen: isStepCount(5), prompt: "Plot this monthly revenue as an SVG chart in /artifacts/revenue.svg: ...",});
for (const r of steps.flatMap((s) => s.toolResults)) { if (r.dynamic) continue; for (const file of r.output.artifacts) console.log(file.name, file.size);}On Slice Cloud with a console key, files are also kept for 1, 7 or 30 days by plan, and each one has a signed url.
Source
Section titled “Source”packages/ai-sdk in slicevm/sdk, MIT licensed.