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.
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
withblock ends. - The model gets one tool,
run_python, fromtool.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.
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.
pytest -qE2B features for later
| Topic | What it is for |
|---|---|
| Custom templates | Sandboxes with your own packages and files, built from a Dockerfile. |
| Pause, resume and snapshots | Keeping a sandbox's state beyond its timeout. |
| Volumes | Storage that outlives sandboxes. |
| Public URLs | Serving a web app from inside a sandbox. |
| Desktop sandboxes | Computer-use agents with a graphical desktop. |
| Coding agents in sandboxes | Running Claude Code, Codex and others isolated. |
This is what real progress feels like.