execute_code tool backed by a SandboxRuntime interface. The LLM writes a short self-contained script, passes it to the tool, and reads the actual output. The sandbox handles isolation — the SDK never runs code itself.
What the LLM does vs. what the sandbox does
| LLM | Your sandbox |
|---|---|
| Decide when to run code | Execute the script |
| Write a self-contained script | Enforce resource limits |
Read output and explain it to the user | Isolate from host filesystem and network |
Retry with a fixed script on non-zero exit_code | Return stdout + stderr and exit code |
execute_code first.
Example: Code Execution.
Tool pattern
Execute blocks until the sandbox returns ExecutionResult{Output, ExitCode}. The LLM reads output from the result and replies to the user.
Sandbox options
ImplementSandboxRuntime once per environment. The example ships two; add more by following the same pattern:
| Sandbox | Use case | Activate |
|---|---|---|
| Local os/exec | Development, CI, trusted scripts | default (no env var) |
| Docker | Isolated execution on your host | SANDBOX_ENV=docker |
| AWS Lambda | Serverless, per-request isolation | implement LambdaRuntime |
| Modal / E2B | Cloud sandboxes with language images | implement ModalRuntime / E2BRuntime |
| Firecracker / microVMs | Maximum isolation, low latency | implement FirecrackerRuntime |
main.go changes when you swap runtimes — code_tool.go and the agent config stay the same.
Docker sandbox isolation
The example Docker runner passes safety flags todocker run:
--cpus, --read-only, and a non-root user.
Timeout layers
| Layer | What it limits | Configure |
|---|---|---|
Per Run context | Entire agent call | context.WithTimeout — always wins |
| Agent run | Whole turn | WithTimeout |
| Tool execute | Single execute_code call | WithToolExecutionConfig |
| Sandbox | Script execution wall time | Your runtime config / Docker --stop-timeout |
System prompt guidance
Tell the LLM not to make up output — it must call the tool first:Examples
Code Execution
execute_code tool with local and Docker runtimes
Execution config
Per-operation timeout overrides
Related
Execution config
Tool execute timeout and max attempts
Deterministic Execution
Running predefined workflows from the agent
Tools
Custom tool implementation