Skip to main content
Shows how to give an agent a code execution capability using a custom execute_code tool. The LLM writes a short script, the sandbox runs it, and the output is returned for the LLM to explain. The sandbox is pluggable — swap the runtime without changing the tool. Source: examples/agent_with_code_execution/

What it demonstrates

  • execute_code custom tool — interfaces.Tool with language and code parameters
  • SandboxRuntime interface — pluggable sandbox (local os/exec, Docker, cloud APIs)
  • LLM writes code, runs it, reads output, replies — never makes up results
  • Sandbox isolation — Docker runner uses --network none and memory limits

Run

From examples/: Local sandbox — needs Python or Node installed on host:
Docker sandbox — isolated, no host interpreter needed:
Requires LLM_APIKEY in examples/.env.

Key code

Execute blocks until SandboxRuntime.Execute returns the program output and exit code.

Expected output

Learn more

Code Execution in Agents

Sandbox options, isolation, timeout config, cloud runtimes

Execution config

Tool execute timeout and max attempts