Skip to content

Chapter 10 · Human-in-the-loop and conditional interrupts ​

interrupt_on pauses before selected tool calls; a checkpointer is required. You can also trigger it conditionally, e.g. only when writing paths outside /workspace.

Mechanics and design choices ​

Resume with Command(resume=...) using the same thread_id. The interrupted node reruns from the start; side effects may repeat, so external writes must be idempotent.

Example ​

python
import os
from langchain_openai import ChatOpenAI

model = ChatOpenAI(
    model=os.environ["MODEL_ID"],
    api_key=os.environ["DEEPAGENT_API_KEY"],
    base_url=os.environ.get("DEEPAGENT_BASE_URL", "https://api.openai.com/v1"),
)

from langchain.tools import tool
from langgraph.checkpoint.memory import MemorySaver
from deepagents import create_deep_agent

@tool
def send_email(to: str, subject: str) -> str:
    """Send an email (mock)."""
    return f"sent to {to}: {subject}"

agent = create_deep_agent(
    model=model,
    tools=[send_email],
    interrupt_on={"send_email": True},
    checkpointer=MemorySaver(),
)
config = {"configurable": {"thread_id": "ch10"}}
paused = agent.invoke(
    {"messages": [{"role": "user", "content": "Send an email to ops@example.com with subject 'Deploy OK'."}]},
    config=config,
)
print("Interrupted:", bool(paused["__interrupt__"]))

from langgraph.types import Command
result = agent.invoke(Command(resume=True), config=config, version="v2")
print(result.value["messages"][-1].content)

Run and verify ​

API key required. Acceptance: the first invoke returns an interrupt (non-empty interrupt); after resuming with True, send_email runs.

Pitfalls ​

Do not treat interrupts as transactions: node reruns mean external side effects may repeat. Prefer conditional interrupts (a when predicate) on sensitive tools over enabling them everywhere.

🧪 随堂测验

点击你认为正确的选项。答错时会展示正确答案与原因解析。

1. What does interrupting need?

2. How to resume?

3. How to avoid duplicate side effects?

4. Is enabling approvals everywhere cheap?

Hands-on exercises ​

  1. Restrict the interrupt to external recipient domains.
  2. Prepare batch approvals for two sensitive tool calls in one turn.
  3. Write a reject-with-explanation resume path.

Official references ​

Verified on 2026-09-17 with Deep Agents 0.7.15.