Deep Dives · E2B

    Mastering AI Code Execution in Secure Sandboxes with E2B

    Explore how E2B provides secure, isolated sandboxes for running AI-generated code with LLaMA-3 on Together AI—ideal for building safe, intelligent data workflows and autonomous agents.

    • 5min read
    • 10code blocks
    Mastering AI Code Execution in Secure Sandboxes with E2B

    Running AI-generated code in production or evaluation environments often comes with major security and infrastructure challenges. Whether you’re building intelligent agents or automated data workflows, securely executing dynamic code is no easy task. Enter E2B, an open-source infrastructure that provides lightning-fast, isolated sandboxes for safely executing AI-generated code in the cloud. In this article, we’ll take a deep dive into how E2B works, explore its powerful Python SDK, and walk you through a step-by-step implementation for real-world applications like AI-driven data analysis.

    Table of Content

    1. What is E2B?
    2. How E2B Works
    3. Key Features
    4. Step-by-Step Guide

    Let's start by understanding what E2B is.

    What is E2B?

    E2B (Execute to Build) is an open-source tool providing isolated and secure cloud sandboxes for running AI-generated code. These sandboxes function as small virtual machines, specifically designed for the safe evaluation and execution of code produced by language models. This setup enables users to confidently work with code from models like LLaMA 3 and Code LLaMA.

    The versatility of E2B extends to various applications, including evaluating codegen models, building intelligent coding agents capable of reasoning and acting, visualizing datasets analyzed by AI, and running automatically generated applications such as Fragments. Interaction with E2B is facilitated through convenient SDKs available in both Python and JavaScript, offering flexibility for diverse development workflows.

    How E2B Works

    Beneath the surface, each E2B Sandbox operates as a nimble virtual machine that initializes in approximately 150 milliseconds, delivering swift and scalable runtime environments. These sandboxes are hosted securely in the cloud and maintain complete isolation, ensuring the safety of executed code.

    Furthermore, sandboxes can be allocated on a per-user or per-session basis, offering granular control over resource management. They provide essential functionalities such as file operations, the ability to install Python packages via pip, and network access, effectively serving as a personalized coding VM designed to handle individual LLM requests or user sessions.

    Key Features

    Here are some standout features of E2B:

    • Pre-installed Data Libraries: Pandas, Matplotlib, and more, ready out of the box.
    • Full Isolation: Prevents unsafe code from affecting your systems.
    • Fast Spin-Up: Sandboxes start in under 200ms.
    • File Upload/Download Support: Manage datasets or results easily.
    • LLM Compatibility: Works seamlessly with models from Together AI or OpenAI.

    E2B Key Features

    E2B Key Features

    These features make it ideal for advanced AI workflows where reliability, security, and flexibility are critical.

    Step-by-Step Guide

    This tutorial walks you through how to:

    • Use LLaMA-3 70B on Together AI to generate Python code.
    • Execute that code securely inside a sandboxed E2B Code Interpreter.
    • Upload a dataset and visualize relationships.

    Step 1. Install Required Packages

    text
    %pip install together==1.3.1 e2b_code_interpreter==1.0.0 python-dotenv==1.0.0

    Step 2. Import Libraries and Load Environment Variables

    javascript
    import os
    from dotenv import load_dotenv
    import json
    import re
    from together import Together
    from e2b_code_interpreter import Sandbox
    
    load_dotenv()

    Step 3. Setup Your API Keys

    text
    E2B_API_KEY = "e2b_XXXX"  # Replace with your real E2B key
    TOGETHER_API_KEY = "together_XXXX"  # Replace with your Together API key

    Step 4. Choose a Model from Together AI

    text
    MODEL_NAME = "meta-llama/Llama-3.3-70B-Instruct-Turbo-Free"

    You can explore other models at: https://api.together.ai/models.  Together AI provides $1 in free credits, which you can use to test various models. Some models, like LLaMA-3.3-70B-Instruct-Turbo-Free, are available for free usage as well.

    Step 5. Define the System Prompt

    text
    SYSTEM_PROMPT = """You're a Python data scientist. You are given tasks to complete and you run Python code to solve them.
    
    Information about the csv dataset:
    
    - It's in the `/Users/aniruddha/Documents/data.csv` file
    - The CSV file is using , as the delimiter
    - It has the following columns (examples included):
        - country: "Argentina", "Australia"
        - Region: "SouthAmerica", "Oceania"
        - Surface area (km2): for example, 2780400…. ""

    This prompt tells the LLM how to behave and what the dataset looks like. Here you have to add all the column names as it is in dataset.

    Step 6. Build Helper Function to Run Code

    python
    def code_interpret(e2b_code_interpreter, code):
        print("Running code interpreter...")
        exec = e2b_code_interpreter.run_code(code,
            on_stderr=lambda stderr: print("[Code Interpreter]", stderr),
            on_stdout=lambda stdout: print("[Code Interpreter]", stdout))
    
        if exec.error:
            print("[Code Interpreter ERROR]", exec.error)
        else:
            return exec.results

    Step 7. Extract Python Code Block from LLM Response using Regex

    python
    pattern = re.compile(r"```python\n(.*?)\n```", re.DOTALL)
    
    def match_code_blocks(llm_response):
        match = pattern.search(llm_response)
        if match:
            code = match.group(1)
            print(code)
            return code
        return ""

    Step 8. Send Chat Prompt to LLaMA + Execute Code if Found

    python
    def chat_with_llm(e2b_code_interpreter, user_message):
    
        print(f"\n{'='*50}\nUser message: {user_message}\n{'='*50}")
        messages = [
            {"role": "system", "content": SYSTEM_PROMPT},
            {"role": "user", "content": user_message},
        ]
    
        response = client.chat.completions.create(
            model=MODEL_NAME,
            messages=messages,
        )
    
        response_message = response.choices[0].message
        python_code = match_code_blocks(response_message.content)
    
        if python_code != "":
            code_interpreter_results = code_interpret(e2b_code_interpreter, python_code)
            return code_interpreter_results
    
        else:
            print(f"Failed to match any Python code in model's response {response_message}")
    
            return []

    Step 9. Upload Dataset to the Code Interpreter

    python
    def upload_dataset(code_interpreter):
        print("Uploading dataset to Code Interpreter sandbox...")
        dataset_path = "/content/data.csv"
    
        if not os.path.exists(dataset_path):
            raise FileNotFoundError("Dataset file not found")
    
        try:
            with open(dataset_path, "rb") as f:
                code_interpreter.files.write(dataset_path, f)
            print("Uploaded at", dataset_path)
    
            return dataset_path
    
        except Exception as error:
            print("Error during file upload:", error)
            raise error

    Step 10. Run Everything in a Secure Sandbox

    text
    client = Together(api_key=TOGETHER_API_KEY)
    
    with Sandbox(api_key=E2B_API_KEY) as code_interpreter:
        upload_dataset(code_interpreter)
        code_results = chat_with_llm(
            code_interpreter,
    
            "Create a bar chart showing Unemployment (% of labour force) for 10 countries with the highest GDP per capita (current US$).",
    
        )
    
        if code_results:
            first_result = code_results[0]
    
        else:
            raise Exception("No code interpreter results")
    
    # View the result
    first_result

    Output

    The model will:

    • Parse the dataset.
    • Filter missing or invalid values.
    • Generate and execute a linear regression chart showing the relationship between GDP per capita and life expectancy.

    Step-by-Step Guide

    Final Thoughts

    E2B acts as a game-changer for AI-native development. It will be helpful whether you're working on autonomous agents, code generation tasks, or complex evaluation pipelines, its sandbox infrastructure removes friction and risk. Paired with LLMs from Together AI or others, E2B allows you to safely prototype and test advanced AI workflows in minutes. You can check their official docs and GitHub examples to dive deeper into custom applications.

    References

    • E2B
    • Sandboxes

    Written by

    ADaSci Editorial

    The ADaSci Editorial team publishes research explainers, industry analysis and practical guides for AI and data science professionals.

    More from ADaSci