AI FLUENCY | AI AGENT ENGINEERING
AI Agent Human-in-the-Loop: When Should AI Agents Ask for Human Approval?
AI agents can plan, recommend, and execute actions. But should every action happen without human approval?
Discover how Human-in-the-Loop (HITL) keeps people involved in important AI decisions.
Understand → Apply → Build → Test
AIYING — Accomplishing with AI
🎬 Watch the Short Lesson
Watch how an AI agent pauses before a sensitive action and requests human approval.
1. What Is Human-in-the-Loop?
Human-in-the-Loop (HITL) is an approach where a human participates in an AI-supported process by reviewing, approving, rejecting, or correcting selected actions.
In AI agent engineering, HITL commonly means that an agent pauses before executing a sensitive tool action and waits for authorization.
Simple explanation: The AI agent can propose an action, but an authorized person decides whether that particular action should proceed.
Key Principle
AI Proposes → Human Decides
Human approval provides an additional control but does not replace authentication, authorization, or system-level security.
2. How Does Human-in-the-Loop Work?
If the action is approved, the agent runtime may execute the pending tool call.
If rejected, that specific tool call should not execute. The agent can instead report the rejection or propose another permitted approach.
Important: An approval should apply to the reviewed action and arguments, not automatically authorize every future action.
3. Where Is HITL Useful?
- Customer Support: Supervisor approval before issuing certain refunds or changing customer accounts.
- Communications: Review an AI-generated email before sending it externally.
- Financial Operations: Human authorization before purchases or financial transactions.
- IT Operations: Review high-impact system changes before execution.
- Content Publishing: Editorial approval before publishing customer-facing information.
4. Industry Application: Customer Support
Illustrative scenario: An AI customer-support agent prepares an email about a customer's refund request.
Before sending the email, the agent pauses for supervisor approval.
The supervisor checks:
- Correct customer and recipient
- Refund amount and applicable policy
- Accuracy of the message
- Appropriateness of the proposed action
The supervisor can approve or reject the pending sending action.
Industry benefit: Human review helps organizations retain oversight of consequential customer-facing actions.
This is an illustrative industry scenario, not a documented deployment by a particular company. Production systems also require identity verification, access controls, policy enforcement, and audit records.
5. Build an Approval-Gated AI Email Agent
Let's build a small AI agent that prepares an email and requests approval before executing a simulated sending action.
Learning objective: Understand how to pause an AI agent, inspect a proposed tool call, and approve or reject its execution.
Prerequisites
- Python 3.10 or later
- OpenAI API access
- An API key configured securely
- OpenAI Agents SDK
Step 1: Install the SDK
pip install -U openai-agents
Step 2: Create the Agent
Save the following code as approval_email_agent.py.
This project simulates sending an email. It does not connect to Gmail, Outlook, or any actual email service.
import asyncio
from agents import Agent, Runner
from agents.decorators import tool
@tool(needs_approval=True)
async def send_email(
recipient: str,
subject: str,
body: str
) -> str:
"""Simulate sending an email.
This tool does not contact an
actual email service.
"""
print("DEMO EMAIL ACTION EXECUTED")
print("To:", recipient)
print("Subject:", subject)
print("Body:", body)
return (
"Demo email action completed. "
"No real email was sent."
)
agent = Agent(
name="Approval-Gated Email Assistant",
instructions=(
"Prepare and send demo emails "
"when requested. Use the send_email "
"tool for a demo sending action. "
"Never claim a real email was sent."
),
tools=[send_email],
)
async def main():
result = await Runner.run(
agent,
(
"Prepare and send a short "
"meeting reminder to "
"teammate@example.com "
"for tomorrow at 10 AM. "
"Use the demo email tool."
),
)
while result.interruptions:
state = result.to_state()
for request in result.interruptions:
print(
"PENDING TOOL:",
request.tool_name
)
print(
"ARGUMENTS:",
request.arguments
)
decision = input(
"Approve this DEMO action? "
"(yes/no): "
).strip().lower()
if decision == "yes":
state.approve(request)
else:
state.reject(request)
result = await Runner.run(
agent,
state
)
print(
"Agent response:",
result.final_output
)
if __name__ == "__main__":
asyncio.run(main())
Step 3: Run the Agent
Configure the
OPENAI_API_KEY
environment variable securely.
Then execute:
python approval_email_agent.py
Step 4: Review the Approval Request
When the agent requests the demo sending tool, the SDK should pause and expose the pending tool call for review.
The application displays the proposed recipient, subject, and message body as part of the tool arguments.
You can choose:
- yes: Approve the pending demo action.
- no: Reject the pending demo action.
The application then resumes the agent with the selected decision.
The example follows the OpenAI Agents SDK interruption and resume workflow. Exact model-generated wording may vary. Live API execution has not been independently verified for this lesson.
6. Test Your AI Agent
- Approval Test: Choose "yes" and verify that the demo sending action executes only after approval.
- Rejection Test: Start a new run, choose "no", and verify that the rejected action does not execute.
- Argument Review: Inspect the recipient, subject, and message body before making a decision.
- Approval Scope: Verify that a separate sensitive tool call requires its own approval.
- Security Test: Confirm that human approval does not bypass identity, permission, or authorization requirements.
Engineering takeaway: A safe approval process must distinguish proposing an action from executing it.
It must also ensure that approvals are genuine, appropriately scoped, and recorded when required.
7. Review Questions
- Why should an AI agent pause before executing certain actions?
- What is the difference between proposing and executing an action?
- Why is human approval insufficient without proper security controls?
8. Interactive Quiz
Test your understanding of Human-in-the-Loop AI agents with five questions.
9. Official References and Further Learning
What's Next?
AI Agent Evaluation
How can we test whether an AI agent is reliable, accurate, and safe?
Continue exploring practical AI Agent Engineering.
AIYING — Accomplishing with AI

No comments:
Post a Comment