Run agent code in Agent Sandboxes from TypeScript
Run Python in a remote Agent Sandbox from a Node.js application. Your application is TypeScript or JavaScript. The code that your agent writes, and that the sandbox runs, is Python.
- Enable Agent Sandboxes for your project.
- Get an execution key and set it as the
LOGFIRE_API_KEYenvironment variable. The key also selects the data region, so you do not configure a URL. - The examples use the
await usingsyntax. TypeScript 5.2 or later supports it, and so doestsx. Plain JavaScript needs Node.js 24 or later. Without it, callclose()in afinallyblock.
npm install @pydantic/logfire-agent-sandboxes @pydantic/monty
@pydantic/monty is the client for Pydantic Monty sandboxes. Version 1.5.0 or later is required.
import { agentSandboxes } from '@pydantic/logfire-agent-sandboxes'
await using pool = await agentSandboxes()
await using session = await pool.checkout()
console.log(await session.feedRun('sum([20, 21, 1])'))
Run npx tsx index.mts. It prints 42.
agentSandboxes() returns a pool of sandboxes. The pool opens a connection when you call pool.checkout(), which
gives you a session with its own sandbox. Variables and functions defined in one feedRun() call are available in
later calls on the same session.
Pass your own functions in externalLookup. Sandbox code calls them by name, and each call runs in your application.
Expose only the functions the code needs.
import { agentSandboxes } from '@pydantic/logfire-agent-sandboxes'
const temperatures: Record<string, number> = { London: 14.0, Paris: 17.5, Tokyo: 21.0 }
const getTemperature = (city: string) => temperatures[city] // runs in your application
// written by your agent, runs in the sandbox
const agentCode = `
cities = ['London', 'Paris', 'Tokyo']
temperatures = {city: get_temperature(city) for city in cities}
max(cities, key=lambda city: temperatures[city])
`
await using pool = await agentSandboxes()
await using session = await pool.checkout()
console.log(await session.feedRun(agentCode, { externalLookup: { get_temperature: getTemperature } }))
This prints Tokyo. Host functions can also be async.
@pydantic/logfire-ai-sdk-code-mode connects the Vercel AI SDK to Agent Sandboxes.
It turns your AI SDK tools into Python functions, and gives the model one run_code tool that calls them from a
script. See Use with the Vercel AI SDK
in the Logfire JavaScript SDK docs.
The same page documents saved sessions, snapshots, pool options, and errors for TypeScript.
Run the first snippet, then open the Live view.
You should see a session span with a run code span inside it, showing the code, the result, and how long it took.
AgentSandboxError: Agent Sandboxes require a Logfire API key: no API key was found. SetLOGFIRE_API_KEY, or passapiKeytoagentSandboxes().AgentSandboxRefusedError: the server rejected the API key. Use a project API key, not a personal key, with theproject:execute_agent_sandboxesscope. Check that Agent Sandboxes are enabled for the project. If you setLOGFIRE_BASE_URL, check that it is in the key’s region.AgentSandboxConnectionError: the pool could not reach the sandboxes server. The message names the URL that it tried and the original error, anderror.causeholds the original exception.SyntaxErroratawait using: your runtime does not support the syntax. Use TypeScript 5.2 or later, or Node.js 24 or later for plain JavaScript, or callclose()in afinallyblock.
- What sandbox code can do: the parts of Python that scripts can use.
- Answer questions that need dozens of tool calls: the scripts in the guides are Python, and
run unchanged from TypeScript. Write the host functions that they call in TypeScript, and pass them in
externalLookup. - Persist, resume, and fork sessions: how Logfire stores sessions, and for how long.