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.
pip install e2b_code_interpreter
export E2B_API_KEY=your_api_key
Example
The following example demonstrates how to create an agent that can run Python code in a secure sandbox:
cookbook/tools/e2b_tools.py
from agno.agent import Agent
from agno.models.openai import OpenAIChat
from agno.tools.e2b import E2BTools
e2b_tools = E2BTools(
timeout=600, # 10 minutes timeout (in seconds)
)
agent = Agent(
name="Code Execution Sandbox",
agent_id="e2b-sandbox",
model=OpenAIChat(id="gpt-4o"),
tools=[e2b_tools],
markdown=True,
show_tool_calls=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"
)
Parameter | Type | Default | Description |
---|
api_key | str | None | E2B API key. If not provided, uses E2B_API_KEY env var. |
run_code | bool | True | Whether to register the run_code function |
upload_file | bool | True | Whether to register the upload_file function |
download_result | bool | True | Whether to register the download_result functions |
filesystem | bool | False | Whether to register filesystem operations |
internet_access | bool | False | Whether to register internet access functions |
sandbox_management | bool | False | Whether to register sandbox management functions |
timeout | int | 300 | Timeout in seconds for the sandbox (default: 5 minutes) |
sandbox_options | dict | None | Additional options to pass to the Sandbox constructor |
command_execution | bool | False | Whether to register command execution 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 ImageArtifact 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 |
Developer Resources