Skip to content

LangChain.js code execution tool

@slicevm/langchain adds a code execution tool to LangChain.js (v1). The model writes JavaScript or TypeScript, SliceSandbox 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.

Terminal window
npm install @slicevm/langchain @langchain/core langchain
npm install --save-dev @slicevm/cli # only needed to run locally

Node.js 20 or later.

import { createAgent } from "langchain";
import { SliceSandbox } from "@slicevm/langchain";
const agent = createAgent({
model: "anthropic:claude-sonnet-5-5",
tools: [new SliceSandbox()],
});
const { messages } = await agent.invoke({
messages: [{ role: "user", content: "What is the 50th Fibonacci number? Compute it." }],
});
console.log(messages.at(-1)?.content);

SliceSandbox is a StructuredTool, so it also works with model.bindTools() and LangGraph’s ToolNode. The model sees it as execute_code.

Setting Where the code runs
No API key Locally, through the slice binary from @slicevm/cli
SLICE_API_KEY set, or new SliceSandbox({ apiKey }) Slice Cloud (https://api.slicevm.com)
new SliceSandbox({ 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.

The model sends only code. Limits and grants come from your app, so a model cannot give itself network access or a bigger budget:

new SliceSandbox({
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
name execute_code The tool name the model sees
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 false Send image files to the model as images (see below)
maxOutputChars 20000 Longest console output sent to the model

await sandbox.close() closes the client the tool created.

The tool uses responseFormat: "content_and_artifact":

  • The model gets the ToolMessage content: the run as JSON with ok, result (the top-level return or last expression), stdout, error, deniedCapabilities and the list of files, without their bytes.
  • Your app gets the ToolMessage artifact: the full run, including each file’s bytes (Base64) and, on Slice Cloud, runId.
  • A sandbox limit (fuel, memory, time, a refused capability) is not an exception: the model sees ok: false and error, and can fix its code and try again.
  • An API error (invalid key, quota) throws SliceApiError.
import { ToolMessage } from "@langchain/core/messages";
const { messages } = await agent.invoke({
messages: [{ role: "user", content: "Plot this monthly revenue as an SVG chart in /artifacts/revenue.svg: ..." }],
});
for (const m of messages) {
if (ToolMessage.isInstance(m) && m.name === "execute_code") {
for (const file of m.artifact.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.

With imagesToModel: true, PNG, JPEG, GIF and WebP files are added to the tool result as image blocks, so the model can check a chart it drew. The provider must accept images in tool results:

Model Images in tool results
Anthropic (@langchain/anthropic) Yes
ChatOpenAI with useResponsesApi: true Yes
ChatOpenAI default (Chat Completions) No: the request fails with a 400 error

That is why it is off by default.

packages/langchain in slicevm/sdk, MIT licensed.