Human in the Loop: Adding Human Oversight - AG2

Human in the Loop: Adding Human Oversight

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:

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:

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:

from autogen import ConversableAgent

# Create a human agent that will always prompt for input
human = ConversableAgent(
    name="human",
    human_input_mode="ALWAYS",  # Always ask for human input
)

# Create an AI agent that never asks for human input directly
ai_agent = ConversableAgent(
    name="ai_assistant",
    system_message="You are a helpful AI assistant",
    human_input_mode="NEVER",  # Never ask for human input directly
)

The human_input_mode parameter has three possible values:

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:

The workflow follows this pattern:

Establishing finance and human agents

from autogen import ConversableAgent, LLMConfig
import os
import random
from dotenv import load_dotenv

load_dotenv()

# Note: Make sure to set your API key in your environment first

# Configure the LLM
llm_config = LLMConfig(
    config_list={
        "api_type": "openai",
        "model": "gpt-5-nano",
        "api_key": os.environ.get("OPENAI_API_KEY"),
    }
)

# Define the system message for our finance bot
finance_system_message = """
You are a financial compliance assistant. You will be given a set of transaction descriptions.
For each transaction:
- If it seems suspicious (e.g., amount > $10,000, vendor is unusual, memo is vague), ask the human agent for approval.
- Otherwise, approve it automatically.
Provide the full set of transactions to approve at one time.
If the human gives a general approval, it applies to all transactions requiring approval.
When all transactions are processed, summarize the results and say "You can type exit to finish".
"""

# Create the finance agent with LLM intelligence
finance_bot = ConversableAgent(
    name="finance_bot",
    llm_config=llm_config,
    system_message=finance_system_message,
)

# Create the human agent for oversight
human = ConversableAgent(
    name="human",
    human_input_mode="ALWAYS",  # Always ask for human input
)

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:

# Generate sample transactions - this creates different transactions each time you run
VENDORS = ["Staples", "Acme Corp", "CyberSins Ltd", "Initech", "Globex", "Unicorn LLC"]
MEMOS = ["Quarterly supplies", "Confidential", "NDA services", "Routine payment", "Urgent request", "Reimbursement"]

def generate_transaction():
    amount = random.choice([500, 1500, 9999, 12000, 23000, 4000])
    vendor = random.choice(VENDORS)
    memo = random.choice(MEMOS)
    return f"Transaction: ${amount} to {vendor}. Memo: {memo}."

# Generate 3 random transactions
transactions = [generate_transaction() for _ in range(3)]

# Format the initial message
initial_prompt = (
    "Please process the following transactions one at a time:\n\n" +
    "\n".join([f"{i+1}. {tx}" for i, tx in enumerate(transactions)])
)

# Start the conversation from the human agent
response = human.run(
    recipient=finance_bot,
    message=initial_prompt,
)

# Display the response
response.process()

What Happens During Execution

When this code runs:

Complete Code Example

Here's the complete, ready-to-run code for our financial compliance Human in the Loop example:

from autogen import ConversableAgent, LLMConfig
import os
import random
from dotenv import load_dotenv

load_dotenv()

# Note: Make sure to set your API key in your environment first

# Configure the LLM
llm_config = LLMConfig(
    config_list={
        "api_type": "openai",
        "model": "gpt-5-nano",
        "api_key": os.environ.get("OPENAI_API_KEY"),
    }
)

# Create the human agent for oversight
human = ConversableAgent(
    name="human",
    human_input_mode="ALWAYS",  # Always ask for human input
)

# Generate sample transactions - this creates different transactions each time you run
VENDORS = ["Staples", "Acme Corp", "CyberSins Ltd", "Initech", "Globex", "Unicorn LLC"]
MEMOS = ["Quarterly supplies", "Confidential", "NDA services", "Routine payment", "Urgent request", "Reimbursement"]

# Generate 3 random transactions
transactions = [generate_transaction() for _ in range(3)]

# Start the conversation from the human agent
response = human.run(
    recipient=finance_bot,
    message=initial_prompt,
)

# Display the response
response.process()

How to Run This Example

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.