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
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 usingConversableAgent
whereby the agent is the human in the loop, controlled through the human_input_mode
parameter:
human_input_mode
parameter has three possible values:
ALWAYS
: The agent uses the human input as its responseTERMINATE
: The agent asks for input only when terminating a conversationNEVER
: 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 basicConversableAgent
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.

Establishing finance and human agents
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