human-in-the-loop

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

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.
Human-in-the-Loop(HITL)填补了完全自动化与人工控制之间的差距。它将人类用户视为“特权工具”或“审批者”。这在AI失误无法接受的高风险领域,或是需要人类直觉引导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.
  • 高风险场景:转账、向生产环境部署代码、向客户发送邮件。
  • 歧义消除:当用户意图不明确时(例如“帮我订机票”→“哪天出发?”)。
  • 主动学习:收集人类反馈以优化模型(RLHF)。
  • 质量保障:报告发布前审核最终版本。

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.
  • 审批工作流:Agent草拟回复→人工批准/编辑→Agent发送。
  • 升级处理:聊天机器人处理简单查询→将复杂问题升级至人工支持。
  • 澄清需求:Agent:“我找到了3个文件。您指的是哪一个?”→人类选择。

Implementation Pattern

实现模式

python
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)
python
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."
python
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.
输入:“Agent发送任何邮件前都需要人工批准。”
python
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)
输出:仅在获得明确人工批准后发送邮件,并保留完整审计记录。

Troubleshooting

故障排查

ProblemCauseFix
Humans are a bottleneckToo many approval requestsRaise the automation threshold; only require approval above risk score 0.8
Agent waits indefinitelyNo timeout configuredSet approval timeout; define auto-escalation path on timeout
Reviewer lacks contextUI shows only the actionInclude full context: why the agent wants to take this action
Feedback loop never closesNo mechanism to learn from rejectionsLog rejection reasons; update agent guidelines after 10+ similar rejections
问题原因解决方法
人工成为瓶颈审批请求过多提高自动化阈值;仅对风险评分高于0.8的操作要求审批
Agent无限等待未配置超时设置审批超时;定义超时后的自动升级路径
审核者缺乏上下文UI仅显示操作内容包含完整上下文:Agent执行该操作的原因
反馈循环无法闭合没有从拒绝中学习的机制记录拒绝原因;在出现10次以上类似拒绝后更新Agent准则