
What Are Code Execution Agents and How Sandboxed Interpreters Let LLMs Run Their Own Code
Code execution agents are LLMs that write and run Python inside sandboxed containers. CodeAct showed up to 20% higher task success than JSON tool calling.
Most agents you have already met take action through function calls: the model writes a tidy JSON blob saying “call search_database with these arguments,” and your application is responsible for dispatching that call, formatting the result, and feeding it back. A code execution agent flips this. The model writes a short program — usually Python — and a sandbox actually runs it. The return value becomes the next observation in the reasoning loop. The action space is the language itself.
JSON tool calls compose badly. If you need to fetch three rows, join them on a key, filter by date, and average a column, the agent has to chain four or five separate tool calls, with the model re-reading intermediate JSON each time. A few lines of Python does the same work in one step and one round trip. According to the CodeAct paper (Wang et al., ICML 2024), this pattern produced up to 20% higher task success and roughly 30% fewer steps versus JSON-style actions across 17 evaluated LLMs.
The runtime side matters as much as the model side. Because the agent is generating real code, that code has to execute somewhere that cannot harm the host machine. Modern implementations rely on isolated sandboxes — Firecracker microVMs, container-based environments, or vendor-managed interpreters — that boot fast, expose a controlled filesystem, restrict network access, and tear down cleanly between sessions. The pairing of “code as the action format” plus “interpreter that is safe to run untrusted code in” is what makes the whole pattern viable in production.
The most common entry point for a non-engineer is a chat interface that quietly runs Python for you. When you upload a CSV to ChatGPT and ask “which region had the largest drop in conversions last quarter?”, a code execution agent is what answers. The model writes a pandas snippet, the sandbox runs it on your file, and you get back a chart and a sentence. According to the OpenAI API Docs, this lives under the Code Interpreter tool on the Responses API. According to the Claude API Docs, Anthropic exposes an equivalent code execution tool that runs Python and Bash in a sandboxed container.
Developers building products on top of these APIs use the same pattern for data analysis features, document parsing, on-the-fly calculations, generating downloadable files, and any task where the cleanest expression of the work is a few lines of code rather than a forced sequence of JSON calls.
Treat the sandbox as a hostile environment that happens to live inside your product. Set per-call timeouts, cap memory, restrict network egress to an explicit allowlist, and never share a sandbox session across two end users. Most production incidents with code execution agents come from skipping one of these four steps.
Start with how sandboxed interpreters let LLMs run their own code — it explains why letting the model write and execute Python inside a container often beats structured tool calling, and what a minimal stack looks like. Then read the prerequisites, from ReAct loops to microVM isolation: it lays out the three layers — reasoning loop, sandbox runtime, hardware isolation — that have to exist before any of this is safe, which is what separates a toy demo from a defensible security posture.
Before you build anything, the honest limits read is worth the ten minutes: cold-start latency, flaky benchmark tests, and shrinking effective context are structural, not vendor-specific, so budget for them rather than debug around them later. When you’re ready to wire one up, the E2B, Daytona, and Claude Agent SDK guide specifies the sandbox contract — lifecycle, network rules, timeouts — before a single line of tool code. For the market context behind that choice, the 2026 SWE-bench race coverage shows why the harness wrapped around a model now moves benchmark scores more than swapping the model itself. Close with the accountability question — if your agent will ever touch production credentials, read it before you ship, not after.

Three neighbours get folded into this topic, and each mix-up sends the design in the wrong direction.
| Scenario | Use | Avoid |
|---|---|---|
| Multi-step data manipulation on a user-supplied file | ✅ | |
| Triggering an idempotent action in a system of record (charge, refund, ticket) | ❌ | |
| Chained math, transformations, or library-flavored work (pandas, numpy) | ✅ | |
| Strict, schema-bound integrations where you need an audit trail of typed calls | ❌ | |
| Prototyping a workflow before you know which discrete tools you actually need | ✅ | |
| Single-shot API calls with no chaining and no computation | ❌ |
Q: How is a code execution agent different from a tool-calling agent that returns JSON? A: A tool-calling agent emits one structured call per LLM turn and waits for the result before deciding again. A code execution agent writes a script that chains several operations in one pass, calling the model only when a new decision is needed — fewer round trips, but more damage per turn if the sandbox isn’t isolated. See how sandboxed interpreters let LLMs run their own code.
Q: Is the Claude Agent SDK’s built-in Bash tool safe to use in production? A: No — it runs inside your own process, not a sandbox, so model-written code executes with your process’s own permissions. Use it for local development only; in production, replace it with a custom tool that routes execution through E2B or Daytona, as the build guide specifies.
Q: Do I need microVM isolation, or is a container sandbox enough? A: It depends on what the model is allowed to do: a standard container shares the host kernel, fine for trusted, narrow tasks, but model-written code is untrusted by definition. The prerequisites read covers why production stacks add a hardware-level boundary like Firecracker or gVisor underneath.
Q: Should I evaluate a code execution agent by its base model or by its scaffold? A: By the scaffold first. The same underlying model can post very different SWE-bench results depending on the harness wrapped around it, so a leaderboard comparison across vendors is really a comparison of engineering, not just model quality. The 2026 SWE-bench race breaks down what shifted.
Q: Who is accountable when a code execution agent breaks something in production? A: Legally and organizationally, nobody has fully answered that yet — the credentials were authorized, but not for the specific action the agent took. The accountability piece traces a real incident where an agent deleted a production database during a declared code freeze.
Part of the AI agent architecture theme · closest neighbour: agent planning and reasoning. Coming to sandboxed execution from a software background? Start with the story: Debugging Agents: Reconstruct the Decision Path, Not a Stack Trace.
Code execution agents close the loop between writing code and seeing what it does. Understanding their ReAct cycles, sandbox primitives, and failure modes is the foundation for everything else.
Concepts covered

Code execution agents are LLMs that write and run Python inside sandboxed containers. CodeAct showed up to 20% higher task success than JSON tool calling.

Code execution agents fail at three limits in 2026: sandbox cold-start vs isolation, flaky benchmark tests, and context collapse on long-horizon tasks.

Building a code execution agent requires three layers: a ReAct-style reasoning loop, a sandbox runtime, and microVM or gVisor isolation underneath.
Practical guides walk through wiring a code agent to a real sandbox provider, handling state between runs, and shipping it without burning your laptop or your cloud bill.
Tools & techniques

A code execution agent has three layers: Claude Agent SDK, a tool wrapper, and a sandbox like E2B or Daytona. The built-in Bash tool runs on your host.
The frontier is moving fast: new SWE-bench scores, new sandbox vendors, and shifting opinions on autonomy levels arrive every few weeks.
Models & benchmarks
Updated May 2026

Code execution agents diverge by ~17 points on identical base models. Scaffolding — not the LLM — now decides which agent ships production code in 2026.
When an LLM runs code it wrote, accountability gets blurry. Supply-chain attacks, data exfiltration, and silent failures all change shape, and so do the questions you should ask before deploying.
Risks & metrics

AI coding agents now write and run code in production, sometimes deleting live databases. The accountability chain has no named owner when they fail.