Skip to main content

Agent frameworks & SDKs

CrewAI

Give CrewAI agents an isolated sandbox for running Python. The native E2BPythonTool runs the code your agents write in E2B, not on your machine.

Maintained by
CrewAI
How it works
Built into crewai-tools
Package
crewai-tools[e2b]
Language
Python

Set it up

Get started

Paste the prompt into your coding agent, or follow the steps yourself.

Paste this into your coding agent

Add E2B sandboxes to this CrewAI project.

  1. 1.Install crewai-tools with the e2b extra ("crewai-tools[e2b]") using the project's package manager.
  2. 2.The tool needs the E2B_API_KEY environment variable. If it isn't set, ask me for my API key from https://console.e2b.dev/?tab=keys. Never hardcode it.
  3. 3.Follow the CrewAI guide at https://docs.e2b.dev/agents/crewai and give my agents an E2BPythonTool, so the Python they write runs in an E2B sandbox. Close the tool in a finally block.
  4. 4.Run one task and show me the output that came from the sandbox.

Or set it up yourself

  1. 1. Install the E2B tools

    pip install "crewai-tools[e2b]" python-dotenv
  2. 2. Set your API keys

    export E2B_API_KEY="e2b_..."
    export OPENAI_API_KEY="..."
  3. 3. Give the tool to an agent

    from crewai import Agent, Crew, 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 analysis",
        backstory="You verify every answer by running code in an E2B sandbox.",
        tools=[python_tool],
    )
  4. 4. Run your crew

    task = Task(
        description="Generate 10,000 random integers and return their mean and p95.",
        expected_output="A JSON object with the statistics.",
        agent=analyst,
    )
    
    try:
        print(Crew(agents=[analyst], tasks=[task]).kickoff())
    finally:
        python_tool.close()

Security

What runs in the sandbox

The code your agents write runs in an isolated sandbox, away from the machine running CrewAI.

  • CodePython the agent writes, executed by E2BPythonTool in an E2B Code Interpreter sandbox.
  • StateA new sandbox per tool call by default. persistent=True keeps imports and variables between calls.
  • Lifetimesandbox_timeout bounds how long the sandbox lives. Call close() in finally to shut it down.
  • SecretsYour API keys stay with CrewAI on your machine unless you pass them into the sandbox.

3B+ sandboxes started

Give every agent a machine