Code Interpreter

Execute code in a secure sandbox with full network access

The run_code tool enables Letta agents to execute code in a secure sandboxed environment. Useful for data analysis, calculations, API calls, and programmatic computation.

On Letta Cloud, this tool works out of the box. For self-hosted deployments, you’ll need to configure an E2B API key.

Each execution runs in a fresh environment - variables, files, and state do not persist between runs.

Quick Start

1from letta import Letta
2
3client = Letta(token="LETTA_API_KEY")
4
5agent = client.agents.create(
6 model="openai/gpt-4o",
7 tools=["run_code"],
8 memory_blocks=[{
9 "label": "persona",
10 "value": "I can run Python code for data analysis and API calls."
11 }]
12)

Tool Parameters

ParameterTypeOptionsDescription
codestrRequiredThe code to execute
languagestrpython, js, ts, r, javaProgramming language

Return Format

1{
2 "results": ["Last expression value"],
3 "logs": {
4 "stdout": ["Print statements"],
5 "stderr": ["Error output"]
6 },
7 "error": "Error details if execution failed"
8}

Output types:

  • results[]: Last expression value (Jupyter-style)
  • logs.stdout: Print statements and standard output
  • logs.stderr: Error messages
  • error: Present if execution failed

Supported Languages

LanguageKey Limitations
PythonNone - full ecosystem available
JavaScriptNo npm packages - built-in Node modules only
TypeScriptNo npm packages - built-in Node modules only
RNo tidyverse - base R only
JavaJShell-style execution - no traditional class definitions

Python

Full Python ecosystem with common packages pre-installed:

  • Data: numpy, pandas, scipy, scikit-learn
  • Web: requests, aiohttp, beautifulsoup4
  • Utilities: matplotlib, PyYAML, Pillow

Check available packages:

1import pkg_resources
2print([d.project_name for d in pkg_resources.working_set])

JavaScript & TypeScript

No npm packages available - only built-in Node modules.

1// Works
2const fs = require('fs');
3const http = require('http');
4
5// Fails
6const axios = require('axios');

R

Base R only - no tidyverse packages.

1# Works
2mean(c(1, 2, 3))
3
4# Fails
5library(ggplot2)

Java

JShell-style execution - statement-level only.

1// Works
2System.out.println("Hello");
3int x = 42;
4
5// Fails
6public class Main {
7 public static void main(String[] args) { }
8}

Network Access

The sandbox has full network access for HTTP requests, API calls, and DNS resolution.

1import requests
2
3response = requests.get('https://api.github.com/repos/letta-ai/letta')
4data = response.json()
5print(f"Stars: {data['stargazers_count']}")

No State Persistence

Variables, files, and state do not carry over between executions. Each run_code call is completely isolated.

1# First execution
2x = 42
3
4# Second execution (separate run_code call)
5print(x) # Error: NameError: name 'x' is not defined

Implications:

  • Must re-import libraries each time
  • Files written to disk are lost
  • Cannot build up state across executions

Self-Hosted Setup

For self-hosted servers, configure an E2B API key. E2B provides the sandbox infrastructure.

$docker run \
> -e E2B_API_KEY="your_e2b_api_key" \
> letta/letta:latest

Common Patterns

Data Analysis

1agent = client.agents.create(
2 model="openai/gpt-4o",
3 tools=["run_code"],
4 memory_blocks=[{
5 "label": "persona",
6 "value": "I use Python with pandas and numpy for data analysis."
7 }]
8)

API Integration

1agent = client.agents.create(
2 model="openai/gpt-4o",
3 tools=["run_code", "web_search"],
4 memory_blocks=[{
5 "label": "persona",
6 "value": "I fetch data from APIs using run_code and search docs with web_search."
7 }]
8)

Statistical Analysis

1agent = client.agents.create(
2 model="openai/gpt-4o",
3 tools=["run_code"],
4 memory_blocks=[{
5 "label": "persona",
6 "value": "I perform statistical analysis using scipy and numpy."
7 }]
8)

When to Use

Use CaseToolWhy
Data analysisrun_codeFull Python data stack
Math calculationsrun_codeProgrammatic computation
Live API datarun_codeNetwork + processing
Web scrapingrun_coderequests + BeautifulSoup
Simple searchweb_searchPurpose-built
Persistent dataArchival memoryState persistence