DeepAgents框架介绍与应用实践

1、DeepAgents框架定位与核心价值

1.1 什么是DeepAgents?

DeepAgents(代码库名为 deepagents)是一个基于 LangChainLangGraph 构建的企业级高级智能体框架。它建立在 LangGraph(底层运行时)和 LangChain(工具/模型层)之上,是一个高阶的Agent Harness(智能体装备/套件)

  • 定位:它旨在简化长运行自主智能体 (Long-running Autonomous Agents) 的开发过程,通过内置的最佳实践和中间件,解决复杂任务中的规划、记忆、工具使用和环境交互问题。

  • 核心理念:如果说 LangChain 提供了积木,LangGraph 提供了地基,那么 DeepAgents 就是一套成品级的框架。它预设了最佳实践(规划、文件系统、子智能体),让你能快速构建类似 “OpenAI Deep Research” 或 “Claude Code” 的应用。

  • 论文地址:https://arxiv.org/pdf/2510.21618

  DeepAgents 开源地址:https://github.com/langchain-ai/deepagents

1.2 解决的核心问题

  传统的 Agent 开发通常运行一个简单的循环:思考→调用工具→观察→重复。这种模式在处理多小时或多天的任务时,容易遇到以下”浅层陷阱”(Shallow Agent Problem)

  1. 规划能力缺失:原生 Agent 倾向于“走一步看一步”,缺乏全局视角的任务拆解,容易在多步任务中迷失方向。

  2. 遗忘与混乱:在执行超过 10-20 步的长任务时,由于 Context Window(上下文窗口)限制,传统 Agent 容易忘记初始目标或陷入死循环。

  3. 环境交互困难:文件系统操作、代码执行环境(沙箱)的配置和安全管理复杂。

  4. 上下文污染:所有工具返回结果都堆积在一个对话历史中,导致噪声过大。

  5. 协作编排复杂:多智能体(Multi-Agent)之间的任务分发和上下文隔离难以实现。

  DeepAgents 通过引入”类人”的工作流解决了这些问题:先做计划(Plan),再执行,利用文件系统管理记忆,遇到复杂子任务时”外包”给子 Agent。将规划工具、文件系统访问、子代理和详细提示词等关键机制整合在一起,以支持复杂的深度任务 。

1
2
3
import deepagents

print(dir(deepagents))
/opt/anaconda3/envs/langchain/lib/python3.11/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html
  from .autonotebook import tqdm as notebook_tqdm


['CompiledSubAgent', 'FilesystemMiddleware', 'SubAgent', 'SubAgentMiddleware', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', 'backends', 'create_deep_agent', 'graph', 'middleware']

1.3 应用场景

DeepAgents 不适合用来做简单的聊天机器人(Chatbot),它是为重任务设计的,它适用于任务需规划、上下文海量、需多专家协作、要求持久记忆的场景,将LangChain生态从单步响应提升至自主完成复杂项目的高度:

  • **深度调研 (Deep Research)**:自动进行多轮网页搜索、阅读文档、整理笔记并生成长篇报告(如分析某个行业的市场格局)。

  • **全栈代码生成 (Coding Assistant)**:类似 Claude Code,在沙箱环境中编写、运行、测试和修复代码,甚至重构整个代码库。

  • 复杂数据分析:自动连接数据库,生成 SQL,执行查询,将中间数据存为 CSV 文件(在虚拟文件系统中),最后生成图表。

  • **自动化运维 (DevOps Automation)**:操作文件系统、执行 Shell 命令、管理服务器状态。

  • 复杂工作流编排:需要多角色协作(如产品经理-程序员-测试员)的复杂业务流程。

  当然这个描述相对来说比较抽象,因此我们这里对适用于DeepAgents的场景进行一个总结:

DeepAgents 适用场景

场景类型 能力说明 工作逻辑 / 技术特点 代表性案例
深度调研与报告生成 支持长周期、多步骤、多来源信息整合的研究任务 • 自动生成研究计划(Todo)
• 调用搜索工具获取资料
• 将关键信息写入文件系统(长期记忆)
• 使用子代理(Sub-Agents)深入研究子课题
• 主代理统一规划、整合结果
• LangChain Deep Research 示例(Tavily 搜索 + 多子代理拆分研究)
• OpenAI Deep Research(官方生产级深度调研助理)
自动编程与代码助理 理解代码、修改代码、生成新文件、执行工具链 • 代理可读写虚拟文件系统
• 自动分析源码并输出 diff
• 人工审批(Human-in-the-Loop)保证安全写入
• 调用 Shell / 测试工具执行流程
• 可将项目规范写入 /memories 用作长期记忆
• LangChain DeepAgents CLI(终端自动编码)
• Anthropic Claude Code(深度自动重构与编程)
• Manus(多步骤代码智能体)
复杂流程自动化(业务流程 Orchestration) 将多个步骤串联为可控流程,适合企业级自动化任务 • 任务分解 → 多步骤规划 → 调用不同工具
• 搜索、筛选、处理、生成等多环节协作
• 使用文件系统存储中间数据(如列表、分析结果等)
• 支持多工具、多子任务并行处理
• DeepAgents 求职助手(职位搜索 → 筛选排序 → 求职信生成 → 打包结果)
• 企业场景如:自动生成分析报告 / 客服知识库构建 / 数据采集+处理流

2、与LangChain及LangGraph的区别

  从技术定位看,LangChain 适用于需要自定义提示与工具的基础智能体搭建;LangGraph 更适合构建复杂的多智能体系统与工作流;而 DeepAgents 面向希望省去底层开发、直接采用深度自主机制的用户,可快速实现 AutoGPT 类应用。因此,DeepAgents 本质上是基于 LangChain 的深度模式封装——它并非替代 LangChain 或 LangGraph,而是将其常用抽象与运行时封装为开箱即用的组件,可视为一层“开发加速器”。

特性 LangChain LangGraph DeepAgents
层级 基础组件库 (Foundation) 编排引擎 (Orchestration) 应用框架 (Application Framework)
核心抽象 Chain, Runnable, Tool StateGraph, Node, Edge DeepAgent, Middleware, Backend
灵活性 极高 (积木块) 高 (自定义图结构) 中 (Opinionated / 约定优于配置)
开箱即用 低 (需自行组装) 中 (需定义图逻辑) 高 (内置规划、文件系统、子代理)
适用对象 库开发者/底层构建 复杂流程序列化开发者 应用开发者/企业级解决方案
  • LangChain:提供 Prompt, Models, Tools 等积木。

  • LangGraph:提供 State, Nodes, Edges 等地基和连接逻辑。

  • DeepAgentsLangGraph 的一种“最佳实践实现”。它底层使用 LangGraph 来管理状态和循环,但向上提供了更高级的 API (create_deep_agent),隐藏了底层的图构建细节。

3、DeepAgents核心功能介绍

3.1 安装环境与依赖

1
!python --version
Python 3.11.14
1
2
# 安装deepagents依赖
!pip install deepagents
Requirement already satisfied: deepagents in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (0.3.0)
Requirement already satisfied: langchain-anthropic<2.0.0,>=1.2.0 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from deepagents) (1.2.0)
Requirement already satisfied: langchain<2.0.0,>=1.1.0 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from deepagents) (1.2.0)
Requirement already satisfied: langchain-core<2.0.0,>=1.1.0 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from deepagents) (1.2.1)
Requirement already satisfied: wcmatch in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from deepagents) (10.1)
Requirement already satisfied: langgraph<1.1.0,>=1.0.2 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from langchain<2.0.0,>=1.1.0->deepagents) (1.0.5)
Requirement already satisfied: pydantic<3.0.0,>=2.7.4 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from langchain<2.0.0,>=1.1.0->deepagents) (2.12.5)
Requirement already satisfied: anthropic<1.0.0,>=0.73.0 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from langchain-anthropic<2.0.0,>=1.2.0->deepagents) (0.75.0)
Requirement already satisfied: anyio<5,>=3.5.0 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from anthropic<1.0.0,>=0.73.0->langchain-anthropic<2.0.0,>=1.2.0->deepagents) (4.11.0)
Requirement already satisfied: distro<2,>=1.7.0 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from anthropic<1.0.0,>=0.73.0->langchain-anthropic<2.0.0,>=1.2.0->deepagents) (1.9.0)
Requirement already satisfied: docstring-parser<1,>=0.15 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from anthropic<1.0.0,>=0.73.0->langchain-anthropic<2.0.0,>=1.2.0->deepagents) (0.17.0)
Requirement already satisfied: httpx<1,>=0.25.0 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from anthropic<1.0.0,>=0.73.0->langchain-anthropic<2.0.0,>=1.2.0->deepagents) (0.28.1)
Requirement already satisfied: jiter<1,>=0.4.0 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from anthropic<1.0.0,>=0.73.0->langchain-anthropic<2.0.0,>=1.2.0->deepagents) (0.12.0)
Requirement already satisfied: sniffio in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from anthropic<1.0.0,>=0.73.0->langchain-anthropic<2.0.0,>=1.2.0->deepagents) (1.3.1)
Requirement already satisfied: typing-extensions<5,>=4.10 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from anthropic<1.0.0,>=0.73.0->langchain-anthropic<2.0.0,>=1.2.0->deepagents) (4.15.0)
Requirement already satisfied: idna>=2.8 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from anyio<5,>=3.5.0->anthropic<1.0.0,>=0.73.0->langchain-anthropic<2.0.0,>=1.2.0->deepagents) (3.11)
Requirement already satisfied: certifi in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from httpx<1,>=0.25.0->anthropic<1.0.0,>=0.73.0->langchain-anthropic<2.0.0,>=1.2.0->deepagents) (2025.10.5)
Requirement already satisfied: httpcore==1.* in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from httpx<1,>=0.25.0->anthropic<1.0.0,>=0.73.0->langchain-anthropic<2.0.0,>=1.2.0->deepagents) (1.0.9)
Requirement already satisfied: h11>=0.16 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from httpcore==1.*->httpx<1,>=0.25.0->anthropic<1.0.0,>=0.73.0->langchain-anthropic<2.0.0,>=1.2.0->deepagents) (0.16.0)
Requirement already satisfied: jsonpatch<2.0.0,>=1.33.0 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from langchain-core<2.0.0,>=1.1.0->deepagents) (1.33)
Requirement already satisfied: langsmith<1.0.0,>=0.3.45 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from langchain-core<2.0.0,>=1.1.0->deepagents) (0.4.42)
Requirement already satisfied: packaging<26.0.0,>=23.2.0 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from langchain-core<2.0.0,>=1.1.0->deepagents) (25.0)
Requirement already satisfied: pyyaml<7.0.0,>=5.3.0 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from langchain-core<2.0.0,>=1.1.0->deepagents) (6.0.3)
Requirement already satisfied: tenacity!=8.4.0,<10.0.0,>=8.1.0 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from langchain-core<2.0.0,>=1.1.0->deepagents) (9.1.2)
Requirement already satisfied: uuid-utils<1.0,>=0.12.0 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from langchain-core<2.0.0,>=1.1.0->deepagents) (0.12.0)
Requirement already satisfied: jsonpointer>=1.9 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from jsonpatch<2.0.0,>=1.33.0->langchain-core<2.0.0,>=1.1.0->deepagents) (3.0.0)
Requirement already satisfied: langgraph-checkpoint<4.0.0,>=2.1.0 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from langgraph<1.1.0,>=1.0.2->langchain<2.0.0,>=1.1.0->deepagents) (3.0.1)
Requirement already satisfied: langgraph-prebuilt<1.1.0,>=1.0.2 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from langgraph<1.1.0,>=1.0.2->langchain<2.0.0,>=1.1.0->deepagents) (1.0.2)
Requirement already satisfied: langgraph-sdk<0.4.0,>=0.3.0 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from langgraph<1.1.0,>=1.0.2->langchain<2.0.0,>=1.1.0->deepagents) (0.3.0)
Requirement already satisfied: xxhash>=3.5.0 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from langgraph<1.1.0,>=1.0.2->langchain<2.0.0,>=1.1.0->deepagents) (3.6.0)
Requirement already satisfied: ormsgpack>=1.12.0 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from langgraph-checkpoint<4.0.0,>=2.1.0->langgraph<1.1.0,>=1.0.2->langchain<2.0.0,>=1.1.0->deepagents) (1.12.0)
Requirement already satisfied: orjson>=3.10.1 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from langgraph-sdk<0.4.0,>=0.3.0->langgraph<1.1.0,>=1.0.2->langchain<2.0.0,>=1.1.0->deepagents) (3.11.4)
Requirement already satisfied: requests-toolbelt>=1.0.0 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from langsmith<1.0.0,>=0.3.45->langchain-core<2.0.0,>=1.1.0->deepagents) (1.0.0)
Requirement already satisfied: requests>=2.0.0 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from langsmith<1.0.0,>=0.3.45->langchain-core<2.0.0,>=1.1.0->deepagents) (2.32.5)
Requirement already satisfied: zstandard>=0.23.0 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from langsmith<1.0.0,>=0.3.45->langchain-core<2.0.0,>=1.1.0->deepagents) (0.25.0)
Requirement already satisfied: annotated-types>=0.6.0 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from pydantic<3.0.0,>=2.7.4->langchain<2.0.0,>=1.1.0->deepagents) (0.7.0)
Requirement already satisfied: pydantic-core==2.41.5 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from pydantic<3.0.0,>=2.7.4->langchain<2.0.0,>=1.1.0->deepagents) (2.41.5)
Requirement already satisfied: typing-inspection>=0.4.2 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from pydantic<3.0.0,>=2.7.4->langchain<2.0.0,>=1.1.0->deepagents) (0.4.2)
Requirement already satisfied: charset_normalizer<4,>=2 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from requests>=2.0.0->langsmith<1.0.0,>=0.3.45->langchain-core<2.0.0,>=1.1.0->deepagents) (3.4.4)
Requirement already satisfied: urllib3<3,>=1.21.1 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from requests>=2.0.0->langsmith<1.0.0,>=0.3.45->langchain-core<2.0.0,>=1.1.0->deepagents) (2.3.0)
Requirement already satisfied: bracex>=2.1.1 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from wcmatch->deepagents) (2.6)
1
2
3
# 查看版本
!pip list | grep -E 'langchain|deepagents'

deepagents                               0.3.0
langchain                                1.2.0
langchain-anthropic                      1.2.0
langchain-chroma                         1.0.0
langchain-classic                        1.0.0
langchain-community                      0.4.1
langchain-core                           1.2.1
langchain-deepseek                       1.0.0
langchain-experimental                   0.4.0
langchain-google-genai                   3.0.3
langchain-mcp-adapters                   0.1.13
langchain-ollama                         1.0.0
langchain-openai                         1.0.2
langchain-tavily                         0.2.13
langchain-text-splitters                 1.0.0
1
2
3
4
5
6
7
8
9
10
# from langchain_openai import ChatOpenAI
from langchain_deepseek import ChatDeepSeek
from dotenv import load_dotenv

# 1. 加载.env环境变量
load_dotenv(override=True)

# 2. 初始化模型
model = ChatDeepSeek(model="deepseek-chat", temperature=0)
model.invoke("你好")
AIMessage(content='你好!很高兴见到你!😊 我是DeepSeek,由深度求索公司创造的AI助手。无论你有什么问题、需要什么帮助,或者只是想聊聊天,我都很乐意为你提供支持!\n\n我可以帮你处理各种任务,比如:\n- 回答问题和解释概念\n- 协助写作和编辑\n- 编程和技术支持\n- 分析和总结文档\n- 创意 brainstorming\n- 学习辅导等等\n\n有什么我可以为你做的吗?请随时告诉我你的需求!✨', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 101, 'prompt_tokens': 5, 'total_tokens': 106, 'completion_tokens_details': None, 'prompt_tokens_details': {'audio_tokens': None, 'cached_tokens': 0}, 'prompt_cache_hit_tokens': 0, 'prompt_cache_miss_tokens': 5}, 'model_provider': 'deepseek', 'model_name': 'deepseek-chat', 'system_fingerprint': 'fp_eaab8d114b_prod0820_fp8_kvcache', 'id': 'c27cc27e-3a57-4f56-ad35-7ceacf78316d', 'finish_reason': 'stop', 'logprobs': None}, id='lc_run--019b27b9-8fe3-78b2-9b39-8923d5e97529-0', usage_metadata={'input_tokens': 5, 'output_tokens': 101, 'total_tokens': 106, 'input_token_details': {'cache_read': 0}, 'output_token_details': {}})

3.2 核心入口:create_deep_agent()

这是整个框架的核心函数,它创建了一个功能完整的深度智能体。

默认配置

  • 使用 Claude Sonnet 4 或 GPT-4o 作为默认模型(推荐)。

  • 集成 7 个核心文件操作工具。

  • 提供待办事项管理功能。

  • 支持子代理调用。

关键参数

  • model: 支持自定义语言模型。

  • tools: 自定义工具集。

  • system_prompt : 系统提示词

  • subagents: 子代理配置。

  • backend: 文件存储后端。

  • interrupt_on: **人机交互配置 (Human-in-the-Loop)**。允许在特定节点暂停 Agent 执行,等待人工干预。这对于安全审核(删除文件)、成本控制(调用昂贵 API)和质量保证至关重要。

1
2
3
from deepagents import create_deep_agent

create_deep_agent?
Signature:
create_deep_agent(
    model: str | langchain_core.language_models.chat_models.BaseChatModel | None = None,
    tools: collections.abc.Sequence[langchain_core.tools.base.BaseTool | collections.abc.Callable | dict[str, typing.Any]] | None = None,
    *,
    system_prompt: str | None = None,
    middleware: collections.abc.Sequence[langchain.agents.middleware.types.AgentMiddleware] = (),
    subagents: list[deepagents.middleware.subagents.SubAgent | deepagents.middleware.subagents.CompiledSubAgent] | None = None,
    response_format: Union[langchain.agents.structured_output.ToolStrategy[~SchemaT], langchain.agents.structured_output.ProviderStrategy[~SchemaT], langchain.agents.structured_output.AutoStrategy[~SchemaT], NoneType] = None,
    context_schema: type[typing.Any] | None = None,
    checkpointer: None | bool | langgraph.checkpoint.base.BaseCheckpointSaver = None,
    store: langgraph.store.base.BaseStore | None = None,
    backend: deepagents.backends.protocol.BackendProtocol | collections.abc.Callable[[langgraph.prebuilt.tool_node.ToolRuntime], deepagents.backends.protocol.BackendProtocol] | None = None,
    interrupt_on: dict[str, bool | langchain.agents.middleware.human_in_the_loop.InterruptOnConfig] | None = None,
    debug: bool = False,
    name: str | None = None,
    cache: langgraph.cache.base.BaseCache | None = None,
) -> langgraph.graph.state.CompiledStateGraph
Docstring:
Create a deep agent.

This agent will by default have access to a tool to write todos (write_todos),
seven file and execution tools: ls, read_file, write_file, edit_file, glob, grep, execute,
and a tool to call subagents.

The execute tool allows running shell commands if the backend implements SandboxBackendProtocol.
For non-sandbox backends, the execute tool will return an error message.

Args:
    model: The model to use. Defaults to Claude Sonnet 4.
    tools: The tools the agent should have access to.
    system_prompt: The additional instructions the agent should have. Will go in
        the system prompt.
    middleware: Additional middleware to apply after standard middleware.
    subagents: The subagents to use. Each subagent should be a dictionary with the
        following keys:
            - `name`
            - `description` (used by the main agent to decide whether to call the
              sub agent)
            - `prompt` (used as the system prompt in the subagent)
            - (optional) `tools`
            - (optional) `model` (either a LanguageModelLike instance or dict
              settings)
            - (optional) `middleware` (list of AgentMiddleware)
    response_format: A structured output response format to use for the agent.
    context_schema: The schema of the deep agent.
    checkpointer: Optional checkpointer for persisting agent state between runs.
    store: Optional store for persistent storage (required if backend uses StoreBackend).
    backend: Optional backend for file storage and execution. Pass either a Backend instance
        or a callable factory like `lambda rt: StateBackend(rt)`. For execution support,
        use a backend that implements SandboxBackendProtocol.
    interrupt_on: Optional Dict[str, bool | InterruptOnConfig] mapping tool names to
        interrupt configs.
    debug: Whether to enable debug mode. Passed through to create_agent.
    name: The name of the agent. Passed through to create_agent.
    cache: The cache to use for the agent. Passed through to create_agent.

Returns:
    A configured deep agent.
File:      /opt/anaconda3/envs/langchain/lib/python3.11/site-packages/deepagents/graph.py
Type:      function
1
2
# 安装网络搜索工具
!pip install langchain-tavily
Requirement already satisfied: langchain-tavily in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (0.2.13)
Requirement already satisfied: aiohttp<4.0.0,>=3.11.14 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from langchain-tavily) (3.13.2)
Requirement already satisfied: langchain<2.0.0,>=0.3.20 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from langchain-tavily) (1.2.0)
Requirement already satisfied: langchain-core<2.0.0,>=0.3.15 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from langchain-tavily) (1.2.1)
Requirement already satisfied: requests<3.0.0,>=2.32.3 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from langchain-tavily) (2.32.5)
Requirement already satisfied: aiohappyeyeballs>=2.5.0 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from aiohttp<4.0.0,>=3.11.14->langchain-tavily) (2.6.1)
Requirement already satisfied: aiosignal>=1.4.0 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from aiohttp<4.0.0,>=3.11.14->langchain-tavily) (1.4.0)
Requirement already satisfied: attrs>=17.3.0 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from aiohttp<4.0.0,>=3.11.14->langchain-tavily) (25.4.0)
Requirement already satisfied: frozenlist>=1.1.1 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from aiohttp<4.0.0,>=3.11.14->langchain-tavily) (1.8.0)
Requirement already satisfied: multidict<7.0,>=4.5 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from aiohttp<4.0.0,>=3.11.14->langchain-tavily) (6.7.0)
Requirement already satisfied: propcache>=0.2.0 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from aiohttp<4.0.0,>=3.11.14->langchain-tavily) (0.4.1)
Requirement already satisfied: yarl<2.0,>=1.17.0 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from aiohttp<4.0.0,>=3.11.14->langchain-tavily) (1.22.0)
Requirement already satisfied: langgraph<1.1.0,>=1.0.2 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from langchain<2.0.0,>=0.3.20->langchain-tavily) (1.0.5)
Requirement already satisfied: pydantic<3.0.0,>=2.7.4 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from langchain<2.0.0,>=0.3.20->langchain-tavily) (2.12.5)
Requirement already satisfied: jsonpatch<2.0.0,>=1.33.0 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from langchain-core<2.0.0,>=0.3.15->langchain-tavily) (1.33)
Requirement already satisfied: langsmith<1.0.0,>=0.3.45 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from langchain-core<2.0.0,>=0.3.15->langchain-tavily) (0.4.42)
Requirement already satisfied: packaging<26.0.0,>=23.2.0 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from langchain-core<2.0.0,>=0.3.15->langchain-tavily) (25.0)
Requirement already satisfied: pyyaml<7.0.0,>=5.3.0 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from langchain-core<2.0.0,>=0.3.15->langchain-tavily) (6.0.3)
Requirement already satisfied: tenacity!=8.4.0,<10.0.0,>=8.1.0 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from langchain-core<2.0.0,>=0.3.15->langchain-tavily) (9.1.2)
Requirement already satisfied: typing-extensions<5.0.0,>=4.7.0 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from langchain-core<2.0.0,>=0.3.15->langchain-tavily) (4.15.0)
Requirement already satisfied: uuid-utils<1.0,>=0.12.0 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from langchain-core<2.0.0,>=0.3.15->langchain-tavily) (0.12.0)
Requirement already satisfied: jsonpointer>=1.9 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from jsonpatch<2.0.0,>=1.33.0->langchain-core<2.0.0,>=0.3.15->langchain-tavily) (3.0.0)
Requirement already satisfied: langgraph-checkpoint<4.0.0,>=2.1.0 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from langgraph<1.1.0,>=1.0.2->langchain<2.0.0,>=0.3.20->langchain-tavily) (3.0.1)
Requirement already satisfied: langgraph-prebuilt<1.1.0,>=1.0.2 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from langgraph<1.1.0,>=1.0.2->langchain<2.0.0,>=0.3.20->langchain-tavily) (1.0.2)
Requirement already satisfied: langgraph-sdk<0.4.0,>=0.3.0 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from langgraph<1.1.0,>=1.0.2->langchain<2.0.0,>=0.3.20->langchain-tavily) (0.3.0)
Requirement already satisfied: xxhash>=3.5.0 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from langgraph<1.1.0,>=1.0.2->langchain<2.0.0,>=0.3.20->langchain-tavily) (3.6.0)
Requirement already satisfied: ormsgpack>=1.12.0 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from langgraph-checkpoint<4.0.0,>=2.1.0->langgraph<1.1.0,>=1.0.2->langchain<2.0.0,>=0.3.20->langchain-tavily) (1.12.0)
Requirement already satisfied: httpx>=0.25.2 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from langgraph-sdk<0.4.0,>=0.3.0->langgraph<1.1.0,>=1.0.2->langchain<2.0.0,>=0.3.20->langchain-tavily) (0.28.1)
Requirement already satisfied: orjson>=3.10.1 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from langgraph-sdk<0.4.0,>=0.3.0->langgraph<1.1.0,>=1.0.2->langchain<2.0.0,>=0.3.20->langchain-tavily) (3.11.4)
Requirement already satisfied: requests-toolbelt>=1.0.0 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from langsmith<1.0.0,>=0.3.45->langchain-core<2.0.0,>=0.3.15->langchain-tavily) (1.0.0)
Requirement already satisfied: zstandard>=0.23.0 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from langsmith<1.0.0,>=0.3.45->langchain-core<2.0.0,>=0.3.15->langchain-tavily) (0.25.0)
Requirement already satisfied: anyio in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from httpx>=0.25.2->langgraph-sdk<0.4.0,>=0.3.0->langgraph<1.1.0,>=1.0.2->langchain<2.0.0,>=0.3.20->langchain-tavily) (4.11.0)
Requirement already satisfied: certifi in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from httpx>=0.25.2->langgraph-sdk<0.4.0,>=0.3.0->langgraph<1.1.0,>=1.0.2->langchain<2.0.0,>=0.3.20->langchain-tavily) (2025.10.5)
Requirement already satisfied: httpcore==1.* in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from httpx>=0.25.2->langgraph-sdk<0.4.0,>=0.3.0->langgraph<1.1.0,>=1.0.2->langchain<2.0.0,>=0.3.20->langchain-tavily) (1.0.9)
Requirement already satisfied: idna in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from httpx>=0.25.2->langgraph-sdk<0.4.0,>=0.3.0->langgraph<1.1.0,>=1.0.2->langchain<2.0.0,>=0.3.20->langchain-tavily) (3.11)
Requirement already satisfied: h11>=0.16 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from httpcore==1.*->httpx>=0.25.2->langgraph-sdk<0.4.0,>=0.3.0->langgraph<1.1.0,>=1.0.2->langchain<2.0.0,>=0.3.20->langchain-tavily) (0.16.0)
Requirement already satisfied: annotated-types>=0.6.0 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from pydantic<3.0.0,>=2.7.4->langchain<2.0.0,>=0.3.20->langchain-tavily) (0.7.0)
Requirement already satisfied: pydantic-core==2.41.5 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from pydantic<3.0.0,>=2.7.4->langchain<2.0.0,>=0.3.20->langchain-tavily) (2.41.5)
Requirement already satisfied: typing-inspection>=0.4.2 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from pydantic<3.0.0,>=2.7.4->langchain<2.0.0,>=0.3.20->langchain-tavily) (0.4.2)
Requirement already satisfied: charset_normalizer<4,>=2 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from requests<3.0.0,>=2.32.3->langchain-tavily) (3.4.4)
Requirement already satisfied: urllib3<3,>=1.21.1 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from requests<3.0.0,>=2.32.3->langchain-tavily) (2.3.0)
Requirement already satisfied: sniffio>=1.1 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from anyio->httpx>=0.25.2->langgraph-sdk<0.4.0,>=0.3.0->langgraph<1.1.0,>=1.0.2->langchain<2.0.0,>=0.3.20->langchain-tavily) (1.3.1)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from deepagents import create_deep_agent
from langchain_tavily import TavilySearch
from langgraph.checkpoint.memory import InMemorySaver

