---
title: "Code interpreter"
url: https://www.daytona.com/compute-glossary/agents/code-interpreter
---

# Code interpreter

Part of the [Compute Glossary](https://www.daytona.com/compute-glossary.md), section [Agents](https://www.daytona.com/compute-glossary/agents.md).

Also called Code execution tool.

A code interpreter is a tool that runs code written by a language model and returns what the code printed or produced. The model sends the code as a [tool call](https://www.daytona.com/compute-glossary/agents/tool-call.md), most often in Python, and gets the output back as the result. If the code fails, the error message comes back instead, and the model can fix the code and try again. Files the code writes, such as a chart or a cleaned dataset, can be handed back as well.

Language models are unreliable at exact arithmetic and at handling large amounts of data in their own text. Code does both reliably, so a model that can run code can answer questions it could not answer by writing alone. A model can also check its own work, by running a test or comparing a result against the data. Many chat assistants offer a code interpreter for data analysis, where a user uploads a file and the model explores it with code.

The code runs in a sandbox, because it is [untrusted code](https://www.daytona.com/compute-glossary/agents/untrusted-code.md) that no one reviews before it runs. Each user or conversation usually gets its own sandbox, which is meant to keep one session from reading the files of another. The sandbox stays up between calls for as long as the session lasts, and holds its memory even while the model is thinking. A session that sits idle is usually shut down after a timeout, and its state is lost unless it was saved.

## Stateful sessions

A stateful session is a run of code in which each call can use what earlier calls left behind. Variables and loaded data stay in memory from one call to the next. A model can then load a large file once and explore it over many small calls. Without state, every call would have to reload its data and repeat its setup.

Many code interpreters keep this state in a Jupyter kernel, a long-running [process](https://www.daytona.com/compute-glossary/kernel/process.md) that runs the code sent to it and keeps its variables. The word kernel here means an engine that runs code, not the [operating system kernel](https://www.daytona.com/compute-glossary/kernel/kernel.md). The host program sends code to the kernel over the Jupyter messaging protocol and collects the replies. If the kernel crashes or is restarted, every variable is gone and the model has to rebuild them.

## Outputs

The outputs of a code interpreter are everything a run sends back to the model. Plain text comes from two streams, standard output and standard error, which the host program keeps apart. The value of the last expression is returned as a result, as in a Jupyter notebook. Rich results, such as a chart rendered as a PNG image, come back with a type that says how to show them. When the code raises an error, the result carries the error name and the traceback that points at the failing line.

Long output is usually cut short before it reaches the model, since every character takes up space in its context window. A loop that prints millions of lines would otherwise fill that window in one call. The host program sets the limit and usually marks where it cut the output, so the model knows something is missing. Models therefore tend to print summaries, such as the first rows of a table, rather than whole datasets.

## Files

Files are how data moves into and out of a code interpreter. A file the user uploads is copied into the [file system](https://www.daytona.com/compute-glossary/storage/file-system.md) of the sandbox, where the code can open it by path. Files the code writes, such as a report or a chart, are copied out of the sandbox and handed back. Files last only as long as the sandbox, unless the host program saves them somewhere else first.

Libraries reach the sandbox as files too, either built into its image or installed by the code at run time. Installing at run time needs network access to a package registry. [Egress control](https://www.daytona.com/compute-glossary/networking/egress-control.md) can open that registry while keeping the rest of the network closed. A sandbox image that already holds the common data libraries saves that install time on every session.

## Limits

Limits on a code interpreter are the bounds the host program puts on each run and each session. Each call has a time limit, so a loop that never ends is stopped and reported as an error. The sandbox has a fixed share of processor time and memory, and code that runs out of memory is stopped. The model sees each failure as a result and can try a lighter approach.

Many code interpreters run with no network access, or with only a package registry open. Without a network, code cannot send what it reads to outside servers, but it also cannot install libraries the image lacks. A session that sits idle ends after a timeout, following the [lifecycle](https://www.daytona.com/compute-glossary/sandboxes/lifecycle.md) of its sandbox. That frees its memory for other sandboxes, and every variable in it is lost.

## Use cases

A code interpreter gives a model exact answers wherever code does better than text.

- **Data analysis.** A file the user uploads is copied into the file system of the sandbox. The model loads it once and explores it over many small calls, since the data stays in memory between them.

- **Charts and reports.** Code can draw a chart, which comes back as an image with a type that says how to show it. Files the code writes, such as a report, are copied out of the sandbox and handed back.

- **Exact calculation.** Language models are unreliable at exact arithmetic in their own text. Code does it reliably, and the model can check a result by running a test.

- **Fixing its own errors.** When code fails, the error name and its traceback come back as the result. The model can fix the code and try again.

## Related terms

- [Tool call](https://www.daytona.com/compute-glossary/agents/tool-call.md)
- [AI agent](https://www.daytona.com/compute-glossary/agents/agent.md)
- [Untrusted code](https://www.daytona.com/compute-glossary/agents/untrusted-code.md)
- [Sandbox](https://www.daytona.com/compute-glossary/sandboxes/sandbox.md)
- [Lifecycle](https://www.daytona.com/compute-glossary/sandboxes/lifecycle.md)
- [Process](https://www.daytona.com/compute-glossary/kernel/process.md)
