AutoGen Tutorial (Python): adding human-in-the-loop for beginners

By Cyprian AaronsUpdated 2026-04-21
autogenadding-human-in-the-loop-for-beginnerspython

This tutorial shows how to add a human approval step to an AutoGen Python workflow so an agent can pause, ask for confirmation, and continue only when a person approves. You need this when the agent is about to send an email, run a risky action, or make a decision that should not be fully autonomous.

What You'll Need

  • Python 3.10+
  • pyautogen installed
  • An OpenAI API key set in your environment
  • Basic familiarity with AssistantAgent, UserProxyAgent, and AutoGen chat loops
  • A terminal where you can run Python scripts interactively

Install the package:

pip install pyautogen

Set your API key:

export OPENAI_API_KEY="your-api-key"

Step-by-Step

  1. Start by creating a normal assistant agent and a user proxy agent. The key idea is that the user proxy becomes the human gatekeeper, while the assistant generates the plan or action request.
from autogen import AssistantAgent, UserProxyAgent

llm_config = {
    "model": "gpt-4o-mini",
    "api_key": None,
}

assistant = AssistantAgent(
    name="assistant",
    llm_config=llm_config,
)

human = UserProxyAgent(
    name="human",
    human_input_mode="ALWAYS",
    code_execution_config=False,
)
  1. Next, define a task that clearly requires approval before execution. In production, this is where you would place anything that touches email, payments, customer records, or external systems.
task = """
Draft an email to the customer explaining that their refund has been approved.
Do not send it yet. Wait for human approval before continuing.
"""

result = human.initiate_chat(
    assistant,
    message=task,
)
  1. If you want the assistant to propose an action and wait for explicit approval, add a simple policy function. This keeps the workflow deterministic: the model suggests, the human decides.
def needs_human_approval(message: str) -> bool:
    risky_keywords = ["send", "delete", "approve", "refund", "payment", "submit"]
    return any(word in message.lower() for word in risky_keywords)

proposal = "Send the refund confirmation email to the customer."

if needs_human_approval(proposal):
    print("Human approval required:")
    print(proposal)
else:
    print("Safe to proceed automatically.")
  1. For a cleaner beginner-friendly pattern, use UserProxyAgent as the approval checkpoint and let it prompt in the terminal. This is the simplest way to get real human-in-the-loop behavior without building extra UI.
from autogen import AssistantAgent, UserProxyAgent

llm_config = {
    "model": "gpt-4o-mini",
}

assistant = AssistantAgent(
    name="assistant",
    llm_config=llm_config,
)

approver = UserProxyAgent(
    name="approver",
    human_input_mode="ALWAYS",
    code_execution_config=False,
)

approver.initiate_chat(
    assistant,
    message="Prepare a refund approval note and wait for my confirmation before taking any action.",
)
  1. If you want a more realistic workflow, split generation from execution. The assistant drafts an action plan first, then your code asks the human whether to proceed.
from autogen import AssistantAgent

llm_config = {"model": "gpt-4o-mini"}
assistant = AssistantAgent(name="assistant", llm_config=llm_config)

draft_prompt = """
Write a short action plan for sending a refund confirmation email.
Return only the plan in plain text.
"""

plan = assistant.generate_reply(messages=[{"role": "user", "content": draft_prompt}])
print("Proposed plan:")
print(plan)

approval = input("\nApprove this plan? (yes/no): ").strip().lower()
if approval == "yes":
    print("Proceeding with execution.")
else:
    print("Stopped by human reviewer.")

Testing It

Run the script from your terminal and watch for two things: first, whether AutoGen produces a proposal or response; second, whether execution pauses until you type input. If you used human_input_mode="ALWAYS", AutoGen should prompt you directly in the terminal during the chat loop.

Test both branches of your approval logic. Enter yes once and no once so you confirm that risky actions are only allowed after explicit consent.

If you are wiring this into a real app, log every proposal and every approval decision with timestamps and user identity. That gives you auditability, which matters more than convenience in banking and insurance workflows.

Next Steps

  • Add role-based approvals so only specific users can approve certain actions
  • Store approval events in PostgreSQL or your audit log system
  • Wrap this pattern around tools like email sending, ticket creation, or payment initiation

Keep learning

By Cyprian Aarons, AI Consultant at Topiax.

Want the complete 8-step roadmap?

Grab the free AI Agent Starter Kit — architecture templates, compliance checklists, and a 7-email deep-dive course.

Get the Starter Kit

Related Guides