# 1. 初始化 Tavily 搜索工具
tavily = TavilySearch(max_results=3)

# 2. 编写系统提示词
research_instructions = """
您是一位资深的研究人员。您的工作是进行深入的研究,然后撰写一份精美的报告。
您可以通过互联网搜索引擎作为主要的信息收集工具。
## 可用工具
### `互联网搜索`
使用此功能针对给定的查询进行互联网搜索。您可以指定要返回的最大结果数量、主题以及是否包含原始内容。
### `写入本地文件`
使用此功能将研究报告保存到本地文件。当您完成研究并生成报告后,请使用此工具将完整的报告内容保存到文
件中。
- 文件路径建议使用 .md 格式(Markdown),例如 "research_report.md" 或 "./reports/报告名
称.md"
- 请确保报告内容完整、结构清晰,包含所有章节和引用来源
## 工作流程
在进行研究时:
1. 首先将研究任务分解为清晰的步骤
2. 使用互联网搜索来收集全面的信息
3. 将信息整合成一份结构清晰的报告
4. **重要**:完成报告后,务必使用 `写入本地文件` 工具将完整报告保存到本地文件
5. 务必引用你的资料来源
**注意**:请确保在完成研究后,将完整的报告内容保存到文件中,这样用户可以方便地查看和保存报告。
"""

# 2. 创建 DeepAgents 智能体
agent = create_deep_agent(
name="DeepAgents_Agent", # 智能体名称
tools=[tavily], # 可调用工具Tool
model=model, # 模型Model
system_prompt=research_instructions, # 系统提示词
checkpointer=InMemorySaver(), # 检查点Checkpointer,内存检查点
)

# 3. 配置线程 ID
config = {"configurable": {"thread_id": "1"}}

result = agent.invoke({"messages": [{"role": "user", "content": "帮我查询一下有关deepagents框架的最新动态"}]}, config=config)

1
2
print(result["messages"][-1].content)

## 研究完成总结

我已经完成了对DeepAgents框架最新动态的全面研究,并撰写了一份详细的报告。以下是主要发现:

### 核心发现:

1. **最新版本**:DeepAgents 0.2版本于2025年第四季度发布,引入了**可插拔后端机制**,支持本地文件系统、云存储和数据库等多种存储方案。

2. **技术突破**:
   - 从模型中心向系统中心的范式转变
   - 支持长达7个月的长周期任务处理
   - 响应延迟降低60%,支持毫秒级状态恢复

3. **架构创新**:采用"指挥官-子兵"模式和图式架构,相比传统链式架构在控制流、状态管理和调试体验方面有显著优势。

4. **应用场景**:特别适合复杂任务规划、长期运行任务和企业级自动化场景。

5. **行业定位**:作为LangChain生态系统中的"代理工具",与LangChain(框架)和LangGraph(运行时)形成完整的技术栈。

### 报告内容:
我已经将完整的研究报告保存为 `/deepagents_framework_latest_dynamics_report.md`,报告包含:
- 框架概述与定位
- 最新版本动态分析
- 技术特性深度解析
- 应用场景与案例研究
- 社区反馈与行业趋势
- 未来展望与挑战
- 完整的技术选型建议

DeepAgents框架代表了AI智能体技术从实验阶段向实际应用阶段的重要演进,特别是在处理复杂、长期运行任务方面展现出强大潜力。

3.3 create_deep_agent内部结构

  • 源码参数截图
1
2
3
# 安装美化代码库Rich
!pip install rich

