AutoGen Code Execution

Opt in to sandboxed local code execution for framework: autogen with Docker on by default

Enable sandboxed local code execution for framework: autogen runs via a single YAML flag — off by default so LLM-generated code never runs on your host without consent.

Diagram

None / False

True

dict

config.autogen.
code_execution

Value?

🚫 Disabled
no code runs

🐳 work_dir=coding
use_docker=True

🐳 use_docker=True
+ your overrides

Quick Start

  1. Simple opt-in (Docker on)

    Set code_execution: true — the wrapper defaults work_dir to coding and use_docker to True.

    yaml
    framework: autogen
    topic: Plot a sine wave and save it as sine.png
    config:
      autogen:
        code_execution: true
    bash
    export OPENAI_API_KEY=sk-...
    praisonai agents.yaml --framework autogen
  2. Custom work_dir

    Pass a dict to override the sandbox directory — use_docker still defaults to True.

    yaml
    framework: autogen
    topic: Analyse sales.csv and print totals
    config:
      autogen:
        code_execution:
          work_dir: "sandbox"
          use_docker: true
  3. Local execution (unsafe, dev only)

    Setting use_docker: false runs generated code directly on the host and logs a warning.

    yaml
    framework: autogen
    config:
      autogen:
        code_execution:
          use_docker: false   # LLM code runs on the host — dev only

How It Works

The wrapper resolves code_execution into AutoGen’s code_execution_config before it builds the UserProxyAgent.

Diagram
LLMDockerWrapperYAMLUserLLMDockerWrapperYAMLUserconfig.autogen.code_execution: truepraisonai agents.yaml –framework autogenresolve code_execution_cfg(defaults use_docker: True)run assistant + user_proxyemits code blocksandboxed execution in ./codingstdout/stderrresult (or termination)

The user_proxy is constructed with the resolved code_execution_config and the resolved human_input_mode. When the LLM emits a code block, the user_proxy runs it in the sandbox path; when nothing is opted in, code_execution_config is False and no code runs.


Configuration Options

Every option lives under config.autogen.* in your team YAML.

Option (config.autogen.*)TypeDefaultDescription
code_executionbool \dict \NoneNone (disabled)False/None → off; True → {work_dir: "coding", use_docker: True}; dict merged with use_docker: True default. Unrecognised values log a warning and disable execution.
code_execution.work_dirstr"coding"Working directory for generated code
code_execution.use_dockerboolTrue (when opted-in)Sandbox in Docker. Setting False logs a warning and runs on the host.
human_input_modestr"TERMINATE"Passed to UserProxyAgent. Overrideable per YAML. Previously forced to "NEVER".

Precedence Ladder

code_execution accepts a bool for the common case and a dict for overrides.

yaml
# Level 1: Bool (simplest — off by default)
framework: autogen
config:
  autogen:
    code_execution: true   # Docker on, work_dir=coding

# Level 2: Dict (custom overrides)
framework: autogen
config:
  autogen:
    code_execution:
      work_dir: "sandbox"
      use_docker: true

# Level 3: Override human_input_mode
framework: autogen
config:
  autogen:
    code_execution: true
    human_input_mode: "ALWAYS"   # or NEVER, TERMINATE (default)

Failure Scenario

When use_docker: false the wrapper emits this warning before running code on the host:

framework=autogen: code_execution is enabled with use_docker=False;
LLM-generated code will run directly on the host.
Set config.autogen.code_execution.use_docker: true to sandbox.

Best Practices

Keep Docker on for anything shared

use_docker: true (the opt-in default) sandboxes generated code. Only drop to use_docker: false on a throwaway local machine, never in CI or on a server.

Opt in explicitly after upgrading

Runs that previously relied on the old implicit host execution now do nothing until you add config.autogen.code_execution: true. Add it deliberately so the security posture is visible in the YAML.

Use a dedicated work_dir

Point work_dir at a disposable folder (e.g. sandbox) so generated files stay isolated and easy to clean up between runs.

Choose human_input_mode for your loop

TERMINATE (the new default) lets an operator step in at the end of a turn. Use NEVER for fully automated runs or ALWAYS for interactive review.


Related