Quick Use
- Sign up at e2b.dev → copy your API key
pip install e2b-code-interpreterSandbox.create()thensandbox.run_code('...')— outputs include text, charts, files
Intro
The E2B Code Interpreter SDK gives AI agents a managed Jupyter-like environment to execute Python with file uploads, plot rendering, and persistent state across cells. ~150ms cold start, full Linux underneath. Best for: ChatGPT-style code-interpreter features in your own product, data analysis agents, autonomous coding agents that need to run their own code. Works with: Python SDK and JS SDK. Setup time: 2 minutes.
Run code from agent
from e2b_code_interpreter import Sandbox
with Sandbox.create() as sandbox:
# Upload a CSV
sandbox.files.write("/data/sales.csv", open("sales.csv", "rb").read())
# Execute code
execution = sandbox.run_code('''
import pandas as pd
df = pd.read_csv('/data/sales.csv')
df.groupby('region')['amount'].sum().plot(kind='bar')
''')
# Get text output
print(execution.text)
# Get charts (auto-extracted as PNG)
for chart in execution.results:
if chart.png:
with open(f"chart-{chart.id}.png", "wb") as f:
f.write(chart.png)Persistent state across calls
sandbox = Sandbox.create()
# First call defines a function
sandbox.run_code('''
def analyze_sales(region):
return df[df.region == region]['amount'].sum()
''')
# Second call uses the function — state preserved
result = sandbox.run_code("analyze_sales('US')")
print(result.text) # whatever the function returnedHook into Claude / GPT tool use
from anthropic import Anthropic
from e2b_code_interpreter import Sandbox
sandbox = Sandbox.create()
tools = [{
"name": "run_python",
"description": "Run Python code in a sandbox. Persistent state across calls.",
"input_schema": {
"type": "object",
"properties": {"code": {"type": "string"}},
"required": ["code"]
}
}]
response = anthropic.messages.create(
model="claude-3-5-sonnet-20241022",
tools=tools,
messages=[{"role": "user", "content": "Plot the FAANG stock prices over 2024"}],
)
# When Claude calls run_python, execute in sandbox and return outputFAQ
Q: Is E2B free? A: Yes — generous free tier (~100 hours of sandbox time per month). Paid tiers add longer sessions, more concurrency, persistent disk. Self-hosting is possible via E2B's open-source firecracker-based runtime.
Q: How is this different from running code in Modal Sandboxes? A: E2B is optimized for interactive code execution (Jupyter cells, persistent kernel state, chart auto-extraction). Modal Sandboxes are general-purpose Linux. For ChatGPT-style code interpreter UX, E2B is the more direct fit.
Q: Is it safe to run untrusted code? A: Yes — E2B uses Firecracker microVMs (the same tech as AWS Lambda) for isolation. Each sandbox has its own kernel, filesystem, and network namespace. Sandbox-to-sandbox or sandbox-to-host escalation is the threat model E2B is built to prevent.
Source & Thanks
Built by E2B. Licensed under Apache-2.0.
e2b-dev/code-interpreter — ⭐ 1,500+