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"
)

Toolkit Params

ParameterTypeDefaultDescription
api_keystrNoneE2B API key. If not provided, uses E2B_API_KEY env var.
run_codeboolTrueWhether to register the run_code function
upload_fileboolTrueWhether to register the upload_file function
download_resultboolTrueWhether to register the download_result functions
filesystemboolFalseWhether to register filesystem operations
internet_accessboolFalseWhether to register internet access functions
sandbox_managementboolFalseWhether to register sandbox management functions
timeoutint300Timeout in seconds for the sandbox (default: 5 minutes)
sandbox_optionsdictNoneAdditional options to pass to the Sandbox constructor
command_executionboolFalseWhether to register command execution functions

Toolkit Functions

Code Execution

FunctionDescription
run_python_codeRun Python code in the E2B sandbox environment

File Operations

FunctionDescription
upload_fileUpload a file to the sandbox
download_png_resultAdd a PNG image result as an ImageArtifact to the agent
download_chart_dataExtract chart data from an interactive chart in results
download_file_from_sandboxDownload a file from the sandbox to the local system

Filesystem Operations

FunctionDescription
list_filesList files and directories in a path in the sandbox
read_file_contentRead the content of a file from the sandbox
write_file_contentWrite text content to a file in the sandbox
watch_directoryWatch a directory for changes for a specified duration

Command Execution

FunctionDescription
run_commandRun a shell command in the sandbox environment
stream_commandRun a shell command and stream its output
run_background_commandRun a shell command in the background
kill_background_commandKill a background command

Internet Access

FunctionDescription
get_public_urlGet a public URL for a service running in the sandbox
run_serverStart a server in the sandbox and return its public URL

Sandbox Management

FunctionDescription
set_sandbox_timeoutUpdate the timeout for the sandbox
get_sandbox_statusGet the current status of the sandbox
shutdown_sandboxShutdown the sandbox immediately
list_running_sandboxesList all running sandboxes

Developer Resources