# Code execution

Run Python or JavaScript inside workflows with the Code node, and let agents write and run code with the Code Interpreter tool, in a locked-down Docker sandbox.

Source: https://glamorous-boar.cloudvent.net/code-execution/

---

## Code node

The **Code** node runs Python or JavaScript on its connected value.

<CodeTabs showCopy={true}>
<CodeTab label="Python" language="python">

```
# `inputs` holds the connected value. Set `result` to a JSON value.
numbers = inputs or [3, 1, 4, 1, 5, 9]
result = {"count": len(numbers), "mean": sum(numbers) / len(numbers), "max": max(numbers)}

# Or define main(inputs) and return the value:
# def main(inputs):
#     return sorted(inputs)
```

</CodeTab>
<CodeTab label="JavaScript" language="js">

```
// The code is an async function body; `inputs` is the connected value.
const rows = inputs ?? [];
const byRegion = {};
for (const r of rows) byRegion[r.region] = (byRegion[r.region] ?? 0) + r.amount;
return byRegion;
```

</CodeTab>
</CodeTabs>

Outputs: `result`, `stdout` (what the code printed), `error` and `report` (backend, duration, exit code, notes, stderr). With `on_error: fail` an exception fails the node (`CODE_FAILED`, with the traceback); with `output` it is returned in `error` instead. Settings: `language`, `code` (a monospace editor where Tab indents), `timeout_seconds`, `memory_mb`, `network`.

## Code Interpreter Tool (for agents)

Connect a **Code Interpreter Tool** to an Agent's `tools`. The agent calls `run_code` with `code` and optional `inputs`, gets back `{result, stdout}`, or the exception and traceback as a tool error, so it can fix its code and retry. Each call starts fresh. It needs Agent 1.2.0, Tool Router 1.1.0 or Tool Chain 1.1.0 (older versions refuse it).

Template *Analyse data with code (sandbox)*: a Code node summarises sales by region, and an analyst Agent answers questions like *How much higher is North's average price than South's?* by writing Python.

## The sandbox

The **operator** chooses the backend with `AGENTICMIND_SANDBOX`, never the graph, so an imported project cannot weaken isolation.

| Backend | Isolation |
|---|---|
| `docker` (default) | A fresh container per execution: `--network none`, read-only root filesystem with a 64 MB `/tmp`, memory limit (swap = memory), CPU limit, `--pids-limit 64`, `--cap-drop ALL`, `no-new-privileges`, unprivileged user 65534, killed on timeout. Images: `python:3.12-slim`, `node:22-slim`. |
| `process` | For trusted code on your own machine: isolated Python (`-I`), Node with its permission model (no file access, no child processes), minimal environment (none of the server's keys), temporary directory, timeout; resource limits on POSIX. **Does not block network and is not a security boundary.** |
| `off` | Code nodes fail with `SANDBOX_DISABLED`. |

Operator caps: `AGENTICMIND_SANDBOX_MAX_TIMEOUT` (300 s) and `AGENTICMIND_SANDBOX_MAX_MEMORY_MB` (2048) override node settings. Network is only possible when `AGENTICMIND_SANDBOX_ALLOW_NETWORK=1` *and* the node asks for it.

### Using libraries

Only the standard library is available by default (no pip installs at run time; there is no network). Build an image with what you need and point the sandbox at it:

<CodeBlock language="bash" filename=".env" showCopy={true}>

```
AGENTICMIND_SANDBOX_PYTHON_IMAGE=my-registry/python-pandas:3.12
```

</CodeBlock>

## Limits

- About a second of overhead per Docker execution; nothing persists between calls (no notebook sessions).
- No file inputs or outputs yet.
- Program of thought in the Reasoning node evaluates arithmetic only; use the Code tool for general computation.

See [Security model](/security/#sandboxed-code) for how this fits the platform's threat model.
