Skip to content

AG2 (AutoGen) Recipes

Version: 0.12.0 Last Updated: April 2026 Focus: Ready-to-Use Examples

This collection of recipes provides copy-pasteable code for common AG2 use cases.

A basic agent that chats with the user.

from autogen import Agent, UserProxyAgent
assistant = Agent(name="assistant", system_message="You are a helpful AI assistant.")
user_proxy = UserProxyAgent(name="user_proxy", human_input_mode="ALWAYS")
user_proxy.initiate_chat(assistant, message="Hello!")

An agent that can write and execute Python code.

from autogen import Agent, UserProxyAgent
assistant = Agent(
name="coder",
system_message="You are a python expert. Write code to solve problems."
)
user_proxy = UserProxyAgent(
name="user_proxy",
human_input_mode="NEVER",
code_execution_config={"work_dir": "coding", "use_docker": False}
)
user_proxy.initiate_chat(assistant, message="Plot a chart of NVDA stock price YTD.")

Two agents debating a topic.

from autogen import Agent, GroupChat, GroupChatManager
pro_agent = Agent(name="Pro", system_message="Argue in favor of remote work.")
con_agent = Agent(name="Con", system_message="Argue against remote work.")
judge = Agent(name="Judge", system_message="Evaluate the debate.")
groupchat = GroupChat(agents=[pro_agent, con_agent, judge], messages=[], max_round=6)
manager = GroupChatManager(groupchat=groupchat)
pro_agent.initiate_chat(manager, message="Let's debate remote work.")

An agent that uses a calculator tool.

from autogen import Agent, UserProxyAgent
def calculator(a: int, b: int, op: str) -> int:
if op == "+": return a + b
elif op == "-": return a - b
elif op == "*": return a * b
elif op == "/": return a // b
else: return 0
assistant = Agent(name="assistant", system_message="Use the calculator tool.")
assistant.register_function(function_map={"calculator": calculator})
user_proxy = UserProxyAgent(name="user_proxy", human_input_mode="NEVER")
user_proxy.initiate_chat(assistant, message="Calculate 123 * 456")