【LangChain1.0】第七篇 Deep Agents:构建具备规划与子智能体能力的深度 Agent - node
第七篇 Deep Agents:构建具备规划与子智能体能力的深度 Agent
目标:掌握 deepagents 库,使用 Agent Harness 模式构建能够处理复杂、多步骤、长上下文任务的生产级智能体。
前置准备
环境配置
在开始学习之前,请确保完成以下环境配置:
1. 安装依赖
# 核心库
pip install deepagents
# 推荐工具(用于本篇实战)
pip install tavily-python langchain-community
2. 环境变量配置
import os
# 必须配置
os.environ["ANTHROPIC_API_KEY"] = "sk-..." # 默认基座模型
os.environ["TAVILY_API_KEY"] = "tvly-..." # 用于搜索能力
# 可选配置(但强烈推荐用于追踪)
os.environ["LANGSMITH_API_KEY"] = "lsv2-..."
os.environ["LANGSMITH_TRACING"] = "true"
前置知识
建议具备以下基础知识:
✅ LangGraph 基础 (State, Node, Edge 的概念)✅ Tool Calling (如何定义和使用工具)✅ 异步编程 (async/await)
第1章:Deep Agents 核心架构
1.1 什么是 Agent Harness?
在软件工程中,Harness(挽具/测试套件)通常指用来控制和测试组件的外部框架。Deep Agents 引入了 Agent Harness 的核心设计理念:它不改变底层的 LLM 或 LangGraph 图,而是像给赛马套上挽具一样,在 Agent 循环之外包裹了一层强制性的行为规范。
官方定义:
deepagents is a standalone library for building agents that can tackle complex, multi-step tasks. Built on LangGraph and inspired by applications like Claude Code, Deep Research, and Manus.
#mermaid-svg-ioS4jywYVOfK7yur{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-ioS4jywYVOfK7yur .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-ioS4jywYVOfK7yur .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-ioS4jywYVOfK7yur .error-icon{fill:#552222;}#mermaid-svg-ioS4jywYVOfK7yur .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-ioS4jywYVOfK7yur .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-ioS4jywYVOfK7yur .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-ioS4jywYVOfK7yur .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-ioS4jywYVOfK7yur .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-ioS4jywYVOfK7yur .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-ioS4jywYVOfK7yur .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-ioS4jywYVOfK7yur .marker{fill:#333333;stroke:#333333;}#mermaid-svg-ioS4jywYVOfK7yur .marker.cross{stroke:#333333;}#mermaid-svg-ioS4jywYVOfK7yur svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-ioS4jywYVOfK7yur p{margin:0;}#mermaid-svg-ioS4jywYVOfK7yur .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-ioS4jywYVOfK7yur .cluster-label text{fill:#333;}#mermaid-svg-ioS4jywYVOfK7yur .cluster-label span{color:#333;}#mermaid-svg-ioS4jywYVOfK7yur .cluster-label span p{background-color:transparent;}#mermaid-svg-ioS4jywYVOfK7yur .label text,#mermaid-svg-ioS4jywYVOfK7yur span{fill:#333;color:#333;}#mermaid-svg-ioS4jywYVOfK7yur .node rect,#mermaid-svg-ioS4jywYVOfK7yur .node circle,#mermaid-svg-ioS4jywYVOfK7yur .node ellipse,#mermaid-svg-ioS4jywYVOfK7yur .node polygon,#mermaid-svg-ioS4jywYVOfK7yur .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-ioS4jywYVOfK7yur .rough-node .label text,#mermaid-svg-ioS4jywYVOfK7yur .node .label text,#mermaid-svg-ioS4jywYVOfK7yur .image-shape .label,#mermaid-svg-ioS4jywYVOfK7yur .icon-shape .label{text-anchor:middle;}#mermaid-svg-ioS4jywYVOfK7yur .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-ioS4jywYVOfK7yur .rough-node .label,#mermaid-svg-ioS4jywYVOfK7yur .node .label,#mermaid-svg-ioS4jywYVOfK7yur .image-shape .label,#mermaid-svg-ioS4jywYVOfK7yur .icon-shape .label{text-align:center;}#mermaid-svg-ioS4jywYVOfK7yur .node.clickable{cursor:pointer;}#mermaid-svg-ioS4jywYVOfK7yur .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-ioS4jywYVOfK7yur .arrowheadPath{fill:#333333;}#mermaid-svg-ioS4jywYVOfK7yur .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-ioS4jywYVOfK7yur .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-ioS4jywYVOfK7yur .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-ioS4jywYVOfK7yur .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-ioS4jywYVOfK7yur .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-ioS4jywYVOfK7yur .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-ioS4jywYVOfK7yur .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-ioS4jywYVOfK7yur .cluster text{fill:#333;}#mermaid-svg-ioS4jywYVOfK7yur .cluster span{color:#333;}#mermaid-svg-ioS4jywYVOfK7yur div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-ioS4jywYVOfK7yur .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-ioS4jywYVOfK7yur rect.text{fill:none;stroke-width:0;}#mermaid-svg-ioS4jywYVOfK7yur .icon-shape,#mermaid-svg-ioS4jywYVOfK7yur .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-ioS4jywYVOfK7yur .icon-shape p,#mermaid-svg-ioS4jywYVOfK7yur .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-ioS4jywYVOfK7yur .icon-shape rect,#mermaid-svg-ioS4jywYVOfK7yur .image-shape rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-ioS4jywYVOfK7yur .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-ioS4jywYVOfK7yur .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-ioS4jywYVOfK7yur :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}Agent Harness (DeepAgents)Middleware Stack结果反馈触发规范化指令工具调用用户输入Planning(强制规划)Context(记忆卸载)SubAgent(任务分治)Core Agent Loop(LLM + Tools)
1.2 三大核心能力解析
Deep Agents 通过模块化中间件注入了三大核心能力,解决了构建复杂 Agent 时的“不可能三角”:
核心问题痛点描述Deep Agents 解决方案对应中间件规划混乱面对模糊目标,Agent 容易陷入死循环或过早停止强制规划 (Planning):将隐式思维链转化为显式 Todo ListTodoListMiddleware上下文爆炸中间步骤产生大量数据(如搜索结果),撑爆 Token 窗口上下文卸载 (Context Eviction):将长文本自动转存至虚拟文件系统FilesystemMiddleware单体瓶颈一个 Prompt 塞入过多指令,导致注意力分散分治策略 (Divide & Conquer):动态生成独立的子智能体处理任务SubAgentMiddleware1.3 技术选型决策树
什么时候应该使用 Deep Agents,什么时候用原生的 LangGraph?
#mermaid-svg-zukdT2sjqK5eNVO4{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-zukdT2sjqK5eNVO4 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-zukdT2sjqK5eNVO4 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-zukdT2sjqK5eNVO4 .error-icon{fill:#552222;}#mermaid-svg-zukdT2sjqK5eNVO4 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-zukdT2sjqK5eNVO4 .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-zukdT2sjqK5eNVO4 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-zukdT2sjqK5eNVO4 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-zukdT2sjqK5eNVO4 .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-zukdT2sjqK5eNVO4 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-zukdT2sjqK5eNVO4 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-zukdT2sjqK5eNVO4 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-zukdT2sjqK5eNVO4 .marker.cross{stroke:#333333;}#mermaid-svg-zukdT2sjqK5eNVO4 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-zukdT2sjqK5eNVO4 p{margin:0;}#mermaid-svg-zukdT2sjqK5eNVO4 .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-zukdT2sjqK5eNVO4 .cluster-label text{fill:#333;}#mermaid-svg-zukdT2sjqK5eNVO4 .cluster-label span{color:#333;}#mermaid-svg-zukdT2sjqK5eNVO4 .cluster-label span p{background-color:transparent;}#mermaid-svg-zukdT2sjqK5eNVO4 .label text,#mermaid-svg-zukdT2sjqK5eNVO4 span{fill:#333;color:#333;}#mermaid-svg-zukdT2sjqK5eNVO4 .node rect,#mermaid-svg-zukdT2sjqK5eNVO4 .node circle,#mermaid-svg-zukdT2sjqK5eNVO4 .node ellipse,#mermaid-svg-zukdT2sjqK5eNVO4 .node polygon,#mermaid-svg-zukdT2sjqK5eNVO4 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-zukdT2sjqK5eNVO4 .rough-node .label text,#mermaid-svg-zukdT2sjqK5eNVO4 .node .label text,#mermaid-svg-zukdT2sjqK5eNVO4 .image-shape .label,#mermaid-svg-zukdT2sjqK5eNVO4 .icon-shape .label{text-anchor:middle;}#mermaid-svg-zukdT2sjqK5eNVO4 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-zukdT2sjqK5eNVO4 .rough-node .label,#mermaid-svg-zukdT2sjqK5eNVO4 .node .label,#mermaid-svg-zukdT2sjqK5eNVO4 .image-shape .label,#mermaid-svg-zukdT2sjqK5eNVO4 .icon-shape .label{text-align:center;}#mermaid-svg-zukdT2sjqK5eNVO4 .node.clickable{cursor:pointer;}#mermaid-svg-zukdT2sjqK5eNVO4 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-zukdT2sjqK5eNVO4 .arrowheadPath{fill:#333333;}#mermaid-svg-zukdT2sjqK5eNVO4 .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-zukdT2sjqK5eNVO4 .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-zukdT2sjqK5eNVO4 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-zukdT2sjqK5eNVO4 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-zukdT2sjqK5eNVO4 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-zukdT2sjqK5eNVO4 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-zukdT2sjqK5eNVO4 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-zukdT2sjqK5eNVO4 .cluster text{fill:#333;}#mermaid-svg-zukdT2sjqK5eNVO4 .cluster span{color:#333;}#mermaid-svg-zukdT2sjqK5eNVO4 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-zukdT2sjqK5eNVO4 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-zukdT2sjqK5eNVO4 rect.text{fill:none;stroke-width:0;}#mermaid-svg-zukdT2sjqK5eNVO4 .icon-shape,#mermaid-svg-zukdT2sjqK5eNVO4 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-zukdT2sjqK5eNVO4 .icon-shape p,#mermaid-svg-zukdT2sjqK5eNVO4 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-zukdT2sjqK5eNVO4 .icon-shape rect,#mermaid-svg-zukdT2sjqK5eNVO4 .image-shape rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-zukdT2sjqK5eNVO4 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-zukdT2sjqK5eNVO4 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-zukdT2sjqK5eNVO4 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}否是是 (如固定审批流)否 (开放式任务)否是 (如深度搜索/编码)需求评估任务是否复杂?create_agent(LangChain 快速原型)是否需要精密编排?LangGraph(StateGraph 自定义图)上下文是否会溢出?DeepAgents(生产级 Harness 架构)
第2章:快速上手:构建 Deep Research Agent
本章我们将构建一个能够上网搜索、分析大量资料并撰写报告的“深度研究员”。
2.1 定义核心工具
首先,我们需要一个联网搜索工具。为了演示鲁棒性,我们需要做好错误处理。
import os
from typing import Literal
from tavily import TavilyClient
from langchain_core.tools import tool
tavily_client = TavilyClient(api_key=os.environ.get("TAVILY_API_KEY"))
@tool
def internet_search(
query: str,
max_results: int = 5,
topic: Literal["general", "news"] = "general",
) ->str:
"""
Run a web search using Tavily.
Always use this tool when you need external information.
"""
try:
print(f" [Tool] Searching for: {query}")
results = tavily_client.search(query, max_results=max_results, topic=topic)
output = []
if results.get("answer"):
output.append(f"Answer: {results['answer']}\n")
for res in results.get("results", []):
title = res.get("title", "No title")
url = res.get("url", "#")
content = res.get("content", "")[:300]
output.append(f"- [{title}]({url})\n {content}...")
return "\n".join(output) if output else "No results found."
except Exception as e:
return f"Error during search: {str(e)}"
2.2 构建 Deep Agent
使用 create_deep_agent 工厂函数,系统会自动注入 write_todos、read_file 等基础设施工具。
from deepagents import create_deep_agent
from langchain_core.messages import HumanMessage
# 创建 Deep Agent
agent = create_deep_agent(
tools=[internet_search], # 我们自定义的工具
model="claude-sonnet-4-5-20250929", # 建议使用强推理模型
system_prompt="""
You are an expert researcher. Follow these steps:
1. PLAN: Always use write_todos to breakdown the user request.
2. EXECUTE: Use internet_search to gather information.
3. MANAGE: The system will auto-evict long content to files.
Trust the file system. Use read_file if you need to review details.
4. DELIVER: Synthesize findings into a final markdown report.
"""
)
# 执行任务
print(" Deep Research Agent Started...")
query = "请调查 Deep Agents 库的核心特性,并与 LangGraph 做对比。最后生成一份 report.md。"
# 使用 stream 查看实时过程
for event in agent.stream({"messages": [HumanMessage(content=query)]}):
# 实际开发中可以打印 event 来观察 Agent 的每一步思考
pass
2.3 深度运行分析 (Execution Anatomy)
只要运行上面的代码,你会看到 Agent 完全不同于普通 ChatBot 的行为模式。让我们逐帧拆解它的思考过程:
第一阶段:强制规划 (The Planning Phase)
Agent 收到请求后,并没有直接搜索。TodoListMiddleware 强制它先调用 write_todos。
Agent 思考:
“这是一个复杂的任务。我需要先搜索,再对比,最后写文件。”
工具调用 (write_todos):
{
"todos": [
{"task": "Search for Deep Agents library documentation", "status": "pending"},
{"task": "Search for LangGraph comparison points", "status": "pending"},
{"task": "Write comparison report to report.md", "status": "pending"}
]
}
第二阶段:执行与上下文卸载 (Execution & Eviction)
Agent 开始执行第一个 Todo。它调用 internet_search 获得了一大段关于 DeepAgents 的介绍。
关键时刻:
假设搜索结果非常长(例如 10,000 tokens)。FilesystemMiddleware 会监测到这一情况。它不会让这 10k tokens 直接塞进下一轮的 Prompt,而是自动拦截:
自动保存:将搜索结果写入虚拟文件 /tmp/virtual/output_1.txt。指针替换:在 Context 中,将原来的长文本替换为:
Tool Output:
这样,Agent 依然“知道”结果在哪里,但它的 Context 保持了轻量级。
第三阶段:合成与交付 (Synthesis)
Agent 完成了所有搜索。此时 Todo List 状态变为:
Search Deep Agents Search LangGraph[>] Write report (Current)
Agent 调用 write_file 生成最终报告。整个过程行云流水,没有任何 Context 溢出的风险。
第3章:核心机制深度解析
3.1 规划系统:TodoListMiddleware
普通的 Agent 只有隐式的“思维链”(Chain of Thought),容易在长任务中迷失。TodoListMiddleware 引入了显式状态机。
#mermaid-svg-mC3BqymlPvO66GBK{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-mC3BqymlPvO66GBK .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-mC3BqymlPvO66GBK .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-mC3BqymlPvO66GBK .error-icon{fill:#552222;}#mermaid-svg-mC3BqymlPvO66GBK .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-mC3BqymlPvO66GBK .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-mC3BqymlPvO66GBK .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-mC3BqymlPvO66GBK .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-mC3BqymlPvO66GBK .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-mC3BqymlPvO66GBK .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-mC3BqymlPvO66GBK .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-mC3BqymlPvO66GBK .marker{fill:#333333;stroke:#333333;}#mermaid-svg-mC3BqymlPvO66GBK .marker.cross{stroke:#333333;}#mermaid-svg-mC3BqymlPvO66GBK svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-mC3BqymlPvO66GBK p{margin:0;}#mermaid-svg-mC3BqymlPvO66GBK defs #statediagram-barbEnd{fill:#333333;stroke:#333333;}#mermaid-svg-mC3BqymlPvO66GBK g.stateGroup text{fill:#9370DB;stroke:none;font-size:10px;}#mermaid-svg-mC3BqymlPvO66GBK g.stateGroup text{fill:#333;stroke:none;font-size:10px;}#mermaid-svg-mC3BqymlPvO66GBK g.stateGroup .state-title{font-weight:bolder;fill:#131300;}#mermaid-svg-mC3BqymlPvO66GBK g.stateGroup rect{fill:#ECECFF;stroke:#9370DB;}#mermaid-svg-mC3BqymlPvO66GBK g.stateGroup line{stroke:#333333;stroke-width:1;}#mermaid-svg-mC3BqymlPvO66GBK .transition{stroke:#333333;stroke-width:1;fill:none;}#mermaid-svg-mC3BqymlPvO66GBK .stateGroup .composit{fill:white;border-bottom:1px;}#mermaid-svg-mC3BqymlPvO66GBK .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px;}#mermaid-svg-mC3BqymlPvO66GBK .state-note{stroke:#aaaa33;fill:#fff5ad;}#mermaid-svg-mC3BqymlPvO66GBK .state-note text{fill:black;stroke:none;font-size:10px;}#mermaid-svg-mC3BqymlPvO66GBK .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5;}#mermaid-svg-mC3BqymlPvO66GBK .edgeLabel .label rect{fill:#ECECFF;opacity:0.5;}#mermaid-svg-mC3BqymlPvO66GBK .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-mC3BqymlPvO66GBK .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-mC3BqymlPvO66GBK .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-mC3BqymlPvO66GBK .edgeLabel .label text{fill:#333;}#mermaid-svg-mC3BqymlPvO66GBK .label div .edgeLabel{color:#333;}#mermaid-svg-mC3BqymlPvO66GBK .stateLabel text{fill:#131300;font-size:10px;font-weight:bold;}#mermaid-svg-mC3BqymlPvO66GBK .node circle.state-start{fill:#333333;stroke:#333333;}#mermaid-svg-mC3BqymlPvO66GBK .node .fork-join{fill:#333333;stroke:#333333;}#mermaid-svg-mC3BqymlPvO66GBK .node circle.state-end{fill:#9370DB;stroke:white;stroke-width:1.5;}#mermaid-svg-mC3BqymlPvO66GBK .end-state-inner{fill:white;stroke-width:1.5;}#mermaid-svg-mC3BqymlPvO66GBK .node rect{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-mC3BqymlPvO66GBK .node polygon{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-mC3BqymlPvO66GBK #statediagram-barbEnd{fill:#333333;}#mermaid-svg-mC3BqymlPvO66GBK .statediagram-cluster rect{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-mC3BqymlPvO66GBK .cluster-label,#mermaid-svg-mC3BqymlPvO66GBK .nodeLabel{color:#131300;}#mermaid-svg-mC3BqymlPvO66GBK .statediagram-cluster rect.outer{rx:5px;ry:5px;}#mermaid-svg-mC3BqymlPvO66GBK .statediagram-state .divider{stroke:#9370DB;}#mermaid-svg-mC3BqymlPvO66GBK .statediagram-state .title-state{rx:5px;ry:5px;}#mermaid-svg-mC3BqymlPvO66GBK .statediagram-cluster.statediagram-cluster .inner{fill:white;}#mermaid-svg-mC3BqymlPvO66GBK .statediagram-cluster.statediagram-cluster-alt .inner{fill:#f0f0f0;}#mermaid-svg-mC3BqymlPvO66GBK .statediagram-cluster .inner{rx:0;ry:0;}#mermaid-svg-mC3BqymlPvO66GBK .statediagram-state rect.basic{rx:5px;ry:5px;}#mermaid-svg-mC3BqymlPvO66GBK .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#f0f0f0;}#mermaid-svg-mC3BqymlPvO66GBK .note-edge{stroke-dasharray:5;}#mermaid-svg-mC3BqymlPvO66GBK .statediagram-note rect{fill:#fff5ad;stroke:#aaaa33;stroke-width:1px;rx:0;ry:0;}#mermaid-svg-mC3BqymlPvO66GBK .statediagram-note rect{fill:#fff5ad;stroke:#aaaa33;stroke-width:1px;rx:0;ry:0;}#mermaid-svg-mC3BqymlPvO66GBK .statediagram-note text{fill:black;}#mermaid-svg-mC3BqymlPvO66GBK .statediagram-note .nodeLabel{color:black;}#mermaid-svg-mC3BqymlPvO66GBK .statediagram .edgeLabel{color:red;}#mermaid-svg-mC3BqymlPvO66GBK #dependencyStart,#mermaid-svg-mC3BqymlPvO66GBK #dependencyEnd{fill:#333333;stroke:#333333;stroke-width:1;}#mermaid-svg-mC3BqymlPvO66GBK .statediagramTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-mC3BqymlPvO66GBK :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}Agent选择任务任务完成任务失败/重试PendingInProgressCompleted同一时间只能有一个任务处于 InProgress 状态
状态持久化:Todo List 不是 Prompt 里的一段话,而是一个结构化的对象列表。自我纠正机制:
如果 Agent 尝试执行一个不在 Todo List 里的任务,中间件会报错提示:“请先规划”。如果任务失败(如搜索无结果),Agent 会修改 Todo List(新增 “Retry search”),而不是盲目继续。
3.2 记忆系统:Context Eviction 原理
这是 Deep Agents 最核心的“黑科技”。它通过**虚拟文件系统(VFS)**实现了无限上下文的假象。
算法流程图:
#mermaid-svg-0ObDshNRWN34QlVc{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-0ObDshNRWN34QlVc .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-0ObDshNRWN34QlVc .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-0ObDshNRWN34QlVc .error-icon{fill:#552222;}#mermaid-svg-0ObDshNRWN34QlVc .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-0ObDshNRWN34QlVc .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-0ObDshNRWN34QlVc .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-0ObDshNRWN34QlVc .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-0ObDshNRWN34QlVc .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-0ObDshNRWN34QlVc .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-0ObDshNRWN34QlVc .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-0ObDshNRWN34QlVc .marker{fill:#333333;stroke:#333333;}#mermaid-svg-0ObDshNRWN34QlVc .marker.cross{stroke:#333333;}#mermaid-svg-0ObDshNRWN34QlVc svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-0ObDshNRWN34QlVc p{margin:0;}#mermaid-svg-0ObDshNRWN34QlVc .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-0ObDshNRWN34QlVc .cluster-label text{fill:#333;}#mermaid-svg-0ObDshNRWN34QlVc .cluster-label span{color:#333;}#mermaid-svg-0ObDshNRWN34QlVc .cluster-label span p{background-color:transparent;}#mermaid-svg-0ObDshNRWN34QlVc .label text,#mermaid-svg-0ObDshNRWN34QlVc span{fill:#333;color:#333;}#mermaid-svg-0ObDshNRWN34QlVc .node rect,#mermaid-svg-0ObDshNRWN34QlVc .node circle,#mermaid-svg-0ObDshNRWN34QlVc .node ellipse,#mermaid-svg-0ObDshNRWN34QlVc .node polygon,#mermaid-svg-0ObDshNRWN34QlVc .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-0ObDshNRWN34QlVc .rough-node .label text,#mermaid-svg-0ObDshNRWN34QlVc .node .label text,#mermaid-svg-0ObDshNRWN34QlVc .image-shape .label,#mermaid-svg-0ObDshNRWN34QlVc .icon-shape .label{text-anchor:middle;}#mermaid-svg-0ObDshNRWN34QlVc .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-0ObDshNRWN34QlVc .rough-node .label,#mermaid-svg-0ObDshNRWN34QlVc .node .label,#mermaid-svg-0ObDshNRWN34QlVc .image-shape .label,#mermaid-svg-0ObDshNRWN34QlVc .icon-shape .label{text-align:center;}#mermaid-svg-0ObDshNRWN34QlVc .node.clickable{cursor:pointer;}#mermaid-svg-0ObDshNRWN34QlVc .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-0ObDshNRWN34QlVc .arrowheadPath{fill:#333333;}#mermaid-svg-0ObDshNRWN34QlVc .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-0ObDshNRWN34QlVc .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-0ObDshNRWN34QlVc .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-0ObDshNRWN34QlVc .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-0ObDshNRWN34QlVc .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-0ObDshNRWN34QlVc .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-0ObDshNRWN34QlVc .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-0ObDshNRWN34QlVc .cluster text{fill:#333;}#mermaid-svg-0ObDshNRWN34QlVc .cluster span{color:#333;}#mermaid-svg-0ObDshNRWN34QlVc div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-0ObDshNRWN34QlVc .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-0ObDshNRWN34QlVc rect.text{fill:none;stroke-width:0;}#mermaid-svg-0ObDshNRWN34QlVc .icon-shape,#mermaid-svg-0ObDshNRWN34QlVc .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-0ObDshNRWN34QlVc .icon-shape p,#mermaid-svg-0ObDshNRWN34QlVc .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-0ObDshNRWN34QlVc .icon-shape rect,#mermaid-svg-0ObDshNRWN34QlVc .image-shape rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-0ObDshNRWN34QlVc .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-0ObDshNRWN34QlVc .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-0ObDshNRWN34QlVc :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}No (短文本)Yes (长文本)注入read_file(ID)反馈回工具产生输出Token > 阈值?LLM Context写入虚拟文件系统VFS / Disk生成引用指针File Ref: ID_001Agent 需要详情?检索/读取按需加载具体片段
核心工具集:
ls(path): 查看当前知识库。read_file(path, offset, limit): 最关键的工具。支持分页读取(offset/limit),允许 Agent 只“加载”它当前需要关注的那一部分数据到内存中。write_file(path, content): 创建笔记或报告。edit_file(path, old, new): 精确修改文件。
3.3 分治系统:SubAgent Spawning
当任务复杂度呈指数级上升时,单体 Agent 必然崩溃。SubAgentMiddleware 引入了组织架构。
工作原理:
主 Agent 调用 task(name="researcher", goal="Find info about X")。系统暂停主 Agent,Fork 一个新的子 Agent 实例。Context 隔离:子 Agent 拥有全新的、空白的 Context。主 Agent 累积的历史记录不会污染子 Agent。子 Agent 独立运行,直到完成目标。子 Agent 销毁,只返回一个简短的 String 结果给主 Agent。
3.4 自我修正循环 (Self-Correction Loop)
Deep Agents 不只是执行者,更是反思者。当任务失败时,TodoMiddleware 支持动态重规划(Replanning)。
# 当 Agent 发现原定计划行不通时
# 调用 write_todos 修改计划
{
"todos": [
{"task": "Run Unit Test", "status": "completed"}, # 原计划
{"task": "Debug the AttributeError", "status": "pending"}, # 新增的自我修正任务
{"task": "Run Unit Test Again", "status": "pending"}
]
}
这种动态调整能力使 Agent 能够应对不确定性和错误,是实现 AGI 自主性的关键一步。
第4章:存储后端工程化
Deep Agents 的存储后端(Backends)极其灵活,决定了 Agent 的“文件”和“记忆”到底存在哪里。
4.1 后端架构全景
#mermaid-svg-1IHCJQJXSoR4qtlM{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-1IHCJQJXSoR4qtlM .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-1IHCJQJXSoR4qtlM .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-1IHCJQJXSoR4qtlM .error-icon{fill:#552222;}#mermaid-svg-1IHCJQJXSoR4qtlM .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-1IHCJQJXSoR4qtlM .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-1IHCJQJXSoR4qtlM .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-1IHCJQJXSoR4qtlM .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-1IHCJQJXSoR4qtlM .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-1IHCJQJXSoR4qtlM .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-1IHCJQJXSoR4qtlM .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-1IHCJQJXSoR4qtlM .marker{fill:#333333;stroke:#333333;}#mermaid-svg-1IHCJQJXSoR4qtlM .marker.cross{stroke:#333333;}#mermaid-svg-1IHCJQJXSoR4qtlM svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-1IHCJQJXSoR4qtlM p{margin:0;}#mermaid-svg-1IHCJQJXSoR4qtlM g.classGroup text{fill:#9370DB;stroke:none;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:10px;}#mermaid-svg-1IHCJQJXSoR4qtlM g.classGroup text .title{font-weight:bolder;}#mermaid-svg-1IHCJQJXSoR4qtlM .nodeLabel,#mermaid-svg-1IHCJQJXSoR4qtlM .edgeLabel{color:#131300;}#mermaid-svg-1IHCJQJXSoR4qtlM .edgeLabel .label rect{fill:#ECECFF;}#mermaid-svg-1IHCJQJXSoR4qtlM .label text{fill:#131300;}#mermaid-svg-1IHCJQJXSoR4qtlM .labelBkg{background:#ECECFF;}#mermaid-svg-1IHCJQJXSoR4qtlM .edgeLabel .label span{background:#ECECFF;}#mermaid-svg-1IHCJQJXSoR4qtlM .classTitle{font-weight:bolder;}#mermaid-svg-1IHCJQJXSoR4qtlM .node rect,#mermaid-svg-1IHCJQJXSoR4qtlM .node circle,#mermaid-svg-1IHCJQJXSoR4qtlM .node ellipse,#mermaid-svg-1IHCJQJXSoR4qtlM .node polygon,#mermaid-svg-1IHCJQJXSoR4qtlM .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-1IHCJQJXSoR4qtlM .divider{stroke:#9370DB;stroke-width:1;}#mermaid-svg-1IHCJQJXSoR4qtlM g.clickable{cursor:pointer;}#mermaid-svg-1IHCJQJXSoR4qtlM g.classGroup rect{fill:#ECECFF;stroke:#9370DB;}#mermaid-svg-1IHCJQJXSoR4qtlM g.classGroup line{stroke:#9370DB;stroke-width:1;}#mermaid-svg-1IHCJQJXSoR4qtlM .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5;}#mermaid-svg-1IHCJQJXSoR4qtlM .classLabel .label{fill:#9370DB;font-size:10px;}#mermaid-svg-1IHCJQJXSoR4qtlM .relation{stroke:#333333;stroke-width:1;fill:none;}#mermaid-svg-1IHCJQJXSoR4qtlM .dashed-line{stroke-dasharray:3;}#mermaid-svg-1IHCJQJXSoR4qtlM .dotted-line{stroke-dasharray:1 2;}#mermaid-svg-1IHCJQJXSoR4qtlM #compositionStart,#mermaid-svg-1IHCJQJXSoR4qtlM .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-1IHCJQJXSoR4qtlM #compositionEnd,#mermaid-svg-1IHCJQJXSoR4qtlM .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-1IHCJQJXSoR4qtlM #dependencyStart,#mermaid-svg-1IHCJQJXSoR4qtlM .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-1IHCJQJXSoR4qtlM #dependencyStart,#mermaid-svg-1IHCJQJXSoR4qtlM .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-1IHCJQJXSoR4qtlM #extensionStart,#mermaid-svg-1IHCJQJXSoR4qtlM .extension{fill:transparent!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-1IHCJQJXSoR4qtlM #extensionEnd,#mermaid-svg-1IHCJQJXSoR4qtlM .extension{fill:transparent!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-1IHCJQJXSoR4qtlM #aggregationStart,#mermaid-svg-1IHCJQJXSoR4qtlM .aggregation{fill:transparent!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-1IHCJQJXSoR4qtlM #aggregationEnd,#mermaid-svg-1IHCJQJXSoR4qtlM .aggregation{fill:transparent!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-1IHCJQJXSoR4qtlM #lollipopStart,#mermaid-svg-1IHCJQJXSoR4qtlM .lollipop{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-1IHCJQJXSoR4qtlM #lollipopEnd,#mermaid-svg-1IHCJQJXSoR4qtlM .lollipop{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-1IHCJQJXSoR4qtlM .edgeTerminals{font-size:11px;line-height:initial;}#mermaid-svg-1IHCJQJXSoR4qtlM .classTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-1IHCJQJXSoR4qtlM .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-1IHCJQJXSoR4qtlM .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-1IHCJQJXSoR4qtlM :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}«interface»BaseBackend+read()+write()StateBackend+存入 LangGraph State+生命周期:随会话结束StoreBackend+生命周期:永久持久化+存入 Databases(Redis/Postgres)CompositeBackend+default: Backend+routes: Dict[str, Backend]+路由分发()
4.2 StateBackend vs StoreBackend
特性StateBackend (默认)StoreBackend存储位置内存 (LangGraph State)外部数据库 (BaseStore)生命周期短 (Thread 结束即消失)长 (永久保存)适用场景临时文件、中间草稿用户配置、长期记忆、成品文档速度极快取决于数据库 I/O4.3 生产级最佳实践:CompositeBackend
在生产环境中,我们需要混合使用:临时文件存内存,重要数据存数据库。Deep Agents 提供了基于路径的路由功能。
from deepagents.backends import CompositeBackend, StateBackend, StoreBackend
from langgraph.store.memory import InMemoryStore
# 初始化持久化存储
store = InMemoryStore()
def make_backend(runtime):
return CompositeBackend(
# 默认路由:所有普通文件存内存(State),虽生犹死
default=StateBackend(runtime),
# 特殊路由:/memories/ 开头的文件,存入 Store(持久化)
routes={
"/memories/": StoreBackend(runtime)
}
)
agent = create_deep_agent(
store=store,
backend=make_backend,
system_prompt="Save user preferences to /memories/profile.md"
)
设计哲学:
通过文件路径(Path)来区分数据的生命周期。Agent 不需要学习复杂的数据库指令,它只需要知道:“如果这类信息很重要,我就把它写进 /memories/ 文件夹”。
第5章:CLI 工具与交互实战
DeepAgents 提供了一个开箱即用的命令行工具(CLI),其体验无论是作为 Coding Agent 还是 Research Agent 都非常出色。
5.1 安装与启动
# 推荐使用 uv 工具链安装(隔离环境)
uv tool install deepagents-cli
# 启动 CLI (交互模式)
deepagents
5.2 核心命令参数详解
参数说明示例--agent最重要。指定 Agent 名称。不同名称拥有独立的记忆空间。deepagents --agent my-coder--sandbox启用远程沙箱运行代码(安全模式)。支持 daytona, modal。deepagents --sandbox daytona--resume恢复上一次的会话上下文。deepagents --resume--model指定模型 ID。deepagents --model claude-3-5-sonnet5.3 交互式指令 (Slash Commands)
交互模式下支持以下指令:
/clear: 清空当前对话历史(Token 减负)。/tokens: 查看当前 Token 使用量统计。/threads: 列出历史会话列表,方便切换。/exit: 保存状态并退出。
5.4 手动干预记忆 (Manual Memory Intervention)
由于 Deep Agents 使用文件系统管理记忆,你可以直接在本地文件系统中干预 Agent 的长期记忆。 拼多多帮助力入口
记忆位置:~/.deepagents/{AGENT_NAME}/memories/操作方式:你可以直接用 VS Code 或 Vim 编辑目录下的 .md 文件。下次 Agent 启动时,会自动读取你修改后的内容。这是一条非常高效的“上帝通道”。
第6章:安全机制:Human-in-the-Loop
在生产环境中,让 AI 自动执行 write_file 或 internet_search 可能带来风险。Deep Agents 集成了 LangGraph 的 Checkpointer 机制,实现了精细的人工介入。
6.1 拦截危险操作
我们可以配置 interrupt_on 参数,指定哪些工具在执行前必须暂停。
from langgraph.checkpoint.memory import MemorySaver
# 1. 必须启用 Checkpointer 才能保存暂停时的状态
checkpointer = MemorySaver()
# 2. 创建 Agent,拦截写入操作
agent = create_deep_agent(
tools=[internet_search],
interrupt_on=["write_file", "edit_file"], # <--- 拦截配置
checkpointer=checkpointer
)
6.2 完整的拦截-恢复流程
from langchain_core.messages import HumanMessage
config = {"configurable": {"thread_id": "safe-thread-1"}}
# --- 步骤 1: 触发执行 ---
# 假设 Agent 决定调用 write_file("virus.py", ...)
print("--- Round 1: Agent Running ---")
# 这里的 stream_mode="values" 会在暂停前停止
for event in agent.stream(
{"messages": [HumanMessage(content="写一个 Python 脚本")]},
config
):
pass
# --- 步骤 2: 检查状态 ---
state = agent.get_state(config)
if state.next:
print(f"⚠️ PAUSED. Next action: {state.next}")
# 这里可以加入人工审核逻辑:
# user_input = input("Allow this action? (y/n)")
# --- 步骤 3: 恢复执行 ---
# 传入 None 表示"放行",继续执行被暂停的操作
print("\n✅ Resuming execution...")
for event in agent.stream(None, config):
print(event)
这一机制确保了 Agent 永远不会在人类不知情的情况下修改关键代码或数据。
第7章:进阶定制:子智能体工厂
如何构建更强大的 Agent?答案是定制化子智能体。我们推荐使用字典配置模式,它最简洁且易于维护。
7.1 字典配置模式(推荐)
这是定义 Subagent 最简单的方式。只需提供一个配置字典,Deep Agents 会自动处理上下文隔离和路由。
# 定义子 Agent 配置
coder_agent_config = {
"name": "python_coder",
"description": "A specialist in Python coding tasks.",
"system_prompt": "You are a senior Python engineer. Write type-safe code...",
"tools": [read_file, write_file], # 子 Agent 可以有专属工具集
"model": "claude-3-5-sonnet-20241022", # 子 Agent 可以用更强的模型
}
# 挂载到主 Agent
main_agent = create_deep_agent(
model="gpt-4o", # 主 Agent 用便宜模型做调度
subagents=[coder_agent_config]
)
7.2 编译图模式(高级)
如果你已经有一个非常复杂的 LangGraph 图(比如一个包含 RAG、向量库检索的图),你可以把它直接包装成一个 Subagent。
from deepagents import CompiledSubAgent
# 假设 custom_graph 是一个编译好的 LangGraph CompiledGraph
subagent = CompiledSubAgent(
name="legal_advisor",
description="Consults the legal vector database.",
runnable=custom_graph
)
main_agent = create_deep_agent(subagents=[subagent])
7.3 子代理的高级通信
主 Agent 不仅能给子 Agent 分配任务,还能通过 Prompt 隐式传递上下文。
# 主 Agent 思考
"我需要分析 user_data.csv。我会把 schema 传给子代理。"
# 主 Agent 调用工具
task(
name="data_analyst",
goal="Analyze user_data.csv. Note: The schema includes 'id', 'login_time'."
)
子 Agent 启动时,goal 字段的内容会被注入到它的 System Prompt 中,从而实现“上下文传递”。
第8章:最佳实践与总结
8.1 架构设计原则
Memory-First Protocol(内存优先):
在 Prompt 中明确指示 Agent:“在回答问题前,先检查 /memories/ 目录下的相关文件”。这能大幅提升个性化体验。
Context Quarantine(上下文隔离):
只要任务涉及“读取大量无关数据”或“执行超过 5 步的尝试”,就坚决使用 Subagent。保持主 Agent 的 Context 干净是长期运行并保持高智商的关键。
General Purpose Agent:
如果你懒得配置专门的子 Agent,可以直接在 Prompt 告诉主 Agent:“遇到复杂任务,请调用 task(name='general-purpose', ...)”。系统内置了这个通用分身,可以帮你分担脏活。
8.2 本章小结
Deep Agents 不仅仅是一个库,它是构建 AGI 应用的标准蓝图。
Layer 1: Planning ->解决“怎么做”的问题。Layer 2: FileSystem ->解决“记不住”的问题。Layer 3: Subagents ->解决“干不动”的问题。
8.3 思考与练习
练习 1:尝试修改第 2 章的代码,增加一个 files_list 工具,让 Agent 可以列出当前目录下的文件。练习 2:配置 CompositeBackend,让 Agent 将你的名字写入 /memories/name.txt,重启程序后,验证它是否还记得你的名字。思考题:为什么 Deep Agents 选择用“文件系统”而不是“向量数据库”来作为主要的 Context 卸载机制?它们各自的优缺点是什么?