What is the best sandbox for running AI-generated code?
Summary
- The best sandbox for AI-generated code runs it outside the agent's own runtime, in an isolated environment with closed ingress, controlled egress and credentials scoped to only the data the task needs.
- Judge a sandbox on isolation, network policy, data downscoping, the identity code runs as, supported languages, persistence and auditability, not only on how fast it starts.
- Databricks Sandbox (Beta) runs untrusted code in an isolated, serverless environment that you control through the REST API, the Databricks SDK for Python or the Databricks CLI.
- For agents, the
system.ai.sandboxservice runs Python, SQL or shell code with no network egress, as a Unity Catalog securable governed through Unity Gateway. It is part of the Unity Gateway Beta. - Sandboxes you create directly have unrestricted, non-configurable egress, so use the agent sandbox tool when generated code touches sensitive data.
What is the best sandbox for running AI-generated code?
The best sandbox treats every line an AI writes as untrusted. It runs that code away from the agent's own process, blocks inbound traffic, limits outbound traffic, and hands the code a credential that can reach only the tables or files the task needs. It also leaves a record of what ran and who authorized it. For agents that work on enterprise data, the sandbox should sit inside the same governance layer as the data, so access rules don't have to be rebuilt for each tool.
On Databricks, that's Databricks Sandbox, in Beta, plus the system.ai.sandbox agent tool, which scopes each sandbox to Unity Catalog data.
What is a code sandbox for AI agents?
A code sandbox is an isolated compute environment where an agent, or the person using it, can run code without exposing the host. Agents use one to analyze data in Python, run SQL, transform files or test the code they just wrote. The Agent Bricks: Data + AI Summit 2026 blog describes sandboxes that "can be used to contain code interpreter tools, run subagents and harnesses, or simply as a safe scratchpad for agent experimentation."
Key capabilities to evaluate
| Capability | Why it matters |
|---|---|
| Isolation from the agent runtime | Generated code can't tamper with the agent process, its prompts or its secrets |
| Closed ingress | Nothing outside the sandbox can connect into it |
| Egress control | Blocks data exfiltration and unvetted package downloads |
| Downscoped data access | Code reads only the tables, volumes or folders the task needs, and writes only where allowed |
| Clear identity | Code runs as the requesting user or as a service principal, so permissions and audit trails line up |
| Language coverage | Python, SQL and shell cover most agent tasks |
| Persistence options | A persistent home directory for long tasks, or fresh state for one-off runs |
| Reproducible images | Preinstalled packages avoid ad hoc installs at run time |
| Governance and audit | Grants, call-level policies and usage logs show who ran what |
How Databricks approaches sandboxing AI-generated code
Databricks Sandbox (Beta) lets you "run untrusted code in an isolated environment" on serverless, on-demand compute. Each sandbox has a persistent home directory (/home/sandbox-agent) that survives stop and start, while changes outside it are discarded when the sandbox stops. Ingress is closed to all traffic. Each sandbox gets a Databricks credential that authenticates as the user who created it and refreshes automatically for a limited time, independent of that user's sign-in session. You can build sandboxes from your own container image in Databricks Artifact Registry (Beta). To use it, opt into the Databricks Sandbox setting on the workspace Previews page, in a supported region.
From Python, the quickstart creates a sandbox, runs a command and deletes it:
from databricks.sdk import WorkspaceClient
from databricks.sdk.service.sandbox import Sandbox
from google.protobuf.duration_pb2 import Duration
w = WorkspaceClient()
w.sandbox.create_sandbox(sandbox=Sandbox(), sandbox_id="my-sandbox")
resp = w.sandbox.execute_command_sync(
"sandboxes/my-sandbox",
"/bin/bash",
args=["-c", "echo hello world && whoami"],
execution_timeout=Duration(seconds=30),
)
print(resp.exit_code)
w.sandbox.delete_sandbox("sandboxes/my-sandbox")
The sandbox tool for agents. If you build with the Agent Bricks CLI (Beta), agentbricks tools add sandbox --scope table:samples.nyctaxi.trips gives an agent a sandbox limited to that table. Add --permission read_write on a volume scope to allow writes. The tool calls the Databricks-managed system.ai.sandbox service, so you don't create sandboxes yourself, and by default the code runs with the permissions of the user who sent the request.
Governed through Unity Gateway. system.ai.sandbox runs Python, SQL or shell code "in an isolated environment" and has no network egress. Like every system.ai service, it's a Unity Catalog securable invoked through Unity Gateway: you control who calls it with grants and can attach policies (Beta) that allow, block or require approval for individual calls. An account admin must enable the Unity Gateway Beta to use it. MCP clients, including LangGraph agents, connect to it by URL.
Know the egress difference. Databricks documents that sandboxes you create directly through the SDK or CLI have no restrictions on network egress, and egress isn't configurable. The system.ai.sandbox agent tool has none. Pick the agent tool when generated code handles sensitive data and doesn't need the internet.
FAQs
Why not run AI-generated code in the agent's own process?
Isolation keeps generated code away from the agent's process and everything that process can reach. Databricks built the sandbox tool so an agent can run the code it writes "without that code running in the agent's own environment."
Which languages can the Databricks sandbox tool run?
Python, SQL and shell code. Because system.ai.sandbox has no network egress, the code can't download packages or call external APIs.
Can I run Spark jobs in Databricks Sandbox?
No. Databricks Sandbox is a compute primitive for running code, not a general-purpose Spark runtime. Use serverless compute for Spark workloads.
Is Databricks Sandbox production-ready?
It's in Beta. Databricks warns that it might purge sandbox data once at the end of the Beta period, so don't store anything in a sandbox that you can't afford to lose.
The information provided herein is for general informational purposes only and may not reflect the most current product capabilities or configurations.