Skip to main content
Human in the Loop (HITL) is a powerful pattern that enables your AG2 agents to collaborate with humans during their workflow. Instead of making all decisions independently, agents can check with human operators at critical decision points, combining AI efficiency with human judgment.

An Analogy for HITL

Think of Human in the Loop like a hospital treatment system:
  • Doctors (AI agents) examine patients, run tests, and prepare treatment plans
  • For serious or unusual conditions, they consult with senior specialists (humans) before proceeding
  • The specialist provides final approval for critical treatments or unusual cases
  • The doctor then handles all the details once approval is given
  • This partnership combines the doctor’s efficiency with the specialist’s judgment for important decisions
This approach ensures routine cases are handled quickly while critical decisions receive proper oversight.

When to Use HITL

Human in the Loop is particularly valuable when:
  • Decisions require nuanced judgment (e.g., financial compliance, legal matters)
  • Errors could have significant consequences (e.g., financial transactions, safety-critical systems)
  • The process benefits from subjective input (e.g., content approval, design choices)
  • Regulatory requirements mandate human oversight (e.g., financial services, healthcare)

Implementing HITL in AG2

Creating a Human in the Loop workflow in AG2 is straightforward using ConversableAgent whereby the agent is the human in the loop, controlled through the human_input_mode parameter:
The human_input_mode parameter has three possible values:
  • ALWAYS: The agent uses the human input as its response
  • TERMINATE: The agent asks for input only when terminating a conversation
  • NEVER: The agent never asks for human input

Financial Compliance Example

Let’s build a financial compliance system that automatically reviews transactions but flags suspicious ones for human review. This example builds upon our basic ConversableAgent from the previous section with two critical improvements:
  • Sophisticated Instructions: Instead of just answering questions about suspicious transactions, our finance bot now actively identifies and processes them according to specific criteria.
  • Human Input Mode: Setting human_input_mode="ALWAYS" creates a checkpoint where a human must provide input before the workflow can continue - essential for regulatory compliance.
The workflow follows this pattern: human_in_the_loop_example

Establishing finance and human agents

Without human-in-the-loop, the financial bot might incorrectly approve suspicious transactions. With HITL, we create a crucial safety mechanism where human judgment intervenes precisely when needed.

Starting the Conversation

Now let’s generate some sample transactions and start the conversation between our agents:

What Happens During Execution

When this code runs:
  • The finance bot receives the list of transactions.
  • It analyzes each transaction to determine if it’s suspicious:
    • Amounts over $10,000 are flagged as suspicious
    • Vague memos (like “Confidential”) might trigger review
  • Regular transactions (small amounts, familiar vendors) are automatically approved.
  • All suspicious transactions are collected and presented to the human for approval at once.
  • When the human responds with approval, the finance bot processes all flagged transactions.
  • The finance bot provides a summary of all transactions and indicates the user can type “exit” to finish.
  • The human types “exit” to end the conversation.

Complete Code Example

Here’s the complete, ready-to-run code for our financial compliance Human in the Loop example. You can copy and paste this into a Python file and run it to see the interaction in action: ???+ info “Complete Code Example”

How to Run This Example

  • Save the code above to a file (e.g., financial_compliance.py)
  • Set your OpenAI API key in your environment variable or use your preferred model provider
  • Make sure you have AG2 installed: pip install ag2[openai]
  • Run the script: python financial_compliance.py

Example Output

When you run this code, you’ll see the finance bot analyze each transaction. For suspicious transactions, you’ll be prompted to provide input - type either approve, deny, or provide reasoning. For normal transactions, the finance bot will automatically approve them. At the end, you’ll see a summary report of all the transactions processed.

Next Steps

Now that you understand how to implement Human in the Loop workflows, let’s explore how to connect multiple agents together! Head over to Agent Orchestration to learn how to build more complex multi-agent systems that can handle sophisticated tasks through collaboration.