E2B Sandbox Tools
Description
The E2B sandbox tools let CrewAI agents run code in isolated, ephemeral VMs hosted by E2B. Three tools share a common base class and connection model:E2BExecTool— execute shell commands.E2BPythonTool— execute Python in a Jupyter-style code interpreter (returns stdout, stderr, and rich results such as charts, dataframes, HTML, SVG, and PNG).E2BFileTool— perform filesystem operations (read, write, append, list, delete, mkdir, info, exists), including binary content via base64.
Installation
Install thee2b extra for crewai-tools and set your E2B API key:
Tools
E2BExecTool
Runs shell commands inside the sandbox via sandbox.commands.run.
Arguments
command: str— Required. The shell command to execute.cwd: str | None— Optional. Working directory for the command.envs: dict[str, str] | None— Optional. Per-call environment variables.timeout: float | None— Optional. Timeout in seconds.
E2BPythonTool
Runs Python code in a Jupyter-style code interpreter using the e2b_code_interpreter SDK.
Arguments
code: str— Required. The code to execute.language: str | None— Optional. Language identifier (defaults to Python).envs: dict[str, str] | None— Optional. Per-call environment variables.timeout: float | None— Optional. Timeout in seconds.
results can include charts, dataframes, HTML, SVG, and PNG output produced by the cell.
E2BFileTool
Performs filesystem operations inside the sandbox. Auto-creates parent directories on write and handles binary content via base64.
Arguments
action: "read" | "write" | "append" | "list" | "delete" | "mkdir" | "info" | "exists"— Required.path: str— Required. Target path inside the sandbox.content: str | None— Optional. Content forwrite/append. Base64-encoded whenbinary=True.binary: bool— Optional. Treatcontentas binary (base64). DefaultFalse.depth: int— Optional. Recursion depth forlist.
Shared parameters (E2BBaseTool)
All three tools accept the same connection / lifecycle parameters:
api_key: SecretStr | None— Falls back to theE2B_API_KEYenvironment variable.domain: str | None— Falls back to theE2B_DOMAINenvironment variable.template: str | None— Custom sandbox template or snapshot.persistent: bool— DefaultFalse. See Sandbox modes.sandbox_id: str | None— Attach to an existing sandbox.sandbox_timeout: int— Idle timeout in seconds. Default300.envs: dict[str, str] | None— Environment variables injected at sandbox creation.metadata: dict[str, str] | None— Metadata attached at sandbox creation.
Sandbox modes
| Mode | How to activate | Sandbox lifetime |
|---|---|---|
| Ephemeral (default) | persistent=False | A new sandbox is created and killed for every _run call. |
| Persistent | persistent=True | A sandbox is lazily created on the first call and killed at process exit via atexit. |
| Attach | sandbox_id="sbx_..." | The tool attaches to an existing sandbox and never kills it. |
Examples
One-shot Python (ephemeral)
Code
Persistent shell + filesystem session
Code
atexit.
Attach to an existing sandbox
Code
Custom template, timeout, env vars, and metadata
Code
Full agent example
Code
Security considerations
These tools give agents arbitrary shell, Python, and filesystem access inside the sandbox. The sandbox isolates execution from your host, but you should still treat tool output as untrusted and design with prompt-injection in mind:- Ephemeral mode is the primary blast-radius control — every
_runcall gets a fresh VM. Prefer it unless persistent state is required. - Persistent and attached sandboxes accumulate state across calls. Anything seeded into them (credentials, tokens, files) is reachable by every subsequent tool invocation, including ones whose inputs were influenced by untrusted content.
- Avoid injecting secrets into long-lived sandboxes that an agent can read or exfiltrate. Use short-lived credentials and the smallest scope necessary.
sandbox_timeoutbounds idle time but does not cap total execution. Set it to the smallest value that fits your workload.
