> ## Documentation Index
> Fetch the complete documentation index at: https://docs.cruq.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Code mode

> Let an agent write one short Python script that calls its other tools, instead of making one tool call per item.

An agent that checks 30 late orders makes 30 tool calls. Each one is a model
turn that sends the whole conversation again. With the **Code (Python)**
built-in, the agent writes one script instead:

```python theme={null}
import asyncio
late = await list_orders(status="late")
orders = await asyncio.gather(*[get_order(order_id=o["id"]) for o in late])
[o for o in orders if o["total"] > 1000]
```

That is one model turn and one tool result. The 30 order lookups never enter
the conversation, so they cost no tokens.

## What the agent gets

Add the **Code (Python)** built-in to an agent (Tools, then "Add built-in").
The agent gets a `run_code` tool. Its description lists the agent's other
tools as async Python functions, and a script can call any of them with
`await`, or several at once with `asyncio.gather`. A tool that returns JSON
gives the script a parsed dict or list. The value of the last line
comes back as the result, along with anything the script printed. If the
script fails, the agent gets the error and traceback and can fix it.

A script only sees the tools of the agent it belongs to.

## The sandbox

Scripts run in [Monty](https://github.com/pydantic/monty), a Python
interpreter written in Rust. It has no filesystem, no network and no
environment variables, and the agent's tools are the only way out. `json`,
`re`, `datetime`, `math`, `collections`, `dataclasses` and `asyncio` are
available. Other standard library modules and pip packages (pandas, numpy)
are not.

Every tool a script calls goes through the same path as a direct tool call:
guardrails, metering and the run's budget all apply.

## Limits

| Setting | Default | Maximum |
| - | - | - |
| Seconds of script run time (time spent waiting on tools does not count) | 5 | 30 |
| Memory | 64 MB | 256 MB |
| Tool calls per script | 50 | 200 |

Change them on the tool row in the agent editor, or from the CLI:

```bash theme={null}
cruqai tools code "Code (Python)" --max-seconds 10 --max-tool-calls 100
```

## Approvals and pauses

If a tool inside the script pauses the run (`ask_human`, a browser approval,
`wait_until`), the whole run pauses, the same as a direct call. A script
cannot catch the pause and carry on. When the run resumes, the script runs
again from the top: tool calls that finished before the pause return their
recorded results instead of running a second time, and the paused call gets
the answer.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.