human-in-the-loop
Human-in-the-Loop
Human-in-the-Loop (HITL) bridges the gap between full automation and manual control. It treats the human user as a "privileged tool" or "approver". This is crucial for high-stakes domains where AI errors are unacceptable, or for ambiguous tasks where human intuition is required to guide the agent.
When to Use
- High Consequence: Transferring money, deploying code to production, sending emails to customers.
- Ambiguity Resolution: When the user's intent is unclear ("Book me a flight" -> "Which day?").
- Active Learning: Collecting human feedback to improve the model (RLHF).
- Quality Assurance: Reviewing the final draft of a report before publication.
Use Cases
- Approval Workflow: Agent drafts a reply -> Human approves/edits -> Agent sends.
- Escalation: Chatbot handles simple queries -> Escalates to human support for complex issues.
- Clarification: Agent: "I found 3 files. Which one do you mean?" -> Human selects.
Implementation Pattern
def hitl_workflow(user_request):
# Step 1: Plan/Draft
plan = agent.create_plan(user_request)
# Step 2: Risk Assessment
if is_high_risk(plan):
# Step 3: Pause for Approval
# Send notification to user UI
approval = notify_user(
message="Agent wants to execute the following plan:",
payload=plan
)
if approval.status == "REJECTED":
return "Action cancelled by user."
elif approval.status == "EDITED":
plan = approval.new_plan
# Step 4: Execute
return agent.execute(plan)
Examples
Input: "Require human approval before the agent sends any email."
def send_email_with_approval(draft):
# Present draft to human
approval = review_queue.submit({
"type": "email_approval",
"content": draft,
"timeout_hours": 4
})
if approval.status == "approved":
email_client.send(draft)
elif approval.status == "rejected":
agent.revise(draft, feedback=approval.comment)
else: # timeout
escalate_to_manager(draft)
Output: Email sent only after explicit human approval, with full audit trail.
Troubleshooting
| Problem | Cause | Fix |
|---|---|---|
| Humans are a bottleneck | Too many approval requests | Raise the automation threshold; only require approval above risk score 0.8 |
| Agent waits indefinitely | No timeout configured | Set approval timeout; define auto-escalation path on timeout |
| Reviewer lacks context | UI shows only the action | Include full context: why the agent wants to take this action |
| Feedback loop never closes | No mechanism to learn from rejections | Log rejection reasons; update agent guidelines after 10+ similar rejections |
More from lauraflorentin/skills-marketplace
multi-agent-collaboration
A structural pattern where multiple specialized agents communicate and coordinate to solve a problem that is too complex for a single agent. Use when user asks to "build a multi-agent system", "agents working together", "agent collaboration", or mentions team of agents, distributed agents, or swarm.
21reflection
A recursive pattern where an agent evaluates and critiques its own output to iteratively improve quality and catch errors. Use when user asks to "add self-reflection", "agent introspection", "self-critique", or mentions self-evaluation, meta-cognition, or quality self-assessment.
18planning
A high-level cognitive pattern where an agent formulates a structured sequence of actions (a plan) before executing any of them, ensuring goal-directed behavior. Use when user asks to "add planning to my agent", "task planning", "agent planning", or mentions plan generation, plan execution, or step-by-step planning.
14parallelization
A concurrency pattern where multiple agent tasks are executed at the same time to speed up processing or gather diverse perspectives. Use when user asks to "run agents in parallel", "parallelize tasks", "concurrent execution", or mentions parallel processing, fan-out, or batch execution.
13routing
A control flow pattern where a central component classifies an input request and directs it to the most appropriate specialized agent or tool. Use when user asks to "route between agents", "agent routing", "task dispatch", or mentions classifier routing, intent detection, or agent selection.
12adaptation
A dynamic pattern where an agent system modifies its own behavior, prompts, or tools over time based on feedback or performance metrics. Use when user asks to "make my agent adaptive", "add learning capabilities", "self-improving agent", or mentions adaptive behavior, online learning, or feedback loops.
12