What is a code execution tool?

A code execution tool runs code a language model or agent writes, often Python, in an isolated environment with restricted network and file access, returning output, errors and files; calculations are actually performed rather than guessed as text.

Unlike an AI agent sandbox, the isolated environment where code runs, the code execution tool is the interface letting the model use that environment. The model writes the code; the tool runs it in the sandbox.

Topic
Agents and protocols
Also known as
Code interpreter, code execution sandbox

How a code execution tool works

  1. When a calculation, data analysis or file conversion is needed, the model writes code and calls the tool.
  2. Code runs in an isolated environment such as a container or virtual machine with limited CPU, memory and time; network and file access are restricted by policy.
  3. Output, error messages and generated files return to the model, which can see an error and fix the code.
  4. Each run is logged with its code and result.

In more detail

Isolation architectures: standard containers versus microVMs, gVisor and WASM

Standard OS containers share the host Linux kernel, leaving a large kernel attack surface if prompt-injected code attempts a breakout. Production agent platforms therefore isolate execution inside lightweight microVMs (e.g., Firecracker, Kata Containers), user-space kernels (gVisor) or WebAssembly (WASI) runtimes with read-only root filesystems and no raw socket privileges.

Stateless versus stateful sessions and tenant isolation

An execution tool can be stateless, spinning up a fresh sandbox per call or stateful, keeping variables and intermediate files alive across turns within a single user session. Session-scoped state speeds up iterative data analysis, but requires strict per-user tenant isolation and automatic teardown so leftover files never leak across users.

Indirect prompt injection, keeping secrets out and package restrictions

External documents retrieved into the prompt can contain hidden instructions (indirect prompt injection) that trick the model into writing code that reads environment variables, scans mounted paths or exfiltrates data via generated URLs. Defences include keeping secrets completely outside the sandbox, blocking outbound DNS/HTTP and disabling runtime package installation (`pip install`).

Self-correction loops and standard tool protocols

Code execution tools frequently work alongside deterministic verification and repair loops: when a script fails with a traceback or violates a sanity check, the model reads the error and patches the code up to a bounded retry budget. Standardised tool interfaces (such as MCP servers and managed container execution APIs) make these execution loops portable across models.

Commonly confused terms

TermHow it differs
Function or tool callingGeneral function calling invokes pre-defined, fixed-parameter APIs (e.g., `get_invoice(id)`). A code execution tool gives the model Turing-complete flexibility to write arbitrary scripts, greatly increasing capability, while also widening the security attack surface.
Coding assistantAn LLM code assistant writes or edits code in a developer IDE with human review before execution. A runtime code execution tool runs model-generated code autonomously inside an automated agent loop to compute answers or transform data.

Why a code execution tool matters for on-premises AI

Language models can err in arithmetic and calculations over large data; leaving calculation to code makes results more reliable and repeatable. Research shows that agents expressing actions as executable code improve success on many tasks. But a model may write code under the influence of external content, so the environment keeps network access off or limited, has no direct access to company systems and has capped resources. Run in-house, data stays inside.

ON-PREMISES EXAMPLE

Asked for portfolio returns, an investment firm's assistant writes and runs code in isolation instead of guessing; results go to the analyst, with the code kept for audit.

Common questions

Why is a code execution tool needed?

Language models can err in calculation. Leaving it to code that actually runs makes results accurate and repeatable.

Is a code execution tool safe?

With proper isolation risks fall sharply. Network and file access are restricted, resources capped and every run logged.

Sources

In the glossary: Glossary, A to C

What does this term mean for your infrastructure?

We can work out together in a technical assessment how it maps onto your data, hardware and security requirements.

Request a technical assessment →

This site sets no cookies. Visitor numbers are counted in aggregate using a cookieless method that does not identify anyone. See the Privacy Policy for details.