# E2B Code Interpreter SDK — Sandboxed Python for AI Agents > E2B Code Interpreter is a Python SDK for AI agents to run untrusted code safely. Jupyter-like cells, file uploads, charts auto-rendered. ~150ms cold start. ## Install Save as a script file and run: ## Quick Use 1. Sign up at e2b.dev → copy your API key 2. `pip install e2b-code-interpreter` 3. `Sandbox.create()` then `sandbox.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 ```python 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 ```python 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 returned ``` ### Hook into Claude / GPT tool use ```python 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 output ``` --- ### FAQ **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](https://github.com/e2b-dev). Licensed under Apache-2.0. > > [e2b-dev/code-interpreter](https://github.com/e2b-dev/code-interpreter) — ⭐ 1,500+ --- ## 快速使用 1. 在 e2b.dev 注册,复制 API key 2. `pip install e2b-code-interpreter` 3. `Sandbox.create()` 然后 `sandbox.run_code('...')`,输出含文本、图表、文件 --- ## 简介 E2B Code Interpreter SDK 给 AI agent 一个托管的类 Jupyter 环境,能跑 Python、上传文件、渲染图表、跨 cell 保留状态。~150ms 冷启动,底下是完整 Linux。适合在自己产品里搭 ChatGPT 风格的 code interpreter、数据分析 agent、需要跑自己代码的自主编码 agent。Python SDK 和 JS SDK 都有。装机时间 2 分钟。 --- ### 从 agent 跑代码 ```python from e2b_code_interpreter import Sandbox with Sandbox.create() as sandbox: # 上传 CSV sandbox.files.write("/data/sales.csv", open("sales.csv", "rb").read()) # 执行代码 execution = sandbox.run_code(''' import pandas as pd df = pd.read_csv('/data/sales.csv') df.groupby('region')['amount'].sum().plot(kind='bar') ''') # 拿文本输出 print(execution.text) # 拿图(自动抽取为 PNG) for chart in execution.results: if chart.png: with open(f"chart-{chart.id}.png", "wb") as f: f.write(chart.png) ``` ### 跨调用持久状态 ```python sandbox = Sandbox.create() # 第一次调用定义函数 sandbox.run_code(''' def analyze_sales(region): return df[df.region == region]['amount'].sum() ''') # 第二次调用使用函数 —— 状态保留 result = sandbox.run_code("analyze_sales('US')") print(result.text) # 函数返回的内容 ``` ### 接到 Claude / GPT 的工具使用 ```python 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"}], ) # Claude 调 run_python 时,在 sandbox 里执行并返回输出 ``` --- ### FAQ **Q: E2B 免费吗?** A: 免费 —— 大方的免费档(每月约 100 小时 sandbox 时间)。付费档加更长会话、更高并发、持久磁盘。E2B 的开源 firecracker 运行时也能自托管。 **Q: 跟在 Modal Sandbox 里跑代码啥区别?** A: E2B 优化 *交互式* 代码执行(Jupyter cell、持久 kernel 状态、图自动抽取)。Modal Sandbox 是通用 Linux。要 ChatGPT 风格 code interpreter 体验,E2B 更直接。 **Q: 跑不可信代码安全吗?** A: 安全 —— E2B 用 Firecracker microVM(跟 AWS Lambda 一样的技术)做隔离。每个 sandbox 有独立 kernel / 文件系统 / 网络命名空间。E2B 就是为防 sandbox 之间或 sandbox 到 host 提权设计的。 --- ## 来源与感谢 > Built by [E2B](https://github.com/e2b-dev). Licensed under Apache-2.0. > > [e2b-dev/code-interpreter](https://github.com/e2b-dev/code-interpreter) — ⭐ 1,500+ --- Source: https://tokrepo.com/en/workflows/e2b-code-interpreter-sdk-sandboxed-python-for-ai-agents Author: E2B