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

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

This tutorial shows how to pause an AutoGen workflow for a human decision before the agent continues. You need this when the model is about to make a risky choice, approve a message, or ask for missing context that should not be guessed.

What You'll Need

  • Python 3.10+
  • autogen-agentchat
  • autogen-ext
  • An OpenAI API key set as OPENAI_API_KEY
  • Basic familiarity with AssistantAgent and UserProxyAgent
  • A terminal where you can run a Python script and type responses interactively

Step-by-Step

  1. Start by installing the packages and exporting your API key. AutoGen’s newer package split uses agentchat for agents and ext for model clients, so install both together.
pip install autogen-agentchat autogen-ext openai
export OPENAI_API_KEY="your-key-here"
  1. Create a model client and an assistant agent. This agent will do the normal LLM work, but we’ll route one step through a human before it can finish.
import asyncio
from autogen_agentchat.agents import AssistantAgent
from autogen_ext.models.openai import OpenAIChatCompletionClient

model_client = OpenAIChatCompletionClient(
    model="gpt-4o-mini",
    api_key=None,
)

assistant = AssistantAgent(
    name="assistant",
    model_client=model_client,
    system_message=(
        "You are a support analyst. "
        "Before sending any external message, ask for human approval."
    ),
)
  1. Add a small human-in-the-loop gate. In production, this is where you’d connect to Slack, a web UI, or a ticketing system. For this tutorial, we’ll use terminal input so you can see the control flow clearly.
async def require_human_approval(draft: str) -> str:
    print("\n--- HUMAN REVIEW REQUIRED ---")
    print(draft)
    decision = input("\nApprove? (y/n): ").strip().lower()

    if decision == "y":
        return draft

    edited = input("Enter revised message: ").strip()
    return edited
  1. Wire the approval step into an async workflow. The assistant drafts the message first, then the human approves or edits it before the final output is returned.
async def main():
    task = (
        "Draft a short customer email explaining that their refund "
        "will be processed within 3 business days."
    )

    result = await assistant.run(task=task)
    draft = result.messages[-1].content

    final_message = await require_human_approval(draft)

    print("\n--- FINAL MESSAGE ---")
    print(final_message)

if __name__ == "__main__":
    asyncio.run(main())
  1. If you want stricter control, split generation and execution into separate agents. This pattern is useful when one agent proposes an action and another component decides whether to allow it.
from autogen_agentchat.agents import UserProxyAgent

human_proxy = UserProxyAgent(
    name="human_proxy",
)

async def gated_workflow():
    result = await assistant.run(
        task="Write an internal note summarizing why this refund was delayed."
    )
    draft = result.messages[-1].content

    print("\nDraft from assistant:\n", draft)
    approved = input("\nType APPROVE to continue: ").strip()

    if approved != "APPROVE":
        print("Stopped by human.")
        return

    print("Approved content ready to send.")

Testing It

Run the script and confirm that the assistant produces a draft before any final output is printed. Then type n at the approval prompt and verify that you can replace the draft with your own edited version.

Test both branches:

  • approve with y
  • reject with n and enter revised text

If you want to validate this in a real application, log both the original draft and the human-edited version. That gives you an audit trail for compliance reviews and post-incident analysis.

Next Steps

  • Replace terminal input with a real approval UI using FastAPI or Streamlit
  • Add structured outputs so humans approve JSON actions instead of raw text
  • Learn AutoGen group chat patterns for multi-agent workflows with one human supervisor

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