Sandbox
The sandbox gives an agent the run_code tool to run Python. With tool code mode, that code calls the agent's tools.
On this page
Enabling sandbox gives an LLM agent the run_code tool. The agent writes Python, the sandbox runs it, and the agent gets back the value of the last expression and anything the code printed. Agents use it to calculate, parse CSV and JSON, compare dates, and transform data instead of doing that work in the model.
version: "1.0"
name: order-analyst
type: llm
model: connic/claude-sonnet-5.5
description: "Answers questions about orders"
system_prompt: |
Answer questions about orders. Calculate with run_code.
tools:
- file_read
- file_write
sandbox:
enabled: trueWhat the code can use
Code runs as Python 3.14 with these standard library modules: asyncio, base64, bisect, collections, copy, csv, dataclasses, datetime, decimal, functools, hashlib, itertools, json, math, random, re, statistics, time, typing and unicodedata. There is no io module, so csv reads a list of lines, such as text.splitlines().
The sandbox has no network, no file system, no environment variables and no other packages. Code reaches data only through the agent's file tools and, in tool code mode, its other tools.
Variables, functions and imports stay defined between run_code calls in the same run, so the agent can load data once and work with it over several steps. The sandbox starts empty for a new run and when a run resumes after an approval. If the code raises a runtime error, the result includes a Python traceback.
| Limit | Value |
|---|---|
| Run time | 30 seconds per run_code call. Time spent waiting for tools doesn't count. |
| Memory | 256 MiB per run_code call |
| Printed output | 100,000 bytes per run_code call |
| Result | 100,000 characters for the result and printed output as JSON |
| Tool calls | 100 per run_code call |
After the time or memory limit, the variables from before that call are kept and nothing the call defined is.
Working with files
File tools listed under tools are available as functions in run_code unless configured under approval.tools. Call them with keyword arguments. file_read returns metadata and raw bytes. file_write accepts text, bytes, or a run attachment via input_file. The agent's access rules apply to every call. File tools are also available directly, so the agent can still read an image or PDF itself.
import csv
export = await file_read(path="exports/orders.csv")
rows = list(csv.DictReader(export["content"].decode().splitlines()))
late = [row for row in rows if int(row["days_open"]) > 7]
await file_write(
path="reports/late-orders.csv",
content="id,total\n" + "\n".join(f"{row['id']},{row['total']}" for row in late),
)
len(late)Tool code mode
With tool_code_mode: true, run_code exposes compatible tools as async functions. The run_code description lists them as Python signatures built from their parameters and return types:
tools:
- shop.list_orders
- shop.get_customer
sandbox:
enabled: true
tool_code_mode: trueasync def get_customer(*, customer_id: str) -> dict[str, Any]:
"""Get a customer's profile, including their tier (gold, silver or bronze).
Args:
customer_id: The customer's id, such as "C007"."""The agent can then fetch, filter, join and aggregate in one step. Tool results stay in the sandbox, and only the answer goes back to the model. This saves tokens and model calls on work that needs several tool calls or large results:
import asyncio
orders = await list_orders(status="open")
customer_ids = {order["customer_id"] for order in orders}
customers = await asyncio.gather(*(get_customer(customer_id=cid) for cid in customer_ids))
gold = {c["id"] for c in customers if c["tier"] == "gold"}
late = [order for order in orders if order["customer_id"] in gold and order["days_open"] > 14]
sorted(order["id"] for order in late), round(sum(order["total"] for order in late), 2)Each call from code runs like a direct tool call: tool hooks, access rules and test mocks apply, and the call appears in the trace under the run_code span. Pass arguments by keyword. When a call names the tool and its arguments explicitly, run_code checks for unknown and missing arguments before executing the code.
With discoverable tools, the tool search returns the signature of each tool the code can call, and the agent calls it in run_code.
Tools that need approval or ask for human input, MCP tools with an app UI, app_publish and app_preview, the tools that connect a user's account to an MCP server, and the browser tools stay separate tools in tool code mode.
Voice agents can use run_code. Tool code mode is not available for them.