Skip to content
  • Auto
  • Light
  • Dark
DiscordForumGitHubSign up
View as Markdown
Copy Markdown

Open in Claude
Open in ChatGPT

Code Interpreter

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.

from letta import Letta
client = Letta(token="LETTA_API_KEY")
agent = client.agents.create(
model="openai/gpt-4o",
tools=["run_code"],
memory_blocks=[{
"label": "persona",
"value": "I can run Python code for data analysis and API calls."
}]
)
ParameterTypeOptionsDescription
codestrRequiredThe code to execute
languagestrpython, js, ts, r, javaProgramming language
{
"results": ["Last expression value"],
"logs": {
"stdout": ["Print statements"],
"stderr": ["Error output"]
},
"error": "Error details if execution failed"
}

Output types:

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

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:

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

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

// Works
const fs = require("fs");
const http = require("http");
// Fails
const axios = require("axios");

Base R only - no tidyverse packages.

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

JShell-style execution - statement-level only.

// Works
System.out.println("Hello");
int x = 42;
// Fails
public class Main {
public static void main(String[] args) { }
}

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

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

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

# First execution
x = 42
# Second execution (separate run_code call)
print(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

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

Terminal window
docker run \
-e E2B_API_KEY="your_e2b_api_key" \
letta/letta:latest
agent = client.agents.create(
model="openai/gpt-4o",
tools=["run_code"],
memory_blocks=[{
"label": "persona",
"value": "I use Python with pandas and numpy for data analysis."
}]
)
agent = client.agents.create(
model="openai/gpt-4o",
tools=["run_code", "web_search"],
memory_blocks=[{
"label": "persona",
"value": "I fetch data from APIs using run_code and search docs with web_search."
}]
)
agent = client.agents.create(
model="openai/gpt-4o",
tools=["run_code"],
memory_blocks=[{
"label": "persona",
"value": "I perform statistical analysis using scipy and numpy."
}]
)
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