Skip to main content
Code executors provide sandboxed environments for running LLM-generated code. AutoGen supports multiple execution backends with varying levels of isolation and safety.

Installation

Install the executor you need:

Docker Command Line Executor

The DockerCommandLineCodeExecutor provides the highest level of safety by running code in isolated Docker containers.

Basic Usage

Configuration Options

string
default:"python:3-slim"
Docker image to use for code execution
string
Name for the Docker container. Auto-generated if not provided
int
default:"60"
Timeout in seconds for code execution
Path | str
Working directory for code execution. Uses a temporary directory if not provided
Path | str
Directory to bind mount into the container. Defaults to work_dir
bool
default:"true"
Automatically remove container when stopped
bool
default:"true"
Automatically stop container on exit
string
default:"functions"
Module name for custom functions

Advanced Configuration

GPU Support

Enable GPU access in containers:

Custom Volumes

Mount additional volumes:

Network Configuration

Configure container networking:

Initialization Commands

Run setup commands before each execution:

Docker Jupyter Executor

Execute code in a Jupyter kernel running inside Docker:

Benefits of Jupyter Execution

  • Persistent state: Variables and imports persist between executions
  • Rich output: Supports plots, HTML, and other rich media
  • IPython features: Magic commands and special syntax

Example with Matplotlib

Local Command Line Executor

The LocalCommandLineCodeExecutor executes code directly on your machine without isolation. Only use this with trusted code or in development environments.

Basic Usage

Configuration Options

int
default:"60"
Timeout in seconds for code execution
Path | str
Working directory. Defaults to a temporary directory
string
default:"functions"
Module name for custom functions
bool
default:"true"
Automatically clean up temporary files after execution
SimpleNamespace
Virtual environment to use for execution

Virtual Environment Support

Run code in a specific virtual environment:

Custom Functions

Provide reusable functions to code executors:
Access custom functions in generated code:

Supported Languages

All executors support:
  • Python: .py files
  • Bash/Shell: .sh files (languages: bash, shell, sh)
  • PowerShell: .ps1 files (languages: pwsh, powershell, ps1)

Python Execution

Shell Script Execution

PowerShell Execution

File Handling

Executors support file creation and modification:
Files persist in the work directory between executions.

Error Handling

Handle execution errors:

Security Best Practices

Use Docker in Production

Always use Docker-based executors for production:

Set Resource Limits

Limit container resources:

Use Read-Only Mounts

Mount sensitive data as read-only:

Restrict Network Access

Disable network for untrusted code:

Context Manager Usage

Use context managers for automatic cleanup:

Integration with Agents

Use executors with coding agents:

Troubleshooting

Docker Not Running

Solution: Start Docker Desktop or the Docker daemon:

Permission Denied

Solution: Add your user to the docker group:

Timeout Errors

Solution: Increase the timeout value:

Next Steps

Model Clients

Configure LLM providers

Tools

Add tools and capabilities