Requirement already satisfied: rich in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (14.2.0)
Requirement already satisfied: markdown-it-py>=2.2.0 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from rich) (4.0.0)
Requirement already satisfied: pygments<3.0.0,>=2.13.0 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from rich) (2.19.2)
Requirement already satisfied: mdurl~=0.1 in /opt/anaconda3/envs/langchain/lib/python3.11/site-packages (from markdown-it-py>=2.2.0->rich) (0.1.2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# 导入Rich库,用于美化代码
from rich.console import Console
from rich.table import Table
from rich.panel import Panel
RICH_AVAILABLE = True
console = Console()

def print_agent_tools(agent):
"""
打印 Agent 中加载的所有工具
包括用户自定义工具、文件系统工具、系统工具等
"""
# 获取 agent 的 nodes (LangGraph 的节点)
if hasattr(agent, 'nodes') and 'tools' in agent.nodes:
tools_node = agent.nodes['tools']

# tools_node 是 PregelNode,真正的 ToolNode 在 bound 属性中
if hasattr(tools_node, 'bound'):
tool_node = tools_node.bound

# 从 ToolNode 获取工具
if hasattr(tool_node, 'tools_by_name'):
tools = tool_node.tools_by_name

# 分类工具
user_tools = []
filesystem_tools = []
system_tools = []

for tool_name, tool in tools.items():
tool_info = {
'name': tool_name,
'description': getattr(tool, 'description', '无描述')
}

# 分类
if tool_name in ['ls', 'read_file', 'write_file', 'edit_file', 'glob', 'grep', 'execute']:
filesystem_tools.append(tool_info)
elif tool_name in ['write_todos', 'task']:
system_tools.append(tool_info)
else:
user_tools.append(tool_info)

# 打印加载工具的输出
_print_tools_rich(user_tools, filesystem_tools, system_tools)

else:
print("无法获取工具列表 (tools_by_name 不存在)")
else:
print("无法获取工具列表 (bound 属性不存在)")
else:
print("无法获取工具列表 (nodes 结构不符合预期)")

def _print_tools_rich(user_tools, filesystem_tools, system_tools):
"""使用 Rich 库美化打印工具列表"""
console.print()

# 创建表格
table = Table(title="Agent 加载的工具列表", show_header=True, header_style="bold magenta")
table.add_column("类别", style="cyan", width=20)
table.add_column("工具名称", style="green", width=20)
table.add_column("描述", style="white", width=60)

# 添加用户工具
for i, tool in enumerate(user_tools):
category = "用户工具" if i == 0 else ""
desc = tool['description'][:80] + "..." if len(tool['description']) > 80 else tool['description']
table.add_row(category, tool['name'], desc)

# 添加文件系统工具
for i, tool in enumerate(filesystem_tools):
category = "文件系统工具" if i == 0 else ""
desc = tool['description'][:80] + "..." if len(tool['description']) > 80 else tool['description']
table.add_row(category, tool['name'], desc)

# 添加系统工具
for i, tool in enumerate(system_tools):
category = "系统工具" if i == 0 else ""
desc = tool['description'][:80] + "..." if len(tool['description']) > 80 else tool['description']
table.add_row(category, tool['name'], desc)

console.print(table)

# 打印统计
total = len(user_tools) + len(filesystem_tools) + len(system_tools)
console.print(Panel(
f"[bold green]共计 {total} 个工具[/bold green]\n\n"
f"• 用户工具: {len(user_tools)} 个\n"
f"• 文件系统工具: {len(filesystem_tools)} 个\n"
f"• 系统工具: {len(system_tools)} 个",
title="统计信息",
border_style="green"
))
console.print()
1
print_agent_tools(agent)





                                             Agent 加载的工具列表                                             
┏━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ 类别                  工具名称              描述                                                         ┃
┡━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│ 用户工具              tavily_search         A search engine optimized for comprehensive, accurate, and   │
│                                             trusted results. Usef...                                     │
│ 文件系统工具          ls                    Lists all files in the filesystem, filtering by directory.   │
│                                                                                                          │
│                                             Usage:                                                       │
│                                             - The path pa...                                             │
│                       read_file             Reads a file from the filesystem. You can access any file    │
│                                             directly by using this...                                    │
│                       write_file            Writes to a new file in the filesystem.                      │
│                                                                                                          │
│                                             Usage:                                                       │
│                                             - The file_path parameter must b...                          │
│                       edit_file             Performs exact string replacements in files.                 │
│                                                                                                          │
│                                             Usage:                                                       │
│                                             - You must use your `Read` ...                               │
│                       glob                  Find files matching a glob pattern.                          │
│                                                                                                          │
│                                             Usage:                                                       │
│                                             - The glob tool finds files by match...                      │
│                       grep                  Search for a pattern in files.                               │
│                                                                                                          │
│                                             Usage:                                                       │
│                                             - The grep tool searches for text pattern...                 │
│                       execute               Executes a given command in the sandbox environment with     │
│                                             proper handling and sec...                                   │
│ 系统工具              write_todos           Use this tool to create and manage a structured task list    │
│                                             for your current work ...                                    │
│                       task                  Launch an ephemeral subagent to handle complex, multi-step   │
│                                             independent tasks wit...                                     │
└──────────────────────┴──────────────────────┴──────────────────────────────────────────────────────────────┘
╭─────────────────────────────────────────────────── 统计信息 ────────────────────────────────────────────────────╮
 共计 10 个工具                                                                                                  
                                                                                                                 
 • 用户工具: 1 个                                                                                                
 • 文件系统工具: 7 个                                                                                            
 • 系统工具: 2 个                                                                                                
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯




这里可以看到,除了自己定义的工具(如 Tavily 搜索),DeepAgents 还默认添加了一些其他工具:

  • 文件系统中间件(FileSystemMiddleware): 用于读写、查询、执行文件系统中的文件。

  • 待办事项中间件(TodoListMiddleware): write_todos 用于写入待办事项,task 用于创建子agent来执行待办事项。

这些都是 DeepAgents 特有的功能,用于支持智能体在实际应用中的各种场景。那么,接下来我们来看看这些功能的具体应用。

  • Langgraph Studio 中可视化结构图:能看到

    • PatchToolCallsMiddleware用于 自动检测并修复“悬空”的工具调用 的关键中间件,确保工具调用的完整性和正确性。

    • SummarizationMiddleware上下文压缩中间件,防止上下文过长

4、四大核心内置工具与组件详解

  DeepAgents 通过中间件 (Middleware) 的形式,为智能体注入了四项核心能力,构成了框架的四大支柱(Four Pillars)

  DeepAgents四大内置工具通过角色分离、状态贯通、成本优化的设计哲学,将长周期Agent的开发复杂度降低70%以上,同时通过LangGraph运行时保障生产级可靠性。其核心价值在于将原本需要手动编排的规划-存储-委托-执行流程,固化为中心化、可复用、可观测的中间件体系,标志着AI Agent从”脚本化”向”产品化”的关键演进。

  • 协同价值:系统提示词确保质量,规划工具保证进度,文件系统实现数据共享,子代理隔离分析风险,四者形成高可靠、可追溯、可恢复的完整闭环。
维度 系统提示词 (System Prompt) 规划工具 (Planning Tool) 文件系统 (File System) 子代理 (Sub Agents)
角色定位 行为总导演:定义Agent的”世界观”与工具使用范式,确保三大中间件协同不偏离目标 任务架构师:将模糊需求转化为可执行、可追踪、可动态调整的结构化任务蓝图 上下文仓库:虚拟化存储引擎,解决长任务中的信息溢出与状态持久化难题 执行特派员:实现上下文隔离与专业分工,防止主Agent因深层递归导致状态混乱
核心功能 内置Claude Code风格指令,涵盖规划逻辑、文件操作规范、子代理调用协议;支持场景化自定义覆盖 write_todos: 生成带优先级/依赖关系的JSON任务列表
read_todos: 实时查询任务执行进度与状态
ls/glob: 文件浏览与模式匹配
read/write/edit: CRUD操作
grep: 内容检索
execute: 沙箱命令执行
task: 动态生成同构或异构子Agent
支持独立上下文窗口与工具集配置
结果通过文件系统回传
技术实现 字符串模板,在create_deep_agent时注入;默认提示词约2000 tokens,包含ReAct循环与三大中间件调用示例 TodoListMiddleware:拦截LLM输出中的todo_list字段,解析为agent_state.todos字典,状态变更触发图节点重计算 FilesystemMiddleware:基于LangGraph State的files字段实现内存级虚拟文件系统,大工具结果(>2KB)自动触发write_file落盘 SubAgentMiddleware:将task调用编译为独立的StateGraph子图,通过命名空间隔离状态,父图通过files读取子图输出
状态管理 静态配置,单次会话内不可变;可通过configurable_agent实现热更新 动态状态机:每个todo含id/description/status/priority/dependencies字段,执行后状态从pendingcompleted,支持update_todos动态调整 持久化存储:默认存储在LangGraph State,支持切换StateBackend(内存/Redis/Postgres)实现跨会话文件共享 完全隔离:子Agent拥有独立的messagesfiles命名空间,异常不会污染父Agent状态;支持max_iterations限制防止无限递归
使用场景 垂直领域定制:金融研究/医疗诊断等需强化专业约束的场景
多Agent协作:统一多个子Agent的行为规范
安全合规:注入数据脱敏、权限检查等硬性规则
长周期研究:自动拆解为文献检索→数据收集→分析→撰写的阶段性任务
故障恢复:崩溃后通过read_todos快速定位断点续跑
动态重规划:执行中发现信息不足时新增补充任务
大结果处理:搜索返回100KB内容自动落盘,避免上下文溢出
知识沉淀:中间分析结果写入文件供后续步骤复用
多Agent数据共享:父Agent与子Agent通过文件交换数据,无需序列化传递
高风险操作隔离:网页抓取/代码执行等易失败任务委托给子Agent
专业化分工:主Agent负责任务编排,子Agent专注领域执行(如专门的数据分析Agent)
资源优化:子Agent可使用轻量化模型,降低整体token成本

4.1 系统提示词(System Prompts)

  • 功能:定义 Agent 的“人设”、行为准则和核心目标。

  • 机制:框架会自动将用户定义的 system_prompt 与内置的 BASE_AGENT_PROMPT 结合。

  • 作用:确保 Agent 始终遵循指令,理解其可用的工具集,并保持一致的输出风格。

  • 角色本质:系统提示词是DeepAgents三大中间件协同的”契约”,其默认版本包含:

    • 规划指令:要求LLM在任务开始前必须调用write_todos,输出格式为JSON Schema

    • 文件操作规范:明确write_file用于新内容,edit_file用于局部修改,避免覆盖冲突

    • 子代理调用协议:规定task工具的参数结构及结果通过/subagent_results/.md回传

    • 安全底线:禁止直接执行删除、格式化等危险命令,必须通过execute沙箱

4.1.1 FileSystem_System_prompt 文件系统提示词

4.1.2 Execute_Tool_Descriptition执行工具提示词

  • 文件系统中间件提示词(FileSystemMiddleWare)

  • 文件路径:deepagents/middleware/filesystem.py

4.1.3 Write_Todos_System_prompt 写执行任务提示词

  • TodoList中间件提示词(TodoListMiddleware)

  • 文件路径:deepagents/middleware/todo.py

4.1.4 Task_System_prompt Task执行器提示词

  • 子智能体中间件提示词(SubAgentMiddleware)

  • 文件路径:deepagents/middleware/subagent.py

4.2 规划工具 (Planning System / Todo List)

  • 组件TodoListMiddleware

  • 工具名write_todos

  • 功能:Agent 在行动前先生成 Markdown 格式的待办事项列表 (Todo List),并在执行过程中更新状态(完成/进行中)。

  • 工作流

  1. Agent 接收复杂任务(简单短期的任务不会触发todolist)。

  2. 调用 write_todos 将任务拆解为子步骤 (Pending)。

  3. 每完成一步,更新状态为 (Completed)。

  4. 自我反思:在每一步行动前,Agent 都会看到当前的 Todo List,从而避免迷失方向。这强制模型进行”思维链”的显性化管理。

  • 类似编程工具中的待执行项

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import json
from rich.json import JSON

def debug_agent(query: str, save_to_file: str = None):
"""
运行智能体并打印中间过程(使用 Rich 美化输出)
参数:
query: 用户查询
save_to_file: 保存最终输出到文件(可选)
返回:
str: 最终的研究报告
"""
console.print(Panel.fit(
f"[bold cyan]查询:[/bold cyan] {query}",
border_style="cyan"
))
step_num = 0
final_response = None

config = {"configurable": {"thread_id": "2"}}

# 实时流式输出
for event in agent.stream(
{"messages": [{"role": "user", "content": query}]},
stream_mode="values",
config=config
):
step_num += 1

console.print(f"\n[bold yellow]{'─' * 80}[/bold yellow]")
console.print(f"[bold yellow]步骤 {step_num}[/bold yellow]")
console.print(f"[bold yellow]{'─' * 80}[/bold yellow]")

if "messages" in event:
messages = event["messages"]
if messages:
msg = messages[-1]
# 保存最终响应
if hasattr(msg, 'content') and msg.content and not hasattr(msg,'tool_calls'):
final_response = msg.content
# AI 思考
if hasattr(msg, 'content') and msg.content:
# 如果内容太长,只显示前300字符作为预览
content = msg.content
if len(content) > 300 and not (hasattr(msg, 'tool_calls') and msg.tool_calls):
preview = content[:300] + "..."
console.print(Panel(
f"{preview}\n\n[dim](内容较长,完整内容将在最后显示)[/dim]",
title="[bold green]AI 思考[/bold green]",
border_style="green"
))
else:
console.print(Panel(
content,
title="[bold green]AI 思考[/bold green]",
border_style="green"
))
# 工具调用
if hasattr(msg, 'tool_calls') and msg.tool_calls:
for tool_call in msg.tool_calls:
tool_info = {
"工具名称": tool_call.get('name', 'unknown'),
"参数": tool_call.get('args', {})
}
console.print(Panel(
JSON(json.dumps(tool_info, ensure_ascii=False)),
title="[bold blue]工具调用[/bold blue]",
border_style="blue"
))
# 工具响应
if hasattr(msg, 'name') and msg.name:
response = str(msg.content)[:500]
if len(str(msg.content)) > 500:
response += f"\n... (共 {len(str(msg.content))} 字符)"

console.print(Panel(
response,
title=f"[bold magenta]工具响应: {msg.name}[/bold magenta]",
border_style="magenta"
))
console.print("\n[bold green]任务完成![/bold green]\n")
return final_response

print("调试函数已创建")
调试函数已创建
1
2
3
4
5
# 示例:使用调试函数运行研究任务
query = "详细调研 LangChain DeepAgents 框架的核心特性,并写一份结构化的总结报告。"

# 使用调试函数)
result = debug_agent(query)
╭────────────────────────────────────────────────────────────────────────────────╮
 查询: 详细调研 LangChain DeepAgents 框架的核心特性,并写一份结构化的总结报告。 
╰────────────────────────────────────────────────────────────────────────────────╯
────────────────────────────────────────────────────────────────────────────────
步骤 1
────────────────────────────────────────────────────────────────────────────────
╭──────────────────────────────────────────────────── AI 思考 ────────────────────────────────────────────────────╮
 详细调研 LangChain DeepAgents 框架的核心特性,并写一份结构化的总结报告。                                        
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
────────────────────────────────────────────────────────────────────────────────
步骤 2
────────────────────────────────────────────────────────────────────────────────
╭──────────────────────────────────────────────────── AI 思考 ────────────────────────────────────────────────────╮
 详细调研 LangChain DeepAgents 框架的核心特性,并写一份结构化的总结报告。                                        
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
────────────────────────────────────────────────────────────────────────────────
步骤 3
────────────────────────────────────────────────────────────────────────────────
╭──────────────────────────────────────────────────── AI 思考 ────────────────────────────────────────────────────╮
 我将为您详细调研 LangChain DeepAgents                                                                           
 框架的核心特性,并撰写一份结构化的总结报告。让我先创建一个任务列表来组织这个复杂的研究工作。                    
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─────────────────────────────────────────────────── 工具调用 ────────────────────────────────────────────────────╮
 {                                                                                                               
   "工具名称": "write_todos",                                                                                    
   "参数": {                                                                                                     
     "todos": [                                                                                                  
       {                                                                                                         
         "content": "搜索 LangChain DeepAgents 框架的基本信息和概述",                                            
         "status": "in_progress"                                                                                 
       },                                                                                                        
       {                                                                                                         
         "content": "研究 DeepAgents 的核心架构和设计理念",                                                      
         "status": "pending"                                                                                     
       },                                                                                                        
       {                                                                                                         
         "content": "分析 DeepAgents 的主要特性和功能",                                                          
         "status": "pending"                                                                                     
       },                                                                                                        
       {                                                                                                         
         "content": "调研 DeepAgents 的应用场景和案例",                                                          
         "status": "pending"                                                                                     
       },                                                                                                        
       {                                                                                                         
         "content": "收集 DeepAgents 的技术实现细节",                                                            
         "status": "pending"                                                                                     
       },                                                                                                        
       {                                                                                                         
         "content": "整理并撰写结构化总结报告",                                                                  
         "status": "pending"                                                                                     
       }                                                                                                         
     ]                                                                                                           
   }                                                                                                             
 }                                                                                                               
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭────────────────────────────────────────── 工具响应: DeepAgents_Agent ───────────────────────────────────────────╮
 我将为您详细调研 LangChain DeepAgents                                                                           
 框架的核心特性,并撰写一份结构化的总结报告。让我先创建一个任务列表来组织这个复杂的研究工作。                    
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
────────────────────────────────────────────────────────────────────────────────
步骤 4
────────────────────────────────────────────────────────────────────────────────
╭──────────────────────────────────────────────────── AI 思考 ────────────────────────────────────────────────────╮
 Updated todo list to [{'content': '搜索 LangChain DeepAgents 框架的基本信息和概述', 'status': 'in_progress'},   
 {'content': '研究 DeepAgents 的核心架构和设计理念', 'status': 'pending'}, {'content': '分析 DeepAgents          
 的主要特性和功能', 'status': 'pending'}, {'content': '调研 DeepAgents 的应用场景和案例', 'status': 'pending'},  
 {'content': '收集 Deep...                                                                                       
                                                                                                                 
 (内容较长,完整内容将在最后显示)                                                                                 
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭───────────────────────────────────────────── 工具响应: write_todos ─────────────────────────────────────────────╮
 Updated todo list to [{'content': '搜索 LangChain DeepAgents 框架的基本信息和概述', 'status': 'in_progress'},   
 {'content': '研究 DeepAgents 的核心架构和设计理念', 'status': 'pending'}, {'content': '分析 DeepAgents          
 的主要特性和功能', 'status': 'pending'}, {'content': '调研 DeepAgents 的应用场景和案例', 'status': 'pending'},  
 {'content': '收集 DeepAgents 的技术实现细节', 'status': 'pending'}, {'content': '整理并撰写结构化总结报告',     
 'status': 'pending'}]                                                                                           
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
────────────────────────────────────────────────────────────────────────────────
步骤 5
────────────────────────────────────────────────────────────────────────────────
╭──────────────────────────────────────────────────── AI 思考 ────────────────────────────────────────────────────╮
 现在让我开始搜索 LangChain DeepAgents 框架的基本信息:                                                          
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─────────────────────────────────────────────────── 工具调用 ────────────────────────────────────────────────────╮
 {                                                                                                               
   "工具名称": "tavily_search",                                                                                  
   "参数": {                                                                                                     
     "query": "LangChain DeepAgents 框架 概述 介绍",                                                             
     "search_depth": "advanced"                                                                                  
   }                                                                                                             
 }                                                                                                               
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭────────────────────────────────────────── 工具响应: DeepAgents_Agent ───────────────────────────────────────────╮
 现在让我开始搜索 LangChain DeepAgents 框架的基本信息:                                                          
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
────────────────────────────────────────────────────────────────────────────────
步骤 6
────────────────────────────────────────────────────────────────────────────────
╭──────────────────────────────────────────────────── AI 思考 ────────────────────────────────────────────────────╮
 {"query": "LangChain DeepAgents 框架 概述 介绍", "follow_up_questions": null, "answer": null, "images": [],     
 "results": [{"url": "https://m.aitntnews.com/newDetail.html?newId=20116", "title": "LangChain                   
 最新agent框架deepagents测评:长任务友好,高可控", "content": "deepagents 是 LangChain 团队开源的 agent          
 框架项目,专门用于解决长周期任务的执行问题,其...                                                               
                                                                                                                 
 (内容较长,完整内容将在最后显示)                                                                                 
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭──────────────────────────────────────────── 工具响应: tavily_search ────────────────────────────────────────────╮
 {"query": "LangChain DeepAgents 框架 概述 介绍", "follow_up_questions": null, "answer": null, "images": [],     
 "results": [{"url": "https://m.aitntnews.com/newDetail.html?newId=20116", "title": "LangChain                   
 最新agent框架deepagents测评:长任务友好,高可控", "content": "deepagents 是 LangChain 团队开源的 agent          
 框架项目,专门用于解决长周期任务的执行问题,其核心能力有三:\n\n任务规划(Planning):使用内置 `write_todos         
 write_todos` 和 `read_todos read_todos` 工具将复杂任务分解为结构化待办事项列表,agent                            
 按列表执行并标记完成状态。\n\n文件系统访问(Computer Access):提供 `ls ls`、`read_file read_file`、`write_file    
 write                                                                                                           
 ... (共 4761 字符)                                                                                              
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
────────────────────────────────────────────────────────────────────────────────
步骤 7
────────────────────────────────────────────────────────────────────────────────
╭─────────────────────────────────────────────────── 工具调用 ────────────────────────────────────────────────────╮
 {                                                                                                               
   "工具名称": "write_todos",                                                                                    
   "参数": {                                                                                                     
     "todos": [                                                                                                  
       {                                                                                                         
         "content": "搜索 LangChain DeepAgents 框架的基本信息和概述",                                            
         "status": "completed"                                                                                   
       },                                                                                                        
       {                                                                                                         
         "content": "研究 DeepAgents 的核心架构和设计理念",                                                      
         "status": "in_progress"                                                                                 
       },                                                                                                        
       {                                                                                                         
         "content": "分析 DeepAgents 的主要特性和功能",                                                          
         "status": "pending"                                                                                     
       },                                                                                                        
       {                                                                                                         
         "content": "调研 DeepAgents 的应用场景和案例",                                                          
         "status": "pending"                                                                                     
       },                                                                                                        
       {                                                                                                         
         "content": "收集 DeepAgents 的技术实现细节",                                                            
         "status": "pending"                                                                                     
       },                                                                                                        
       {                                                                                                         
         "content": "整理并撰写结构化总结报告",                                                                  
         "status": "pending"                                                                                     
       }                                                                                                         
     ]                                                                                                           
   }                                                                                                             
 }                                                                                                               
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭────────────────────────────────────────── 工具响应: DeepAgents_Agent ───────────────────────────────────────────╮
                                                                                                                 
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
────────────────────────────────────────────────────────────────────────────────
步骤 8
────────────────────────────────────────────────────────────────────────────────
╭──────────────────────────────────────────────────── AI 思考 ────────────────────────────────────────────────────╮
 Updated todo list to [{'content': '搜索 LangChain DeepAgents 框架的基本信息和概述', 'status': 'completed'},     
 {'content': '研究 DeepAgents 的核心架构和设计理念', 'status': 'in_progress'}, {'content': '分析 DeepAgents      
 的主要特性和功能', 'status': 'pending'}, {'content': '调研 DeepAgents 的应用场景和案例', 'status': 'pending'},  
 {'content': '收集 De...                                                                                         
                                                                                                                 
 (内容较长,完整内容将在最后显示)                                                                                 
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭───────────────────────────────────────────── 工具响应: write_todos ─────────────────────────────────────────────╮
 Updated todo list to [{'content': '搜索 LangChain DeepAgents 框架的基本信息和概述', 'status': 'completed'},     
 {'content': '研究 DeepAgents 的核心架构和设计理念', 'status': 'in_progress'}, {'content': '分析 DeepAgents      
 的主要特性和功能', 'status': 'pending'}, {'content': '调研 DeepAgents 的应用场景和案例', 'status': 'pending'},  
 {'content': '收集 DeepAgents 的技术实现细节', 'status': 'pending'}, {'content': '整理并撰写结构化总结报告',     
 'status': 'pending'}]                                                                                           
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
────────────────────────────────────────────────────────────────────────────────
步骤 9
────────────────────────────────────────────────────────────────────────────────
╭──────────────────────────────────────────────────── AI 思考 ────────────────────────────────────────────────────╮
 现在让我搜索更多关于 DeepAgents 架构和设计理念的信息:                                                          
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─────────────────────────────────────────────────── 工具调用 ────────────────────────────────────────────────────╮
 {                                                                                                               
   "工具名称": "tavily_search",                                                                                  
   "参数": {                                                                                                     
     "query": "DeepAgents 架构设计 核心机制 中间件",                                                             
     "search_depth": "advanced"                                                                                  
   }                                                                                                             
 }                                                                                                               
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭────────────────────────────────────────── 工具响应: DeepAgents_Agent ───────────────────────────────────────────╮
 现在让我搜索更多关于 DeepAgents 架构和设计理念的信息:                                                          
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
────────────────────────────────────────────────────────────────────────────────
步骤 10
────────────────────────────────────────────────────────────────────────────────
╭──────────────────────────────────────────────────── AI 思考 ────────────────────────────────────────────────────╮
 {"query": "DeepAgents 架构设计 核心机制 中间件", "follow_up_questions": null, "answer": null, "images": [],     
 "results": [{"url": "https://blog.csdn.net/yanqianglifei/article/details/150361432", "title": "Deep             
 Agents:用于复杂任务自动化的AI 代理框架原创", "content": "_DeepAgents_ 深度解析:LangCh _ai_                    
 n生态下构建智能Agent的终极指南!\n\nWANGJUNAI...                                                                
                                                                                                                 
 (内容较长,完整内容将在最后显示)                                                                                 
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭──────────────────────────────────────────── 工具响应: tavily_search ────────────────────────────────────────────╮
 {"query": "DeepAgents 架构设计 核心机制 中间件", "follow_up_questions": null, "answer": null, "images": [],     
 "results": [{"url": "https://blog.csdn.net/yanqianglifei/article/details/150361432", "title": "Deep             
 Agents:用于复杂任务自动化的AI 代理框架原创", "content": "_DeepAgents_ 深度解析:LangCh _ai_                    
 n生态下构建智能Agent的终极指南!\n\nWANGJUNAIJIAO的博客\n\n11-21Image 48 1276 \n\n_DeepAgents_ 是LangCh _ai_    
 n推出的第三个独立开源Agent _框架_,通过三大核心机制解决传统Agent在 _复杂_ 环境下的\"脆弱\"问题:_任务_          
 规划工具帮助执行前先列步骤;文件系统支持虚拟文件操作与持久化;子智能体机制实现上下文隔离和专长分离。这些能力通  
 过中间件实现,使开发者能快速构建 _复杂_                                                                         
 ... (共 4533 字符)                                                                                              
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
────────────────────────────────────────────────────────────────────────────────
步骤 11
────────────────────────────────────────────────────────────────────────────────
╭─────────────────────────────────────────────────── 工具调用 ────────────────────────────────────────────────────╮
 {                                                                                                               
   "工具名称": "write_todos",                                                                                    
   "参数": {                                                                                                     
     "todos": [                                                                                                  
       {                                                                                                         
         "content": "搜索 LangChain DeepAgents 框架的基本信息和概述",                                            
         "status": "completed"                                                                                   
       },                                                                                                        
       {                                                                                                         
         "content": "研究 DeepAgents 的核心架构和设计理念",                                                      
         "status": "completed"                                                                                   
       },                                                                                                        
       {                                                                                                         
         "content": "分析 DeepAgents 的主要特性和功能",                                                          
         "status": "in_progress"                                                                                 
       },                                                                                                        
       {                                                                                                         
         "content": "调研 DeepAgents 的应用场景和案例",                                                          
         "status": "pending"                                                                                     
       },                                                                                                        
       {                                                                                                         
         "content": "收集 DeepAgents 的技术实现细节",                                                            
         "status": "pending"                                                                                     
       },                                                                                                        
       {                                                                                                         
         "content": "整理并撰写结构化总结报告",                                                                  
         "status": "pending"                                                                                     
       }                                                                                                         
     ]                                                                                                           
   }                                                                                                             
 }                                                                                                               
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭────────────────────────────────────────── 工具响应: DeepAgents_Agent ───────────────────────────────────────────╮
                                                                                                                 
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
────────────────────────────────────────────────────────────────────────────────
步骤 12
────────────────────────────────────────────────────────────────────────────────
╭──────────────────────────────────────────────────── AI 思考 ────────────────────────────────────────────────────╮
 Updated todo list to [{'content': '搜索 LangChain DeepAgents 框架的基本信息和概述', 'status': 'completed'},     
 {'content': '研究 DeepAgents 的核心架构和设计理念', 'status': 'completed'}, {'content': '分析 DeepAgents        
 的主要特性和功能', 'status': 'in_progress'}, {'content': '调研 DeepAgents 的应用场景和案例', 'status':          
 'pending'}, {'content': '收集 ...                                                                               
                                                                                                                 
 (内容较长,完整内容将在最后显示)                                                                                 
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭───────────────────────────────────────────── 工具响应: write_todos ─────────────────────────────────────────────╮
 Updated todo list to [{'content': '搜索 LangChain DeepAgents 框架的基本信息和概述', 'status': 'completed'},     
 {'content': '研究 DeepAgents 的核心架构和设计理念', 'status': 'completed'}, {'content': '分析 DeepAgents        
 的主要特性和功能', 'status': 'in_progress'}, {'content': '调研 DeepAgents 的应用场景和案例', 'status':          
 'pending'}, {'content': '收集 DeepAgents 的技术实现细节', 'status': 'pending'}, {'content':                     
 '整理并撰写结构化总结报告', 'status': 'pending'}]                                                               
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
────────────────────────────────────────────────────────────────────────────────
步骤 13
────────────────────────────────────────────────────────────────────────────────
╭──────────────────────────────────────────────────── AI 思考 ────────────────────────────────────────────────────╮
 现在让我搜索 DeepAgents 的主要特性和功能:                                                                      
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─────────────────────────────────────────────────── 工具调用 ────────────────────────────────────────────────────╮
 {                                                                                                               
   "工具名称": "tavily_search",                                                                                  
   "参数": {                                                                                                     
     "query": "DeepAgents 特性 功能 任务规划 文件系统 子代理",                                                   
     "search_depth": "advanced"                                                                                  
   }                                                                                                             
 }                                                                                                               
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭────────────────────────────────────────── 工具响应: DeepAgents_Agent ───────────────────────────────────────────╮
 现在让我搜索 DeepAgents 的主要特性和功能:                                                                      
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
────────────────────────────────────────────────────────────────────────────────
步骤 14
────────────────────────────────────────────────────────────────────────────────
╭──────────────────────────────────────────────────── AI 思考 ────────────────────────────────────────────────────╮
 {"query": "DeepAgents 特性 功能 任务规划 文件系统 子代理", "follow_up_questions": null, "answer": null,         
 "images": [], "results": [{"url": "https://www.cnblogs.com/itech/p/19189589", "title": "LangChain、LangGraph 和 
 DeepAgents - iTech - 博客园", "content": "| 特性 (Feature) | LangChain | LangGraph | DeepAgents |\n ---  -...   
                                                                                                                 
 (内容较长,完整内容将在最后显示)                                                                                 
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭──────────────────────────────────────────── 工具响应: tavily_search ────────────────────────────────────────────╮
 {"query": "DeepAgents 特性 功能 任务规划 文件系统 子代理", "follow_up_questions": null, "answer": null,         
 "images": [], "results": [{"url": "https://www.cnblogs.com/itech/p/19189589", "title": "LangChain、LangGraph 和 
 DeepAgents - iTech - 博客园", "content": "| 特性 (Feature) | LangChain | LangGraph | DeepAgents |\n ---  ---    
 |\n| 主要目标 (Primary Goal) | 构建基本的 LLM 应用和简单的顺序式工作流。 |                                      
 编排具有复杂控制流和状态管理的、可生产的 (production-ready) 智能体工作流。 |                                    
 创建高度自主、能执行复杂、多步骤任务的长期运行的智能体。 |\n| 工作流类型 (Workflow Type) | 顺序式               
 (Sequential)、线性链 (Linear Chains)                                                                            
 ... (共 4245 字符)                                                                                              
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
────────────────────────────────────────────────────────────────────────────────
步骤 15
────────────────────────────────────────────────────────────────────────────────
╭─────────────────────────────────────────────────── 工具调用 ────────────────────────────────────────────────────╮
 {                                                                                                               
   "工具名称": "write_todos",                                                                                    
   "参数": {                                                                                                     
     "todos": [                                                                                                  
       {                                                                                                         
         "content": "搜索 LangChain DeepAgents 框架的基本信息和概述",                                            
         "status": "completed"                                                                                   
       },                                                                                                        
       {                                                                                                         
         "content": "研究 DeepAgents 的核心架构和设计理念",                                                      
         "status": "completed"                                                                                   
       },                                                                                                        
       {                                                                                                         
         "content": "分析 DeepAgents 的主要特性和功能",                                                          
         "status": "completed"                                                                                   
       },                                                                                                        
       {                                                                                                         
         "content": "调研 DeepAgents 的应用场景和案例",                                                          
         "status": "in_progress"                                                                                 
       },                                                                                                        
       {                                                                                                         
         "content": "收集 DeepAgents 的技术实现细节",                                                            
         "status": "pending"                                                                                     
       },                                                                                                        
       {                                                                                                         
         "content": "整理并撰写结构化总结报告",                                                                  
         "status": "pending"                                                                                     
       }                                                                                                         
     ]                                                                                                           
   }                                                                                                             
 }                                                                                                               
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭────────────────────────────────────────── 工具响应: DeepAgents_Agent ───────────────────────────────────────────╮
                                                                                                                 
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
────────────────────────────────────────────────────────────────────────────────
步骤 16
────────────────────────────────────────────────────────────────────────────────
╭──────────────────────────────────────────────────── AI 思考 ────────────────────────────────────────────────────╮
 Updated todo list to [{'content': '搜索 LangChain DeepAgents 框架的基本信息和概述', 'status': 'completed'},     
 {'content': '研究 DeepAgents 的核心架构和设计理念', 'status': 'completed'}, {'content': '分析 DeepAgents        
 的主要特性和功能', 'status': 'completed'}, {'content': '调研 DeepAgents 的应用场景和案例', 'status':            
 'in_progress'}, {'content': '收...                                                                              
                                                                                                                 
 (内容较长,完整内容将在最后显示)                                                                                 
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭───────────────────────────────────────────── 工具响应: write_todos ─────────────────────────────────────────────╮
 Updated todo list to [{'content': '搜索 LangChain DeepAgents 框架的基本信息和概述', 'status': 'completed'},     
 {'content': '研究 DeepAgents 的核心架构和设计理念', 'status': 'completed'}, {'content': '分析 DeepAgents        
 的主要特性和功能', 'status': 'completed'}, {'content': '调研 DeepAgents 的应用场景和案例', 'status':            
 'in_progress'}, {'content': '收集 DeepAgents 的技术实现细节', 'status': 'pending'}, {'content':                 
 '整理并撰写结构化总结报告', 'status': 'pending'}]                                                               
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
────────────────────────────────────────────────────────────────────────────────
步骤 17
────────────────────────────────────────────────────────────────────────────────
╭──────────────────────────────────────────────────── AI 思考 ────────────────────────────────────────────────────╮
 现在让我搜索 DeepAgents 的应用场景和案例:                                                                      
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─────────────────────────────────────────────────── 工具调用 ────────────────────────────────────────────────────╮
 {                                                                                                               
   "工具名称": "tavily_search",                                                                                  
   "参数": {                                                                                                     
     "query": "DeepAgents 应用场景 案例 研究 编程 自动化",                                                       
     "search_depth": "advanced"                                                                                  
   }                                                                                                             
 }                                                                                                               
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭────────────────────────────────────────── 工具响应: DeepAgents_Agent ───────────────────────────────────────────╮
 现在让我搜索 DeepAgents 的应用场景和案例:                                                                      
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
────────────────────────────────────────────────────────────────────────────────
步骤 18
────────────────────────────────────────────────────────────────────────────────
╭──────────────────────────────────────────────────── AI 思考 ────────────────────────────────────────────────────╮
 {"query": "DeepAgents 应用场景 案例 研究 编程 自动化", "follow_up_questions": null, "answer": null, "images":   
 [], "results": [{"url": "https://blog.csdn.net/yanqianglifei/article/details/150361432", "title": "Deep         
 Agents:用于复杂任务自动化的AI 代理框架原创 - CSDN博客", "content": "-还原+1:1 还原 [...] _DeepAgents_          
 是一个独立的 Python 库,专门 ...                                                                                 
                                                                                                                 
 (内容较长,完整内容将在最后显示)                                                                                 
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭──────────────────────────────────────────── 工具响应: tavily_search ────────────────────────────────────────────╮
 {"query": "DeepAgents 应用场景 案例 研究 编程 自动化", "follow_up_questions": null, "answer": null, "images":   
 [], "results": [{"url": "https://blog.csdn.net/yanqianglifei/article/details/150361432", "title": "Deep         
 Agents:用于复杂任务自动化的AI 代理框架原创 - CSDN博客", "content": "-还原+1:1 还原 [...] _DeepAgents_          
 是一个独立的 Python 库,专门 _用于_ 构建能够处理 _复杂_、多步骤 _任务_ 的智能体(Agent)。它基于LangGraph构建,受   
 Claude Code、_Deep_ Research 和 Manus 等应用的启发,提供了规划能力、文件系统 _用于_                              
 上下文管理,以及生成子智能体的能力。 核心定位 _DeepAgents_ 专注于解决传统智能体 _框架_ 在处理 _复杂_ _任务_      
 时的局限性,提供了...\n\nL                                                                                       
 ... (共 4426 字符)                                                                                              
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
────────────────────────────────────────────────────────────────────────────────
步骤 19
────────────────────────────────────────────────────────────────────────────────
╭─────────────────────────────────────────────────── 工具调用 ────────────────────────────────────────────────────╮
 {                                                                                                               
   "工具名称": "write_todos",                                                                                    
   "参数": {                                                                                                     
     "todos": [                                                                                                  
       {                                                                                                         
         "content": "搜索 LangChain DeepAgents 框架的基本信息和概述",                                            
         "status": "completed"                                                                                   
       },                                                                                                        
       {                                                                                                         
         "content": "研究 DeepAgents 的核心架构和设计理念",                                                      
         "status": "completed"                                                                                   
       },                                                                                                        
       {                                                                                                         
         "content": "分析 DeepAgents 的主要特性和功能",                                                          
         "status": "completed"                                                                                   
       },                                                                                                        
       {                                                                                                         
         "content": "调研 DeepAgents 的应用场景和案例",                                                          
         "status": "completed"                                                                                   
       },                                                                                                        
       {                                                                                                         
         "content": "收集 DeepAgents 的技术实现细节",                                                            
         "status": "in_progress"                                                                                 
       },                                                                                                        
       {                                                                                                         
         "content": "整理并撰写结构化总结报告",                                                                  
         "status": "pending"                                                                                     
       }                                                                                                         
     ]                                                                                                           
   }                                                                                                             
 }                                                                                                               
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭────────────────────────────────────────── 工具响应: DeepAgents_Agent ───────────────────────────────────────────╮
                                                                                                                 
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
────────────────────────────────────────────────────────────────────────────────
步骤 20
────────────────────────────────────────────────────────────────────────────────
╭──────────────────────────────────────────────────── AI 思考 ────────────────────────────────────────────────────╮
 Updated todo list to [{'content': '搜索 LangChain DeepAgents 框架的基本信息和概述', 'status': 'completed'},     
 {'content': '研究 DeepAgents 的核心架构和设计理念', 'status': 'completed'}, {'content': '分析 DeepAgents        
 的主要特性和功能', 'status': 'completed'}, {'content': '调研 DeepAgents 的应用场景和案例', 'status':            
 'completed'}, {'content': '收集 ...                                                                             
                                                                                                                 
 (内容较长,完整内容将在最后显示)                                                                                 
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭───────────────────────────────────────────── 工具响应: write_todos ─────────────────────────────────────────────╮
 Updated todo list to [{'content': '搜索 LangChain DeepAgents 框架的基本信息和概述', 'status': 'completed'},     
 {'content': '研究 DeepAgents 的核心架构和设计理念', 'status': 'completed'}, {'content': '分析 DeepAgents        
 的主要特性和功能', 'status': 'completed'}, {'content': '调研 DeepAgents 的应用场景和案例', 'status':            
 'completed'}, {'content': '收集 DeepAgents 的技术实现细节', 'status': 'in_progress'}, {'content':               
 '整理并撰写结构化总结报告', 'status': 'pending'}]                                                               
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
────────────────────────────────────────────────────────────────────────────────
步骤 21
────────────────────────────────────────────────────────────────────────────────
╭──────────────────────────────────────────────────── AI 思考 ────────────────────────────────────────────────────╮
 现在让我搜索 DeepAgents 的技术实现细节:                                                                        
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─────────────────────────────────────────────────── 工具调用 ────────────────────────────────────────────────────╮
 {                                                                                                               
   "工具名称": "tavily_search",                                                                                  
   "参数": {                                                                                                     
     "query": "DeepAgents 技术实现 中间件 架构图 代码示例",                                                      
     "search_depth": "advanced"                                                                                  
   }                                                                                                             
 }                                                                                                               
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭────────────────────────────────────────── 工具响应: DeepAgents_Agent ───────────────────────────────────────────╮
 现在让我搜索 DeepAgents 的技术实现细节:                                                                        
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
────────────────────────────────────────────────────────────────────────────────
步骤 22
────────────────────────────────────────────────────────────────────────────────
╭──────────────────────────────────────────────────── AI 思考 ────────────────────────────────────────────────────╮
 {"query": "DeepAgents 技术实现 中间件 架构图 代码示例", "follow_up_questions": null, "answer": null, "images":  
 [], "results": [{"url": "https://zhuanlan.zhihu.com/p/1935481980641863575", "title": "Deep                      
 Agents:构建深度思考和执行复杂任务的Agent", "content": "长期记忆:Agent                                         
 可以将中间思考、发现和笔记记录到文件中,以便后续随时读取。这解决了 LLM 有限上下文窗口的问题。\n                 
 共享工作区:所有...                                                                                             
                                                                                                                 
 (内容较长,完整内容将在最后显示)                                                                                 
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭──────────────────────────────────────────── 工具响应: tavily_search ────────────────────────────────────────────╮
 {"query": "DeepAgents 技术实现 中间件 架构图 代码示例", "follow_up_questions": null, "answer": null, "images":  
 [], "results": [{"url": "https://zhuanlan.zhihu.com/p/1935481980641863575", "title": "Deep                      
 Agents:构建深度思考和执行复杂任务的Agent", "content": "长期记忆:Agent                                         
 可以将中间思考、发现和笔记记录到文件中,以便后续随时读取。这解决了 LLM 有限上下文窗口的问题。\n                 
 共享工作区:所有 Agent(包括主 Agent 和所有子 Agent)都可以访问这个共享空间,实现高效协作。例如,研究子 Agent   
 可以将发现写入报告,编码子 Agent 则可以读取该报告来指导其工作。\n\n另一个 Deep Agent 的例子                     
 Manus,也大量利用文件系统作为其“记忆”的核心。\n\n## 动手构建你自己的Deep                                        
 Agent\n\n为了让开发者能更轻松地为特定垂直领域构建 Deep A                                                        
 ... (共 4817 字符)                                                                                              
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
────────────────────────────────────────────────────────────────────────────────
步骤 23
────────────────────────────────────────────────────────────────────────────────
╭─────────────────────────────────────────────────── 工具调用 ────────────────────────────────────────────────────╮
 {                                                                                                               
   "工具名称": "write_todos",                                                                                    
   "参数": {                                                                                                     
     "todos": [                                                                                                  
       {                                                                                                         
         "content": "搜索 LangChain DeepAgents 框架的基本信息和概述",                                            
         "status": "completed"                                                                                   
       },                                                                                                        
       {                                                                                                         
         "content": "研究 DeepAgents 的核心架构和设计理念",                                                      
         "status": "completed"                                                                                   
       },                                                                                                        
       {                                                                                                         
         "content": "分析 DeepAgents 的主要特性和功能",                                                          
         "status": "completed"                                                                                   
       },                                                                                                        
       {                                                                                                         
         "content": "调研 DeepAgents 的应用场景和案例",                                                          
         "status": "completed"                                                                                   
       },                                                                                                        
       {                                                                                                         
         "content": "收集 DeepAgents 的技术实现细节",                                                            
         "status": "completed"                                                                                   
       },                                                                                                        
       {                                                                                                         
         "content": "整理并撰写结构化总结报告",                                                                  
         "status": "in_progress"                                                                                 
       }                                                                                                         
     ]                                                                                                           
   }                                                                                                             
 }                                                                                                               
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭────────────────────────────────────────── 工具响应: DeepAgents_Agent ───────────────────────────────────────────╮
                                                                                                                 
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
────────────────────────────────────────────────────────────────────────────────
步骤 24
────────────────────────────────────────────────────────────────────────────────
╭──────────────────────────────────────────────────── AI 思考 ────────────────────────────────────────────────────╮
 Updated todo list to [{'content': '搜索 LangChain DeepAgents 框架的基本信息和概述', 'status': 'completed'},     
 {'content': '研究 DeepAgents 的核心架构和设计理念', 'status': 'completed'}, {'content': '分析 DeepAgents        
 的主要特性和功能', 'status': 'completed'}, {'content': '调研 DeepAgents 的应用场景和案例', 'status':            
 'completed'}, {'content': '收集 ...                                                                             
                                                                                                                 
 (内容较长,完整内容将在最后显示)                                                                                 
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭───────────────────────────────────────────── 工具响应: write_todos ─────────────────────────────────────────────╮
 Updated todo list to [{'content': '搜索 LangChain DeepAgents 框架的基本信息和概述', 'status': 'completed'},     
 {'content': '研究 DeepAgents 的核心架构和设计理念', 'status': 'completed'}, {'content': '分析 DeepAgents        
 的主要特性和功能', 'status': 'completed'}, {'content': '调研 DeepAgents 的应用场景和案例', 'status':            
 'completed'}, {'content': '收集 DeepAgents 的技术实现细节', 'status': 'completed'}, {'content':                 
 '整理并撰写结构化总结报告', 'status': 'in_progress'}]                                                           
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
────────────────────────────────────────────────────────────────────────────────
步骤 25
────────────────────────────────────────────────────────────────────────────────
╭──────────────────────────────────────────────────── AI 思考 ────────────────────────────────────────────────────╮
 现在让我整理所有收集到的信息,撰写一份结构化的总结报告:                                                        
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─────────────────────────────────────────────────── 工具调用 ────────────────────────────────────────────────────╮
 {                                                                                                               
   "工具名称": "write_file",                                                                                     
   "参数": {                                                                                                     
     "file_path": "/DeepAgents_Research_Report.md",                                                              
     "content": "# LangChain DeepAgents 框架核心特性研究报告\n\n## 执行摘要\n\nDeepAgents 是 LangChain 生态系统  
   }                                                                                                             
 }                                                                                                               
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭────────────────────────────────────────── 工具响应: DeepAgents_Agent ───────────────────────────────────────────╮
 现在让我整理所有收集到的信息,撰写一份结构化的总结报告:                                                        
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
────────────────────────────────────────────────────────────────────────────────
步骤 26
────────────────────────────────────────────────────────────────────────────────
╭──────────────────────────────────────────────────── AI 思考 ────────────────────────────────────────────────────╮
 Updated file /DeepAgents_Research_Report.md                                                                     
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭───────────────────────────────────────────── 工具响应: write_file ──────────────────────────────────────────────╮
 Updated file /DeepAgents_Research_Report.md                                                                     
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
────────────────────────────────────────────────────────────────────────────────
步骤 27
────────────────────────────────────────────────────────────────────────────────
╭─────────────────────────────────────────────────── 工具调用 ────────────────────────────────────────────────────╮
 {                                                                                                               
   "工具名称": "write_todos",                                                                                    
   "参数": {                                                                                                     
     "todos": [                                                                                                  
       {                                                                                                         
         "content": "搜索 LangChain DeepAgents 框架的基本信息和概述",                                            
         "status": "completed"                                                                                   
       },                                                                                                        
       {                                                                                                         
         "content": "研究 DeepAgents 的核心架构和设计理念",                                                      
         "status": "completed"                                                                                   
       },                                                                                                        
       {                                                                                                         
         "content": "分析 DeepAgents 的主要特性和功能",                                                          
         "status": "completed"                                                                                   
       },                                                                                                        
       {                                                                                                         
         "content": "调研 DeepAgents 的应用场景和案例",                                                          
         "status": "completed"                                                                                   
       },                                                                                                        
       {                                                                                                         
         "content": "收集 DeepAgents 的技术实现细节",                                                            
         "status": "completed"                                                                                   
       },                                                                                                        
       {                                                                                                         
         "content": "整理并撰写结构化总结报告",                                                                  
         "status": "completed"                                                                                   
       }                                                                                                         
     ]                                                                                                           
   }                                                                                                             
 }                                                                                                               
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭────────────────────────────────────────── 工具响应: DeepAgents_Agent ───────────────────────────────────────────╮
                                                                                                                 
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
────────────────────────────────────────────────────────────────────────────────
步骤 28
────────────────────────────────────────────────────────────────────────────────
╭──────────────────────────────────────────────────── AI 思考 ────────────────────────────────────────────────────╮
 Updated todo list to [{'content': '搜索 LangChain DeepAgents 框架的基本信息和概述', 'status': 'completed'},     
 {'content': '研究 DeepAgents 的核心架构和设计理念', 'status': 'completed'}, {'content': '分析 DeepAgents        
 的主要特性和功能', 'status': 'completed'}, {'content': '调研 DeepAgents 的应用场景和案例', 'status':            
 'completed'}, {'content': '收集 ...                                                                             
                                                                                                                 
 (内容较长,完整内容将在最后显示)                                                                                 
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭───────────────────────────────────────────── 工具响应: write_todos ─────────────────────────────────────────────╮
 Updated todo list to [{'content': '搜索 LangChain DeepAgents 框架的基本信息和概述', 'status': 'completed'},     
 {'content': '研究 DeepAgents 的核心架构和设计理念', 'status': 'completed'}, {'content': '分析 DeepAgents        
 的主要特性和功能', 'status': 'completed'}, {'content': '调研 DeepAgents 的应用场景和案例', 'status':            
 'completed'}, {'content': '收集 DeepAgents 的技术实现细节', 'status': 'completed'}, {'content':                 
 '整理并撰写结构化总结报告', 'status': 'completed'}]                                                             
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
────────────────────────────────────────────────────────────────────────────────
步骤 29
────────────────────────────────────────────────────────────────────────────────
╭──────────────────────────────────────────────────── AI 思考 ────────────────────────────────────────────────────╮
 我已经完成了对 LangChain DeepAgents                                                                             
 框架的详细调研,并撰写了一份全面的结构化总结报告。以下是报告的主要内容和发现:                                  
                                                                                                                 
 ## 调研总结                                                                                                     
                                                                                                                 
 ### 核心发现:                                                                                                  
                                                                                                                 
 1. **框架定位**:DeepAgents 是 LangChain                                                                        
 生态系统中的最新成员,专门用于构建能够处理复杂、多步骤任务的智能体框架。                                        
                                                                                                                 
 2. **四大核心支柱**:                                                                                           
    - 详细系统提示                                                                                               
    - 规划工具(通过 `write_todos`/`read_todos`)                                                                
    - 子Agent系统                                                                                                
    - 虚拟文件系统                                                                                               
                                                                                                                 
 3. **关键技术特性**:                                                                                           
    - 自主推理和深度规划能力                                                                                     
    - 长期记忆和...                                                                                              
                                                                                                                 
 (内容较长,完整内容将在最后显示)                                                                                 
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭────────────────────────────────────────── 工具响应: DeepAgents_Agent ───────────────────────────────────────────╮
 我已经完成了对 LangChain DeepAgents                                                                             
 框架的详细调研,并撰写了一份全面的结构化总结报告。以下是报告的主要内容和发现:                                  
                                                                                                                 
 ## 调研总结                                                                                                     
                                                                                                                 
 ### 核心发现:                                                                                                  
                                                                                                                 
 1. **框架定位**:DeepAgents 是 LangChain                                                                        
 生态系统中的最新成员,专门用于构建能够处理复杂、多步骤任务的智能体框架。                                        
                                                                                                                 
 2. **四大核心支柱**:                                                                                           
    - 详细系统提示                                                                                               
    - 规划工具(通过 `write_todos`/`read_todos`)                                                                
    - 子Agent系统                                                                                                
    - 虚拟文件系统                                                                                               
                                                                                                                 
 3. **关键技术特性**:                                                                                           
    - 自主推理和深度规划能力                                                                                     
    - 长期记忆和状态管理                                                                                         
    - 子智能体委托机制                                                                                           
    - 文件系统集成                                                                                               
                                                                                                                 
 4. **应用场景**:                                                                                               
    - 复杂研究分析任务                                                                                           
    - 自主问题解决                                                                                               
    - 深度推理链应用                                                                                             
    - 多领域知识整合                                                                                             
                                                                                                                 
 5. **技术优势**:                                                                                               
    - 打破 Token 限制                                                                                            
    - 支持长期运行任务                                                                                           
    - 提供确定性工作流                                                                                           
    - 适合生产环境部署                                                                                           
                                                                                                                 
 ### 报告结构:                                                                                                  
 报告包含了十个主要章节,从框架概述到技                                                                          
 ... (共 662 字符)                                                                                               
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
任务完成!

4.3 子代理(Sub-Agent Delegation)

  • 组件SubAgentMiddleware

  • 工具名task (delegate_task)

  • 核心概念

    • 任务隔离:每个子代理有独立的上下文窗口。

    • 并行执行:支持同时启动多个子代理。

    • 结果聚合:智能整合多个子代理的输出。

  • 机制

  • 当任务过于具体(如”爬取并分析这篇长论文”)时,主 Agent 会生成一个隔离环境的子 Agent 去执行。

  • 子 Agent 启动时,只继承必要的环境配置,但拥有全新的、空白的消息历史

  • 子 Agent 执行完毕后只返回一个总结性的结果。这保证了主 Agent 的时间线(Context)保持干净,极大地节省了 Token。

4.3.1 自动触发默认的 SubAgentMiddleware

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import asyncio
import os
from dotenv import load_dotenv
from rich.console import Console
from rich.panel import Panel
from rich.tree import Tree
from deepagents import create_deep_agent
from langchain_openai import ChatOpenAI
from langchain_community.tools import TavilySearchResults

from langchain_core.messages import ToolMessage, BaseMessage

# 加载环境变量
load_dotenv(override=True)

# 配置 Rich Console
console = Console()

async def run_auto_subagent_demo():
"""
演示:不显式传入 subagents 参数,自动触发默认的 SubAgentMiddleware
"""
console.print(Panel.fit("[bold magenta]DeepAgents 自动 SubAgent 中间件演示[/bold magenta]", border_style="magenta"))
console.print("[dim]本演示验证:即使不传入 subagents 参数,Agent 默认也会启用 'general-purpose' 子 Agent。[/dim]")

# 1. 初始化模型
# 使用 GPT-4o 以确保对复杂指令的理解和工具调用的准确性
llm = ChatOpenAI(model="gpt-4o", temperature=0)

# 2. 定义工具
# 给 Agent 一些工具,以便子 Agent 也有工具可用
tools = [TavilySearchResults(max_results=2)]

# 3. 创建 Agent (不传入 subagents)
# create_deep_agent 默认会加载 SubAgentMiddleware(general_purpose_agent=True)
# 这意味着 Agent 会自动获得一个名为 'task' 的工具,可以调用 'general-purpose' 子 Agent
console.print("[bold cyan]正在创建 Agent (subagents=None)...[/bold cyan]")

agent = create_deep_agent(
model=llm,
tools=tools,
# subagents=[], # 故意不传或传空
system_prompt="""你是一个能够高效处理并发任务的智能助手。
对于包含多个独立部分的复杂任务,你必须使用 'task' 工具来创建 'general-purpose' 子 Agent 进行处理。
不要自己在主线程中串行执行所有操作。利用子 Agent 来隔离上下文并提高效率。"""
)

# 4. 定义一个适合并行/隔离的任务
task = """请同时调研以下两个完全不同的主题,并分别给出简短总结:
1. Python 语言的历史起源。
2. Rust 语言的内存安全机制。
请务必使用子 Agent 分别处理这两个任务。"""

console.print(f"\n[bold green]用户任务:[/bold green] {task}\n")

# 5. 运行并可视化
step = 0

# 检查 API Key
if not os.getenv("OPENAI_API_KEY"):
console.print("[bold red]❌ 错误: 未找到 OPENAI_API_KEY,请检查 .env 文件[/bold red]")
return

console.print("[dim]开始流式输出...[/dim]")
try:
async for event in agent.astream({"messages": [("user", task)]}):
step += 1
# 遍历所有节点输出 (e.g., 'agent', 'tools')
for node_name, node_data in event.items():
if node_data is None:
continue

if "messages" in node_data:
msgs = node_data["messages"]
# 确保是列表
if not isinstance(msgs, list):
msgs = [msgs]

for msg in msgs:
# 0. 过滤非消息对象
if not isinstance(msg, BaseMessage):
continue

# 1. 检测工具调用 (期望看到 'task' 工具)
if hasattr(msg, "tool_calls") and msg.tool_calls:
tree = Tree(f"[bold yellow]Step {step}: 决策与调用 (Node: {node_name})[/bold yellow]")
for tc in msg.tool_calls:
tool_name = tc['name']
tool_args = tc['args']

if tool_name == "task":
# 验证成功!
branch = tree.add(f"[bold red]🚀 触发 'task' 工具 (Sub-Agent)[/bold red]")
branch.add(f"[cyan]子 Agent 类型:[/cyan] {tool_args.get('subagent_type')}")
branch.add(f"[cyan]任务指令:[/cyan] {tool_args.get('description')}")
else:
tree.add(f"[blue]普通工具调用:[/blue] {tool_name}")

console.print(tree)

# 2. 检测工具输出 (Sub-Agent 的返回结果)
elif isinstance(msg, ToolMessage):
if msg.name == "task":
# Sub-Agent 完成任务返回
panel = Panel(
msg.content,
title=f"[bold magenta]Sub-Agent 完成任务 (Node: {node_name})[/bold magenta]",
border_style="magenta"
)
console.print(panel)
else:
console.print(f"[dim]Tool Output ({msg.name}): {msg.content[:100]}...[/dim]")

# 3. 检测 AI 最终回复
elif msg.content and not msg.tool_calls:
title = f"[bold green]Agent 回复 (Node: {node_name})[/bold green]"
console.print(Panel(msg.content, title=title, border_style="green"))

except Exception as e:
console.print(f"[bold red]❌ 运行时错误: {e}[/bold red]")

console.print("\n[bold magenta]演示结束[/bold magenta]")

if __name__ == "__main__":
await run_auto_subagent_demo()

╭─────────────────────────────────────╮
 DeepAgents 自动 SubAgent 中间件演示 
╰─────────────────────────────────────╯
本演示验证:即使不传入 subagents 参数,Agent 默认也会启用 'general-purpose' 子 Agent。
/var/folders/fl/8wq5_lz53ln9ypplts4z_1tr0000gn/T/ipykernel_17865/2256871362.py:32: LangChainDeprecationWarning: The class `TavilySearchResults` was deprecated in LangChain 0.3.25 and will be removed in 1.0. An updated version of the class exists in the `langchain-tavily package and should be used instead. To use it run `pip install -U `langchain-tavily` and import as `from `langchain_tavily import TavilySearch``.
  tools = [TavilySearchResults(max_results=2)]
正在创建 Agent (subagents=None)...
用户任务: 请同时调研以下两个完全不同的主题,并分别给出简短总结:
            1. Python 语言的历史起源。
            2. Rust 语言的内存安全机制。
            请务必使用子 Agent 分别处理这两个任务。

开始流式输出...
Step 3: 决策与调用 (Node: model)
├── 🚀 触发 'task' 工具 (Sub-Agent)
│   ├── 子 Agent 类型: general-purpose
│   └── 任务指令: Research the history and origin of the Python programming language and provide a brief summary.
└── 🚀 触发 'task' 工具 (Sub-Agent)
    ├── 子 Agent 类型: general-purpose
    └── 任务指令: Research the memory safety mechanisms of the Rust programming language and provide a brief 
        summary.
╭─────────────────────────────────────── Sub-Agent 完成任务 (Node: tools) ────────────────────────────────────────╮
 Python is a high-level, interpreted programming language known for its simplicity and readability. It was       
 created by Guido van Rossum and first released in 1991. The development of Python began in the late 1980s at    
 the Centrum Wiskunde & Informatica (CWI) in the Netherlands. Van Rossum was inspired by the ABC language, which 
 was designed for teaching programming, and he aimed to create a language that was easy to read and write, with  
 a clean syntax.                                                                                                 
                                                                                                                 
 Python's name was inspired by the British comedy group Monty Python, which van Rossum enjoyed. The language was 
 designed to emphasize code readability and simplicity, allowing programmers to express concepts in fewer lines  
 of code compared to other languages like C++ or Java.                                                           
                                                                                                                 
 Python has evolved significantly since its inception, with major versions introducing new features and          
 improvements. Python 2.0 was released in 2000, introducing features like list comprehensions and a garbage      
 collection system. Python 3.0, released in 2008, was a major revision that aimed to rectify design flaws in the 
 language, though it was not backward compatible with Python 2.x.                                                
                                                                                                                 
 Over the years, Python has gained immense popularity due to its versatility and wide range of applications,     
 including web development, data analysis, artificial intelligence, scientific computing, and more. Its          
 extensive standard library and active community have contributed to its status as one of the most popular       
 programming languages in the world.                                                                             
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─────────────────────────────────────── Sub-Agent 完成任务 (Node: tools) ────────────────────────────────────────╮
 Rust is a systems programming language that emphasizes memory safety without sacrificing performance. It        
 achieves this through several key mechanisms:                                                                   
                                                                                                                 
 1. **Ownership**: Rust uses a unique ownership model to manage memory. Each value in Rust has a single owner,   
 and when the owner goes out of scope, the value is automatically deallocated. This prevents memory leaks and    
 ensures that memory is freed when it is no longer needed.                                                       
                                                                                                                 
 2. **Borrowing and References**: Rust allows references to data, which can be either mutable or immutable.      
 Borrowing rules ensure that you cannot have mutable references while immutable ones exist, preventing data      
 races at compile time.                                                                                          
                                                                                                                 
 3. **Lifetimes**: Rust uses lifetimes to track how long references are valid. This ensures that references do   
 not outlive the data they point to, preventing dangling pointers.                                               
                                                                                                                 
 4. **Type System**: Rust's strong static type system helps catch errors at compile time. It enforces strict     
 type checks, which prevent many common programming errors.                                                      
                                                                                                                 
 5. **No Null Pointers**: Rust does not have null pointers. Instead, it uses the `Option` type to represent a    
 value that might be absent, forcing the programmer to handle the absence of a value explicitly.                 
                                                                                                                 
 6. **Pattern Matching**: Rust's pattern matching allows for safe and expressive handling of different data      
 structures and control flow, reducing the likelihood of errors.                                                 
                                                                                                                 
 7. **Concurrency**: Rust's concurrency model is built on the ownership and type system, ensuring that data      
 races are caught at compile time. This makes concurrent programming safer and more reliable.                    
                                                                                                                 
 These mechanisms collectively ensure that Rust programs are memory safe, preventing common issues like buffer   
 overflows, null pointer dereferences, and data races, all while maintaining high performance.                   
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─────────────────────────────────────────── Agent 回复 (Node: model) ────────────────────────────────────────────╮
 ### Python 语言的历史起源                                                                                       
                                                                                                                 
 Python 是一种高级、解释型编程语言,以其简洁和可读性著称。由 Guido van Rossum 创建,并于 1991 年首次发布。Python 
 的开发始于 20 世纪 80 年代末,在荷兰的 Centrum Wiskunde & Informatica (CWI) 进行。Van Rossum 受到 ABC           
 语言的启发,旨在创建一种易于阅读和编写、语法简洁的语言。                                                        
                                                                                                                 
 Python 的名字来源于英国喜剧团体 Monty Python,van Rossum                                                        
 非常喜欢他们的作品。该语言设计强调代码的可读性和简洁性,使程序员能够用比 C++ 或 Java 更少的代码行数表达概念。   
                                                                                                                 
 自诞生以来,Python 经过了显著的发展,主要版本引入了新功能和改进。Python 2.0 于 2000                             
 年发布,引入了列表推导式和垃圾回收系统。Python 3.0 于 2008                                                      
 年发布,是一次重大修订,旨在纠正语言中的设计缺陷,但与 Python 2.x 不兼容。                                      
                                                                                                                 
 多年来,Python 因其多功能性和广泛的应用而获得了极大的欢迎,包括 Web                                             
 开发、数据分析、人工智能、科学计算等。其广泛的标准库和活跃的社区也促成了其成为世界上最受欢迎的编程语言之一。    
                                                                                                                 
 ### Rust 语言的内存安全机制                                                                                     
                                                                                                                 
 Rust 是一种系统编程语言,强调内存安全而不牺牲性能。它通过以下关键机制实现这一点:                               
                                                                                                                 
 1. **所有权**:Rust 使用独特的所有权模型来管理内存。每个值在 Rust                                               
 中都有一个单一的所有者,当所有者超出作用域时,值会自动释放。这防止了内存泄漏,并确保内存在不再需要时被释放。    
                                                                                                                 
 2. **借用和引用**:Rust                                                                                         
 允许对数据的引用,可以是可变的或不可变的。借用规则确保在存在不可变引用时不能有可变引用,从而在编译时防止数据竞  
 争。                                                                                                            
                                                                                                                 
 3. **生命周期**:Rust                                                                                           
 使用生命周期来跟踪引用的有效期。这确保了引用不会超出其指向的数据的生命周期,从而防止悬空指针。                  
                                                                                                                 
 4. **类型系统**:Rust                                                                                           
 的强静态类型系统有助于在编译时捕获错误。它强制执行严格的类型检查,防止许多常见的编程错误。                      
                                                                                                                 
 5. **无空指针**:Rust 没有空指针。相反,它使用 `Option` 类型来表示可能不存在的值,迫使程序员显式处理值的缺失。  
                                                                                                                 
 6. **模式匹配**:Rust 的模式匹配允许安全且富有表现力地处理不同的数据结构和控制流,减少错误的可能性。            
                                                                                                                 
 7. **并发性**:Rust 的并发模型基于所有权和类型系统,确保在编译时捕获数据竞争。这使得并发编程更安全和可靠。      
                                                                                                                 
 这些机制共同确保 Rust 程序的内存安全,防止常见问题如缓冲区溢出、空指针解引用和数据竞争,同时保持高性能。        
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
演示结束

4.3.2 显示传入subAgent参数

1
2
# 安装 MCP 适配器(关键依赖)\MCP 服务器开发库(如需自定义工具)
#!pip install langchain-mcp-adapters mcp

检查 Node.js

  • node –version

检查 npm/npx

  • npx –version

手动安装 MCP 服务器包

  • npm install -g @amap/amap-maps-mcp-server
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import asyncio
import os
from dotenv import load_dotenv
from rich.console import Console
from rich.panel import Panel
from rich.tree import Tree
from deepagents import create_deep_agent
from langchain_deepseek import ChatDeepSeek
from langchain_mcp_adapters.client import MultiServerMCPClient
from langchain_community.tools import TavilySearchResults

from langchain_core.messages import ToolMessage, BaseMessage

# 加载环境变量
load_dotenv(override=True)

# 1. 配置 Rich Console
console = Console()

# 2. 配置 Context7 MCP (连接官方文档)
async def setup_mcp_tools():
console.print("[dim]正在连接 Context7 MCP 服务器...[/dim]")
# 检查 node 环境
try:
client = MultiServerMCPClient({
"context7": {
"transport": "stdio",
"command": "npx",
"args": ["-y", "@upstash/context7-mcp@latest"],
}
})
# 获取工具
tools = await client.get_tools()
console.print(f"[green]成功加载 {len(tools)} 个 MCP 工具[/green]")
return client, tools
except Exception as e:
console.print(f"[red]连接 MCP 失败: {e}[/red]")
console.print("[yellow]将使用模拟工具继续...[/yellow]")
return None, []

# 3. 定义子 Agent 配置
def get_subagents_config(mcp_tools):
# 子 Agent 1: 官方文档专家
doc_tools = mcp_tools if mcp_tools else [TavilySearchResults(max_results=3)]

docs_researcher = {
"name": "DocsResearcher",
"description": "负责查阅官方文档和技术规范的专家 Agent。",
"system_prompt": "你是一名专门查阅官方文档的技术专家。请使用工具获取准确的技术细节。不要猜测。",
"tools": doc_tools,
"model": "deepseek-chat"
}

# 子 Agent 2: 社区生态专家
community_researcher = {
"name": "CommunityResearcher",
"description": "负责搜索社区博客、教程和最佳实践的专家 Agent。",
"system_prompt": "你是一名关注社区动态的开发者。请搜索博客、论坛和 GitHub 讨论。",
"tools": [TavilySearchResults(max_results=3)],
"model": "deepseek-chat"
}

return [docs_researcher, community_researcher]

# 4. 主运行逻辑
async def run_parallel_demo():
console.print(Panel.fit("[bold blue]DeepAgents 并行子 Agent 演示[/bold blue]", border_style="blue"))

# 初始化 MCP
mcp_client, mcp_tools = await setup_mcp_tools()

# 获取子 Agent 配置
subagents = get_subagents_config(mcp_tools)

# 创建主 Agent
llm = ChatDeepSeek(model="deepseek-chat", temperature=0)

agent = create_deep_agent(
model=llm,
tools=[],
subagents=subagents, # 传入子 Agent 配置
system_prompt="""你是一名技术总监。你的任务是协调 DocsResearcher 和 CommunityResearcher 完成调研任务。
请根据用户需求,将任务拆解并分发给这两个子 Agent。
如果任务允许,请务必并行调用它们以提高效率。
最后汇总它们的报告。"""
)

task = "请详细调研 'LangChain DeepAgents' 框架。我需要官方的技术架构说明(来自文档)以及社区的最佳实践案例。请对比两者。"

console.print(f"\n[bold green]任务指令:[/bold green] {task}\n")

# 运行并可视化
step = 0
try:
async for event in agent.astream({"messages": [("user", task)]}):
step += 1
# 遍历所有节点输出 (e.g., 'agent', 'tools')
for node_name, node_data in event.items():
if node_data is None:
continue

if "messages" in node_data:
msgs = node_data["messages"]
# 确保是列表
if not isinstance(msgs, list):
msgs = [msgs]

for msg in msgs:
# 0. 过滤非消息对象
if not isinstance(msg, BaseMessage):
continue

# 1. 检测工具调用 (期望看到 'task' 工具)
if hasattr(msg, "tool_calls") and msg.tool_calls:
tree = Tree(f"[bold yellow]Step {step}: 决策与调用 (Node: {node_name})[/bold yellow]")
for tc in msg.tool_calls:
tool_name = tc['name']
tool_args = tc['args']

if tool_name == "task":
# 验证成功!
branch = tree.add(f"[bold red]🚀 触发 'task' 工具 (Sub-Agent)[/bold red]")
branch.add(f"[cyan]子 Agent 类型:[/cyan] {tool_args.get('subagent_type')}")
branch.add(f"[cyan]任务指令:[/cyan] {tool_args.get('description')}")
else:
tree.add(f"[blue]普通工具调用:[/blue] {tool_name}")

console.print(tree)

# 2. 检测工具输出 (Sub-Agent 的返回结果)
elif isinstance(msg, ToolMessage):
if msg.name == "task":
# Sub-Agent 完成任务返回
panel = Panel(
msg.content,
title=f"[bold magenta]Sub-Agent 完成任务 (Node: {node_name})[/bold magenta]",
border_style="magenta"
)
console.print(panel)
else:
console.print(f"[dim]Tool Output ({msg.name}): {msg.content[:100]}...[/dim]")

# 3. 检测 AI 最终回复
elif msg.content and not msg.tool_calls:
title = f"[bold green]Agent 回复 (Node: {node_name})[/bold green]"
console.print(Panel(msg.content, title=title, border_style="green"))

except Exception as e:
console.print(f"[bold red]❌ 运行时错误: {e}[/bold red]")


console.print("\n[bold blue]演示结束[/bold blue]")

# 运行演示
if __name__ == "__main__":
await run_parallel_demo()

╭──────────────────────────────╮
 DeepAgents 并行子 Agent 演示 
╰──────────────────────────────╯
正在连接 Context7 MCP 服务器...
成功加载 2 个 MCP 工具
任务指令: 请详细调研 'LangChain DeepAgents' 
框架。我需要官方的技术架构说明(来自文档)以及社区的最佳实践案例。请对比两者。

Step 3: 决策与调用 (Node: model)
└── 普通工具调用: write_todos
Tool Output (write_todos): Updated todo list to [{'content': '并行启动 DocsResearcher 和 CommunityResearcher 
进行调研', 'status': 'in_pr...
Step 6: 决策与调用 (Node: model)
├── 🚀 触发 'task' 工具 (Sub-Agent)
│   ├── 子 Agent 类型: DocsResearcher
│   └── 任务指令: 请详细调研 'LangChain DeepAgents' 框架的官方技术架构说明。需要从官方文档中获取以下信息:
│       
│       1. DeepAgents 的核心概念和设计理念
│       2. 技术架构图或组件说明
│       3. 主要模块和功能特性
│       4. 与其他 LangChain 组件的集成方式
│       5. 部署和配置要求
│       6. 性能特点和限制
│       
│       请提供详细的官方文档引用,包括具体的架构描述、API接口、配置参数等。重点关注技术实现细节和架构设计原则。
└── 🚀 触发 'task' 工具 (Sub-Agent)
    ├── 子 Agent 类型: CommunityResearcher
    └── 任务指令: 请调研 'LangChain DeepAgents' 
        框架的社区最佳实践案例。需要从社区博客、教程、GitHub项目等渠道收集:

        1. 实际应用案例和使用场景
        2. 社区分享的最佳实践和技巧
        3. 性能优化建议
        4. 常见问题解决方案
        5. 与其他工具/框架的集成经验
        6. 实际部署和生产环境经验
    
        请提供具体的案例链接、代码示例、经验分享等。重点关注实际应用中的成功经验和教训。
╭─────────────────────────────────────── Sub-Agent 完成任务 (Node: tools) ────────────────────────────────────────╮
 ## 总结                                                                                                         
                                                                                                                 
 基于对 LangChain DeepAgents 官方文档的详细调研,以下是该框架的关键技术架构要点:                                
                                                                                                                 
 ### 核心创新                                                                                                    
 DeepAgents 通过四个核心组件解决了传统 LLM 代理的"浅层"问题:                                                    
 1. **规划工具**(`write_todos`)实现任务分解                                                                    
 2. **子代理系统**实现上下文隔离                                                                                 
 3. **文件系统访问**管理大上下文                                                                                 
 4. **动态提示构建**支持渐进式技能披露                                                                           
                                                                                                                 
 ### 架构优势                                                                                                    
 - **中间件架构**:支持增量功能组合,易于扩展                                                                    
 - **可插拔后端**:支持多种存储策略(内存、文件系统、持久存储)                                                  
 - **LangGraph 集成**:与 LangGraph 生态系统无缝集成                                                             
 - **生产就绪**:支持可视化、部署和监控                                                                          
                                                                                                                 
 ### 适用场景                                                                                                    
 - 复杂多步骤任务处理                                                                                            
 - 需要长期记忆的对话系统                                                                                        
 - 专业领域研究助手                                                                                              
 - 需要人工监督的敏感操作                                                                                        
                                                                                                                 
 ### 技术成熟度                                                                                                  
 DeepAgents 是一个相对成熟的框架,借鉴了 Claude Code、Manus                                                      
 等成功应用的经验,提供了完整的工具链和部署选项,适合构建生产级的智能代理应用。                                  
                                                                                                                 
 该框架代表了当前 LLM                                                                                            
 代理技术的前沿,通过系统化的架构设计解决了复杂任务处理的挑战,为构建下一代智能应用提供了强大的基础。            
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─────────────────────────────────────── Sub-Agent 完成任务 (Node: tools) ────────────────────────────────────────╮
 # LangChain DeepAgents 框架社区最佳实践调研报告                                                                 
                                                                                                                 
 基于对社区博客、教程、GitHub项目等渠道的调研,以下是 LangChain DeepAgents 框架的社区最佳实践案例汇总:          
                                                                                                                 
 ## 1. 框架概述和定位                                                                                            
                                                                                                                 
 **官方定义**:DeepAgents 是 LangChain 生态系统中的一个独立库,用于构建能够处理复杂多步骤任务的智能体。它基于    
 LangGraph 构建,受 Claude Code、Deep Research 和 Manus 等应用的启发。                                           
                                                                                                                 
 **核心能力**:                                                                                                  
 - 任务规划和分解                                                                                                
 - 文件系统工具用于上下文管理                                                                                    
 - 子智能体生成和委托                                                                                            
 - 跨会话的持久化内存                                                                                            
 - 内置中间件架构                                                                                                
                                                                                                                 
 **技术栈关系**:                                                                                                
 - **LangGraph**:提供底层图执行和状态管理                                                                       
 - **LangChain**:工具和模型集成                                                                                 
 - **LangSmith**:可观测性、评估和部署                                                                           
 - **DeepAgents**:智能体抽象层                                                                                  
                                                                                                                 
 ## 2. 实际应用案例和使用场景                                                                                    
                                                                                                                 
 ### 2.1 深度研究智能体                                                                                          
 **案例**:构建研究助手智能体,能够进行多步骤研究、收集信息并生成报告                                            
 - **GitHub 示例**:`langchain-ai/deepagents-quickstarts` 中的研究智能体示例                                     
 - **核心功能**:使用文件系统存储中间结果,防止上下文溢出                                                        
 - **工具集成**:Tavily API 进行网络搜索,文件操作工具管理研究过程                                               
                                                                                                                 
 ### 2.2 物理实验助手                                                                                            
 **案例**:AI 驱动的物理实验助手(Grade 9 物理实验指南生成器)                                                   
 - **架构**:主智能体 + 研究子智能体 + 批判子智能体                                                              
 - **技术栈**:DeepAgents + FastAPI + 网络搜索工具                                                               
 - **特点**:多智能体协作,每个智能体专注于特定领域                                                              
                                                                                                                 
 ### 2.3 股票研究智能体                                                                                          
 **案例**:基于 LangGraph + LangSmith 的股票研究智能体(V3版本)                                                 
 - **GitHub 项目**:`sagar-n/deepagents`                                                                         
 - **特性**:生产级架构,模块化后端,交互式前端(Deep Agents UI)                                                
 - **监控**:UI 基础交互,实时可观测性                                                                           
                                                                                                                 
 ### 2.4 代码重构和开发助手                                                                                      
 **案例**:类似 Claude Code 的代码开发智能体                                                                     
 - **功能**:文件系统访问、代码修改、笔记记录                                                                    
 - **特点**:作为所有智能体(和子智能体)的共享工作空间                                                          
                                                                                                                 
 ## 3. 社区分享的最佳实践和技巧                                                                                  
                                                                                                                 
 ### 3.1 项目结构组织                                                                                            
 ```python                                                                                                       
 deepagents-project/                                                                                             
 ├── deepagents_research.ipynb  # 主笔记本                                                                       
 ├── .env                       # API密钥(不要提交到git)                                                       
 ├── .gitignore                 # 忽略敏感文件                                                                   
 ├── requirements.txt           # 依赖列表                                                                       
 └── README.md                  # 项目文档                                                                       
 ```                                                                                                             
                                                                                                                 
 ### 3.2 智能体设计原则                                                                                          
 1. **战略规划**:充分利用 `write_todos` 工具进行显式规划                                                        
 2. **上下文管理**:使用文件系统操作(`read_file`, `write_file`, `edit_file`)防止令牌限制                       
 3. **子智能体委托**:为专门职责创建专门的子智能体                                                               
 4. **自定义系统提示**:设计详细的工作流特定指令                                                                 
                                                                                                                 
 ### 3.3 中间件架构最佳实践                                                                                      
 DeepAgents 0.2 引入了可插拔后端架构:                                                                           
 - **本地文件系统**:开发环境                                                                                    
 - **数据库后端**:生产环境                                                                                      
 - **远程VM/云存储**:分布式部署                                                                                 
                                                                                                                 
 ### 3.4 内存管理策略                                                                                            
 - **文件系统作为内存**:存储中间结果在对话上下文之外                                                            
 - **需要时检索特定信息**:按需加载                                                                              
 - **增量构建复杂输出**:分阶段构建                                                                              
 - **跨多个会话继续工作**:持久化状态                                                                            
                                                                                                                 
 ## 4. 性能优化建议                                                                                              
                                                                                                                 
 ### 4.1 令牌管理                                                                                                
 - **令牌计数**:使用 `llm.get_num_tokens()` 或 `PromptValue.get_token_count()`                                  
 - **令牌预算**:为输入和输出设置令牌限制(例如:上下文不超过3000令牌,回答不超过1000令牌)                      
 - **基于任务调整**:简单任务降低 `max_tokens` 设置                                                              
                                                                                                                 
 ### 4.2 语义缓存策略                                                                                            
 - **成本降低**:通过语义缓存可减少高达65%的API成本                                                              
 - **响应时间**:从1-2秒减少到微秒级                                                                             
 - **实现**:识别相似查询并重用结果                                                                              
                                                                                                                 
 ### 4.3 流式处理优化                                                                                            
 - **用户体验**:流式处理改善感知延迟和用户体验                                                                  
 - **LCEL 暴露**:使用 `.stream()` 进行渐进式块处理                                                              
 - **避免缓冲**:当客户端可以直接流式传输时,避免在服务器内存中缓冲整个响应                                      
                                                                                                                 
 ### 4.4 并行执行                                                                                                
 - **队列基础架构**:解耦生成和消费                                                                              
 - **角色专业化**:通过专注于特定领域提高输出质量                                                                
 - **并行执行**:减少复杂任务的总体响应时间                                                                      
                                                                                                                 
 ## 5. 常见问题解决方案                                                                                          
                                                                                                                 
 ### 5.1 安装问题                                                                                                
 **问题**:`ModuleNotFoundError: No module named 'deepagents'`                                                   
 **解决方案**:                                                                                                  
 ```bash                                                                                                         
 # 创建虚拟环境                                                                                                  
 python -m venv venv                                                                                             
                                                                                                                 
 # 激活(Windows)                                                                                               
 venv\Scripts\activate                                                                                           
                                                                                                                 
 # 激活(Mac/Linux)                                                                                             
 source venv/bin/activate                                                                                        
                                                                                                                 
 # 安装核心依赖                                                                                                  
 pip install deepagents tavily-python langchain-google-genai langchain-openai                                    
 ```                                                                                                             
                                                                                                                 
 ### 5.2 上下文污染问题                                                                                          
 **问题**:智能体在复杂任务中忘记上下文                                                                          
 **解决方案**:                                                                                                  
 1. 使用文件系统操作存储中间结果                                                                                 
 2. 实现三文件策略:                                                                                             
    - `question.txt`:存储原始研究查询                                                                           
    - `final_report.md`:保存演进的研究报告                                                                      
    - 工作内存文件:防止上下文溢出                                                                               
                                                                                                                 
 ### 5.3 子智能体委托问题                                                                                        
 **问题**:委托过于宽泛或过于琐碎                                                                                
 **解决方案**:                                                                                                  
 - **过于宽泛**:一个子智能体过载(子智能体是隔离的,最好给它们具体的任务和清晰的预期输出)                      
 - **过于琐碎**:10个子智能体执行 `ls` 命令(只是浪费资金做琐碎任务)                                            
 - **最佳实践**:需要详细的委托指令                                                                              
                                                                                                                 
 ### 5.4 调试困难                                                                                                
 **问题**:需要重新运行整个工作流并手动跟踪步骤                                                                  
 **解决方案**:                                                                                                  
 - 使用 LangSmith 进行跟踪和调试                                                                                 
 - 实现观察者智能体用于日志记录                                                                                  
 - 利用中间件的可观测性功能                                                                                      
                                                                                                                 
 ## 6. 与其他工具/框架的集成经验                                                                                 
                                                                                                                 
 ### 6.1 LangSmith 集成                                                                                          
 - **跟踪设置**:几分钟内即可设置完成                                                                            
 - **运行树结构**:LLM模型调用和工具调用                                                                         
 - **线程管理**:用户和应用程序之间的完整对话集合                                                                
 - **调试工具**:`langsmith-cli` 工具桥接 LangSmith UI 和本地工作流                                              
                                                                                                                 
 ### 6.2 FastAPI 集成                                                                                            
 **生产部署模式**:                                                                                              
 ```python                                                                                                       
 from fastapi import FastAPI                                                                                     
 from deepagents import create_deep_agent                                                                        
                                                                                                                 
 app = FastAPI()                                                                                                 
                                                                                                                 
 # 创建智能体端点                                                                                                
 @app.post("/agent/task")                                                                                        
 async def execute_agent_task(task_request: TaskRequest):                                                        
     agent = create_deep_agent(tools=[...])                                                                      
     result = await agent.arun(task_request.prompt)                                                              
     return {"result": result}                                                                                   
 ```                                                                                                             
                                                                                                                 
 ### 6.3 Streamlit 集成                                                                                          
 **前端界面**:                                                                                                  
 ```python                                                                                                       
 import streamlit as st                                                                                          
 from deepagents import create_deep_agent                                                                        
                                                                                                                 
 st.title("AI Agent Task Tracker")                                                                               
 agent_output = agent.run(user_input)                                                                            
 st.json(agent_output)                                                                                           
 ```                                                                                                             
                                                                                                                 
 ### 6.4 Docker 容器化                                                                                           
 **Docker Compose 配置**:                                                                                       
 ```yaml                                                                                                         
 version: '3'                                                                                                    
 services:                                                                                                       
   deepagents-app:                                                                                               
     image: deepagents-app:latest                                                                                
     build: ./app                                                                                                
     command: streamlit run app/main.py --server.port 8080                                                       
     volumes:                                                                                                    
       - ./app/:/app                                                                                             
     ports:                                                                                                      
       - 8080:8080                                                                                               
     environment:                                                                                                
       - OPENAI_API_KEY=${OPENAI_API_KEY}                                                                        
 ```                                                                                                             
                                                                                                                 
 ## 7. 实际部署和生产环境经验                                                                                    
                                                                                                                 
 ### 7.1 部署架构演进                                                                                            
 **版本对比**:                                                                                                  
 - **V1**:基础功能,无监控                                                                                      
 - **V2**:控制台日志,基础可观测性                                                                              
 - **V3**:UI基础交互,生产级架构                                                                                
                                                                                                                 
 ### 7.2 可扩展性考虑                                                                                            
 **DeepAgents 0.2 改进**:                                                                                       
 - **可插拔后端**:支持本地文件系统、数据库、远程VM等                                                            
 - **内存管理**:大型工具结果逐出和对话历史摘要                                                                  
 - **成本降低**:组织报告运营成本平均减少40-60%                                                                  
                                                                                                                 
 ### 7.3 生产监控                                                                                                
 **关键指标**:                                                                                                  
 - **令牌使用**:监控上下文窗口利用率                                                                            
 - **执行时间**:跟踪复杂任务的完成时间                                                                          
 - **错误率**:监控工具调用失败率                                                                                
 - **成本指标**:跟踪API调用成本                                                                                 
                                                                                                                 
 ### 7.4 测试策略                                                                                                
 **LangSmith 测试集成**:                                                                                        
 - **Pytest 装饰器**:`@pytest.mark.langsmith`                                                                   
 - **自动日志记录**:所有测试用例记录到实验                                                                      
 - **轨迹检查**:在 LangSmith 中查看失败测试的确切情况                                                           
 - **LLM作为评判者**:使用多个评估器评估智能体行为的不同方面                                                     
                                                                                                                 
 ## 8. 成功经验和教训总结                                                                                        
                                                                                                                 
 ### 成功经验                                                                                                    
 1. **中间件架构**:DeepAgents 的中间件架构提供了灵活性和可组合性                                                
 2. **文件系统作为内存**:有效解决了上下文窗口限制问题                                                           
 3. **子智能体专业化**:显著提高了复杂任务的输出质量                                                             
 4. **LangSmith 集成**:提供了强大的调试和监控能力                                                               
                                                                                                                 
 ### 重要教训                                                                                                    
 1. **显式规划的重要性**:`write_todos` 工具将混乱的执行转变为有条不紊的工作流                                   
 2. **上下文管理是关键**:早期智能体会在研究中途达到令牌限制并忘记一切                                           
 3. **专业化优于泛化**:专门的子智能体比一个巨型提示产生更好的结果                                               
 4. **可观测性不可忽视**:没有适当的监控,调试复杂智能体行为几乎不可能                                           
                                                                                                                 
 ### 推荐资源                                                                                                    
 1. **官方文档**:https://docs.langchain.com/oss/python/deepagents/overview                                      
 2. **GitHub 仓库**:                                                                                            
    - `langchain-ai/deepagents-quickstarts`                                                                      
    - `dtunai/streaming-deepagents`                                                                              
    - `langchain-ai/deep-agents-ui`                                                                              
 3. **社区教程**:                                                                                               
    - "Building Advanced AI Agents with LangChain's DeepAgents" (Dev.to)                                         
    - "Streaming deepagents and task delegation with real-time output" (Medium)                                  
    - "Building Production-Ready Deep Agents with LangChain 1.0" (Medium)                                        
                                                                                                                 
 ## 结论                                                                                                         
                                                                                                                 
 LangChain DeepAgents 框架代表了构建复杂、多步骤AI智能体的重要进展。社区最佳实践表明,成功的关键在于:           
 1. **充分利用中间件架构**进行上下文工程                                                                         
 2. **实施有效的内存管理策略**防止上下文溢出                                                                     
 3. **设计专门的子智能体**处理特定任务                                                                           
 4. **集成强大的监控和调试工具**(特别是 LangSmith)                                                             
 5. **遵循渐进式部署策略**从本地开发到生产环境                                                                   
                                                                                                                 
 该框架特别适合需要长期运行、具有复杂规划和上下文管理需求的智能体应用场景。随着 DeepAgents 0.2                   
 的发布,框架在可扩展性和生产就绪性方面有了显著改进,为企业级部署提供了更好的支持。                              
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
Step 10: 决策与调用 (Node: model)
└── 普通工具调用: write_todos
Tool Output (write_todos): Updated todo list to [{'content': '并行启动 DocsResearcher 和 CommunityResearcher 
进行调研', 'status': 'compl...
Step 13: 决策与调用 (Node: model)
└── 普通工具调用: write_todos
Tool Output (write_todos): Updated todo list to [{'content': '并行启动 DocsResearcher 和 CommunityResearcher 
进行调研', 'status': 'compl...
Step 16: 决策与调用 (Node: model)
└── 普通工具调用: write_todos
Tool Output (write_todos): Updated todo list to [{'content': '并行启动 DocsResearcher 和 CommunityResearcher 
进行调研', 'status': 'compl...
Step 19: 决策与调用 (Node: model)
└── 普通工具调用: write_todos
Tool Output (write_todos): Updated todo list to [{'content': '并行启动 DocsResearcher 和 CommunityResearcher 
进行调研', 'status': 'compl...
Step 22: 决策与调用 (Node: model)
└── 普通工具调用: write_todos
Tool Output (write_todos): Updated todo list to [{'content': '并行启动 DocsResearcher 和 CommunityResearcher 
进行调研', 'status': 'compl...
╭─────────────────────────────────────────── Agent 回复 (Node: model) ────────────────────────────────────────────╮
 ## 调研完成总结                                                                                                 
                                                                                                                 
 我已经完成了对 'LangChain DeepAgents' 框架的详细调研,通过并行调用 DocsResearcher 和 CommunityResearcher        
 高效地收集了以下信息:                                                                                          
                                                                                                                 
 ### 调研成果概述:                                                                                              
                                                                                                                 
 1. **官方技术架构**:深入分析了 DeepAgents 的核心创新、技术架构特点、主要模块和集成方式                         
 2. **社区最佳实践**:收集了丰富的应用案例、性能优化建议、问题解决方案和部署经验                                 
 3. **对比分析**:系统比较了官方设计与社区实践的异同点,发现了良好的互补性                                       
 4. **综合报告**:生成了包含实施建议和前景展望的完整调研报告                                                     
                                                                                                                 
 ### 关键发现:                                                                                                  
                                                                                                                 
 1. **技术先进性**:DeepAgents 通过创新的上下文管理机制解决了传统LLM代理的局限性                                 
 2. **生态成熟度**:与 LangChain 生态系统的深度集成提供了完整的工具链支持                                        
 3. **社区活跃度**:丰富的应用案例验证了框架的实用性和灵活性                                                     
 4. **生产就绪性**:从开发到部署的全流程支持,适合企业级应用                                                     
                                                                                                                 
 ### 推荐资源:                                                                                                  
 - **官方文档**:https://docs.langchain.com/oss/python/deepagents/overview                                       
 - **GitHub 项目**:`langchain-ai/deepagents-quickstarts`                                                        
 - **社区教程**:Dev.to 和 Medium 上的深度技术文章                                                               
                                                                                                                 
 该调研报告为您提供了全面的技术评估和实施指导,可以帮助您更好地理解和应用 LangChain DeepAgents 框架。            
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
演示结束

4.4 文件系统集成 (Filesystem & Sandbox)

  • 组件FilesystemMiddleware & Backend

  • 功能:这是 DeepAgent 的核心”外挂大脑”。Agent 不再将所有检索到的长文塞入上下文(Context),而是将其写入 Filesystem(如 /workspace),仅在需要时读取。这极大地扩展了 Agent 的”工作记忆”。

  • 核心工具集

    • ls: 浏览目录结构。

    • read_file: 读取文件内容。关键封装:支持 offset(偏移量)和 limit(行数),强制 Agent 对大文件进行分页读取,避免一次性读爆上下文。

    • write_file: 创建新文件。

    • edit_file: 修改文件。支持精确的字符串替换和 replace_all 模式,并有防呆设计(要求先读后改)。

    • glob: 通配符模糊查找文件。

    • grep: 正则表达式搜索文件内容,像命令行一样在代码库中定位目标。

    • execute: 执行 Shell 命令(需 Sandbox 支持),用于运行代码、安装依赖等。

  • 亮点功能:大结果自动转存

    • 当 Agent 调用工具产生的结果过长时,系统会自动拦截,将完整结果写入文件系统(如 /large_tool_results/{id}),并只给 Agent 返回一个摘要和文件路径。这完美解决了搜索结果或日志文件过长导致 Agent 崩溃的问题。
  • **后端支持 (Backends)**:

  • FilesystemBackend: 直接操作本地磁盘。

  • DockerBackend: 在 Docker 容器中执行,提供隔离环境。

  • StoreBackend: 存储在数据库中,支持查询和检索。

  • E2BBackend: 使用 E2B 云端沙箱。

  • CompositeBackend: 混合模式(如:本地存文件,Docker 跑代码)。

4.4.1 核心工具集测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# --- FilesystemMiddleware 工具测试 ---
print("\n" + "="*50)
print("🚀 开始测试 FilesystemMiddleware 工具集")
print("="*50 + "\n")

from deepagents import create_deep_agent
from deepagents.backends import FilesystemBackend
from langchain_openai import ChatOpenAI
from dotenv import load_dotenv

# 1. 初始化环境
load_dotenv(override=True)
model = ChatOpenAI(model="gpt-4o", temperature=0)

# 使用本地文件系统后端,根目录设为 ./workspace
# 这样我们可以看到真实文件的创建
# 注意:设置 virtual_mode=True 以支持绝对路径 (如 /hello_world.py) 映射到 ./workspace
backend = FilesystemBackend(root_dir="./workspace", virtual_mode=True)

# 创建 Agent
# 注意:create_deep_agent 默认会自动包含 FilesystemMiddleware
agent = create_deep_agent(
model=model,
backend=backend,
system_prompt="你是一个文件系统操作助手。请根据用户指令使用相应的工具。"
)

# 辅助函数:运行并打印结果
def run_test(task_name, instruction):
print(f"\n🔹 [测试: {task_name}]")
print(f"指令: {instruction}")
try:
# 使用 invoke 而不是 stream 以简化输出
result = agent.invoke({"messages": [("user", instruction)]})
last_msg = result["messages"][-1]
print(f"🤖 Agent 回复: {last_msg.content}")

# 打印工具调用详情 (如果有)
for msg in result["messages"]:
if hasattr(msg, 'tool_calls') and msg.tool_calls:
for tool in msg.tool_calls:
print(f"🛠️ 调用工具: {tool['name']} args={tool['args']}")
except Exception as e:
print(f"❌ 发生错误: {e}")

==================================================
🚀 开始测试 FilesystemMiddleware 工具集
==================================================

write_file测试

1
2
3
4
5
6
# 2. 依次测试各个工具

# 案例 1: write_file
# 创建一个 Python 脚本文件
run_test("write_file",
"请在当前目录下创建一个名为 'hello_world.py' 的文件,内容是:\nprint('Hello from DeepAgents!')")
🔹 [测试: write_file]
指令: 请在当前目录下创建一个名为 'hello_world.py' 的文件,内容是:
print('Hello from DeepAgents!')
🤖 Agent 回复: 已在当前目录下创建了一个名为 `hello_world.py` 的文件,内容为:

1
print('Hello from DeepAgents!')
🛠️ 调用工具: write_file args={'file_path': '/hello_world.py', 'content': "print('Hello from DeepAgents!')"}

ls测试

1
2
3
# 案例 2: ls
# 查看目录内容,确认文件创建成功
run_test("ls", "请列出当前目录下的所有文件,确认 hello_world.py 是否存在。")
🔹 [测试: ls]
指令: 请列出当前目录下的所有文件,确认 hello_world.py 是否存在。
🤖 Agent 回复: 在当前目录下找到了文件 `hello_world.py`。
🛠️  调用工具: ls args={'path': '/'}

read_file测试

1
2
3
# 案例 3: read_file
# 读取刚才创建的文件内容
run_test("read_file", "请读取 'hello_world.py' 的内容并展示给我。")
🔹 [测试: read_file]
指令: 请读取 'hello_world.py' 的内容并展示给我。
🤖 Agent 回复: 文件 `hello_world.py` 的内容如下:

1
print('Hello from DeepAgents!')
🛠️ 调用工具: read_file args={'file_path': '/hello_world.py'}

edit_file测试

1
2
3
4
# 案例 4: edit_file
# 修改文件内容
run_test("edit_file",
"请修改 'hello_world.py' 文件,将 print 内容改为 'Hello from Modified File!'。")
🔹 [测试: edit_file]
指令: 请修改 'hello_world.py' 文件,将 print 内容改为 'Hello from Modified File!'。
🤖 Agent 回复: The content of the `hello_world.py` file has been successfully modified to print "Hello from Modified File!".
🛠️  调用工具: ls args={'path': '/'}
🛠️  调用工具: read_file args={'file_path': '/hello_world.py'}
🛠️  调用工具: edit_file args={'file_path': '/hello_world.py', 'old_string': "print('Hello from DeepAgents!')", 'new_string': "print('Hello from Modified File!')"}

grep测试

1
2
3
# 案例 5: grep
# 搜索文件中的特定字符串
run_test("grep", "请在当前目录下搜索包含 'Modified' 字符串的文件。")
🔹 [测试: grep]
指令: 请在当前目录下搜索包含 'Modified' 字符串的文件。
🤖 Agent 回复: 在当前目录下,文件 `hello_world.py` 包含字符串 "Modified"。
🛠️  调用工具: grep args={'pattern': 'Modified', 'output_mode': 'files_with_matches'}

glob测试

1
2
3
# 案例 6: glob
# 使用通配符查找文件
run_test("glob", "请找出当前目录下所有的 .py 文件。")
🔹 [测试: glob]
指令: 请找出当前目录下所有的 .py 文件。
🤖 Agent 回复: 当前目录下有一个 `.py` 文件:`hello_world.py`。
🛠️  调用工具: glob args={'pattern': '*.py'}

execute测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 案例 7: execute
# 尝试执行脚本
# 注意:execute 工具通常需要 SandboxBackend 支持。
# 如果当前使用的 FilesystemBackend 不支持执行,Agent 会收到错误提示或无法调用。
print("\n🔹 [测试: execute]")
print("指令: 尝试运行 hello_world.py 脚本")
try:
# 我们尝试强行要求 Agent 运行,看它如何反应
# 如果没有 execute 工具,Agent 可能会说无法执行
response = agent.invoke({"messages": [("user", "请使用 execute 工具运行 python hello_world.py")]})
print(f"🤖 Agent 回复: {response['messages'][-1].content}")
except Exception as e:
print(f"⚠️ 测试说明: execute 工具可能不可用 (取决于 Backend 支持): {e}")

print("\n" + "="*50)
print("✅ 测试结束")
🔹 [测试: execute]
指令: 尝试运行 hello_world.py 脚本
🤖 Agent 回复: 目前我没有直接执行代码的能力,但我可以帮助你创建和编辑文件。如果你需要运行一个 Python 脚本,你可以在你的本地环境中执行以下命令:

1
python hello_world.py
如果你需要帮助创建或编辑 `hello_world.py` 文件,请告诉我! ================================================== ✅ 测试结束

  能看到在FilesystemBackend下,没有execute方法,因为FilesystemBackend是一个文件系统后端,不支持执行Shell命令。接下来,我们测试一下其他后端。

4.4.2 Backend 后端应用

默认模式(内存沙箱)

  • 内存沙箱,支持在内存中执行代码,防止代码注入攻击。代码执行结果会被存储在内存中,不会对本地环境造成影响。执行结束后,内存中的数据会被清除。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from deepagents import create_deep_agent
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI

# 加载环境变量
load_dotenv(override=True)

# 定义 LLM
model = ChatOpenAI(model="gpt-4o", temperature=0)

# 默认情况下,DeepAgent 会自动加载 FilesystemMiddleware 并使用内存后端
agent = create_deep_agent(
model=model,
system_prompt="你是一个数据处理助手。"
)

# Agent 可以自由创建文件、读取文件,但这些文件只存在于内存中
# 任务结束后,这些文件会自动消失

agent.invoke({"messages": [("user", "创建一个名为 test.txt 的文件并写入 'Hello, World!'")]})
{'messages': [HumanMessage(content="创建一个名为 test.txt 的文件并写入 'Hello, World!'", additional_kwargs={}, response_metadata={}, id='6ede5591-229d-4e62-82d3-b908c0750c02'),
  AIMessage(content='', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 24, 'prompt_tokens': 4387, 'total_tokens': 4411, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}}, 'model_provider': 'openai', 'model_name': 'gpt-4o-2024-08-06', 'system_fingerprint': 'fp_e819e3438b', 'id': 'chatcmpl-CnTSPLLfo66rndZQ2IkMg6OsMwK2M', 'service_tier': 'default', 'finish_reason': 'tool_calls', 'logprobs': None}, id='lc_run--019b2847-9764-7ad3-83c6-cb863c663dfc-0', tool_calls=[{'name': 'write_file', 'args': {'file_path': '/test.txt', 'content': 'Hello, World!'}, 'id': 'call_Q3XKRlcGV1oOUlpdrEztoDyI', 'type': 'tool_call'}], usage_metadata={'input_tokens': 4387, 'output_tokens': 24, 'total_tokens': 4411, 'input_token_details': {'audio': 0, 'cache_read': 0}, 'output_token_details': {'audio': 0, 'reasoning': 0}}),
  ToolMessage(content='Updated file /test.txt', name='write_file', id='4384d221-e736-46a1-a366-79e463a31930', tool_call_id='call_Q3XKRlcGV1oOUlpdrEztoDyI'),
  AIMessage(content="The file `test.txt` has been created and the content 'Hello, World!' has been written to it.", additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 24, 'prompt_tokens': 4424, 'total_tokens': 4448, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 4352}}, 'model_provider': 'openai', 'model_name': 'gpt-4o-2024-08-06', 'system_fingerprint': 'fp_e819e3438b', 'id': 'chatcmpl-CnTSR4cVlpeZb09Vuz2AUD5laYgqQ', 'service_tier': 'default', 'finish_reason': 'stop', 'logprobs': None}, id='lc_run--019b2847-a125-72e3-a5cb-95cbe646a86d-0', usage_metadata={'input_tokens': 4424, 'output_tokens': 24, 'total_tokens': 4448, 'input_token_details': {'audio': 0, 'cache_read': 4352}, 'output_token_details': {'audio': 0, 'reasoning': 0}})],
 'files': {'/test.txt': {'content': ['Hello, World!'],
   'created_at': '2025-12-16T17:49:01.590574+00:00',
   'modified_at': '2025-12-16T17:49:01.590574+00:00'}}}
1
2
result = agent.invoke({"messages": [("user", "读取 test.txt 文件的内容")]})
print(result["messages"][-1].content)
看起来文件 `/test.txt` 不存在。请确认文件路径是否正确,或者检查文件是否存在于其他目录中。

持久化模式(操作真实文件)

  • 持久化沙箱,支持将代码执行结果持久化到本地磁盘,防止代码执行过程中数据丢失。代码执行结束后,内存中的数据会被清除,但是磁盘上的文件会被保留。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
from deepagents import create_deep_agent
from deepagents.backends import FilesystemBackend
from langchain_openai import ChatOpenAI

model = ChatOpenAI(model="gpt-4o", temperature=0)

# 将 Agent 的根目录映射到本地的 "./workspace" 文件夹,virtual_mode=True 表示启用虚拟文件系统,作用是将 Agent 执行的所有文件操作都映射到本地的 "./workspace" 文件夹,而不是直接操作本地文件系统。
backend = FilesystemBackend(root_dir="./workspace", virtual_mode=True)

agent = create_deep_agent(
model=model,
backend=backend # 传入后端,中间件会自动使用它
)

# 此时 Agent 执行 write_file("/readme.md", ...) 会在本地 ./workspace/readme.md 创建文件
agent.invoke({"messages": [("user", "创建一个名为 readme.md 的文件并写入 'Hello, World!'")]})
{'messages': [HumanMessage(content="创建一个名为 readme.md 的文件并写入 'Hello, World!'", additional_kwargs={}, response_metadata={}, id='3d03443f-c11b-43c8-8125-9443bd2b1645'),
  AIMessage(content='', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 25, 'prompt_tokens': 4381, 'total_tokens': 4406, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 3840}}, 'model_provider': 'openai', 'model_name': 'gpt-4o-2024-08-06', 'system_fingerprint': 'fp_e1dc348028', 'id': 'chatcmpl-CnTVJBkMDw7BSag9qA308p3L7dDIa', 'service_tier': 'default', 'finish_reason': 'tool_calls', 'logprobs': None}, id='lc_run--019b284a-53bd-7312-97c2-9a245734eecb-0', tool_calls=[{'name': 'write_file', 'args': {'file_path': '/readme.md', 'content': 'Hello, World!'}, 'id': 'call_1pHNU7CF2WCvaxqYLr6QRvYN', 'type': 'tool_call'}], usage_metadata={'input_tokens': 4381, 'output_tokens': 25, 'total_tokens': 4406, 'input_token_details': {'audio': 0, 'cache_read': 3840}, 'output_token_details': {'audio': 0, 'reasoning': 0}}),
  ToolMessage(content='Updated file /readme.md', name='write_file', id='0d6570e1-6c63-4d80-a2ae-8a854ee71b80', tool_call_id='call_1pHNU7CF2WCvaxqYLr6QRvYN'),
  AIMessage(content='The file `readme.md` has been created with the content "Hello, World!".', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 19, 'prompt_tokens': 4420, 'total_tokens': 4439, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 4352}}, 'model_provider': 'openai', 'model_name': 'gpt-4o-2024-08-06', 'system_fingerprint': 'fp_e1dc348028', 'id': 'chatcmpl-CnTVLLbuyT6cdjFRHsgZxhMKrPh4W', 'service_tier': 'default', 'finish_reason': 'stop', 'logprobs': None}, id='lc_run--019b284a-5fd5-7990-bde5-1a8c54521a87-0', usage_metadata={'input_tokens': 4420, 'output_tokens': 19, 'total_tokens': 4439, 'input_token_details': {'audio': 0, 'cache_read': 4352}, 'output_token_details': {'audio': 0, 'reasoning': 0}})]}
1
2
result = agent.invoke({"messages": [("user", "读取 readme.md 文件的内容")]})
print(result["messages"][-1].content)
The content of the `readme.md` file is:

1
Hello, World!

演示大文件读取分页

  • 在FilesystemBackend分页的参数是固定死的,默认每次读取500行,我们可以在系统提示词中指定每次读取的行数。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import asyncio
import os
import shutil
from pathlib import Path
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
from langchain_core.messages import BaseMessage, ToolMessage
from deepagents import create_deep_agent
from deepagents.backends import FilesystemBackend

load_dotenv(override=True)

# 定义工作目录
WORK_DIR = Path("workspace/pagination_demo").resolve()
LARGE_FILE_NAME = "server_logs.txt"
LARGE_FILE_PATH = WORK_DIR / LARGE_FILE_NAME
TARGET_SECRET = "CRITICAL_ERROR_CODE_998877"

async def run_pagination_demo():
print("\n" + "="*80)
print("DeepAgents FilesystemMiddleware 分页读取演示")
print("="*80)

# 2. 初始化 FilesystemBackend
# 将 backend 指向我们的测试目录
# virtual_mode=True 确保 Agent 只能访问该目录下的文件,不能访问宿主机其他目录
backend = FilesystemBackend(root_dir=WORK_DIR, virtual_mode=True)

# 3. 创建 Agent
# 我们明确指示 Agent 使用分页读取,每次读取 300 行,Agent默认每次读取500行(DEFAULT_READ_LIMIT)
system_prompt = """
你是一个专业的系统管理员。
你的任务是从日志文件中查找特定的错误代码。
注意:日志文件可能非常大,为了避免上下文溢出,你必须使用 `read_file` 工具的分页功能。
每次读取请限制在 300 行以内 (limit=300),并使用 offset 参数向后滚动。
直到找到目标信息为止。
"""

llm = ChatOpenAI(model="gpt-4o", temperature=0)

agent = create_deep_agent(
model=llm,
backend=backend,
system_prompt=system_prompt
)

task = f"请在 '/{LARGE_FILE_NAME}' 中查找包含 '{TARGET_SECRET}' 的行,并告诉我它的具体内容。"

print(f"\n任务: {task}")
print("-" * 60)

# 4. 执行任务并观察分页行为
step = 0
print("\n开始流式输出...")
try:
async for event in agent.astream({"messages": [("user", task)]}):
for node_name, node_data in event.items():
# debug: print(f"DEBUG: Node: {node_name}")
if not node_data: continue

# 处理 Overwrite 对象
if hasattr(node_data, "value"):
node_data = node_data.value

if not isinstance(node_data, dict):
continue

if "messages" in node_data:
msgs = node_data["messages"]
if hasattr(msgs, "value"):
msgs = msgs.value

if not isinstance(msgs, list): msgs = [msgs]

for msg in msgs:
# 1. 打印 Agent 的思考 (AIMessage with tool_calls)
if hasattr(msg, "tool_calls") and msg.tool_calls:
step += 1
print(f"\n[Step {step}] Agent 决定调用工具 (Node: {node_name}):")
for tc in msg.tool_calls:
name = tc['name']
args = tc['args']
print(f" >>> 工具: {name}")

if name == "read_file":
offset = args.get('offset', 0)
limit = args.get('limit', 'Default')
path_val = args.get('path') or args.get('file_path')
print(f" >>> 参数: path='{path_val}', offset={offset}, limit={limit}")
print(f" (说明: 正在读取从第 {offset} 行开始的 {limit} 行数据)")
else:
print(f" >>> 参数: {args}")

# 2. 打印工具的输出 (ToolMessage)
elif isinstance(msg, ToolMessage):
content = msg.content
line_count = len(content.splitlines())
# 检查是否包含目标 Secret
found_secret = TARGET_SECRET in content

preview = content[:100].replace('\n', ' ') + "..."
print(f"\n[Tool Output] (Node: {node_name}) 读取了 {line_count} 行数据")
print(f" 内容预览: {preview}")
if found_secret:
print(f" ✨ 成功: 在此分块中发现了目标 Secret: {TARGET_SECRET}")

# 3. 打印 Agent 的最终回复 (AIMessage without tool_calls)
elif isinstance(msg, BaseMessage) and msg.type == "ai" and msg.content:
print(f"\n[Agent 最终回复] (Node: {node_name}):")
print("-" * 40)
print(msg.content)
print("-" * 40)


except Exception as e:
print(f"❌ 运行出错: {e}")
import traceback
traceback.print_exc()

if __name__ == "__main__":
await run_pagination_demo()

================================================================================
DeepAgents FilesystemMiddleware 分页读取演示
================================================================================

任务: 请在 '/server_logs.txt' 中查找包含 'CRITICAL_ERROR_CODE_998877' 的行,并告诉我它的具体内容。
------------------------------------------------------------

开始流式输出...

[Step 1] Agent 决定调用工具 (Node: model):
  >>> 工具: read_file
  >>> 参数: path='/server_logs.txt', offset=0, limit=300
      (说明: 正在读取从第 0 行开始的 300 行数据)

[Tool Output] (Node: tools) 读取了 300 行数据
  内容预览:      1	Log Start      2	[1] INFO: Normal operation processing request id a58ff127-d6ac-452b-b559-8e5...

[Step 2] Agent 决定调用工具 (Node: model):
  >>> 工具: read_file
  >>> 参数: path='/server_logs.txt', offset=300, limit=300
      (说明: 正在读取从第 300 行开始的 300 行数据)

[Tool Output] (Node: tools) 读取了 300 行数据
  内容预览:    301	[300] INFO: Normal operation processing request id b9be9cb2-816f-4dd4-94bd-88af41497e8c    30...

[Step 3] Agent 决定调用工具 (Node: model):
  >>> 工具: read_file
  >>> 参数: path='/server_logs.txt', offset=600, limit=300
      (说明: 正在读取从第 600 行开始的 300 行数据)

[Tool Output] (Node: tools) 读取了 300 行数据
  内容预览:    601	[600] INFO: Normal operation processing request id 804c010a-13dc-4cc0-a231-5122a04b87b8    60...
  ✨ 成功: 在此分块中发现了目标 Secret: CRITICAL_ERROR_CODE_998877

[Agent 最终回复] (Node: model):
----------------------------------------
在日志文件中找到了包含 `CRITICAL_ERROR_CODE_998877` 的行:

1
[850] ERROR: CRITICAL_ERROR_CODE_998877 occurred in module X
这行日志记录了一个关键错误代码 `CRITICAL_ERROR_CODE_998877`,发生在模块 X 中。 ----------------------------------------

E2BBackend使用E2B云沙箱

E2B (Environment To Be) 是一个 专为 AI 智能体设计的云端安全执行环境 。

你可以把它想象成一台 云端电脑 或 远程服务器 ,DeepAgents 将其作为“外挂大脑”和“执行手脚”。当 AI 需要写代码、运行脚本或操作文件时,它不会在你的本地机器上操作,而是连接到 E2B 的云端环境中进行。

  • 核心特性
  1. 安全性与隔离性 (Security & Isolation) :

    • AI 生成的代码(可能包含错误或恶意逻辑)完全运行在云端沙箱中, 绝不会破坏你本地的电脑环境 。
    • 演示代码中,Agent 即使执行了 rm -rf / ,也只是删除了云端临时的沙箱,对宿主机毫发无损。
  2. 持久化会话 (Long-running Sessions) :

    • 沙箱可以保持运行状态。Agent 可以先创建一个文件(如演示中的 /home/user/hello.py ),然后在后续步骤中运行它。环境状态在会话期间是保持的。
  3. 标准 Linux 环境 :

    • 它提供标准的 Linux Shell。演示中 Agent 执行了 uname -a 和 python –version ,就像在真实的服务器上一样。

step1: 访问 https://e2b.dev 注册登陆,然后并获取 API_Key

  • 登陆后进入首页

  • 在左侧选择API Keys选项,创建API

  • 创建API Key后记得保存好,保存到.env文件中,作为:E2B_API_KEY

step2: 安装依赖

!pip install e2b

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# 定义 E2B 沙箱后端类
# 该类继承自 BaseSandbox,实现了在 E2B 沙箱中执行命令的功能

import base64
from typing import Any, Optional

# 导入 DeepAgents 后端协议,定义了沙箱后端的接口
from deepagents.backends.protocol import (
ExecuteResponse,
FileDownloadResponse,
FileUploadResponse,
SandboxBackendProtocol,
)
# 导入基础沙箱类,用于实现沙箱后端的基本功能
from deepagents.backends.sandbox import BaseSandbox

try:
from e2b import Sandbox
except ImportError:
Sandbox = None

class E2BBackend(BaseSandbox):
"""E2B 沙箱后端实现,用于 DeepAgents。

该后端使用 E2B(https://e2b.dev)提供安全、隔离的执行环境。
"""

def __init__(
self,
template: str = "base",
api_key: Optional[str] = None,
timeout: Optional[int] = None,
metadata: Optional[dict[str, str]] = None,
) -> None:
"""初始化 E2B 沙箱。

参数:
template: E2B 沙箱模板 ID(默认:"base")
api_key: E2B API 密钥(可选,默认使用 E2B_API_KEY 环境变量)
timeout: 沙箱超时时间(秒)
metadata: 自定义沙箱元数据
"""
if Sandbox is None:
raise ImportError(
"e2b package is not installed. "
"Please install it with `pip install e2b`."
)

self.sandbox = Sandbox.create(
template=template,
api_key=api_key,
timeout=timeout,
metadata=metadata,
)

@property
def id(self) -> str:
"""Unique identifier for the sandbox backend."""
return self.sandbox.sandbox_id

def execute(self, command: str) -> ExecuteResponse:
"""Execute a command in the sandbox."""
try:
# E2B commands.run returns CommandResult with stdout, stderr, exit_code
result = self.sandbox.commands.run(command)

# 返回执行结果,包含 stdout 是标准输出,stderr 是标准错误输出,exit_code 是退出码
return ExecuteResponse(
output=result.stdout + result.stderr,
exit_code=result.exit_code,
truncated=False,
)
except Exception as e:
return ExecuteResponse(
output=f"Error executing command: {str(e)}",
exit_code=1,
truncated=False,
)

def upload_files(self, files: list[tuple[str, bytes]]) -> list[FileUploadResponse]:
"""Upload multiple files to the sandbox."""
responses = []
for path, content in files:
try:
# Ensure directory exists before writing
# We can use execute to mkdir -p
parent_dir = path.rsplit("/", 1)[0]
if parent_dir:
self.sandbox.commands.run(f"mkdir -p {parent_dir}")

# Write file
self.sandbox.files.write(path, content)
responses.append(FileUploadResponse(path=path, error=None))
except Exception as e:
error_msg = str(e).lower()
error = "invalid_path"
if "permission" in error_msg:
error = "permission_denied"

responses.append(FileUploadResponse(path=path, error=error))
return responses

def download_files(self, paths: list[str]) -> list[FileDownloadResponse]:
"""Download multiple files from the sandbox."""
responses = []
for path in paths:
try:
content = self.sandbox.files.read(path)
# Ensure content is bytes
if isinstance(content, str):
content = content.encode("utf-8")

responses.append(FileDownloadResponse(path=path, content=content, error=None))
except Exception as e:
error_msg = str(e).lower()
error = "invalid_path"
if "not found" in error_msg:
error = "file_not_found"
elif "directory" in error_msg:
error = "is_directory"
elif "permission" in error_msg:
error = "permission_denied"

responses.append(FileDownloadResponse(path=path, content=None, error=error))
return responses

def close(self):
"""Close the sandbox session."""
self.sandbox.kill()

  • 将自定义好的沙箱后端类注册到 DeepAgents 中
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import asyncio
import os
import sys
from dotenv import load_dotenv
from deepagents import create_deep_agent
from langchain_openai import ChatOpenAI
from langchain_mcp_adapters.client import MultiServerMCPClient
from langchain_core.messages import BaseMessage, ToolMessage

# 假设 e2b_backend.py 在同一目录下
try:
from e2b_backend import E2BBackend
except ImportError:
# 尝试从当前路径导入
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from e2b_backend import E2BBackend

# 加载环境变量
load_dotenv(override=True)

async def setup_mcp_tools():
"""连接 Context7 MCP 服务器并获取工具"""
print("正在连接 Context7 MCP 服务器...")
try:
# 使用官方 Context7 MCP 配置
client = MultiServerMCPClient({
"context7": {
"transport": "stdio",
"command": "npx",
"args": ["-y", "@upstash/context7-mcp@latest"],
}
})
# 获取工具
tools = await client.get_tools()
print(f"成功加载 {len(tools)} 个 MCP 工具")
return client, tools
except Exception as e:
print(f"❌ 连接 MCP 失败: {e}")
return None, []

async def run_e2b_demo():
print("\n" + "="*80)
print("DeepAgents E2BBackend (Sandbox) 演示 ")
print("="*80)

# 1. 检查 API Key
if not os.getenv("E2B_API_KEY"):
print("❌ 错误: 未找到 E2B_API_KEY 环境变量。")
print("请在 .env 文件中设置 E2B_API_KEY,或直接导出该变量。")
return

# 2. 初始化 MCP (用于查询文档等辅助任务)
mcp_client, mcp_tools = await setup_mcp_tools()

# 3. 初始化 E2B Backend
print("正在初始化 E2B 沙箱 (Template: base)...")
try:
backend = E2BBackend(template="base")
print(f"沙箱已启动 (ID: {backend.id})")
except Exception as e:
print(f"❌ 沙箱启动失败: {e}")
return

try:
# 4. 创建 DeepAgent
llm = ChatOpenAI(model="gpt-4o", temperature=0)

agent = create_deep_agent(
model=llm,
tools=mcp_tools, # 赋予 MCP 工具能力
backend=backend, # 赋予 E2B 沙箱能力
system_prompt="""你是一个拥有云端沙箱环境的高级技术助手。
你的任务是演示如何在沙箱中进行操作。
请执行以下步骤:
1. 使用 'execute_command' 运行 'uname -a' 和 'python --version' 来展示环境信息。
2. 创建一个 Python 脚本 '/home/user/hello.py',内容是打印 'Hello from E2B Sandbox!'。
3. 运行这个 Python 脚本并显示输出。
"""
)

print(f"\n任务开始")

# 5. 执行任务
task = "请开始演示沙箱操作流程。"

step = 0
async for event in agent.astream({"messages": [("user", task)]}):
step += 1
for node_name, node_data in event.items():
if node_data is None: continue

if "messages" in node_data:
msgs = node_data["messages"]
if not isinstance(msgs, list): msgs = [msgs]

for msg in msgs:
if isinstance(msg, BaseMessage) and msg.content:
# 过滤掉空的工具调用消息
is_tool_call_msg = getattr(msg, "tool_calls", None)
if not is_tool_call_msg:
print(f"\n[Agent ({node_name})]")
print("-" * 40)
print(msg.content)
print("-" * 40)

if hasattr(msg, "tool_calls") and msg.tool_calls:
print(f"\n[Step {step}: 工具调用]")
for tc in msg.tool_calls:
args_str = str(tc['args'])
if len(args_str) > 500:
args_str = args_str[:500] + "..."
print(f" • {tc['name']}: {args_str}")

if isinstance(msg, ToolMessage):
content_preview = str(msg.content)
if len(content_preview) > 200:
content_preview = content_preview[:200] + "..."
print(f"[Tool Output ({msg.name})]: {content_preview}")

except Exception as e:
print(f"❌ 运行时错误: {e}")
import traceback
traceback.print_exc()
finally:
# 6. 清理资源
print("\n正在关闭沙箱...")
backend.close()
print("沙箱已关闭")
print("演示结束")

if __name__ == "__main__":
try:
# asyncio.run(run_e2b_demo())
await run_e2b_demo()
except RuntimeError as e:
if "asyncio.run() cannot be called from a running event loop" in str(e):
print("请在 Jupyter 中使用 await run_e2b_demo()")
else:
raise e

================================================================================
DeepAgents E2BBackend (Sandbox) 演示 
================================================================================
正在连接 Context7 MCP 服务器...
成功加载 2 个 MCP 工具
正在初始化 E2B 沙箱 (Template: base)...
沙箱已启动 (ID: ifbjxvlc69x9acwc6hegi)

任务开始

[Step 3: 工具调用]
  • execute: {'command': 'uname -a'}
  • execute: {'command': 'python --version'}

[Agent (tools)]
----------------------------------------
Linux e2b.local 6.1.158 #2 SMP PREEMPT_DYNAMIC Tue Nov 25 15:58:27 UTC 2025 x86_64 GNU/Linux

[Command succeeded with exit code 0]
----------------------------------------
[Tool Output (execute)]: Linux e2b.local 6.1.158 #2 SMP PREEMPT_DYNAMIC Tue Nov 25 15:58:27 UTC 2025 x86_64 GNU/Linux

[Command succeeded with exit code 0]

[Agent (tools)]
----------------------------------------
Python 3.11.6

[Command succeeded with exit code 0]
----------------------------------------
[Tool Output (execute)]: Python 3.11.6

[Command succeeded with exit code 0]

[Step 7: 工具调用]
  • write_file: {'file_path': '/home/user/hello.py', 'content': "print('Hello from E2B Sandbox!')"}

[Agent (tools)]
----------------------------------------
Updated file /home/user/hello.py
----------------------------------------
[Tool Output (write_file)]: Updated file /home/user/hello.py

[Step 10: 工具调用]
  • execute: {'command': 'python /home/user/hello.py'}

[Agent (tools)]
----------------------------------------
Hello from E2B Sandbox!

[Command succeeded with exit code 0]
----------------------------------------
[Tool Output (execute)]: Hello from E2B Sandbox!

[Command succeeded with exit code 0]

[Agent (model)]
----------------------------------------
演示完成!以下是我在沙箱环境中执行的步骤:

1. **环境信息**:
   - 操作系统信息:`Linux e2b.local 6.1.158 #2 SMP PREEMPT_DYNAMIC Tue Nov 25 15:58:27 UTC 2025 x86_64 GNU/Linux`
   - Python 版本:`Python 3.11.6`

2. **创建并运行 Python 脚本**:
   - 创建了一个 Python 脚本 `/home/user/hello.py`,内容是打印 `Hello from E2B Sandbox!`。
   - 运行该脚本,输出为:`Hello from E2B Sandbox!`

如果你有其他需要演示的操作或问题,请随时告诉我!
----------------------------------------

正在关闭沙箱...
沙箱已关闭
演示结束

DockerBackend使用Docker容器

DockerBackend 的核心作用是为 AI 智能体提供一个 安全沙箱(Sandbox) 。

如果不使用 Docker,Agent 执行的每一条命令(如 rm -rf 、 pip install )都会直接发生在您的宿主机(Mac)上,这极其危险且环境不可控。

  • 作用与优势

  • 安全隔离 (Security & Isolation)

    • 作用 :Agent 的所有操作(文件读写、代码执行、系统命令)都被限制在 Docker 容器内部。

    • 优势 :即使 Agent 产生幻觉执行了恶意代码(如删除系统文件),也只会破坏容器, 您的 Mac 宿主机毫发无损 。演示代码中的 auto_remove=True 确保任务结束后容器自动销毁,不留痕迹。

  • 环境一致性 (Reproducibility)

    • 作用 :代码指定了镜像 image=”python:3.11-slim” 。

    • 优势 :无论您的电脑安装的是 Python 3.9 还是 3.12,Agent 永远在一个干净、标准的 Python 3.11 环境中运行。这解决了“在我的机器上能跑”的经典依赖问题。

  • 生命周期管理 (Lifecycle Management)

    • 作用 : DockerBackend 自动处理容器的 启动 -> 连接 -> 执行 -> 销毁 全过程。

    • 优势 :开发者无需手动编写复杂的 Docker 命令,像使用本地对象一样简单地调用 backend.execute() 或 backend.write_file() 。

1
2
# 需要先安装一下docker依赖
#!pip install docker
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249

#这个 Backend 会在本地启动一个 Docker 容器,并将会话隔离在容器内部。

# - 核心功能 :
# - 自动生命周期管理 :初始化时启动容器,结束时自动销毁 ( auto_remove=True )。
# - 高效文件传输 :使用 tar 流在宿主机和容器之间传输文件,支持批量操作。
# - 资源限制 :支持设置 CPU ( cpu_quota ) 和内存 ( memory_limit ) 限制,防止 Agent 耗尽本机资源。
# - 网络控制 :可选禁用网络 ( network_disabled=True ) 以增强安全性。

import io
import tarfile
import time
import uuid
from typing import Optional

# 加载 DeepAgents 后端协议
from deepagents.backends.protocol import (
ExecuteResponse,
FileDownloadResponse,
FileUploadResponse,
SandboxBackendProtocol,
)

# 加载 DeepAgents 基础沙箱类
from deepagents.backends.sandbox import BaseSandbox

try:
import docker
from docker.errors import NotFound, APIError
except ImportError:
docker = None

class DockerBackend(BaseSandbox):
"""Docker 沙箱后端实现,用于 DeepAgents。

该后端使用本地 Docker 守护进程提供隔离的执行环境。
需要安装 `docker` Python 包,并确保 Docker 守护进程正在运行。
"""

def __init__(
self,
image: str = "python:3.11-slim",
auto_remove: bool = True,
cpu_quota: int = 50000, # 50% CPU
memory_limit: str = "512m",
network_disabled: bool = False,
working_dir: str = "/workspace",
volumes: dict[str, dict[str, str]] | None = None,
) -> None:
"""初始化 Docker 沙箱。

参数:
image: 使用的 Docker 镜像(默认:"python:3.11-slim")
auto_remove: 是否在关闭时移除容器(默认:True)
cpu_quota: CPU 配额,单位为微秒(默认:50000)
memory_limit: 内存限制(默认:"512m")
network_disabled: 是否禁用网络访问(默认:False)
working_dir: 容器内的工作目录(默认:"/workspace")
volumes: Docker 卷配置,例如 {'/宿主机路径': {'bind': '/容器路径', 'mode': 'rw'}}
"""
if docker is None:
raise ImportError(
"docker package is not installed. "
"Please install it with `pip install docker`."
)
# 初始化 Docker 客户端,from_env() 会自动从环境变量中读取 Docker 配置
self.client = docker.from_env()
self.image = image
self.auto_remove = auto_remove
self.working_dir = working_dir
self.volumes = volumes or {}
self._container = None

# Start container
try:
# Ensure image exists
try:
self.client.images.get(image)
except NotFound:
print(f"Pulling image {image}...")
self.client.images.pull(image)

self._container = self.client.containers.run(
image,
command="tail -f /dev/null", # Keep container running
detach=True,
tty=True,
cpu_quota=cpu_quota,
mem_limit=memory_limit,
network_disabled=network_disabled,
working_dir=working_dir,
volumes=self.volumes,
)

# Ensure working directory exists
self.execute(f"mkdir -p {working_dir}")

except Exception as e:
raise RuntimeError(f"Failed to start Docker container: {e}")

@property
def id(self) -> str:
"""Unique identifier for the sandbox backend."""
return self._container.id if self._container else "unknown"

def execute(self, command: str) -> ExecuteResponse:
"""Execute a command in the sandbox."""
if not self._container:
return ExecuteResponse(
output="Container not running",
exit_code=1,
truncated=False
)

try:
# Docker exec_run 返回 (exit_code, output)
# output 是字节类型
# 使用列表形式的 cmd 以避免 shell 转义问题
exit_code, output = self._container.exec_run(
cmd=["bash", "-c", command],
workdir=self.working_dir,
demux=False # Combine stdout and stderr
)

return ExecuteResponse(
output=output.decode("utf-8", errors="replace"),
exit_code=exit_code,
truncated=False,
)
except Exception as e:
return ExecuteResponse(
output=f"Error executing command: {str(e)}",
exit_code=1,
truncated=False,
)

def upload_files(self, files: list[tuple[str, bytes]]) -> list[FileUploadResponse]:
"""Upload multiple files to the sandbox using tar archive."""
if not self._container:
return [FileUploadResponse(path=p, error="permission_denied") for p, _ in files]

responses = []

# Create a tar archive in memory
tar_stream = io.BytesIO()
with tarfile.open(fileobj=tar_stream, mode='w') as tar:
for path, content in files:
# Docker put_archive expects relative paths inside the tar to be relative to the destination
# But here we want absolute paths to be respected.
# Actually put_archive extracts to a directory.
# To support absolute paths, we should probably upload to root /?
# Or handle relative paths relative to working_dir.

# Let's handle paths:
# If path is absolute, we strip leading / and upload to root.
# If path is relative, we upload to working_dir.

# Simplification: We will create a tar with full structure and extract to /

# Normalize path
if path.startswith("/"):
arcname = path.lstrip("/")
dest_path = "/"
else:
arcname = path
dest_path = self.working_dir

info = tarfile.TarInfo(name=arcname)
info.size = len(content)
info.mtime = time.time()
tar.addfile(info, io.BytesIO(content))

responses.append(FileUploadResponse(path=path, error=None))

tar_stream.seek(0)

try:
# We extract to / to support absolute paths in the tar
# Note: This assumes all files in the batch can be extracted to the same root.
# If mixed absolute/relative, this might be tricky.
# For robustness, we might need to upload one by one if paths are mixed,
# or group them.
# Strategy: Always extract to / (root), and ensure arcnames are full paths (without leading /)

self._container.put_archive(
path="/",
data=tar_stream
)
except Exception as e:
# Mark all as failed if batch fails
return [FileUploadResponse(path=p, error="permission_denied") for p, _ in files]

return responses

def download_files(self, paths: list[str]) -> list[FileDownloadResponse]:
"""Download multiple files from the sandbox."""
if not self._container:
return [FileDownloadResponse(path=p, error="permission_denied") for p in paths]

responses = []
for path in paths:
try:
# get_archive returns a tuple (generator, stat)
bits, stat = self._container.get_archive(path)

# Reconstruct tar from bits
file_content = io.BytesIO()
for chunk in bits:
file_content.write(chunk)
file_content.seek(0)

# Extract file from tar
with tarfile.open(fileobj=file_content, mode='r') as tar:
# There should be only one file/dir
member = tar.next()
if member.isdir():
responses.append(FileDownloadResponse(path=path, error="is_directory"))
continue

f = tar.extractfile(member)
if f:
content = f.read()
responses.append(FileDownloadResponse(path=path, content=content, error=None))
else:
responses.append(FileDownloadResponse(path=path, error="file_not_found"))

except NotFound:
responses.append(FileDownloadResponse(path=path, error="file_not_found"))
except Exception as e:
error_msg = str(e).lower()
error = "invalid_path"
if "permission" in error_msg:
error = "permission_denied"

responses.append(FileDownloadResponse(path=path, content=None, error=error))
return responses

def close(self):
"""Close the sandbox session."""
if self._container:
try:
if self.auto_remove:
self._container.remove(force=True)
else:
self._container.stop()
except Exception:
pass
self._container = None

  • 使用自定义的DockerBackend实现沙箱环境
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import asyncio
import os
import sys
from dotenv import load_dotenv
from deepagents import create_deep_agent
from langchain_openai import ChatOpenAI
from langchain_core.messages import BaseMessage

# 尝试导入 DockerBackend
try:
from docker_backend import DockerBackend
except ImportError:
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from docker_backend import DockerBackend

load_dotenv(override=True)

async def run_docker_demo():
print("\n" + "="*80)
print("DeepAgents DockerBackend 演示 (极简版)")
print("="*80)

# 1. 检查 Docker 环境
try:
import docker
docker.from_env().ping()
except Exception as e:
print(f"❌ Docker 未运行或未安装: {e}")
print("请确保 Docker Desktop 已启动。")
return

# 2. 初始化 Docker Backend
print("正在启动 Docker 容器 (Image: python:3.11-slim)...")
try:
backend = DockerBackend(
image="python:3.11-slim",
auto_remove=True
)
print(f"容器已启动 (ID: {backend.id[:12]})")
except Exception as e:
print(f"❌ 启动失败: {e}")
return

try:
# 3. 创建 Agent
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)

agent = create_deep_agent(
model=llm,
backend=backend,
system_prompt="""你是一个运行在 Docker 容器中的 AI 助手。
你的任务是演示环境隔离性。

请执行以下步骤:
1. 运行 'cat /etc/os-release' 查看容器操作系统。
2. 运行 'python --version' 确认 Python 环境。
3. 创建文件 '/workspace/hello_docker.py',内容为打印 'Hello from Docker Container!'。
4. 运行该脚本。
"""
)

print(f"\n任务开始")

# 4. 执行任务
async for event in agent.astream({"messages": [("user", "请开始演示。")]}):
for node_name, node_data in event.items():
if not node_data:
continue

# 处理 Overwrite 对象
if hasattr(node_data, "value"):
node_data = node_data.value

# 再次检查是否为字典
if not isinstance(node_data, dict):
continue

if "messages" in node_data:
messages = node_data["messages"]
if hasattr(messages, "value"):
messages = messages.value

if isinstance(messages, list) and messages:
last_msg = messages[-1]
if isinstance(last_msg, BaseMessage) and last_msg.content:
print(f"\n[Agent ({node_name})]")
print("-" * 40)
print(last_msg.content)
print("-" * 40)

except Exception as e:
print(f"❌ 运行时错误: {e}")
import traceback
traceback.print_exc()
finally:
print("\n正在清理容器...")
backend.close()
print("容器已移除")
print("\n演示结束")

if __name__ == "__main__":
await run_docker_demo()

================================================================================
DeepAgents DockerBackend 演示 (极简版)
================================================================================
正在启动 Docker 容器 (Image: python:3.11-slim)...
容器已启动 (ID: 53d6e631eec4)

任务开始

[Agent (PatchToolCallsMiddleware.before_agent)]
----------------------------------------
请开始演示。
----------------------------------------

[Agent (tools)]
----------------------------------------
PRETTY_NAME="Debian GNU/Linux 13 (trixie)"
NAME="Debian GNU/Linux"
VERSION_ID="13"
VERSION="13 (trixie)"
VERSION_CODENAME=trixie
DEBIAN_VERSION_FULL=13.2
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"

[Command succeeded with exit code 0]
----------------------------------------

[Agent (tools)]
----------------------------------------
Python 3.11.14

[Command succeeded with exit code 0]
----------------------------------------

[Agent (tools)]
----------------------------------------
Updated file /workspace/hello_docker.py
----------------------------------------

[Agent (tools)]
----------------------------------------
Hello from Docker Container!

[Command succeeded with exit code 0]
----------------------------------------

[Agent (model)]
----------------------------------------
演示已完成,以下是结果:

1. **操作系统信息**:
   - 操作系统:Debian GNU/Linux 13 (trixie)

2. **Python 版本**:
   - Python 3.11.14

3. **执行的脚本**:
   - 创建了文件 `/workspace/hello_docker.py`,内容为 `print('Hello from Docker Container!')`。
   - 脚本输出:`Hello from Docker Container!`

如需进一步操作,请告诉我!
----------------------------------------

正在清理容器...
容器已移除

演示结束
  • 这里可以看一下DockerBackend和E2BBackend的区别
特性 DockerBackend E2BBackend
核心定位 本地轻量级容器化沙箱 云端安全沙箱环境 (SaaS)
部署位置 运行在本地机器 (Localhost) 运行在 E2B 云端集群 (Remote Cloud)
依赖环境 需要本地安装并运行 Docker Desktop/Daemon 仅需安装 e2b Python SDK,无需本地 Docker
资源消耗 消耗本地 CPU/内存资源 消耗 E2B 云端资源 (不占用本地算力)
启动速度 快 (本地镜像启动,毫秒-秒级) 较快 (云端冷启动约 1-3秒)
网络隔离 可配置 (支持完全离线 network_disabled=True) 默认联网 (支持访问公网 API)
持久化 支持挂载本地卷 (Volumes) 实现数据持久化 临时环境 (会话结束即销毁),数据需手动导出
适用场景 • 本地开发/调试
• 数据隐私敏感 (不想数据出本地)
• 离线环境使用
• 生产环境部署 (无需维护 Docker)
• 多租户隔离 (每个用户一个云沙箱)
• 本地资源受限设备
成本 免费 (使用自有硬件) 付费 (按使用时长/资源计费)
配置复杂度 中 (需管理镜像、卷挂载、Docker 进程) 低 (API Key 开箱即用)

StoreBackend 使用数据库存储

  • step1 : 这里我们使用postgresql数据库进行数据的存储,所以需要先系统安装postgresql在本地环境

  • step2 : 安装完成之后,可以使用Docker来部署启动postgresql数据库,我这里使用的docker-compose.yml文件启动的,需要把password密码,user用户名,database数据库名称修改为自己的,使用docker-compose up -d启动数据库,如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#docker-compose.yml 文件:

services:
postgres:
image: postgres:15
container_name: my-postgres
environment:
- POSTGRES_PASSWORD=123456
- POSTGRES_USER=myuser
- POSTGRES_DB=mydatabase
ports:
- "5432:5432"
volumes:
- pg_data:/data/db
- ./conf.d:/data/conf.d
- ./init.sql:/docker-entrypoint-initdb.d/init.sql
restart: unless-stopped

volumes:
pg_data:
driver: local

1
#!pip install langgraph-checkpoint-postgres  # 生产环境使用
1
2
# 测试数据库是否连接正常,-U 指定用户名,-d 指定数据库名称
!psql -U myuser -d mydatabase -c "SELECT version();"
                                                           version                                                            
------------------------------------------------------------------------------------------------------------------------------
 PostgreSQL 14.20 (Homebrew) on aarch64-apple-darwin25.1.0, compiled by Apple clang version 17.0.0 (clang-1700.4.4.1), 64-bit
(1 row)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import asyncio
import os
import uuid
import traceback
from dotenv import load_dotenv

# LangChain / LangGraph Imports
from langchain_openai import ChatOpenAI
from langchain_mcp_adapters.client import MultiServerMCPClient
from langchain_core.messages import BaseMessage, ToolMessage
from langgraph.store.postgres import PostgresStore
from langgraph.checkpoint.memory import MemorySaver
from psycopg_pool import ConnectionPool

# DeepAgents Imports
from deepagents import create_deep_agent
from deepagents.backends import StoreBackend

# 加载环境变量
load_dotenv(override=True)

# 数据库连接字符串 (请根据实际情况修改)
DB_URI = "postgresql://myuser:123456@localhost:5432/mydatabase"

def print_header():
print("\n" + "="*80)
print("DeepAgents StoreBackend (PostgreSQL) 演示 (极简版)")
print("="*80)

async def setup_mcp_tools():
"""
连接 Context7 MCP 服务器并获取工具。
"""
print("Step 1.1: 正在连接 Context7 MCP 服务器...")
try:
# 使用官方 Context7 MCP 配置
client = MultiServerMCPClient({
"context7": {
"transport": "stdio",
"command": "npx",
"args": ["-y", "@upstash/context7-mcp@latest"],
}
})

# 获取工具列表
tools = await client.get_tools()

print(f"Step 1.2: 成功加载 {len(tools)} 个 MCP 工具")
return client, tools
except Exception as e:
print(f"ERROR: 连接 MCP 失败: {e}")
return None, []


async def run_store_backend_demo():
"""
运行 StoreBackend 演示:
展示如何使用 PostgreSQL 作为 DeepAgents 的文件系统后端。
"""
print_header()

# =================================================================================================
# Step 1. 初始化 MCP 工具 (Connect to Context7 MCP Server)
# =================================================================================================
mcp_client, mcp_tools = await setup_mcp_tools()


# =================================================================================================
# Step 2. 初始化 PostgreSQL 连接池和存储组件 (Init DB & Store)
# =================================================================================================
print(f"\nStep 2: 连接数据库 {DB_URI} 并初始化存储...")

# 使用 ConnectionPool 管理数据库连接
# StoreBackend 需要同步的 ConnectionPool (因为它在线程中运行同步操作)
try:
with ConnectionPool(conninfo=DB_URI, kwargs={"autocommit": True}) as pool:

# 2.1 初始化 Checkpointer (用于保存会话状态/聊天记录)
# 注意: 这里使用 MemorySaver 是为了避开 langgraph-checkpoint-postgres 在某些环境下的异步兼容性问题
checkpointer = MemorySaver()

# 2.2 初始化 Store (用于保存文件/长期记忆)
# DeepAgents 的 StoreBackend 会将文件系统操作映射到这个 PostgresStore
store = PostgresStore(pool)

# 2.3 执行数据库迁移 (首次运行需要初始化表结构)
# 这会在数据库中创建 'store' 表,用于存储 JSON 数据 (即我们的文件)
with pool.connection() as conn:
with conn.cursor() as cur:
for migration in store.MIGRATIONS:
cur.execute(migration)

print("Step 2: 数据库表结构就绪")


# =================================================================================================
# Step 3. 创建基于 StoreBackend 的 Agent (Create Agent)
# =================================================================================================

# StoreBackend 将文件系统操作映射到 LangGraph 的 Store
# 这意味着文件将持久化存储在 PostgreSQL 数据库中,跨重启可用
llm = ChatOpenAI(model="gpt-4o", temperature=0)

# 定义 Backend Factory
# 这是一个工厂函数,用于在运行时(runtime)把 StoreBackend 实例化。
# create_deep_agent 会在内部把 LangGraph 的 runtime 对象作为参数 rt 传进来,
# 这样 StoreBackend 就能拿到 store/checkpointer 等资源,实现文件系统到 PostgreSQL 的映射。
backend_factory = lambda rt: StoreBackend(rt)
print("Step 3: 正在初始化 DeepAgent (Backend: StoreBackend -> PostgresStore)...")

agent = create_deep_agent(
model=llm,
tools=mcp_tools,
backend=backend_factory,
store=store, # 关键: 必须传入 store 实例
checkpointer=checkpointer, # 关键: 传入 checkpointer 实例
system_prompt="""你是一个高级技术助手。
你的任务是使用 Context7 工具查询关于 'DeepAgents StoreBackend' 的用法。
查询后,创建一个总结文件 '/knowledge/store_backend_notes.md',并写入关键信息。
由于你使用的是 StoreBackend,这个文件将直接存储在 PostgreSQL 数据库中。
最后,请读取该文件以验证存储成功。"""
)


# =================================================================================================
# Step 4. 执行任务 (Execute Task)
# =================================================================================================
thread_id = str(uuid.uuid4())
config = {"configurable": {"thread_id": thread_id}}

task = "请查询 StoreBackend 的用法,并将总结写入 /knowledge/store_backend_notes.md,最后读取它验证。"

# --- UI Display ------------------------------------------------------------------------------
print(f"Session Thread ID: {thread_id}")
print("\n" + "-"*40)
print(f"Step 4: 开始执行任务: {task}")
print("-"*40)
# ---------------------------------------------------------------------------------------------

step = 0
message_history_len = 0

try:
# 使用 astream 观察执行过程
async for event in agent.astream({"messages": [("user", task)]}, config=config):
if "messages" in event:
current_messages = event["messages"]
if len(current_messages) > message_history_len:
for i in range(message_history_len, len(current_messages)):
msg = current_messages[i]

# 1. 显示普通文本消息 (Agent 回复)
if isinstance(msg, BaseMessage) and msg.content:
is_tool_call = getattr(msg, "tool_calls", None)
if not is_tool_call:
print(f"\n[Agent]:\n{msg.content}")

# 2. 显示工具调用 (Agent 请求工具)
if hasattr(msg, "tool_calls") and msg.tool_calls:
step += 1
print(f"\n[Step {step}: 工具调用]:")
for tc in msg.tool_calls:
print(f" • 工具: {tc['name']}")
print(f" • 参数: {tc['args']}")

# 3. 显示工具输出 (Tool 执行结果)
if isinstance(msg, ToolMessage):
content_preview = msg.content[:200] + "..." if len(msg.content) > 200 else msg.content
print(f"\n[Tool Output ({msg.name})]: {content_preview}")

message_history_len = len(current_messages)

except Exception as e:
print(f"❌ 运行时错误: {e}")
traceback.print_exc()


# =================================================================================================
# Step 5. 验证持久化存储 (Verify Persistence)
# =================================================================================================
print("\n" + "="*50)
print("Step 5: 验证 StoreBackend (Postgres) 持久化")
print("="*50)

print("\n验证操作: 使用新 Agent 实例 (模拟重启) 读取同一文件...")

# 模拟重启:创建一个新的 Agent 实例,但连接同一个 Database Store
# 只要 DB 连接不变,文件系统状态就是持久的
verify_agent = create_deep_agent(
model=llm,
backend=backend_factory,
store=store,
checkpointer=checkpointer,
system_prompt="验证助手"
)

# 读取之前创建的文件
verify_result = await verify_agent.ainvoke({
"messages": [("user", "请读取 /knowledge/store_backend_notes.md 的内容")]
}, config=config)

last_msg = verify_result["messages"][-1]

# --- UI Display ------------------------------------------------------------------------------
print("\n[数据库读取验证结果]:")
print("-" * 20)
print(last_msg.content)
print("-" * 20)
print("\n说明: 'prefix' 对应 StoreBackend 的 namespace (默认为 'filesystem'),'key' 对应文件绝对路径。")

# SQL 查询提示
print("\n提示: 你可以使用以下 SQL 在数据库中直接查询此文件:")
sql_query = """
SELECT
key,
value->>'content' as content,
updated_at
FROM store
WHERE prefix = 'filesystem'
AND key = '/knowledge/store_backend_notes.md';
"""
print(sql_query.strip())
# ---------------------------------------------------------------------------------------------

print("\n演示结束")

except Exception as e:
print(f"❌ 数据库连接或初始化失败: {e}")
print("请检查 PostgreSQL 是否正在运行,以及 DB_URI 是否正确。")


if __name__ == "__main__":
# 确保 asyncio.run 兼容 Jupyter 环境
try:
# asyncio.run(run_store_backend_demo())
await run_store_backend_demo()
except RuntimeError as e:
if "asyncio.run() cannot be called from a running event loop" in str(e):
print("请在 Jupyter 中使用 await run_store_backend_demo()")
else:
raise e

================================================================================
DeepAgents StoreBackend (PostgreSQL) 演示 (极简版)
================================================================================
Step 1.1: 正在连接 Context7 MCP 服务器...
Step 1.2: 成功加载 2 个 MCP 工具

Step 2: 连接数据库 postgresql://myuser:123456@localhost:5432/mydatabase 并初始化存储...
Step 2: 数据库表结构就绪
Step 3: 正在初始化 DeepAgent (Backend: StoreBackend -> PostgresStore)...
Session Thread ID: 5cb70a7e-d3b2-49be-976c-d4eaf13091ad

----------------------------------------
Step 4: 开始执行任务: 请查询 StoreBackend 的用法,并将总结写入 /knowledge/store_backend_notes.md,最后读取它验证。
----------------------------------------

==================================================
Step 5: 验证 StoreBackend (Postgres) 持久化
==================================================

验证操作: 使用新 Agent 实例 (模拟重启) 读取同一文件...

[数据库读取验证结果]:
--------------------
Here is the content of `/knowledge/store_backend_notes.md`:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# DeepAgents StoreBackend Usage Summary

## Overview
The DeepAgents framework provides a flexible architecture for creating advanced agents capable of handling complex tasks. A key component of this architecture is the use of pluggable backends, which determine how agents interact with the filesystem and manage memory.

## StoreBackend
The `StoreBackend` is one of the pluggable backends available in DeepAgents. It is designed for persistent cross-conversation memory, allowing agents to maintain state and information across different interactions. This is particularly useful for applications requiring long-term memory retention, such as user preferences or knowledge bases.

### Integration Patterns
- **CompositeBackend**: Combines multiple backend types to route different paths to appropriate storage mechanisms. For example, ephemeral data can be stored in-memory while important data is persisted using `StoreBackend`.
- **FilesystemMiddleware**: Provides tools (`ls`, `read_file`, `write_file`, `edit_file`) for interacting with both short-term and long-term memory, enabling agents to manage context effectively.

### Core Capabilities
- **Long-term Memory**: Agents can use `StoreBackend` to save and retrieve information from previous conversations, ensuring continuity and context retention.
- **Context Management**: File system tools help offload large context to memory, preventing overflow and enabling work with variable-length tool results.

### Built-in Tools and Middleware
- **FilesystemMiddleware**: Offers a suite of tools for file operations, crucial for agents needing to persist or retrieve information.
- **SubAgentMiddleware**: Facilitates the use of sub-agents for task delegation, with guidance on when and how to use them effectively.

### Practical Applications
- **Persistent Storage**: Ideal for applications that require maintaining state across sessions, such as chatbots or virtual assistants.
- **Hybrid Memory**: Supports a mix of ephemeral and persistent storage, allowing for flexible memory management strategies.

## Example Usage
```python
from deepagents import create_deep_agent
from deepagents.backends import CompositeBackend, StateBackend, StoreBackend
from langgraph.store.memory import InMemoryStore

# Create a persistent store
store = InMemoryStore()

# Configure backend with persistent /memories/ directory using factory functions
backend = CompositeBackend(
default=lambda rt: StateBackend(rt),
routes={"/memories/": lambda rt: StoreBackend(rt)}
)

# Create agent with hybrid storage
agent = create_deep_agent(
store=store,
backend=backend,
system_prompt="""You have access to long-term memory. Files prefixed with
/memories/ persist across conversations."""
)

# First conversation
config1 = {"configurable": {"thread_id": "user-123"}}
agent.invoke({
"messages": [{
"role": "user",
"content": "Remember my favorite color is blue. Write to /memories/preferences.txt"
}]
}, config=config1)

# Later conversation - different thread, same memory
config2 = {"configurable": {"thread_id": "user-456"}}
result = agent.invoke({
"messages": [{"role": "user", "content": "What's my favorite color?"}]
}, config=config2)

# Agent reads from /memories/preferences.txt automatically
print(result["messages"][-1].content) # "Your favorite color is blue"
## Additional Features and Documentation - **Todo List Tracking**: Deepagents supports tracking multi-step tasks through a visual todo list. The agent can automatically create and update this list, providing a clear overview of the progress and remaining steps for complex operations. - **Web Research Skill**: This involves creating a dedicated folder for research files, analyzing the main research question by breaking it down into specific subtopics, and documenting this plan in a `research_plan.md` file. The plan should outline the main question, 2-5 subtopics, expected information for each, and how the findings will be synthesized. - **Synthesize Findings**: After all subagents have completed their tasks, the crucial step is to synthesize the collected information. Review the findings files saved locally by the subagents, and create a comprehensive response that directly answers the original research question, integrates insights from all subtopics, cites specific sources, and identifies any limitations or gaps in the research. - **LangGraph Documentation**: Provides access to LangGraph Python documentation to help answer questions and guide implementation. - **arXiv Search Skill**: Provides access to arXiv for finding preprints and recent research papers in various fields. ## Conclusion The `StoreBackend` in DeepAgents is a powerful feature for managing persistent memory across conversations, enhancing the agent's ability to handle complex, multi-step tasks with continuity and context awareness.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
    
If you need further assistance or modifications, feel free to ask!
--------------------

说明: 'prefix' 对应 StoreBackend 的 namespace (默认为 'filesystem'),'key' 对应文件绝对路径。

提示: 你可以使用以下 SQL 在数据库中直接查询此文件:
SELECT
key,
value->>'content' as content,
updated_at
FROM store
WHERE prefix = 'filesystem'
AND key = '/knowledge/store_backend_notes.md';

演示结束



```python
# 查看存入数据库中的结果
!psql -U myuser -d mydatabase -c "SELECT key, value->>'content' as content,updated_at FROM store WHERE prefix = 'filesystem' AND key = '/knowledge/store_backend_notes.md';"

key | content | updated_at -----------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------------------------- /knowledge/store_backend_notes.md | ["# DeepAgents StoreBackend Usage Summary", "", "## Overview", "The DeepAgents framework provides a flexible architecture for creating advanced agents capable of handling complex tasks. A key component of this architecture is the use of pluggable backends, which determine how agents interact with the filesystem and manage memory.", "", "## StoreBackend", "The `StoreBackend` is one of the pluggable backends available in DeepAgents. It is designed for persistent cross-conversation memory, allowing agents to maintain state and information across different interactions. This is particularly useful for applications requiring long-term memory retention, such as user preferences or knowledge bases.", "", "### Integration Patterns", "- **CompositeBackend**: Combines multiple backend types to route different paths to appropriate storage mechanisms. For example, ephemeral data can be stored in-memory while important data is persisted using `StoreBackend`.", "- **FilesystemMiddleware**: Provides tools (`ls`, `read_file`, `write_file`, `edit_file`) for interacting with both short-term and long-term memory, enabling agents to manage context effectively.", "", "### Core Capabilities", "- **Long-term Memory**: Agents can use `StoreBackend` to save and retrieve information from previous conversations, ensuring continuity and context retention.", "- **Context Management**: File system tools help offload large context to memory, preventing overflow and enabling work with variable-length tool results.", "", "### Built-in Tools and Middleware", "- **FilesystemMiddleware**: Offers a suite of tools for file operations, crucial for agents needing to persist or retrieve information.", "- **SubAgentMiddleware**: Facilitates the use of sub-agents for task delegation, with guidance on when and how to use them effectively.", "", "### Practical Applications", "- **Persistent Storage**: Ideal for applications that require maintaining state across sessions, such as chatbots or virtual assistants.", "- **Hybrid Memory**: Supports a mix of ephemeral and persistent storage, allowing for flexible memory management strategies.", "", "## Example Usage", "```python", "from deepagents import create_deep_agent", "from deepagents.backends import CompositeBackend, StateBackend, StoreBackend", "from langgraph.store.memory import InMemoryStore", "", "# Create a persistent store", "store = InMemoryStore()", "", "# Configure backend with persistent /memories/ directory using factory functions", "backend = CompositeBackend(", " default=lambda rt: StateBackend(rt),", " routes={\"/memories/\": lambda rt: StoreBackend(rt)}", ")", "", "# Create agent with hybrid storage", "agent = create_deep_agent(", " store=store,", " backend=backend,", " system_prompt=\"\"\"You have access to long-term memory. Files prefixed with", " /memories/ persist across conversations.\"\"\"", ")", "", "# First conversation", "config1 = {\"configurable\": {\"thread_id\": \"user-123\"}}", "agent.invoke({", " \"messages\": [{", " \"role\": \"user\",", " \"content\": \"Remember my favorite color is blue. Write to /memories/preferences.txt\"", " }]", "}, config=config1)", "", "# Later conversation - different thread, same memory", "config2 = {\"configurable\": {\"thread_id\": \"user-456\"}}", "result = agent.invoke({", " \"messages\": [{\"role\": \"user\", \"content\": \"What's my favorite color?\"}]", "}, config=config2)", "", "# Agent reads from /memories/preferences.txt automatically", "print(result[\"messages\"][-1].content) # \"Your favorite color is blue\"", "```", "", "## Additional Features and Documentation", "", "- **Todo List Tracking**: Deepagents supports tracking multi-step tasks through a visual todo list. The agent can automatically create and update this list, providing a clear overview of the progress and remaining steps for complex operations.", "", "- **Web Research Skill**: This involves creating a dedicated folder for research files, analyzing the main research question by breaking it down into specific subtopics, and documenting this plan in a `research_plan.md` file. The plan should outline the main question, 2-5 subtopics, expected information for each, and how the findings will be synthesized.", "", "- **Synthesize Findings**: After all subagents have completed their tasks, the crucial step is to synthesize the collected information. Review the findings files saved locally by the subagents, and create a comprehensive response that directly answers the original research question, integrates insights from all subtopics, cites specific sources, and identifies any limitations or gaps in the research.", "", "- **LangGraph Documentation**: Provides access to LangGraph Python documentation to help answer questions and guide implementation.", "", "- **arXiv Search Skill**: Provides access to arXiv for finding preprints and recent research papers in various fields.", "", "## Conclusion", "The `StoreBackend` in DeepAgents is a powerful feature for managing persistent memory across conversations, enhancing the agent's ability to handle complex, multi-step tasks with continuity and context awareness."] | 2025-12-17 11:11:10.305117+08 (1 row)
  • 这里看一下Backend vs Checkpointer vs Store 这三者的区别
参数组件 Backend (后端) Checkpointer (检查点) Store (存储)
核心定义 环境层 (Environment) 状态层 (State / Short-term Memory) 记忆层 (Memory / Long-term Memory)
负责什么? “外部世界” 的交互能力。
即:文件存在哪?代码在哪跑?
“当前对话” 的上下文。
即:刚才说了什么?现在运行到哪一步了?
“跨会话” 的知识积累。
即:用户叫什么名字?上次任务学到了什么?
数据类型 非结构化文件 (.py, .md, .txt)
运行时环境 (Shell, Process)
BaseMessage 列表 (User/AI/Tool Message)
Graph 节点状态
结构化 JSON 数据 (Key-Value)
用户偏好、长期笔记
典型实现 DockerBackend (容器)
FilesystemBackend (磁盘)
E2BBackend (云沙箱)
MemorySaver (内存)
PostgresSaver (数据库)
SqliteSaver (本地DB)
InMemoryStore (内存)
PostgresStore (数据库)
生命周期 任务级
(任务结束容器可能销毁)
线程级 (Thread)
(换个 thread_id 就没了)
全局级 (Global)
(所有 thread 都能查到)
形象比喻 工作台 / 电脑 大脑的工作记忆 (只会死记硬背当前对话) 日记本 / 知识库 (记录永久信息)

CompositeBackend 使用混合模式

CompositeBackend是 Agent 文件操作的“智能路由器” 。

在单一后端模式下,Agent 所有的文件操作(读、写、列出目录)都只能去往同一个地方(要么全是本地磁盘,要么全是 Docker 容器内)。而 CompositeBackend 允许你根据 文件路径前缀 ,将请求分发给不同的后端。

  • 核心优势
  1. 性能与开销优化 (Performance) 这是最关键的技术优势。

    • DockerBackend 的局限 : 向 Docker 容器内读写文件(尤其是大文件)需要经过 Docker Daemon 的 API (如 put_archive / get_archive ),涉及网络通信和打包解包,开销较大。

    • CompositeBackend 的解法 : 对于数据文件( /data ),直接通过 FilesystemBackend 进行本地 I/O 操作, 完全绕过了 Docker API ,读写速度是操作系统原生的速度。

  2. 计算与存储分离 (Decoupling)

    • 计算是临时的 : 你的 processor.py 脚本可能只需要运行一次,运行环境(Python 依赖)可能很复杂且容易冲突。放在 Docker 里最合适。

    • 数据是永恒的 : 你的 raw_metrics.txt 和 health_report.txt 是业务资产。通过路由直接落盘到宿主机,即使 Docker 容器崩溃、被删除或重启, 数据毫发无损 且立即可在宿主机访问(如代码 Line 212-222 所示的验证步骤)。

  3. 给予 Agent “混合云” 的能力,Agent 可以像人类工程师一样工作:

    • “我在临时的沙箱里写代码测试(Docker)。”

    • “测试好了,我把结果保存到公司的共享网盘里(Filesystem/Mount)。”

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import asyncio
import shutil
import os
import time
from pathlib import Path
from dotenv import load_dotenv

# DeepAgents 导入
from deepagents import create_deep_agent
from deepagents.backends.composite import CompositeBackend
from deepagents.backends.filesystem import FilesystemBackend
from langchain_openai import ChatOpenAI
from langchain_mcp_adapters.client import MultiServerMCPClient
from langchain_core.messages import BaseMessage, ToolMessage

# 导入 DockerBackend
try:
from docker_backend import DockerBackend
except ImportError:
try:
from deepagents.backends.docker import DockerBackend
except ImportError:
DockerBackend = None

def print_header():
print("\n" + "="*80)
print("DeepAgents CompositeBackend 混合后端演示 (极简版)")
print("架构:混合云原生模式 (Docker 执行 + 本地持久化)")
print("="*80)

async def setup_mcp_tools():
print(" → 正在连接 Context7 MCP 服务器...")
try:
client = MultiServerMCPClient({
"context7": {
"transport": "stdio",
"command": "npx",
"args": ["-y", "@upstash/context7-mcp@latest"],
}
})
tools = await client.get_tools()
print(" → MCP 工具加载成功")
return client, tools
except Exception as e:
print(f"ERROR: MCP 连接失败: {e}")
return None, []

async def run_composite_demo():
load_dotenv(override=True)
print_header()

if DockerBackend is None:
print("严重错误: 未找到 DockerBackend。请确保 docker_backend.py 存在。")
return

# Step 1
print("\n" + "-"*40)
print("步骤 1: 配置混合环境")
print("-"*40)

host_work_dir = Path("workspace/data_analysis_project").resolve()
if host_work_dir.exists():
shutil.rmtree(host_work_dir)
host_work_dir.mkdir(parents=True, exist_ok=True)
print(f" • 宿主机持久层: {host_work_dir}")

container_mount_path = "/data"
docker_volumes = {
str(host_work_dir): {'bind': container_mount_path, 'mode': 'rw'}
}
print(f" • 容器挂载: {host_work_dir}{container_mount_path}")

# Step 2
print("\n" + "-"*40)
print("步骤 2: 初始化混合后端 (Composite Backend)")
print("-"*40)

fs_backend = FilesystemBackend(root_dir=host_work_dir, virtual_mode=True)

print(" • 正在启动 Docker 容器 (python:3.11-slim)...")
docker_backend = DockerBackend(
image="python:3.11-slim",
auto_remove=True,
volumes=docker_volumes
)

routes = {
container_mount_path: fs_backend
}
backend = CompositeBackend(default=docker_backend, routes=routes)

print("\n[路由表配置]")
print(f"1. 默认路由 (/): DockerBackend (临时执行)")
print(f"2. 持久化路由 ({container_mount_path}/*): FilesystemBackend (宿主机存储)")

# Step 3
print("\n" + "-"*40)
print("步骤 3: 部署 Agent")
print("-"*40)

mcp_client, mcp_tools = await setup_mcp_tools()

system_prompt = f"""你是一名在混合环境中工作的高级数据工程师。

环境地图:
1. 执行层 (根目录 `/`):
- 临时的 Docker 容器。
- 用于创建脚本 (`.py`) 和运行命令。
- 这里的文会在会话结束后消失。

2. 存储层 (`{container_mount_path}`):
- 从宿主机挂载的持久化存储。
- 用于存放 输入 数据和 输出 报告。
- 这里的文件会永久保存。

你的任务:
1. **摄入**: 创建一个文件 `{container_mount_path}/raw_metrics.txt`,内容为 "CPU: 45%, Mem: 60%"。
(注意: 这使用了 'write_file' 工具,该工具通过路由直接写入宿主机文件系统)。

2. **处理**: 创建一个 Python 脚本 `/processor.py` (在根目录),该脚本:
- 读取 `{container_mount_path}/raw_metrics.txt`。
- 计算 "健康分数" (模拟一下即可)。
- 将报告写入 `{container_mount_path}/health_report.txt`。
- 打印 "Analysis Complete"。

3. **执行**: 使用 `python /processor.py` 运行脚本。
(注意: 这在 Docker 内部运行。Docker 因为卷挂载能看到这些文件)。

4. **验证**: 读取 `{container_mount_path}/health_report.txt` 并显示它。
"""

agent = create_deep_agent(
model=ChatOpenAI(model="gpt-4o", temperature=0),
tools=mcp_tools,
backend=backend,
system_prompt=system_prompt
)

# Step 4
print("\n" + "-"*40)
print("步骤 4: 任务执行")
print("-"*40)

task_input = "开始工程流水线。"
config = {"configurable": {"thread_id": "composite_demo_simple_v1"}}

step_count = 0
try:
message_history_len = 0

async for event in agent.astream({"messages": [("user", task_input)]}, config=config):
if "messages" in event:
current_messages = event["messages"]
if len(current_messages) > message_history_len:
for i in range(message_history_len, len(current_messages)):
msg = current_messages[i]

# Agent Thinking
if isinstance(msg, BaseMessage) and msg.content and not getattr(msg, "tool_calls", None):
step_count += 1
print(f"\n[🧠 Agent 思考 (步骤 {step_count})]:\n{msg.content}")

# Tool Calls
if hasattr(msg, "tool_calls") and msg.tool_calls:
step_count += 1
for tc in msg.tool_calls:
tool_name = tc['name']
args = tc['args']

# Routing logic visualization
target = "Docker 容器 🐳"
path_arg = args.get('file_path') or args.get('path')
if path_arg and str(path_arg).startswith(container_mount_path):
target = "宿主机文件系统 💾"

print(f"\n[🛠️ 工具执行 (步骤 {step_count})]:")
print(f" • 工具: {tool_name}")

# Special handling for code content
if tool_name == "write_file" and path_arg and str(path_arg).endswith(".py"):
code_content = args.get("content", "")
# Print args without content first
args_copy = args.copy()
args_copy['content'] = "(代码内容如下...)"
print(f" • 参数: {args_copy}")
print(f" • 路由: → {target}")
print(f" • 📝 写入代码内容:\n")
print("-" * 20)
print(code_content)
print("-" * 20)
else:
print(f" • 参数: {str(args)[:200] + '...' if len(str(args)) > 200 else args}")
print(f" • 路由: → {target}")

# Tool Outputs
if isinstance(msg, ToolMessage):
content = msg.content
if len(content) > 300:
content = content[:300] + "... [已截断]"
print(f"\n[↳ 输出]: {content}")

message_history_len = len(current_messages)

except Exception as e:
print(f"\n运行时错误: {e}")

# Step 5
print("\n" + "-"*40)
print("步骤 5: 宿主机侧验证")
print("-"*40)

report_path = host_work_dir / "health_report.txt"
raw_path = host_work_dir / "raw_metrics.txt"

if raw_path.exists():
print(f"✅ 原始数据已找到: {raw_path} (通过直接 FS 路由创建)")
else:
print(f"❌ 原始数据丢失: {raw_path}")

if report_path.exists():
content = report_path.read_text()
print(f"\n🏆 持久化验证成功! 文件: {report_path}")
print("内容:")
print("-" * 20)
print(content)
print("-" * 20)
else:
print(f"❌ 报告丢失: {report_path}")

# Step 6
print("\n正在关闭基础设施...")
if 'docker_backend' in locals() and hasattr(docker_backend, "close"):
docker_backend.close()
print(" • Docker 容器已终止")

print("\n✨ 演示圆满完成!")

if __name__ == "__main__":
try:
# asyncio.run(run_composite_demo())
await run_composite_demo()
except RuntimeError as e:
if "asyncio.run() cannot be called from a running event loop" in str(e):
print("检测到正在运行的事件循环。请在单元格中使用 'await run_composite_demo()'。")
else:
raise e

================================================================================
DeepAgents CompositeBackend 混合后端演示 (极简版)
架构:混合云原生模式 (Docker 执行 + 本地持久化)
================================================================================

----------------------------------------
步骤 1: 配置混合环境
----------------------------------------
  • 宿主机持久层: /Users/mac/PycharmProjects/JupyterProject/LangChainProject/DeepAgents/workspace/data_analysis_project
  • 容器挂载:      /Users/mac/PycharmProjects/JupyterProject/LangChainProject/DeepAgents/workspace/data_analysis_project ↔ /data

----------------------------------------
步骤 2: 初始化混合后端 (Composite Backend)
----------------------------------------
  • 正在启动 Docker 容器 (python:3.11-slim)...

[路由表配置]
1. 默认路由 (/): DockerBackend (临时执行)
2. 持久化路由 (/data/*): FilesystemBackend (宿主机存储)

----------------------------------------
步骤 3: 部署 Agent
----------------------------------------
  → 正在连接 Context7 MCP 服务器...
  → MCP 工具加载成功

----------------------------------------
步骤 4: 任务执行
----------------------------------------

----------------------------------------
步骤 5: 宿主机侧验证
----------------------------------------
✅ 原始数据已找到: /Users/mac/PycharmProjects/JupyterProject/LangChainProject/DeepAgents/workspace/data_analysis_project/raw_metrics.txt (通过直接 FS 路由创建)

🏆 持久化验证成功! 文件: /Users/mac/PycharmProjects/JupyterProject/LangChainProject/DeepAgents/workspace/data_analysis_project/health_report.txt
内容:
--------------------
Health Score: 47.5

--------------------

正在关闭基础设施...
  • Docker 容器已终止

✨ 演示圆满完成!

5、interrupt_on参数

5.1 Human-in-the-loop(HITL)人工干预

  1. **Human-in-the-loop (HITL)**:对于 write_filedelegate_task 等关键操作,利用 LangGraph 的中断机制加入人工审批。

  2. 异步运行:DeepAgent 的任务通常耗时较长,务必使用异步 Webhooks 接收结果。

  3. 监控与调试:强烈建议结合 LangSmith 使用。由于 DeepAgent 内部有复杂的子 Agent 递归调用,使用 LangSmith 的 Tracing 功能是排查问题的有效手段。

  4. 后端挂载:在生产环境中,建议将 VFS 挂载到云端存储(如 S3),以防止容器重启导致 Agent 的”记忆”丢失。

  5. interrupt_on:这个参数其实是一个HITL的开关,就是把HITL的中间件插入到DeepAgent的执行流程中,当DeepAgent执行到需要人工审批的操作时,就会中断执行,等待人工审批。

  • 类型 : dict[str, bool | InterruptOnConfig]

  • 作用 : 映射“工具名称”到“中断配置”。

  • 示例 : interrupt_on={“write_file”: True} 表示当 Agent 试图调用 write_file 工具时,程序会暂停(Suspend),等待人工(Human)在 LangGraph 层面进行 Approve 、 Reject 或 Edit 操作后才能继续。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import os
import asyncio
from typing import Optional, Set
from langchain_openai import ChatOpenAI
from deepagents import create_deep_agent
from deepagents.backends import FilesystemBackend
from langgraph.checkpoint.memory import InMemorySaver
from dotenv import load_dotenv
from langchain_tavily import TavilySearch
from langchain.agents.middleware.human_in_the_loop import (
HITLResponse,
ApproveDecision,
EditDecision,
RejectDecision
)
from langgraph.types import Command
from langchain_core.messages import BaseMessage, ToolMessage, AIMessage

load_dotenv(override=True)

async def run_interrupt_test():
"""
示例 1: 基础中断功能 (封装版)
在工具调用前中断,让用户确认是否继续执行
"""
print("\n" + "="*80)
print("📚 示例 1: interrupt_on 使用")
print("="*80)
print("\n功能:在工具调用前暂停,等待人工确认\n")

# 创建 LLM 和工具
llm = ChatOpenAI(model="gpt-4o", temperature=0)
search_tool = TavilySearch(max_results=2)

# 创建 Agent,设置在 "tools" 节点中断
agent = create_deep_agent(
model=llm,
tools=[search_tool],
backend=FilesystemBackend(root_dir="./workspace",virtual_mode=True),
checkpointer=InMemorySaver(), # 必需!用于支持中断和恢复
interrupt_on={"tavily_search": True}, # 在特定工具调用时中断
)

# 定义任务
task = "搜索 'Python 异步编程' 的最新信息,并创建一个总结文件"

# 配置会话 ID
# 为了避免之前的状态干扰,我们使用一个新的 thread_id
config = {"configurable": {"thread_id": "demo_basic_refactored_v1"}}

print(f"📋 任务: {task}\n")
print("🚀 开始执行...\n")

# 追踪已打印的消息数量,避免重复打印
message_history_len = 0

# --- 第一次执行 ---
print("【第一次执行 - 预期会中断】")
async for event in agent.astream({"messages": [("user", task)]}, config=config):
if "messages" in event:
current_messages = event["messages"]
if len(current_messages) > message_history_len:
# 打印新增的消息
for i in range(message_history_len, len(current_messages)):
msg = current_messages[i]
if msg.type == "ai":
if hasattr(msg, 'tool_calls') and msg.tool_calls:
print(f"🔧 AI 决定调用工具: {msg.tool_calls[0]['name']}")
print(f" 参数: {msg.tool_calls[0]['args']}")
elif msg.content:
print(f"💬 AI: {msg.content}")
elif msg.type == "tool":
print(f"✅ 工具输出: {msg.content[:100]}..." if len(msg.content) > 100 else f"✅ 工具输出: {msg.content}")

message_history_len = len(current_messages)

# 检查是否中断
# 使用 aget_state (async) 获取状态
state = await agent.aget_state(config)
print(f"\n⏸️ 执行状态: {state.next}")

if state.tasks:
print(f"\n--- 🛑 执行已暂停 (HITL Middleware) ---")
print(f"下一步骤 (Next): {state.next}")

last_message = state.values["messages"][-1]

if hasattr(last_message, "tool_calls") and last_message.tool_calls:
tool_call = last_message.tool_calls[0]
print(f"\n[待审批操作]:")
print(f" - 工具: {tool_call['name']}")
print(f" - 参数: {tool_call['args']}")

# === 人工介入 ===
approval = input("\n[管理员]: 是否批准执行此操作? (y/n/e[编辑]): ")

if approval.lower() == 'y':
print("\n[系统]: 操作已批准,继续执行...")

hitl_response = HITLResponse(
decisions=[ApproveDecision(type="approve")]
)

# === 恢复执行 ===
# 使用 Command(resume=...)
async for event in agent.astream(
Command(resume=hitl_response),
config=config,
stream_mode="values"
):
if "messages" in event:
current_messages = event["messages"]
if len(current_messages) > message_history_len:
for i in range(message_history_len, len(current_messages)):
msg = current_messages[i]

# 优化打印逻辑,清晰展示 AI 回复
if msg.type == "tool":
print(f"\n[工具输出]:\n{msg.content[:300]}..." if len(msg.content) > 300 else f"\n[工具输出]:\n{msg.content}")
elif msg.type == "ai":
if msg.content:
print(f"\n[AI 回复]:\n{msg.content}\n")
elif msg.tool_calls:
print(f"\n🔧 AI 决定调用工具: {msg.tool_calls[0]['name']}")
print(f" 参数: {msg.tool_calls[0]['args']}")

message_history_len = len(current_messages)

else:
print("\n[系统]: 操作被拒绝或您选择了其他选项 (本演示仅处理 'y')。")
else:
print("流程已完成,没有触发中断。")
if state.values.get("messages"):
last_msg = state.values["messages"][-1]
if last_msg.type == "ai" and last_msg.content:
print(f"\n[最终回复]: {last_msg.content}")

if __name__ == "__main__":
try:
# asyncio.run(run_interrupt_test())
await run_interrupt_test()
except KeyboardInterrupt:
print("\n程序已停止")

================================================================================
📚 示例 1: interrupt_on 使用
================================================================================

功能:在工具调用前暂停,等待人工确认

📋 任务: 搜索 'Python 异步编程' 的最新信息,并创建一个总结文件

🚀 开始执行...

【第一次执行 - 预期会中断】

⏸️  执行状态: ('HumanInTheLoopMiddleware.after_model',)

--- 🛑 执行已暂停 (HITL Middleware) ---
下一步骤 (Next): ('HumanInTheLoopMiddleware.after_model',)

[待审批操作]:
  - 工具: tavily_search
  - 参数: {'query': 'Python 异步编程', 'time_range': 'month', 'topic': 'general'}

[系统]: 操作已批准,继续执行...

🔧 AI 决定调用工具: tavily_search
   参数: {'query': 'Python 异步编程', 'time_range': 'month', 'topic': 'general'}

[工具输出]:
{"query": "Python 异步编程", "follow_up_questions": null, "answer": null, "images": [], "results": [{"url": "https://comate.baidu.com/zh/page/g6hytf7u8qi", "title": "Python异步编程:从基础到高阶的完整实现指南_文心快码 - Comate", "content": "在Python中,异步编程通过 async/await 语法和 asyncio 库实现,能够显著提升I/O密集型任务的性能。本文系统梳理了异步函数的核心机制、并发控制方法...

🔧 AI 决定调用工具: write_file
   参数: {'file_path': '/Python_Async_Programming_Summary.txt', 'content': '# Python 异步编程最新信息总结\n\n## 1. Python异步编程:从基础到高阶的完整实现指南\n- **来源**: [文心快码](https://comate.baidu.com/zh/page/g6hytf7u8qi)\n- **内容概述**: 在Python中,异步编程通过 `async/await` 语法和 `asyncio` 库实现,能够显著提升I/O密集型任务的性能。本文系统梳理了异步函数的核心机制、并发控制方法及典型应用场景。\n\n## 2. Python异步编程:核心方法与高效IO并发实践\n- **来源**: [文心快码](https://comate.baidu.com/zh/page/2dgqb86weoh)\n- **内容概述**: Python异步编程通过 `async/await` 语法、事件循环和任务管理机制,显著提升了高IO密集型应用的性能。本文详细梳理了协程定义、并发控制、异步IO操作等核心方法。'}

[工具输出]:
Updated file /Python_Async_Programming_Summary.txt

[AI 回复]:
我已经为您创建了一个总结文件,包含了关于Python异步编程的最新信息。您可以在文件 `/Python_Async_Programming_Summary.txt` 中查看详细内容。

6、总结

DeepAgents 采用 中间件模式 来增强 Agent 能力,这些中间件在 create_deep_agent 时自动装配:

  • FilesystemMiddleware :

    • 提供 ls , read_file , write_file , edit_file , glob , grep , execute 等标准工具。

    • 亮点功能 : 大结果自动转存 。当工具(如搜索或爬虫)返回内容过长时,自动截断并保存到文件系统,仅返回文件路径给 LLM,极大节省 Token 并防止 Crash。

  • TodoListMiddleware :

    • 拦截 LLM 输出的 todo_list ,将其解析为结构化状态。

    • 支持任务的增删改查(CRUD)和状态流转(Pending -> Completed)。

  • SubAgentMiddleware :

    • 自动创建 task 工具。

    • 支持 General Purpose Agent (通用分身)和 Custom Agent (专家分身)。

    • 实现父子 Agent 间通过文件系统交换数据,无需序列化传递大量文本。

  • HumanInTheLoopMiddleware :

    • 通过 interrupt_on 参数配置。

    • 支持在特定工具调用前(如 write_file , execute )暂停,等待人工审批、修改或拒绝。

  • PatchToolCallsMiddleware :

    • 自动检测并修复 LLM 产生的“悬空”工具调用(Dangling Tool Calls),增强稳定性。

一句话总结 :DeepAgents 是 LangGraph 的 最佳实践参考实现 ,它将构建“类人智能体”所需的规划、记忆、工具使用等复杂逻辑,封装成了标准化的基础设施,让开发者专注于业务逻辑而非底层编排。