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

# Code tools

> Run a short script when a request alone won't do.

A code tool runs a small piece of Python or JavaScript during the call. Use one
when you need to work something out rather than just fetch it — a price with rules
in it, a date calculation, or tidying up a messy response from somewhere else.

If you're only calling an API, use an [HTTP tool](/tools/http-tools)
instead.

## Writing one

Your code gets the arguments the assistant worked out in a variable called
`_input`. **Print your result as JSON** — whatever you print is what the assistant
reads back.

```python theme={null}
import json

nights = _input["nights"]
room_type = _input["room_type"]

rates = {"single": 119, "double": 164, "suite": 245}
rate = rates.get(room_type, 164)

total = rate * nights
if nights >= 7:
    total = round(total * 0.9)

print(json.dumps({
    "total_eur": total,
    "rate_per_night_eur": rate,
    "discount_applied": nights >= 7,
}))
```

JavaScript works the same way, with `_input` and `console.log`:

```javascript theme={null}
const nights = _input.nights;
console.log(JSON.stringify({ total_eur: nights * 164 }));
```

Print a small object with clearly named fields. The assistant reads those names,
so `total_eur` tells it more than `t`.

<Note>
  Only the printed JSON comes back. If you print several lines, the last valid
  JSON line is used; if nothing parses as JSON, the assistant gets the raw text
  instead. Print once, at the end.
</Note>

### Reaching other values

Besides `_input`, you can drop [variables](/variables) straight into
your code before it runs:

```python theme={null}
caller = "{{contact.first_name}}"
api_key = "{{secret.pms_api_key}}"
```

`_context` holds the same call details programmatically.

## Setting one up

| Field | What it does |
| - | - |
| **Language** | Python or JavaScript. |
| **Code** | What runs. Reads `_input`, prints JSON. Can contain [variables](/variables). |
| **Parameters** | What the assistant must work out, each with a description. |
| **Packages** | Libraries your code needs, with versions. |
| **Timeout** | How long it may run. Defaults to 10 seconds. |
| **If it fails** | What the assistant says if the code errors or times out. |

## Using libraries

You can add packages — pip for Python, npm for JavaScript — with a pinned version:

```text theme={null}
requests==2.32.3
```

`requests` is kept warm, so Python code using it starts immediately. Anything else
is installed the first time it's needed, which adds a few seconds to that call.
Pin versions so a release upstream doesn't change behaviour mid-week.

## Where it runs

Your code runs in an isolated sandbox, separate from everything else. It can't
reach your other tools, your data, or the call itself — it gets its arguments,
runs, and prints.

| | |
| - | - |
| Time limit | 10 seconds by default |
| Memory | 192 MB |

Keep it short. A code tool runs while a caller is waiting, so anything slow is
better done in your own system behind an HTTP tool.

## Testing

**Test** runs your code with arguments you supply and shows the result, so you can
work out the logic before attaching the tool to an assistant.


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