E2B
Enable your Agents to run code in a remote, secure sandbox.
E2BTools enable an Agent to execute code in a secure sandboxed environment with support for Python, file operations, and web server capabilities.
Prerequisites
The E2B tools require the e2b_code_interpreter Python package and an E2B API key.
uv pip install agno e2b_code_interpreter openaiexport E2B_API_KEY=your_api_keyExample
The following example demonstrates how to create an agent that can run Python code in a secure sandbox:
from agno.agent import Agent
from agno.models.openai import OpenAIResponses
from agno.tools.e2b import E2BTools
e2b_tools = E2BTools(
timeout=600, # 10 minutes timeout (in seconds)
)
agent = Agent(
name="Code Execution Sandbox",
id="e2b-sandbox",
model=OpenAIResponses(id="gpt-5.2"),
tools=[e2b_tools],
markdown=True,
instructions=[
"You are an expert at writing and validating Python code using a secure E2B sandbox environment.",
"Your primary purpose is to:",
"1. Write clear, efficient Python code based on user requests",
"2. Execute and verify the code in the E2B sandbox",
"3. Share the complete code with the user, as this is the main use case",
"4. Provide thorough explanations of how the code works",
"",
"You can use these tools:",
"1. Run Python code (run_python_code)",
"2. Upload files to the sandbox (upload_file)",
"3. Download files from the sandbox (download_file_from_sandbox)",
"4. Generate and add visualizations as image artifacts (download_png_result)",
"5. List files in the sandbox (list_files)",
"6. Read and write file content (read_file_content, write_file_content)",
"7. Start web servers and get public URLs (run_server, get_public_url)",
"8. Manage the sandbox lifecycle (set_sandbox_timeout, get_sandbox_status, shutdown_sandbox)",
],
)
# Example: Generate Fibonacci numbers
agent.print_response(
"Write Python code to generate the first 10 Fibonacci numbers and calculate their sum and average"
)
Toolkit Params
| Parameter | Type | Default | Description |
|---|---|---|---|
api_key | str | None | E2B API key. If not provided, uses E2B_API_KEY env var. |
timeout | int | 300 | Timeout in seconds for the sandbox (default: 5 minutes) |
sandbox_options | dict | None | Additional options to pass to the Sandbox constructor |
Toolkit Functions
Code Execution
| Function | Description |
|---|---|
run_python_code | Run Python code in the E2B sandbox environment |
File Operations
| Function | Description |
|---|---|
upload_file | Upload a file to the sandbox |
download_png_result | Add a PNG image result as an Image object to the agent |
download_chart_data | Extract chart data from an interactive chart in results |
download_file_from_sandbox | Download a file from the sandbox to the local system |
Filesystem Operations
| Function | Description |
|---|---|
list_files | List files and directories in a path in the sandbox |
read_file_content | Read the content of a file from the sandbox |
write_file_content | Write text content to a file in the sandbox |
watch_directory | Watch a directory for changes for a specified duration |
Command Execution
| Function | Description |
|---|---|
run_command | Run a shell command in the sandbox environment |
stream_command | Run a shell command and stream its output |
run_background_command | Run a shell command in the background |
kill_background_command | Kill a background command |
Internet Access
| Function | Description |
|---|---|
get_public_url | Get a public URL for a service running in the sandbox |
run_server | Start a server in the sandbox and return its public URL |
Sandbox Management
| Function | Description |
|---|---|
set_sandbox_timeout | Update the timeout for the sandbox |
get_sandbox_status | Get the current status of the sandbox |
shutdown_sandbox | Shutdown the sandbox immediately |
list_running_sandboxes | List all running sandboxes |
You can use include_tools or exclude_tools to modify the list of tools the agent has access to. Learn more about selecting tools.
Sandbox lifecycle and current SDK compatibility
E2BTools() provisions a sandbox immediately. Save the following adapted example as e2b_demo.py and run it with python e2b_demo.py. The finally block shuts down the sandbox even when the agent run raises:
from agno.agent import Agent
from agno.tools.e2b import E2BTools
tools = E2BTools(timeout=600, include_tools=["run_python_code"])
try:
agent = Agent(tools=[tools], markdown=True)
agent.print_response("Use Python to calculate the first ten Fibonacci numbers and their sum.")
finally:
print(tools.shutdown_sandbox())The broader cookbook above illustrates the available tools, with these current SDK limitations:
download_file_from_sandboxreads text by default and then writes to a binary file. For downloads, usePath(local_path).write_bytes(tools.sandbox.files.read(sandbox_path, format="bytes"))in your application, importingPathfrompathlib.get_sandbox_statusreads an olderidattribute and may returnUnknown; it is not a running/stopped status check. Use the provider SDK'sget_info()oris_running()for lifecycle information.- Background commands return a Python handle. Keep that object in application code for
kill_background_command; it is not a JSON job ID the model can round-trip.
Check the shutdown result for an error if the provider is unavailable. The sandbox timeout remains a provider-side fallback.