E2B Sandbox
The ag2.extensions.e2b module runs AG2 shell and code tools in isolated E2B cloud sandboxes. It implements AG2's native sandbox factory, so one environment keeps files across tool calls and cleans up deterministically.
Installation#
Install AG2, your model provider, and E2B's Python SDK:
Create an API key in the E2B dashboard and export it:
Use E2B with an agent#
Use the environment as an async context manager. Every shell and code call inside the scope runs in the same sandbox, and the sandbox is killed when the scope exits.
The default template ships python and node, so SandboxCodeTool(env, languages=("python", "bash", "javascript")) works without a custom template. TypeScript needs a template with ts-node.
Configure the sandbox#
| Parameter | Default | Purpose |
|---|---|---|
api_key | E2B_API_KEY | API authentication |
template | E2B default template | Template name or ID the sandbox starts from |
env_vars | {} | Environment variables set for every command |
timeout | 60 seconds | Per-command timeout; a command that runs longer is killed and returns exit code 124 |
sandbox_timeout | E2B default (300 seconds) | Server-side lifetime that commands keep extending; see Lifecycle |
workdir | /home/user | Working directory for commands and files, created if missing |
create_options | {} | Extra keyword arguments forwarded to AsyncSandbox.create, such as domain, metadata, secure, allow_internet_access, network (e2b>=2.7.0), or lifecycle (e2b>=2.15.0) |
api_key, template, and env_vars accept Variable values for per-request resolution, for example per-tenant credentials. Sandboxes are cached by their resolved values: opens that resolve to different values get different sandboxes, and opens that resolve to the same values share one, whichever conversation makes them.
E2BEnvironment satisfies WorkdirAware, so SandboxShellTool(env) reports the real working directory to the model.
Lifecycle#
E2B reclaims a sandbox once its server-side lifetime runs out. A command extends that lifetime back to sandbox_timeout when less than half of it is left, and always far enough to cover the command's own timeout, so a running command is not cut off. The lifetime is never shortened. How long an idle sandbox survives after its last command depends on when that command ran relative to the last refresh, so set sandbox_timeout with a comfortable margin above the longest pause you expect between tool calls, such as a slow model turn or a human-in-the-loop wait.
Once a sandbox is gone, the next command reports it, and the environment does not silently create a new one, because a new sandbox would not have the old one's files.
Always close the environment with async with or await env.aclose(). Closing waits for a sandbox that is still being created and kills it too, and calling aclose() again retries a kill that failed. An atexit fallback kills sandboxes that are still open when the process exits. The server-side lifetime is the backstop when neither runs, for example when a task is cancelled while E2B is still creating its sandbox.
Testing#
The bundled unit tests mock the E2B SDK and run offline, so no E2B account is needed for the AG2 test suite. Exercising the integration against real sandboxes requires an E2B account and an E2B_API_KEY.
Maintainer: @OndrejDrapalik / E2B.