# Code interpreter

Give the agent a sandboxed Python (or R / Bash) runtime so it can compute, transform data, and plot.

## Overview

`code_interpreter` exposes a single function to the LLM: *"run this code and tell me what it printed."* The code runs in an **ephemeral isolated sandbox** with kernel-level isolation from the host, no network by default, and a hard wall-clock timeout. Each call gets a fresh sandbox — there is no state carried between calls.

Reach for it when the model needs to do something it cannot reliably do in its head:

- Math beyond a few decimals (statistics, finance formulas, unit conversions).
- Data manipulation on JSON/CSV the user pasted or a previous node returned.
- One-off plotting (matplotlib → PNG returned as a file).
- Parsing and reformatting structured text.

Skip it for: long-running jobs, anything that needs the open internet (use `shell` with internet enabled, or a `function` tool), persistent storage between turns.

## Configuration

| Field | Values | Notes |
| --- | --- | --- |
| Language | `python`, `r`, `bash` | Default `python`. The sandbox image must support the language. |
| Timeout | 5–600 seconds | Default 60. Hard kill. |
| Allow internet | boolean | Off by default. Turn on for `pip install` or HTTP calls inside the snippet. |
| Extra pip packages | one per line | Pre-installed before the code runs (Python only). |

## Example

User turn: *"What's the variance of these numbers: 4, 7, 13, 16, 22, 24, 31?"*

The LLM generates a code_interpreter call:

```python
import statistics
values = [4, 7, 13, 16, 22, 24, 31]
print({
    "sample_variance": statistics.variance(values),
    "population_variance": statistics.pvariance(values),
})
```

The sandbox returns:

```json
{
  "output": "{'sample_variance': 95.95238095238095, 'population_variance': 82.24489795918367}",
  "details": { "exit_code": 0, "duration_ms": 412 }
}
```

The model then replies: *"The sample variance is ~95.95 (population variance ~82.24)."*

## Best practices

- **Tell the agent to use it for math.** Without a system-prompt hint, the model will happily approximate. *"For any numeric calculation, use the code interpreter"* is a one-line fix.
- **Pre-declare pip packages.** Add `pandas`, `numpy` etc. to the **Extra pip packages** field so the first call does not pay an install penalty.
- **Print, don't return.** The sandbox captures stdout. `print(result)` works, a bare expression at the bottom of a cell does not.
- **Plots become files.** When the snippet writes a PNG with matplotlib, the file is surfaced as an attachment on the agent's reply.

## Gotchas

- **No persistence.** Variables defined in one call are gone in the next. If the model needs multi-step state, ask it to write the whole pipeline in one snippet.
- **30s default for the LLM-facing parameter, 60s default in the modal.** The model is told the snippet has 30 seconds; the actual hard timeout follows the modal value. If they disagree the modal wins.
- **No network by default.** `requests.get(...)` will raise a DNS error unless **Allow internet** is on.
- **Sandbox quotas.** A long chain of code_interpreter calls in one workflow run can saturate the sandbox pool — prefer one call per turn.

---

*Source: https://agentbuilder.systalink.sn/docs/tool-code-interpreter — human documentation.*
*Other language: [/docs-md/fr/tool-code-interpreter.md](/docs-md/fr/tool-code-interpreter.md).*
*Machine-readable index: [/llms.txt](/llms.txt).*
