> ## Documentation Index
> Fetch the complete documentation index at: https://cndoc-langchain.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Deep Agents 概述

> 构建能够规划、使用子代理并利用文件系统处理复杂任务的代理

开始构建由大语言模型驱动的代理和应用程序的最简单方式——内置任务规划、用于上下文管理的文件系统、子代理生成和长期记忆功能。
您可以将深度代理用于任何任务，包括复杂的多步骤任务。

我们将 `deepagents` 视为一种["代理工具集"](/oss/python/concepts/products#agent-harnesses-like-the-deep-agents-sdk)。它与其他代理框架具有相同的核心工具调用循环，但内置了工具和功能。

[`deepagents`](https://pypi.org/project/deepagents/) 是一个独立的库，构建在 [LangChain](/oss/python/langchain/) 的代理核心构建模块之上。它使用 [LangGraph](/oss/python/langgraph/) 运行时实现持久执行、流式处理、人机协作等功能。

[`deepagents` 仓库](https://github.com/langchain-ai/deepagents) 包含：

* **Deep Agents SDK**：一个用于构建能处理任何任务的代理的包
* [**Deep Agents CLI**](/oss/python/deepagents/cli)：基于 Deep Agents SDK 构建的终端编码代理
* [**ACP 集成**](/oss/python/deepagents/acp)：用于在 Zed 等代码编辑器中使用深度代理的 Agent Client Protocol 连接器

[LangChain](/oss/python/langchain/) 是为您的代理提供核心构建模块的框架。
要了解更多关于 LangChain、LangGraph 和 Deep Agents 之间的区别，请参阅[框架、运行时和工具集](/oss/python/concepts/products)。

## <Icon icon="wand" /> 创建深度代理

<Tabs>
  <Tab title="Google">
    ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    # pip install -qU deepagents langchain-google-genai
    from deepagents import create_deep_agent

    def get_weather(city: str) -> str:
        """获取给定城市的天气。"""
        return f"It's always sunny in {city}!"

    agent = create_deep_agent(
        model="google_genai:gemini-3.1-pro-preview",
        tools=[get_weather],
        system_prompt="You are a helpful assistant",
    )

    # 运行代理
    agent.invoke(
        {"messages": [{"role": "user", "content": "what is the weather in sf"}]}
    )
    ```
  </Tab>

  <Tab title="OpenAI">
    ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    # pip install -qU deepagents langchain-openai
    from deepagents import create_deep_agent

    def get_weather(city: str) -> str:
        """获取给定城市的天气。"""
        return f"It's always sunny in {city}!"

    agent = create_deep_agent(
        model="openai:gpt-5.4",
        tools=[get_weather],
        system_prompt="You are a helpful assistant",
    )

    # 运行代理
    agent.invoke(
        {"messages": [{"role": "user", "content": "what is the weather in sf"}]}
    )
    ```
  </Tab>

  <Tab title="Anthropic">
    ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    # pip install -qU deepagents langchain-anthropic
    from deepagents import create_deep_agent

    def get_weather(city: str) -> str:
        """获取给定城市的天气。"""
        return f"It's always sunny in {city}!"

    agent = create_deep_agent(
        model="anthropic:claude-sonnet-4-6",
        tools=[get_weather],
        system_prompt="You are a helpful assistant",
    )

    # 运行代理
    agent.invoke(
        {"messages": [{"role": "user", "content": "what is the weather in sf"}]}
    )
    ```
  </Tab>

  <Tab title="OpenRouter">
    ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    # pip install -qU deepagents langchain-openrouter
    from deepagents import create_deep_agent

    def get_weather(city: str) -> str:
        """获取给定城市的天气。"""
        return f"It's always sunny in {city}!"

    agent = create_deep_agent(
        model="openrouter:anthropic/claude-sonnet-4-6",
        tools=[get_weather],
        system_prompt="You are a helpful assistant",
    )

    # 运行代理
    agent.invoke(
        {"messages": [{"role": "user", "content": "what is the weather in sf"}]}
    )
    ```
  </Tab>

  <Tab title="Fireworks">
    ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    # pip install -qU deepagents langchain-fireworks
    from deepagents import create_deep_agent

    def get_weather(city: str) -> str:
        """获取给定城市的天气。"""
        return f"It's always sunny in {city}!"

    agent = create_deep_agent(
        model="fireworks:accounts/fireworks/models/qwen3p5-397b-a17b",
        tools=[get_weather],
        system_prompt="You are a helpful assistant",
    )

    # 运行代理
    agent.invoke(
        {"messages": [{"role": "user", "content": "what is the weather in sf"}]}
    )
    ```
  </Tab>

  <Tab title="Baseten">
    ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    # pip install -qU deepagents langchain-baseten
    from deepagents import create_deep_agent

    def get_weather(city: str) -> str:
        """获取给定城市的天气。"""
        return f"It's always sunny in {city}!"

    agent = create_deep_agent(
        model="baseten:zai-org/GLM-5",
        tools=[get_weather],
        system_prompt="You are a helpful assistant",
    )

    # 运行代理
    agent.invoke(
        {"messages": [{"role": "user", "content": "what is the weather in sf"}]}
    )
    ```
  </Tab>

  <Tab title="Ollama">
    ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    # pip install -qU deepagents langchain-ollama
    from deepagents import create_deep_agent

    def get_weather(city: str) -> str:
        """获取给定城市的天气。"""
        return f"It's always sunny in {city}!"

    agent = create_deep_agent(
        model="ollama:devstral-2",
        tools=[get_weather],
        system_prompt="You are a helpful assistant",
    )

    # 运行代理
    agent.invoke(
        {"messages": [{"role": "user", "content": "what is the weather in sf"}]}
    )
    ```
  </Tab>
</Tabs>

请参阅[快速入门](/oss/python/deepagents/quickstart/)和[自定义指南](/oss/python/deepagents/customization/)，开始使用 Deep Agents 构建您自己的代理和应用程序。

<Tip>
  使用 [LangSmith](/langsmith/home) 追踪请求、调试代理行为并评估输出。设置 `LANGSMITH_TRACING=true` 和您的 API 密钥即可开始。
</Tip>

## 何时使用 Deep Agents

当您想要构建能够执行以下操作的代理时，请使用 **Deep Agents SDK**：

* **处理需要规划和分解的复杂多步骤任务**
* **通过文件系统工具和[摘要功能](/oss/python/deepagents/context-engineering#summarization)管理大量上下文**
* **切换文件系统后端**以使用内存状态、本地磁盘、持久存储、[沙箱](/oss/python/deepagents/sandboxes)或[您自己的自定义后端](/oss/python/deepagents/backends)
* **在使用[沙箱后端](/oss/python/deepagents/sandboxes)时通过 `execute` 工具执行 shell 命令**
* **将工作委派给专门的子代理**以实现上下文隔离
* **跨对话和线程持久化记忆**
* **使用声明式[权限规则](/oss/python/deepagents/permissions)控制文件系统访问**，限制代理可以读取或写入的文件
* **通过[人机协作](/oss/python/deepagents/human-in-the-loop)工作流要求敏感操作的人工批准**
* **使用任何模型**——[与提供商无关](/oss/python/deepagents/models)，支持前沿模型和开源模型

对于构建更简单的代理，请考虑使用 LangChain 的 [`create_agent`](/oss/python/langchain/agents) 或构建自定义 [LangGraph](/oss/python/langgraph/overview) 工作流。

## 核心功能

<Card title="规划和任务分解" icon="timeline">
  Deep Agents 包含一个内置的 [`write_todos`](/oss/python/langchain/middleware/built-in#to-do-list) 工具，使代理能够将复杂任务分解为离散步骤、跟踪进度并在新信息出现时调整计划。
</Card>

<Card title="上下文管理" icon="scissors">
  文件系统工具（[`ls`](/oss/python/deepagents/harness#virtual-filesystem-access)、[`read_file`](/oss/python/deepagents/harness#virtual-filesystem-access)、[`write_file`](/oss/python/deepagents/harness#virtual-filesystem-access)、[`edit_file`](/oss/python/deepagents/harness#virtual-filesystem-access)）允许代理将大量上下文卸载到内存或文件系统存储，防止上下文窗口溢出，并支持处理可变长度的工具结果。当上下文窗口变长时，自动摘要功能会压缩较旧的对话消息，使代理在长时间会话中保持有效。
</Card>

<Card title="Shell 执行" icon="terminal">
  使用[沙箱后端](/oss/python/deepagents/sandboxes)时，代理会获得一个 `execute` 工具来运行 shell 命令，用于测试、构建、git 操作和系统任务。沙箱后端提供隔离，使代理可以执行代码而不会危及您的主机系统。
</Card>

<Card title="可插拔文件系统后端" icon="plug">
  虚拟文件系统由[可插拔后端](/oss/python/deepagents/backends)驱动，您可以根据用例进行切换。选择包括内存状态、本地磁盘、用于跨线程持久化的 LangGraph 存储、用于隔离代码执行的[沙箱](/oss/python/deepagents/sandboxes)（Modal、Daytona、Deno），或使用组合路由组合多个后端。您也可以实现自己的自定义后端。
</Card>

<Card title="子代理生成" icon="users-group">
  内置的 `task` 工具使代理能够生成专门的子代理以实现上下文隔离。这保持了主代理上下文的清晰，同时仍然可以深入处理特定子任务。
</Card>

<Card title="长期记忆" icon="database">
  使用 LangGraph 的[内存存储](/oss/python/langgraph/persistence#memory-store)扩展代理，实现跨线程的持久记忆。代理可以保存和检索先前对话中的信息。
</Card>

<Card title="文件系统权限" icon="lock">
  声明[权限规则](/oss/python/deepagents/permissions)来控制代理可以读取或写入的文件和目录。子代理可以继承或覆盖父级的规则。
</Card>

<Card title="人机协作" icon="user-check">
  使用 LangGraph 的中断功能配置敏感工具操作的[人工批准](/oss/python/deepagents/human-in-the-loop)。控制哪些工具在执行前需要确认。
</Card>

<Card title="技能" icon="puzzle">
  使用可重用的[技能](/oss/python/deepagents/skills)扩展代理，这些技能提供专门的工作流、领域知识和自定义指令。
</Card>

<Card title="智能默认值" icon="wand">
  附带经过深思熟虑的系统提示，教导模型如何有效使用其工具——行动前规划、验证工作并管理上下文。根据需要自定义或替换默认值。
</Card>

## 开始使用

<CardGroup cols={2}>
  <Card title="快速入门" icon="rocket" href="/oss/python/deepagents/quickstart">
    构建您的第一个深度代理
  </Card>

  <Card title="自定义" icon="adjustments" href="/oss/python/deepagents/customization">
    了解自定义选项
  </Card>

  <Card title="模型" icon="cpu" href="/oss/python/deepagents/models">
    配置模型和提供商
  </Card>

  <Card title="后端" icon="plug" href="/oss/python/deepagents/backends">
    选择和配置可插拔文件系统后端
  </Card>

  <Card title="沙箱" icon="cube" href="/oss/python/deepagents/sandboxes">
    在隔离环境中执行代码
  </Card>

  <Card title="权限" icon="lock" href="/oss/python/deepagents/permissions">
    使用权限规则控制文件系统访问
  </Card>

  <Card title="人机协作" icon="user-check" href="/oss/python/deepagents/human-in-the-loop">
    配置敏感操作的批准
  </Card>

  <Card title="CLI" icon="terminal" href="/oss/python/deepagents/cli/overview">
    使用 Deep Agents CLI
  </Card>

  <Card title="ACP" icon="plug-connected" href="/oss/python/deepagents/acp">
    通过 ACP 在代码编辑器中使用深度代理
  </Card>

  <Card title="参考" icon="external-link" href="https://reference.langchain.com/python/deepagents/">
    查看 `deepagents` API 参考
  </Card>
</CardGroup>

***

<div className="source-links">
  <Callout icon="terminal-2">
    [将这些文档](/use-these-docs)通过 MCP 连接到 Claude、VSCode 等，以获取实时答案。
  </Callout>

  <Callout icon="edit">
    [在 GitHub 上编辑此页面](https://github.com/langchain-ai/docs/edit/main/src/oss/deepagents/overview.mdx)或[提交问题](https://github.com/langchain-ai/docs/issues/new/choose)。
  </Callout>
</div>
