Code Executor (autogen.coding ) | Environment | Platform |
---|---|---|
LocalCommandLineCodeExecutor | Shell | Local |
DockerCommandLineCodeExecutor | Shell | Docker |
jupyter.JupyterCodeExecutor | Jupyter Kernel (e.g., python3) | Local/Docker |
Local Execution
The figure below shows the architecture of the local command line code executor (autogen.coding.LocalCommandLineCodeExecutor
).
Executing LLM-generated code poses a security risk to your host environment.

matplotlib
and numpy
are installed.
human_input_mode="ALWAYS"
to manually validate the safety of the code being executed.
Docker Execution
To mitigate the security risk of running LLM-generated code locally, we can use the docker command line code executor (autogen.coding.DockerCommandLineCodeExecutor
) to execute code in a docker container.
This way, the generated code can only access resources that are explicitly given to it.
The figure below illustrates how docker execution works.

work_dir
in the constructor points to a local file system directory just like in the local execution case.
The docker container will mount this directory and the executor write code files and output to it.
Use Code Execution in Conversation
Writing and executing code is necessary for many tasks such as data analysis, machine learning, and mathematical modeling. In AG2, coding can be a conversation between a code writer agent and a code executor agent, mirroring the interaction between a programmer and a code interpreter.
system_message
. The system message contains important instruction on how to use the code executor in the code executor agent.

Command Line or Jupyter Code Executor?
The command line code executor does not keep any state in memory between executions of different code blocks it receives, as it writes each code block to a separate file and executes the code block in a new process. Contrast to the command line code executor, the Jupyter code executor runs all code blocks in the same Jupyter kernel, which keeps the state in memory between executions. The choice between command line and Jupyter code executor depends on the nature of the code blocks in agents’ conversation. If each code block is a “script” that does not use variables from previous code blocks, the command line code executor is a good choice. If some code blocks contain expensive computations (e.g., training a machine learning model and loading a large amount of data), and you want to keep the state in memory to avoid repeated computations, the Jupyter code executor is a better choice.More Code Execution examples
- Task Solving with Code Generation, Execution, and Debugging
- Auto-Generated Agent Chat: Task Solving with Code Gen, Execution, Debugging & Human Feedback