Daytona
Enable your Agents to run code in a remote, secure sandbox.
Daytona offers secure and elastic infrastructure for running your AI-generated code. At Agno, we integrate with it to enable your Agents and Teams to run code in your Daytona sandboxes.
Prerequisites
The following example requires the daytona and openai packages:
uv pip install agno daytona openaiYou will also need a Daytona API key. You can get it from your Daytona dashboard:
export DAYTONA_API_KEY=your_api_keyExample
This example creates an agent that can run code in a Daytona sandbox:
from agno.agent import Agent
from agno.tools.daytona import DaytonaTools
agent = Agent(
name="Coding Agent with Daytona tools",
tools=[DaytonaTools()],
markdown=True,
instructions=[
"You are an expert at writing and executing code. You have access to a remote, secure Daytona sandbox.",
"Your primary purpose is to:",
"1. Write clear, efficient code based on user requests",
"2. ALWAYS execute the code in the Daytona sandbox using run_code",
"3. Show the actual execution results to the user",
"4. Provide explanations of how the code works and what the output means",
"Guidelines:",
"- NEVER just provide code without executing it",
"- Execute all code using the run_code tool to show real results",
"- Support Python, JavaScript, and TypeScript execution",
"- Use file operations (create_file, read_file) when working with scripts",
"- Install missing packages when needed using run_shell_command",
"- Always show both the code AND the execution output",
"- Handle errors gracefully and explain any issues encountered",
],
)
agent.print_response(
"Write JavaScript code to generate 10 random numbers between 1 and 100, sort them in ascending order, and print each number"
)Toolkit Params
| Parameter | Type | Default | Description |
|---|---|---|---|
api_key | Optional[str] | None | Daytona API key. If not provided, uses DAYTONA_API_KEY env var |
api_url | Optional[str] | None | Daytona API URL. If not provided, uses DAYTONA_API_URL env var |
sandbox_id | Optional[str] | None | Existing sandbox ID to connect to |
sandbox_language | Optional[CodeLanguage] | None | The programming language to run on the sandbox. Defaults to Python when not set |
sandbox_target | Optional[str] | None | The target configuration for sandbox creation |
sandbox_os | Optional[str] | None | Stored for compatibility; currently not forwarded to sandbox creation |
auto_stop_interval | Optional[int] | 60 | Stop sandbox after this many minutes of inactivity |
sandbox_os_user | Optional[str] | None | The user to run the sandbox as |
sandbox_env_vars | Optional[Dict[str, str]] | None | Environment variables to set in the sandbox |
sandbox_labels | Optional[Dict[str, str]] | None | Labels to set on the sandbox |
sandbox_public | Optional[bool] | None | Whether the sandbox should be public |
organization_id | Optional[str] | None | The organization ID to use for the sandbox |
timeout | int | 300 | Timeout in seconds for communication with the sandbox |
auto_create_sandbox | bool | True | Fall back to creating a fresh sandbox when sandbox management errors. When False, the error is raised. A sandbox is always created when none exists |
verify_ssl | Optional[bool] | False | When false, patches the Daytona SDK configuration process-wide to disable certificate verification |
persistent | bool | True | Store the sandbox ID in agent session state for reuse; persistence across processes requires persisted session state |
instructions | Optional[str] | None | Custom instructions for using the Daytona tools |
add_instructions | bool | False | Whether to add default instructions |
Toolkit Functions
| Function | Description |
|---|---|
run_code | Execute code in the Daytona sandbox (language set by sandbox_language) |
run_shell_command | Execute a shell command in the sandbox |
create_file | Create or update a file in the sandbox |
read_file | Read a file from the sandbox |
list_files | List files in a directory |
delete_file | Delete a file or directory from the sandbox |
change_directory | Change the current working directory |
You can use include_tools or exclude_tools to modify the list of tools the agent has access to. Learn more about selecting tools.
Run the example with the default Python sandbox
The cookbook above requests JavaScript, but the default sandbox language is Python. For a standalone script, replace that example with:
from agno.agent import Agent
from agno.tools.daytona import DaytonaTools
agent = Agent(
tools=[DaytonaTools(verify_ssl=True, include_tools=["run_code"])],
instructions=["Write and execute Python using run_code, then explain the result."],
markdown=True,
)
agent.print_response(
"Write Python code to generate 10 random integers between 1 and 100 and print them sorted."
)Save this as daytona_demo.py and run python daytona_demo.py. The toolkit creates a sandbox on first use and can retain its ID in session state. Stop or delete it through Daytona when finished; auto_stop_interval configures provider inactivity stopping and does not delete the sandbox.
The current create_file implementation alters single quotes in file contents. Use the provider's filesystem API for writing quoted code until that adapter path is repaired.