Tools · Files and code

run_code

Changes things

Execute Python or shell code in a local subprocess workspace.

When to use: Use this for deterministic local execution, transformations, parsing, and script-based validation.

Example

Run for real by scripts/tool-examples.py in a small demo project, with Haiku 5.5 where the tool calls a model. The output is exactly what the tool returned.

The call

tool call
{
  "tool": "run_code",
  "arguments": {
    "language": "python",
    "code": "import statistics\nprices = [12, 29, 79]\nprint('mean', statistics.mean(prices), 'median', statistics.median(prices))"
  }
}

Real output

exit_code: 0
stdout:
mean 40 median 29
stderr:

Parameters

NameTypeDescription
language*stringInterpreter to usebashjavascriptluaperlphppythonrrubyshtypescriptzsh
code*stringSource code to execute
timeout_secondsnumberOptional execution timeout
sandboxbooleanRun inside a disposable Docker container with --network none and a read-only rootfs. Safer for untrusted snippets. Requires `docker` on PATH.
networkbooleanWhen sandbox=true, allow outbound network. Default false.
imagestringWhen sandbox=true, override the default Docker image for the chosen language.
workspace_rootstringOverride the workspace directory for this call. Lets specialists (developer, debugger, designer, researcher) work inside the user's project instead of the shared default. Absolute or cwd-relative path.

* required

Read, search and edit the project; run commands, code and git.