实战:人工审批 (Human-in-the-Loop)
在 Agent 执行敏感操作时配置人工审批。
使用 interrupt_on 参数,可以在 Agent 调用特定工具时暂停执行,等待人工批准。
基本用法
python
from langchain.agents import create_agent
from langchain.tools import tool
agent = create_agent(
model="openai:gpt-4o",
tools=tools,
interrupt_on={
"execute_shell_command": True,
"send_email": True,
},
)当 Agent 调用标记了 interrupt_on 的工具时,执行暂停,等待人工审批。
审批流程
- Agent 调用标记了
interrupt_on的工具 - 执行暂停,等待人工审批
- 人工可以 批准(继续执行)或 拒绝(跳过该工具调用)
- Agent 根据审批结果继续执行
审批状态会被持久化(通过 Checkpointer),支持中断后恢复执行。
下一步
- Hello Agent — Agent 基础
- 人机协同 — Deep Agents 审批机制
- LangGraph Interrupt — LangGraph 中断机制