E2B Code Interpreter Tutorial: Complete Guide to Secure Code Execution for AI Applications

# Tutorial Lengkap E2B Code Interpreter: Eksekusi Kode yang Aman untuk Aplikasi AI E2B Code Interpreter adalah platform sandboxed code execution yang memungkinkan aplikasi AI menjalankan kode secara...

By Ruby Abdullah · · tutorial
E2BCode InterpreterAI AgentsSandboxPython

Complete E2B Code Interpreter Tutorial: Secure Code Execution for AI Applications

E2B Code Interpreter is a sandboxed code execution platform that enables AI applications to run code safely in isolated environments. In this tutorial, we'll learn how to use E2B to build AI applications that can execute Python code, generate visualizations, and process data in real-time.

What Is E2B?

E2B (Environment to Binary) is a cloud platform that provides sandboxes for code execution. These sandboxes run in isolated microVMs, ensuring that executed code cannot affect the host system. E2B is particularly useful for building AI agents that need to run code, code interpreters, data analysis tools, and other interactive AI applications.

Key advantages of E2B:

  • Security: Each sandbox runs in an isolated microVM with separate filesystem and network
  • Speed: Sandboxes can be spun up in milliseconds
  • Flexibility: Supports Python, JavaScript, R, and other languages
  • Integration: SDKs available for Python and JavaScript/TypeScript
  • Persistence: Supports file upload/download and data persistence between executions

Installation

Prerequisites

Before getting started, make sure you have:

  • Python 3.8+ or Node.js 18+
  • An E2B account (free for basic usage)
  • An API key from the E2B dashboard

Python SDK Installation

pip install e2b-code-interpreter

JavaScript/TypeScript SDK Installation

npm install @e2b/code-interpreter

Getting Your API Key

  • Sign up at e2b.dev
  • Go to Dashboard > API Keys
  • Create a new API key
  • Store the key as an environment variable:
  • export E2BAPIKEY="e2bxxxxxxxxxxxxxxxxxxxx"
    

    Or create a .env file:

    E2BAPIKEY=e2bxxxxxxxxxxxxxxxxxxxx
    

    Basic Usage

    Running Simple Python Code

    Here's a basic example of running Python code in an E2B sandbox:

    from e2bcodeinterpreter import Sandbox
    
    

    Create a new sandbox

    sandbox = Sandbox()

    Run Python code

    execution = sandbox.runcode("print('Hello from E2B sandbox!')")

    Get the output

    print(execution.text) # Output: Hello from E2B sandbox!

    Close the sandbox when done

    sandbox.close()

    Using Context Manager

    A more idiomatic approach using context managers:

    from e2bcodeinterpreter import Sandbox
    
    

    with Sandbox() as sandbox:

    execution = sandbox.runcode("""

    import sys

    print(f"Python version: {sys.version}")

    print(f"Platform: {sys.platform}")

    """)

    print(execution.text)

    Running Multiple Cells

    You can run multiple code blocks sequentially, and state is preserved between executions:

    from e2bcodeinterpreter import Sandbox
    
    

    with Sandbox() as sandbox:

    # Cell 1: Define variables

    sandbox.runcode("""

    x = 10

    y = 20

    data = [1, 2, 3, 4, 5]

    """)

    # Cell 2: Use variables from the previous cell

    execution = sandbox.runcode("""

    result = x + y

    filtered = [d for d in data if d > 2]

    print(f"Sum: {result}")

    print(f"Filtered: {filtered}")

    """)

    print(execution.text)

    # Output:

    # Sum: 30

    # Filtered: [3, 4, 5]

    Error Handling

    E2B provides detailed error information:

    from e2bcodeinterpreter import Sandbox
    
    

    with Sandbox() as sandbox:

    execution = sandbox.runcode("""

    This code will produce an error

    result = 1 / 0

    """)

    if execution.error:

    print(f"Error type: {execution.error.name}")

    print(f"Error message: {execution.error.value}")

    print(f"Traceback: {execution.error.traceback}")

    else:

    print(execution.text)

    Working with Files

    Uploading Files to Sandbox

    from e2bcodeinterpreter import Sandbox
    
    

    with Sandbox() as sandbox:

    # Upload a file from the local filesystem

    Related Articles

    Composio Tutorial: Tool Integration Platform for AI Agents

    Tutorial Composio: Platform Integrasi Tool untuk AI Agents Composio adalah platform open-source yang memungkinkan AI age...

    LangChain Tutorial: The Most Popular Framework for Building LLM Applications

    Tutorial LangChain: Framework Paling Populer untuk Membangun Aplikasi LLM LangChain adalah framework open-source yang di...

    PydanticAI Tutorial: A Type-Safe Agent Framework for LLM Apps

    Membangun Agen LLM yang Type-Safe dengan PydanticAI PydanticAI adalah framework agen dari tim di balik Pydantic, diranca...

    Complete LangGraph Tutorial: Building Complex AI Agents

    Tutorial Lengkap LangGraph: Membangun AI Agents yang Kompleks LangGraph adalah library dari LangChain untuk membangun st...