# Code mode

> How agents run short Python scripts against your API through run_script — the allowed subset, stubs, dry run, limits and the security layers.

> Pre-release: Neurouter v0.1 is in development; this page documents the v0.1 design.

Source: https://neurouter.com/docs/code-mode · Last updated: 2026-11-26

In code mode the agent sends one short Python script instead of many tool calls. The script calls your tools as functions, such as `acme_orders.list_orders(status="unpaid")`, and returns a small JSON result. Every inner call goes through the same policy pipeline as a direct call.

## When it is offered

A server's `exposure.mode` decides what `tools/list` returns: `tools` (tools only), `code` (`search_tools` + `run_script`), `both`, or `auto` (both when code mode is available, else tools). `run_script` is listed only when code mode is enabled globally, for the server and for the environment, the executor is healthy, and the caller has the `code:run` scope.

A client can narrow its own surface with the `X-Neurouter-Exposure: code` (or `tools`) request header, whatever kind of credential it uses; the console's **Connect** page offers code-mode-only snippets that set it. When code mode is not available to that caller, `code` falls back to tools.

## The `run_script` tool

| Argument | Meaning |
|---|---|
| `code` | The script, up to 16 KiB |
| `mode` | `execute` (default) or `dry_run` |
| `inputs` | Values bound as global variables |

The last expression or a top-level `return` is the result. Signatures come from `search_tools`, which returns Python stubs when code mode is on.

```python
orders = acme_orders.list_orders(status="unpaid", limit=200)
late = [o for o in orders if ops.days_since(o["due_date"]) > 30]
for o in late:
    acme_orders.add_order_note(order_id=o["id"], text="Reminder sent")
return {"late": len(late)}
```

## The allowed subset

- Keyword arguments only; tools are already available as `<namespace>.<function>`.
- Not allowed: `import`, `class`, `global`/`nonlocal`, `async`/`await`/`yield`, dunder access, and `eval`, `exec`, `open`, `getattr` and similar built-ins.
- Allowed built-ins: `len range enumerate zip sorted reversed min max sum any all abs round int float str bool list dict set tuple isinstance print`.
- Helpers: `ops.parallel`, `ops.days_since`, `ops.now`, `ops.log_decision`, `ops.report_unmet_need`, `ops.report_issue`.
- Tool errors arrive as `RuntimeError("<code>: <message> (error_id=…)")`, which scripts can catch.

Scripts are type-checked against the stubs before any call is made, so a wrong argument name fails fast with a line and column.

## Dry run

With `mode="dry_run"`, write calls are not executed: each one returns `{"dry_run": true, "planned": {…}}` and is added to the run's **planned changes**. Reads still run. On `prod`, executing a script that writes is always preceded by a dry run and a confirmation, and the execute run may only perform the approved writes.

## Limits (defaults)

| Limit | Default |
|---|---|
| Wall time | 30 s |
| CPU time | 10 s |
| Memory | 64 MiB |
| Tool calls | 200 |
| Concurrent calls (`ops.parallel`) | 8 |
| Result size | 1 MiB (larger results return a preview and a handle) |
| Source | 16 KiB, 400 lines |

Per-environment `limits.code` overrides any of them, up to the instance cap set in **Settings → Code mode** (by default 1000 calls, 120 s wall, 60 s CPU, 512 MiB, 32 concurrent calls and 4 MiB results). The console refuses higher values; higher values in `neurouter.yaml` are clamped to the cap with a warning. Lowering the cap applies to every environment. The CPU-time budget is measured as CPU time on Linux and as wall time on other platforms.

## Security layers

| Layer | What it does |
|---|---|
| Language | The Monty interpreter: no file system, network or OS access; frozen clock; seeded randomness |
| Runtime | The limits above; one session per worker, and the worker is discarded after every run |
| Process | A separate executor process with a minimal environment and no secrets |
| Network | In Kubernetes, the executor can run as its own deployment with all egress denied |
| Capabilities | A per-run capability and an allow-list of functions; every call re-checked by the server |

Scripts never see credentials, base URLs or tokens. Running everything in a single container cannot isolate the executor's network; for production on Kubernetes use the separate executor deployment (see [Deploy](https://neurouter.com/docs/deployment)).
