How do I let an AI agent run code securely with access to my data?
Summary
- Give the agent a sandbox tool instead of letting it run code where it lives. The sandbox should be isolated, have no network egress, and be scoped to the specific tables, volumes or folders the task needs.
- Default to read-only scopes, and grant write access only to a dedicated output location.
- Run the code as the user who made the request when people should see different data, so existing Unity Catalog permissions and audit trails apply. Use a service principal only when every user should get the same access.
- Govern every call: who may invoke the tool, which calls are allowed, how often, and an audit record of what ran.
- On Databricks, the Agent Bricks CLI (Beta) adds a scoped sandbox tool backed by
system.ai.sandbox, which runs Python, SQL or shell code with no network egress and is part of the Unity Gateway Beta.
How do I let an AI agent run code securely with access to my data?
Don't hand the agent a general-purpose environment and your credentials. Give it a sandbox tool: an isolated place to run the code it writes, with no route to the internet, scoped to the exact data the task needs and read-only by default. Run that code under the requesting user's identity so your existing permissions decide what it can touch, and put every call behind grants, policies and audit logs. Code an agent writes can be wrong or can do more than you asked, so the controls have to hold even when the code doesn't behave.
On Databricks, you add that tool with agentbricks tools add sandbox. It calls the managed system.ai.sandbox service, which is governed by Unity Catalog and Unity Gateway. Both the Agent Bricks CLI and system.ai.sandbox are in Beta.
Key controls to put in place
| Control | Why it matters |
|---|---|
| Isolation from the agent runtime | Generated code can't reach the agent's own process, prompts or secrets |
| No network egress | Data the code reads can't be sent to an outside endpoint |
| Data downscoping | The code sees only named tables, volumes or folders, not everything the agent could reach |
| Read-only by default | Mistakes can't overwrite source data; writes go to one output location |
| Per-user identity | Each user's existing permissions apply, and audit logs show who asked |
| Invocation grants | Only approved users and agents can call the code tool at all |
| Call-level policies | Block or require approval for calls that carry sensitive data |
| Rate limits | A runaway loop can't flood the tool |
| Usage and audit logs | You can reconstruct what ran, for whom and when |
How Databricks approaches secure code execution for agents
1. Turn on the previews. The Databricks Sandbox docs carry a workspace Previews banner (Beta), and the system.ai.sandbox docs say an account admin must enable the Unity Gateway Beta before agents can use it. Your workspace must be in a supported region.
2. Scope the data. With the Agent Bricks CLI, you configure only which data the sandbox can access and whether it can write:
# Read one table.
agentbricks tools add sandbox --scope table:samples.nyctaxi.trips
# Read a volume and a workspace folder.
agentbricks tools add sandbox --scope volume:main.default.reports --scope workspace:/Workspace/Shared/data
# Allow writes to a volume.
agentbricks tools add sandbox --scope volume:main.default.output --permission read_write
The command records the tool in the project's agent.toml as a downscope policy. The docs' example binding for a single-table scope records permission = "read_only". You don't create a sandbox first: at runtime, the agent calls system.ai.sandbox, which runs the code in a sandbox for you.
3. Pick the identity. By default, the sandbox runs with the permissions of the user who sent the request, the user authorization model that Databricks describes for user-specific permissions, audit trails and fine-grained access control with Unity Catalog. Add --auth app to run as the app's service principal instead; agentbricks deploy then grants that principal access to the scoped resources.
4. Keep data inside. system.ai.sandbox has no network egress, so generated code can't download packages or call external APIs. Use it for work on the scoped data, such as analysis in Python or SQL.
5. Govern the tool. Each system.ai service is a Unity Catalog securable invoked through Unity Gateway. Governance controls include:
EXECUTEgrants on the service, plusUSE CATALOGandUSE SCHEMAon its parents. Databricks-provided services can inherit a defaultEXECUTEgrant onsystem.ai, so review those broad grants.- Service policies (Beta) that evaluate a call before it runs and can evaluate its result, then allow it, block it or require approval. The built-in Sensitive Data Detection policy can block calls when tool arguments or results contain personally identifiable information.
- Rate limits in queries per minute; calls over the limit are rejected.
6. Watch what ran. Query system.ai_gateway.usage with service_type = 'MCP_SERVICE' for call volume and errors, and review mcpCall events in system.access.audit.
Not using the Agent Bricks CLI? A LangGraph agent can connect to the same service through DatabricksMCPServer and the URL https://<workspace-hostname>/ai-gateway/mcp-services/system.ai.sandbox.
FAQs
Can the agent's code send my data to the internet?
Not through system.ai.sandbox, which has no network egress. Sandboxes you create directly with the SDK or CLI are different: Databricks documents that their egress is unrestricted and not configurable, so use the agent tool for code that handles sensitive data.
When should the sandbox run as a service principal instead of the user?
When all users should have the same access to the scoped resources. Databricks calls this app authorization; user authorization fits user-specific permissions and audit trails.
Who can use the sandbox tool?
Callers need EXECUTE on the service and access to the underlying data. system.ai.sandbox isn't available to users who have only the consumer access entitlement.
What about the older system.ai.python_exec function?
Databricks now labels the system.ai.python_exec integration through MCP as legacy. New agents should use system.ai.sandbox.
The information provided herein is for general informational purposes only and may not reflect the most current product capabilities or configurations.