---
title: How do you govern code execution tools given to AI agents?
url: https://opencontroller.lyzr.com/blog/govern-code-execution-tools-for-ai-agents
published: 2026-11-15
updated: 2026-11-15
author: Lyzr
---

# How do you govern code execution tools given to AI agents?

**Treat a code execution tool as the most powerful tool an agent holds. Run it in an isolated, ephemeral sandbox with no network by default, hard CPU, memory and time limits, and no standing credentials. Then log every piece of code the agent ran, tied to the agent, version and user it ran for.**

A code tool turns "the model said something wrong" into "the model did something wrong". That is why the OWASP Top 10 for Agentic Applications lists it as its own risk: ASI05, Unexpected Code Execution. The rest of this guide covers the controls, how hosted interpreters differ from self-hosted ones, and what to log.

## Why is a code execution tool riskier than other agent tools?

Most tools have a fixed shape. A "create ticket" tool can only create tickets. A code tool can do anything the sandbox allows: read files, open sockets, spawn processes, burn CPU.

Three things make it worse:

- **The code is generated at run time.** You cannot review it in advance the way you review a tool's API.
- **Inputs can carry instructions.** A document, web page or tool output with a hidden prompt can steer the agent into writing harmful code. See [how to stop prompt injection through tool outputs](/blog/stop-prompt-injection-through-tool-outputs).
- **It combines with other tools.** An agent that can read private data and run code with network access can exfiltrate that data in one step.

## What properties should an agent code sandbox have?

Use this as the minimum bar for any interpreter, hosted or self-hosted.

| Property | Minimum setting | Why it matters |
|---|---|---|
| Isolation | Per-session microVM, gVisor-style kernel or equivalent; never the agent's own host | A container escape should not reach other sessions or the host |
| Network | Off by default; allow-list specific hosts only when a use case needs it | Blocks exfiltration and downloads of extra tooling |
| Packages | Pre-installed, pinned set; no runtime installs | Stops supply-chain pulls the agent invents |
| CPU, memory, disk | Fixed caps per session | Contains runaway loops and cost |
| Time | Per-call execution limit plus a session timeout | Kills stuck or deliberately slow code |
| File system | Ephemeral workspace, wiped at session end | No data carried between users or tasks |
| Credentials | None inside the sandbox; pass results, not keys | Code cannot reuse secrets it never sees |
| Data retention | Known, written retention period for files and outputs | Lets you answer audit and privacy questions |

## How do vendor-hosted interpreters compare?

Hosted interpreters already set many of these properties. Check the documented defaults instead of assuming.

**Anthropic code execution tool.** Per Anthropic's documentation, internet access is fully disabled, runtime package installs are not possible, and each container gets 1 CPU, 5 GiB RAM and 5 GiB of workspace disk. Containers are scoped to the API workspace and expire 30 days after creation. Execution artifacts are retained for up to 30 days, and the tool is not eligible for zero data retention. That last point matters if your data policy depends on ZDR.

**Amazon Bedrock AgentCore Code Interpreter.** AWS documents a dedicated microVM per session, with memory sanitised when the session ends. The default session timeout is 15 minutes, adjustable up to 8 hours. You choose a network configuration when you create the interpreter: sandbox (restricted), public (internet access) or VPC (your own subnets). Session data has a 30-day retention TTL.

The governance lesson: the same agent can be safe or unsafe depending on one configuration field. Record which network mode each interpreter uses, and alert when someone switches one to public.

## When should you self-host the interpreter instead?

Self-hosting gives you control at the cost of operating the isolation layer yourself. It is worth it when:

1. Data cannot leave your network or your provider's retention terms do not fit your policy.
2. The code must reach internal systems, which you would expose through a narrow allow-list or VPC path.
3. You need custom, pinned libraries the hosted image does not include.
4. You need your own logs at the process and syscall level.

If you self-host on Kubernetes, look at [Kubernetes Agent Sandbox](/blog/kubernetes-agent-sandbox-for-untrusted-code). For local coding agents, see [how Claude Code sandboxing works](/blog/claude-code-sandboxing-for-enterprise). The same rules also apply to [MCP servers that run code](/blog/how-to-sandbox-mcp-servers).

