DaytonaCodeExecutor
autogen.coding.DaytonaCodeExecutor #
DaytonaCodeExecutor(api_key=None, api_url=None, target=None, timeout=60, snapshot=None, image=None, name=None, env_vars=None, resources=None)
A code executor that runs code blocks inside a Daytona sandbox.
Creates a single sandbox on initialization and reuses it across all execute_code_blocks() calls. Auto-stop is disabled so the sandbox stays alive for the duration of the session. The sandbox is deleted when delete() is called, on process exit (via atexit), or when used as a context manager.
Python blocks are executed via process.code_run(). All other supported languages (bash, sh, javascript, typescript) are written to a temp file in the sandbox and executed with the appropriate runtime binary.
| PARAMETER | DESCRIPTION |
|---|---|
api_key | Daytona API key. If None, reads from DAYTONA_API_KEY env var. TYPE: |
api_url | Daytona API URL. If None, reads from DAYTONA_API_URL env var. TYPE: |
target | Target region (e.g. "us", "eu"). If None, reads from DAYTONA_TARGET env var. TYPE: |
timeout | Per-execution timeout in seconds. Default is 60. TYPE: |
snapshot | Sandbox snapshot name to use. Takes priority over TYPE: |
image | Custom Docker image for the sandbox. Accepts an image name string (e.g. TYPE: |
name | Human-readable name for the sandbox. Auto-generated if None. TYPE: |
env_vars | Environment variables to set inside the sandbox. Useful for passing secrets (API keys, database URLs, etc.) to the running code. |
resources | CPU/memory/disk limits for the sandbox. Only applied when using a custom image; ignored for snapshot-based sandboxes. TYPE: |
| RAISES | DESCRIPTION |
|---|---|
ImportError | If the daytona package is not installed. |
ValueError | If timeout < 1 or both snapshot and image are provided. |
RuntimeError | If sandbox creation fails. |
Example
Basic usage:
executor = DaytonaCodeExecutor(api_key="...", timeout=120)
agent = ConversableAgent("coder", code_execution_config={"executor": executor})
As a context manager — sandbox is deleted when the with-block exits:
Source code in autogen/coding/daytona_code_executor.py
SUPPORTED_LANGUAGES class-attribute #
execute_code_blocks #
Execute code blocks sequentially inside the Daytona sandbox.
Python blocks are executed via process.code_run(). All other languages are written to a temp file and run with the appropriate runtime binary via process.exec().
Execution stops on the first failure and returns that block's result. On full success, outputs from all blocks are joined with newlines.
| PARAMETER | DESCRIPTION |
|---|---|
code_blocks | The code blocks to execute. |
| RETURNS | DESCRIPTION |
|---|---|
DaytonaCodeResult | DaytonaCodeResult with exit_code, combined output, and sandbox_id. |
Source code in autogen/coding/daytona_code_executor.py
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 | |
restart #
Reset the executor by deleting the current sandbox and creating a fresh one.
Called by agent.reset() when starting a new conversation. Clears all accumulated state — created files, installed packages, process state.
Source code in autogen/coding/daytona_code_executor.py
delete #
Delete the sandbox and release all associated resources.
Safe to call multiple times. Called automatically on process exit via atexit. Unregisters the atexit handler to prevent double-execution and handler accumulation.