Skip to main content
CrewAI is a framework for building agents and multi-agent workflows. Its native E2BPythonTool runs agent-generated Python in an isolated E2B Code Interpreter sandbox instead of executing it on your host.

Install the dependencies

Install CrewAI with its E2B tool dependencies:
Set API keys for E2B and your model provider:

Run a data analysis agent

Create one persistent tool instance and give it to the agent. Persistent mode lets the Python kernel retain imports and variables if the agent needs to retry its work.
The complete runnable version, including environment validation and locked dependencies, is available in the E2B cookbook.

Sandbox lifecycle

By default, an E2B CrewAI tool creates and closes a sandbox for every tool call. Use persistent=True only when an agent needs state across calls. A persistent tool creates its sandbox lazily and reuses it until it is closed. Call close() in finally so cleanup runs after both successful and failed crew executions. If you pass an existing sandbox_id, your application owns that sandbox and the tool will not close it. Keep sandbox_timeout as short as the workload allows. CrewAI applies it when the tool creates or connects to the sandbox and does not refresh it after each tool call. The tool’s per-execution timeout separately controls how long an individual code execution may run.

Results and errors

E2BPythonTool returns structured data:
Tell the agent to check error before accepting an answer. If code execution fails, the agent can inspect the error, correct the code, and run it again in the same persistent sandbox.

Security considerations

E2B isolates agent-generated code from the machine running CrewAI. The sandbox still processes model-generated inputs and produces untrusted output:
  • do not expose API keys or other host secrets to the sandbox unless required;
  • use persistent mode only when state must survive between calls;
  • explicitly close sandboxes owned by the tool;
  • use bounded sandbox and execution timeouts;
  • validate tool output before using it in another trusted system.
Learn more about E2B sandbox lifecycle.