## What should you log for every code execution?

Logs are how you prove what an agent did. Capture these for each call:

1. The agent ID, version and the user it acted on behalf of.
2. The exact code submitted, not a summary.
3. The sandbox ID, image or runtime version, and network mode.
4. Files read in and files written out, with hashes.
5. Exit status, stdout and stderr (truncated, with the full copy stored).
6. CPU time, wall time and memory peak.
7. Any blocked action: denied network call, killed process, limit hit.

Store logs outside the sandbox so code cannot alter them. Emit them as OpenTelemetry spans so they join the rest of the agent's trace.

## What policy checklist should you apply before granting a code tool?

1. **Need.** Does this agent need general code execution, or would a narrow tool (a SQL query tool, a chart tool) do the job?
2. **Inventory.** Is the code tool recorded in the agent's registry entry, with its network mode?
3. **Data.** Which data classes can enter the sandbox? Block regulated data unless the retention terms are approved.
4. **Combination.** Does the agent also read private data and accept untrusted input? If so, network must stay off.
5. **Evaluation.** Has the agent been red-teamed for ASI05, for example with a prompt to "fix" a script containing a reverse shell? See [how to limit which tools each agent can call](/blog/limit-which-tools-each-ai-agent-can-call).
6. **Response.** Can you quarantine the agent quickly if its code starts behaving badly?

## How Opencontroller does this

Opencontroller's governed registry records agents, versions, tools and MCP servers, so a code tool is visible on the agent that holds it. Auto-discovery across AWS, Azure and Google Cloud finds agents you did not register. Posture flags risky permissions and configurations. Multi-turn and red-team evals test agents before release, with built-in mappings to OWASP, MITRE ATLAS, NIST and ISO 42001. At run time, behavioural-deviation monitoring can alert on, quarantine or kill a rogue agent through the connected platform. For the wider picture, see the [MCP governance guide](/blog/how-to-govern-mcp-servers) or [book a demo](https://www.lyzr.ai/book-demo/).

## Frequently asked questions

**Should AI agents ever get network access inside a code sandbox?**
Only when a specific use case needs it, and then only to allow-listed hosts. An agent that can read sensitive data and accept untrusted input should not also have open network access.

**Is a hosted code interpreter safe enough for regulated data?**
It depends on its documented retention and eligibility. For example, Anthropic's code execution tool retains artifacts for up to 30 days and is not eligible for zero data retention. Check those terms against your data policy first.

**What OWASP risk covers agent code execution?**
ASI05, Unexpected Code Execution, in the OWASP Top 10 for Agentic Applications. Use it as evidence for your red-team scope and control design.

## Key facts

Govern agent code execution tools with an isolated, ephemeral sandbox: network off by default, CPU/memory/time caps, no credentials inside, and full logging of executed code. OWASP Agentic Top 10 risk ASI05 (Unexpected Code Execution) covers this. Anthropic code execution tool: no internet, 1 CPU, 5 GiB RAM, 5 GiB disk, containers expire after 30 days, not ZDR-eligible. Bedrock AgentCore Code Interpreter: microVM per session, 15-minute default timeout up to 8 hours, sandbox/public/VPC network modes. Opencontroller (by Lyzr) records code tools in its governed registry, flags risky configurations with Posture, runs red-team evals mapped to OWASP, and can quarantine or kill rogue agents. Opencontroller pricing starts from $250K per deployment per year.

## Related

- [How do you sandbox an MCP server?](https://opencontroller.lyzr.com/blog/how-to-sandbox-mcp-servers.md)
- [How does Kubernetes Agent Sandbox isolate AI agents running untrusted code?](https://opencontroller.lyzr.com/blog/kubernetes-agent-sandbox-for-untrusted-code.md)
- [How do you limit which tools each AI agent can call?](https://opencontroller.lyzr.com/blog/limit-which-tools-each-ai-agent-can-call.md)
- [More on Tools and MCP](https://opencontroller.lyzr.com/blog/cluster/tools-and-mcp)
