import os
from crewai import Agent, Crew, Process, Task
from crewai_tools import E2BPythonTool
python_tool = E2BPythonTool(
persistent=True,
sandbox_timeout=600,
)
analyst = Agent(
role="Data Analyst",
goal="Use sandboxed Python to produce accurate, reproducible analysis",
backstory=(
"You verify every numerical answer by running code in an isolated "
"E2B sandbox."
),
tools=[python_tool],
llm=os.getenv("MODEL", "openai/gpt-4.1-mini"),
allow_delegation=False,
max_iter=8,
verbose=True,
)
task = Task(
description="""
Use the E2B Sandbox Python tool to run Python code. Create `random.Random(23)`,
generate 10,000 integers with `randint(1, 1000)`, and return a JSON object with
their count, mean, population standard deviation, p95, and SHA-256 digest.
Inspect the tool result's `error` field and correct and rerun failed code.
Do not estimate the answer yourself.
""",
expected_output="A JSON object containing the computed statistics.",
agent=analyst,
)
crew = Crew(
agents=[analyst],
tasks=[task],
process=Process.sequential,
verbose=True,
)
try:
result = crew.kickoff()
print(result)
finally:
python_tool.close()