Skip to main content
The problem: You want the agent to answer questions by running real code — computations, data transformations, script verification — without trusting LLM-hallucinated output and without bloating the context window with raw data. The solution: Expose a custom 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

The LLM never invents output — it always calls 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

Implement SandboxRuntime once per environment. The example ships two; add more by following the same pattern: Only 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 to docker run:
Adjust these to match your security policy. In production, also add --cpus, --read-only, and a non-root user.

Timeout layers

System prompt guidance

Tell the LLM not to make up output — it must call the tool first:
Example: Code Execution.

Examples

Code Execution

execute_code tool with local and Docker runtimes

Execution config

Per-operation timeout overrides

Execution config

Tool execute timeout and max attempts

Deterministic Execution

Running predefined workflows from the agent

Tools

Custom tool implementation