This example demonstrates how to build an AI-powered bug fixer using two AG2 agents and Daytona sandboxes. A bug_fixer LLM agent analyzes broken code and proposes fixes, while a code_executor agent runs each fix attempt inside an isolated Daytona sandbox. If execution fails, the bug fixer sees the full error output and retries — looping until the code passes or the turn limit is reached.
The executor supports Python, JavaScript, TypeScript, and Bash — inferring the language automatically from the code block returned by the LLM.
- Execution-verified fixes: Every proposed fix is actually run in a sandbox — the agent only terminates when the code passes, not just when it looks correct
- Secure execution: Fix attempts run in isolated Daytona sandboxes, not on your machine
- Multi-language support: Python, JavaScript, TypeScript, and Bash — language is inferred automatically from the LLM's fenced code block
- Iterative refinement: If a fix fails, the agent sees the full error output and retries automatically
- Automatic cleanup: The sandbox is deleted as soon as
fix_bugreturns, regardless of outcome
- Python: Version 3.10 or higher
DAYTONA_API_KEY: Required for access to Daytona sandboxes. Get it from Daytona DashboardOPENAI_API_KEY: Required for GPT-4o-mini access. Get it from OpenAI Platform
- Create and activate a virtual environment:
python3.10 -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate- Install dependencies:
pip install "ag2[openai]<1.0" daytona python-dotenv- Set your API keys in
.env:
DAYTONA_API_KEY=your_daytona_api_key
OPENAI_API_KEY=your_openai_api_key- Run the example:
python main.pycode_executorsends the broken code tobug_fixeras the opening messagebug_fixer(GPT-4o-mini) analyzes it and replies with a fix wrapped in a fenced code block- AG2 detects the code block, runs it in a Daytona sandbox via
DaytonaCodeExecutor, and sends the result back - If execution fails,
bug_fixersees the error output and proposes a new fix - The loop continues until the code passes or
max_turnsis reached - The sandbox is automatically deleted when
fix_bugreturns
The script runs three examples — one per supported language:
- Python — postfix expression evaluator with swapped operands for
-and/ - JavaScript — run-length encoder with wrong concatenation order in two places
- TypeScript — Kadane's max subarray algorithm using
Math.mininstead ofMath.max
============================================================
Example 1: Python — Postfix Expression Evaluator Bug
============================================================
code_executor (to bug_fixer):
Fix this broken code:
...
>>>>>>>> USING AUTO REPLY...
bug_fixer (to code_executor):
```python
def eval_postfix(expression):
...
elif token == '-':
stack.append(a - b) # Fixed order of operands for subtraction
elif token == '/':
stack.append(a // b) # Fixed order of operands for division
...
```
>>>>>>>> USING AUTO REPLY...
>>>>>>>> EXECUTING CODE BLOCK (inferred language is python)...
code_executor (to bug_fixer):
exitcode: 0 (execution succeeded)
Code output: All postfix tests passed!
>>>>>>>> USING AUTO REPLY...
bug_fixer (to code_executor):
TERMINATE
For the complete API reference, see the DaytonaCodeExecutor documentation.
See the main project LICENSE file for details.