E2BE2B SDK 2.50 · Code Interpreter 2.10 · Python 3.10+
0%
1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
10 small wins to finish your path

Data analysis agent with a tested tool

Put it together: a tool loop where a model writes Python, the code runs in an isolated sandbox without internet, and the tested tool describes each result.

Exampleagent.py, runs with OPENAI_API_KEY and E2B_API_KEY
import json

from e2b_code_interpreter import Sandbox
from openai import OpenAI

from tool import run_python

TOOLS = [{
    "type": "function",
    "function": {
        "name": "run_python",
        "description": "Run Python in a Jupyter cell in an isolated sandbox. The last expression is returned.",
        "parameters": {"type": "object", "properties": {"code": {"type": "string"}}, "required": ["code"]},
    },
}]


def answer(question: str) -> str:
    client = OpenAI()
    messages = [{"role": "user", "content": question}]
    with Sandbox.create(timeout=120, allow_internet_access=False) as sandbox:
        for _ in range(5):
            reply = client.chat.completions.create(model="gpt-4.1-mini", messages=messages, tools=TOOLS).choices[0].message
            messages.append(reply)
            if not reply.tool_calls:
                return reply.content
            for call in reply.tool_calls:
                code = json.loads(call.function.arguments)["code"]
                messages.append({"role": "tool", "tool_call_id": call.id, "content": run_python(sandbox, code)})
    return "Stopped after 5 steps."
  • One sandbox per question, created with a 120 second lifetime and no internet access, killed when the with block ends.
  • The model gets one tool, run_python, from tool.py, so every call has the 30 second limit, the output cap and error reporting you tested.
  • The loop stops after 5 steps, so a model that keeps writing broken code cannot run forever.
  • State carries between steps in the same sandbox: the model can load data in one call and aggregate it in the next.
One question, one sandbox
one sandbox per questionno tool callcodedescriptionquestionfrom a usergpt-4.1-miniat most 5 stepsanswerthe final replyrun_python30 s limit, capped outputJupyter kernelno internet, 120 s lifetime
Hover or tap a piece to see what it is and which lesson built it.
Follow a question

Running it needs both keys, so it is not run here. Before it answers real users, upload the customer's data with files.write instead of letting the model fetch it, log each piece of code the model ran, and test tool.py in CI as in lesson 8.

Example
pytest -q

E2B features for later

TopicWhat it is for
Custom templatesSandboxes with your own packages and files, built from a Dockerfile.
Pause, resume and snapshotsKeeping a sandbox's state beyond its timeout.
VolumesStorage that outlives sandboxes.
Public URLsServing a web app from inside a sandbox.
Desktop sandboxesComputer-use agents with a graphical desktop.
Coding agents in sandboxesRunning Claude Code, Codex and others isolated.

This is what real progress feels like.