interrupt_on参数配置哪些工具需要批准。
基本配置
interrupt_on参数接受一个字典,将工具名称映射到中断配置。每个工具都可以配置为:
True:启用中断并使用默认行为(允许批准、编辑、拒绝)False:禁用此工具的中断{"allowed_decisions": [...]}:具有特定允许决策的自定义配置
复制
向 AI 提问
from langchain_core.tools import tool
from deepagents import create_deep_agent
from langgraph.checkpoint.memory import MemorySaver
@tool
def delete_file(path: str) -> str:
"""Delete a file from the filesystem."""
return f"Deleted {path}"
@tool
def read_file(path: str) -> str:
"""Read a file from the filesystem."""
return f"Contents of {path}"
@tool
def send_email(to: str, subject: str, body: str) -> str:
"""Send an email."""
return f"Sent email to {to}"
# Checkpointer is REQUIRED for human-in-the-loop
checkpointer = MemorySaver()
agent = create_deep_agent(
model="claude-sonnet-4-5-20250929",
tools=[delete_file, read_file, send_email],
interrupt_on={
"delete_file": True, # Default: approve, edit, reject
"read_file": False, # No interrupts needed
"send_email": {"allowed_decisions": ["approve", "reject"]}, # No editing
},
checkpointer=checkpointer # Required!
)
决策类型
allowed_decisions列表控制人类在审查工具调用时可以采取的操作:
"approve":使用代理提议的原始参数执行工具"edit":在执行前修改工具参数"reject":完全跳过执行此工具调用
复制
向 AI 提问
interrupt_on = {
# Sensitive operations: allow all options
"delete_file": {"allowed_decisions": ["approve", "edit", "reject"]},
# Moderate risk: approval or rejection only
"write_file": {"allowed_decisions": ["approve", "reject"]},
# Must approve (no rejection allowed)
"critical_operation": {"allowed_decisions": ["approve"]},
}
处理中断
当中断触发时,代理会暂停执行并返回控制权。检查结果中的中断并相应地处理它们。复制
向 AI 提问
import uuid
from langgraph.types import Command
# Create config with thread_id for state persistence
config = {"configurable": {"thread_id": str(uuid.uuid4())}}
# Invoke the agent
result = agent.invoke({
"messages": [{"role": "user", "content": "Delete the file temp.txt"}]
}, config=config)
# Check if execution was interrupted
if result.get("__interrupt__"):
# Extract interrupt information
interrupts = result["__interrupt__"][0].value
action_requests = interrupts["action_requests"]
review_configs = interrupts["review_configs"]
# Create a lookup map from tool name to review config
config_map = {cfg["action_name"]: cfg for cfg in review_configs}
# Display the pending actions to the user
for action in action_requests:
review_config = config_map[action["name"]]
print(f"Tool: {action['name']}")
print(f"Arguments: {action['args']}")
print(f"Allowed decisions: {review_config['allowed_decisions']}")
# Get user decisions (one per action_request, in order)
decisions = [
{"type": "approve"} # User approved the deletion
]
# Resume execution with decisions
result = agent.invoke(
Command(resume={"decisions": decisions}),
config=config # Must use the same config!
)
# Process final result
print(result["messages"][-1]["content"])
多次工具调用
当代理调用多个需要批准的工具时,所有中断都会批量处理为一个中断。您必须按顺序为每个中断提供决策。复制
向 AI 提问
config = {"configurable": {"thread_id": str(uuid.uuid4())}}
result = agent.invoke({
"messages": [{
"role": "user",
"content": "Delete temp.txt and send an email to admin@example.com"
}]
}, config=config)
if result.get("__interrupt__"):
interrupts = result["__interrupt__"][0].value
action_requests = interrupts["action_requests"]
# Two tools need approval
assert len(action_requests) == 2
# Provide decisions in the same order as action_requests
decisions = [
{"type": "approve"}, # First tool: delete_file
{"type": "reject"} # Second tool: send_email
]
result = agent.invoke(
Command(resume={"decisions": decisions}),
config=config
)
编辑工具参数
当"edit"在允许的决策中时,您可以在执行前修改工具参数
复制
向 AI 提问
if result.get("__interrupt__"):
interrupts = result["__interrupt__"][0].value
action_request = interrupts["action_requests"][0]
# Original args from the agent
print(action_request["args"]) # {"to": "everyone@company.com", ...}
# User decides to edit the recipient
decisions = [{
"type": "edit",
"edited_action": {
"name": action_request["name"], # Must include the tool name
"args": {"to": "team@company.com", "subject": "...", "body": "..."}
}
}]
result = agent.invoke(
Command(resume={"decisions": decisions}),
config=config
)
子代理中断
每个子代理都可以有自己的interrupt_on配置,它会覆盖主代理的设置
复制
向 AI 提问
agent = create_deep_agent(
tools=[delete_file, read_file],
interrupt_on={
"delete_file": True,
"read_file": False,
},
subagents=[{
"name": "file-manager",
"description": "Manages file operations",
"system_prompt": "You are a file management assistant.",
"tools": [delete_file, read_file],
"interrupt_on": {
# Override: require approval for reads in this subagent
"delete_file": True,
"read_file": True, # Different from main agent!
}
}],
checkpointer=checkpointer
)
__interrupt__并使用Command恢复。
最佳实践
始终使用检查点
人工在环需要检查点来在中断和恢复之间持久化代理状态复制
向 AI 提问
from langgraph.checkpoint.memory import MemorySaver
checkpointer = MemorySaver()
agent = create_deep_agent(
tools=[...],
interrupt_on={...},
checkpointer=checkpointer # Required for HITL
)
使用相同的线程ID
恢复时,您必须使用具有相同thread_id的相同配置
复制
向 AI 提问
# First call
config = {"configurable": {"thread_id": "my-thread"}}
result = agent.invoke(input, config=config)
# Resume (use same config)
result = agent.invoke(Command(resume={...}), config=config)
决策顺序与操作匹配
决策列表必须与action_requests的顺序匹配
复制
向 AI 提问
if result.get("__interrupt__"):
interrupts = result["__interrupt__"][0].value
action_requests = interrupts["action_requests"]
# Create one decision per action, in order
decisions = []
for action in action_requests:
decision = get_user_decision(action) # Your logic
decisions.append(decision)
result = agent.invoke(
Command(resume={"decisions": decisions}),
config=config
)
根据风险定制配置
根据不同工具的风险级别进行配置复制
向 AI 提问
interrupt_on = {
# High risk: full control (approve, edit, reject)
"delete_file": {"allowed_decisions": ["approve", "edit", "reject"]},
"send_email": {"allowed_decisions": ["approve", "edit", "reject"]},
# Medium risk: no editing allowed
"write_file": {"allowed_decisions": ["approve", "reject"]},
# Low risk: no interrupts
"read_file": False,
"list_files": False,
}
以编程方式连接这些文档到 Claude、VSCode 等,通过 MCP 获取实时答案。