Cette page est affichée en anglais. Une traduction française est en cours.
SkillsMay 7, 2026·3 min de lecture

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.

E2B
E2B · Community
Prêt pour agents

Installation avec revue préalable

Cet actif nécessite une revue. Le prompt copié demande un dry-run, affiche les écritures, puis continue seulement après confirmation.

Needs Confirmation · 66/100Policy : confirmer
Surface agent
Tout agent MCP/CLI
Type
Skill
Installation
Single
Confiance
Confiance : Community
Point d'entrée
Asset
Commande avec revue préalable
npx -y tokrepo@latest install 0fa3f58f-9f65-43e8-884f-09b0fe0d86e4 --target codex

Dry-run d'abord, confirmez les écritures, puis lancez cette commande.

Introduction

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 returned

Hook 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 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.


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

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 returned

Hook 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 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. Licensed under Apache-2.0.

e2b-dev/code-interpreter — ⭐ 1,500+

🙏

Source et remerciements

Built by E2B. Licensed under Apache-2.0.

e2b-dev/code-interpreter — ⭐ 1,500+

Fil de discussion

Connectez-vous pour rejoindre la discussion.
Aucun commentaire pour l'instant. Soyez le premier à partager votre avis.

Actifs similaires