In AG2, assistant agents write code and a UserProxyAgent executes it. The proxy is the safety boundary: it runs the code, returns the result to the group, and the human behind it can see what ran and stop the next run.
Before you start
You need a working AG2 group chat with an assistant agent that writes code. Decide where code may run: a scratch folder or a container is safer than the project folder, and the proxy should never run code that deletes files or sends data out.
Add the UserProxyAgent as the executor
Create a UserProxyAgent with code execution enabled and add it to the group chat. Give it a name that says what it does, for example executor, and set it to ask for human input before running code that changes files. The engineer agent writes the code, the executor runs it, and the result goes back to the group.
Test with a safe script, then a risky one
Ask the group to write and run a script that prints a number, and check the executor runs it and returns the output. Then ask for a script that deletes a file, and check the human is asked first. If code runs without a prompt, the execution setting is too open.
Worked example: Code runs for a fictional research group
The AG2 research example uses an executor UserProxyAgent to run the engineer code. The table shows two runs.
| Script | Human asked? | Result |
|---|---|---|
| Print the paper count | No, it only reads | Runs and returns the count |
| Delete the results folder | Yes, it changes files | Waits for the human |
Expected result (UNTESTED): the print script runs and returns its output, and the delete script waits for human approval. We checked the AG2 research example, which describes the engineer and executor roles, but we did not run the code on this machine.
Limits
A proxy that runs code is powerful and dangerous: a prompt injection in a fetched page can become a command. Only let it run code in a scratch area, review the code before each run, and never give it credentials it does not need.
