LangChain
This guide shows how to give a LangChain agent a tool that executes code in Vercel Sandbox, so model-generated code runs in an isolated microVM instead of on your host. Model requests route through AI Gateway.
Create a new directory for your project and initialize it:
terminalmkdir langchain-sandbox cd langchain-sandbox npm init -yInstall LangChain, the Anthropic and OpenAI provider packages, and the Sandbox SDK:
Terminalpnpm i langchain @langchain/core @langchain/anthropic @langchain/openai @vercel/sandbox zodTerminalyarn add langchain @langchain/core @langchain/anthropic @langchain/openai @vercel/sandbox zodTerminalnpm i langchain @langchain/core @langchain/anthropic @langchain/openai @vercel/sandbox zodTerminalbun add langchain @langchain/core @langchain/anthropic @langchain/openai @vercel/sandbox zodLink the project to Vercel and pull your development environment. This creates a
.env.localfile with aVERCEL_OIDC_TOKEN, which authenticates the Sandbox SDK:terminalvercel link vercel env pullCreate an AI Gateway API key, then add it to
.env.local:.env.localAI_GATEWAY_API_KEY=your_ai_gateway_api_keyCreate a new file called
index.tswith the following code:index.tsimport * as z from 'zod'; import { createAgent, tool } from 'langchain'; import { ChatAnthropic } from '@langchain/anthropic'; import { Sandbox } from '@vercel/sandbox'; const sandbox = await Sandbox.create({ timeout: 300_000 }); try { const runCode = tool( async ({ code }) => { const result = await sandbox.runCommand('node', ['-e', code]); return result.exitCode === 0 ? await result.stdout() : `Command failed: ${await result.stderr()}`; }, { name: 'run_code', description: 'Run Node.js code in an isolated Vercel Sandbox and return its output.', schema: z.object({ code: z.string().describe('Node.js code to execute'), }), }, ); const model = new ChatAnthropic({ model: 'anthropic/claude-opus-5', apiKey: process.env.AI_GATEWAY_API_KEY, anthropicApiUrl: 'https://ai-gateway.vercel.sh', }); const agent = createAgent({ model, tools: [runCode] }); const result = await agent.invoke({ messages: [ { role: 'user', content: "Compute the SHA-256 hex digest of the exact string 'vercel sandbox' with Node.js, then report the digest.", }, ], }); console.log(result.messages.at(-1)?.content); } finally { await sandbox.stop(); }The
timeoutvalue gives the sandbox up to five minutes to finish the agent loop. Thefinallyblock stops it sooner when the loop finishes or throws.The following code:
- Creates a sandbox that the agent reuses across tool calls
- Defines a
run_codetool that executes code withsandbox.runCommand() - Points the Anthropic chat model at AI Gateway, authenticated with your Vercel credentials
- Passes the tool to
createAgent, which runs the tool-calling loop
The example uses an Anthropic model. To use an OpenAI model instead, import
ChatOpenAIfrom@langchain/openaiand point it at the same gateway:index.tsimport { ChatOpenAI } from '@langchain/openai'; const model = new ChatOpenAI({ model: 'openai/gpt-5.6-sol', apiKey: process.env.AI_GATEWAY_API_KEY ?? process.env.VERCEL_OIDC_TOKEN, configuration: { baseURL: 'https://ai-gateway.vercel.sh/v1' }, });Any AI Gateway model works the same way.
Run your agent with the environment variables from
.env.local:terminalnpx tsx --env-file=.env.local index.tsThe agent writes Node.js code, executes it in the sandbox, and reports the digest.
Was this helpful?