> ## 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.

# 快速入门

> 在几分钟内构建你的第一个深度代理

本指南将引导你创建第一个具备规划、文件系统工具和子代理能力的深度代理。你将构建一个能够进行研究并撰写报告的研究代理。

<Tip>
  **正在使用 AI 编程助手？**

  * 安装 [LangChain 文档 MCP 服务器](/use-these-docs)，让你的代理能够访问最新的 LangChain 文档和示例。
  * 安装 [LangChain 技能](https://github.com/langchain-ai/langchain-skills)，以提升你的代理在 LangChain 生态系统任务上的表现。
</Tip>

## 前提条件

在开始之前，请确保你拥有来自模型提供商（例如 Gemini、Anthropic、OpenAI）的 API 密钥。

<Note>
  深度代理需要支持[工具调用](/oss/python/langchain/models#tool-calling)的模型。有关如何配置模型，请参阅[自定义](/oss/python/deepagents/customization#model)。
</Note>

## 步骤 1：安装依赖

<CodeGroup>
  ```bash pip theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  pip install deepagents tavily-python
  ```

  ```bash uv theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  uv init
  uv add deepagents tavily-python
  uv sync
  ```
</CodeGroup>

<Note>
  本指南使用 [Tavily](https://tavily.com/) 作为搜索提供商的示例，但你可以替换为任何搜索 API（例如 DuckDuckGo、SerpAPI、Brave Search）。
</Note>

## 步骤 2：设置你的 API 密钥

<Tabs>
  <Tab title="Google">
    ```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    export GOOGLE_API_KEY="your-api-key"
    export TAVILY_API_KEY="your-tavily-api-key"
    ```
  </Tab>

  <Tab title="OpenAI">
    ```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    export OPENAI_API_KEY="your-api-key"
    export TAVILY_API_KEY="your-tavily-api-key"
    ```
  </Tab>

  <Tab title="Anthropic">
    ```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    export ANTHROPIC_API_KEY="your-api-key"
    export TAVILY_API_KEY="your-tavily-api-key"
    ```
  </Tab>

  <Tab title="OpenRouter">
    ```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    export OPENROUTER_API_KEY="your-api-key"
    export TAVILY_API_KEY="your-tavily-api-key"
    ```
  </Tab>

  <Tab title="Fireworks">
    ```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    export FIREWORKS_API_KEY="your-api-key"
    export TAVILY_API_KEY="your-tavily-api-key"
    ```
  </Tab>

  <Tab title="Baseten">
    ```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    export BASETEN_API_KEY="your-api-key"
    export TAVILY_API_KEY="your-tavily-api-key"
    ```
  </Tab>

  <Tab title="Ollama">
    ```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    # 本地：Ollama 必须在你的机器上运行
    # 云：为你托管的推理设置 Ollama API 密钥
    export OLLAMA_API_KEY="your-api-key"
    export TAVILY_API_KEY="your-tavily-api-key"
    ```
  </Tab>

  <Tab title="其他">
    ```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    # 设置你的提供商的 API 密钥
    export <PROVIDER>_API_KEY="your-api-key"
    export TAVILY_API_KEY="your-tavily-api-key"
    ```

    深度代理适用于任何 [LangChain 聊天模型](/oss/python/deepagents/models#supported-models)。请设置你的提供商的 API 密钥。
  </Tab>
</Tabs>

## 步骤 3：创建搜索工具

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import os
from typing import Literal
from tavily import TavilyClient
from deepagents import create_deep_agent

tavily_client = TavilyClient(api_key=os.environ["TAVILY_API_KEY"])

def internet_search(
    query: str,
    max_results: int = 5,
    topic: Literal["general", "news", "finance"] = "general",
    include_raw_content: bool = False,
):
    """运行网络搜索"""
    return tavily_client.search(
        query,
        max_results=max_results,
        include_raw_content=include_raw_content,
        topic=topic,
    )
```

## 步骤 4：创建深度代理

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
# 指导代理成为专家研究员的系统提示
research_instructions = """你是一名专家研究员。你的工作是进行彻底的研究，然后撰写一份精炼的报告。

你可以使用互联网搜索工具作为收集信息的主要手段。

## `internet_search`

使用此工具对给定查询运行互联网搜索。你可以指定返回的最大结果数、主题以及是否应包含原始内容。
"""
```

传入 `provider:model` 格式的 `model` 字符串，或已初始化的模型实例。有关所有提供商，请参阅[支持的模型](/oss/python/deepagents/models#supported-models)，有关经过测试的推荐，请参阅[建议的模型](/oss/python/deepagents/models#suggested-models)。

<Tabs>
  <Tab title="Google">
    ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    agent = create_deep_agent(
        model="google_genai:gemini-3.1-pro-preview",
        tools=[internet_search],
        system_prompt=research_instructions,
    )
    ```
  </Tab>

  <Tab title="OpenAI">
    ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    agent = create_deep_agent(
        model="openai:gpt-5.4",
        tools=[internet_search],
        system_prompt=research_instructions,
    )
    ```
  </Tab>

  <Tab title="Anthropic">
    ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    agent = create_deep_agent(
        model="anthropic:claude-sonnet-4-6",
        tools=[internet_search],
        system_prompt=research_instructions,
    )
    ```
  </Tab>

  <Tab title="OpenRouter">
    ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    agent = create_deep_agent(
        model="openrouter:anthropic/claude-sonnet-4-6",
        tools=[internet_search],
        system_prompt=research_instructions,
    )
    ```
  </Tab>

  <Tab title="Fireworks">
    ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    agent = create_deep_agent(
        model="fireworks:accounts/fireworks/models/qwen3p5-397b-a17b",
        tools=[internet_search],
        system_prompt=research_instructions,
    )
    ```
  </Tab>

  <Tab title="Baseten">
    ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    agent = create_deep_agent(
        model="baseten:zai-org/GLM-5",
        tools=[internet_search],
        system_prompt=research_instructions,
    )
    ```
  </Tab>

  <Tab title="Ollama">
    ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    agent = create_deep_agent(
        model="ollama:devstral-2",
        tools=[internet_search],
        system_prompt=research_instructions,
    )
    ```
  </Tab>

  <Tab title="其他">
    传入任何[支持的模型字符串](/oss/python/deepagents/models#supported-models)，或已初始化的模型实例：

    <CodeGroup>
      ```python 模型字符串 theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
      agent = create_deep_agent(
          model="provider:model-name",
          tools=[internet_search],
          system_prompt=research_instructions,
      )
      ```

      ```python init_chat_model theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
      from langchain.chat_models import init_chat_model

      model = init_chat_model("provider:model-name")
      agent = create_deep_agent(
          model=model,
          tools=[internet_search],
          system_prompt=research_instructions,
      )
      ```

      ```python 模型类 theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
      from langchain_<provider> import Chat<Provider>

      model = Chat<Provider>(model="model-name")
      agent = create_deep_agent(
          model=model,
          tools=[internet_search],
          system_prompt=research_instructions,
      )
      ```
    </CodeGroup>
  </Tab>
</Tabs>

## 步骤 5：运行代理

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
result = agent.invoke({"messages": [{"role": "user", "content": "What is langgraph?"}]})

# 打印代理的响应
print(result["messages"][-1].content)
```

## 工作原理

你的深度代理会自动：

1. **规划其方法**：使用内置的 [`write_todos`](/oss/python/deepagents/harness#planning-capabilities) 工具来分解研究任务。
2. **进行研究**：调用 `internet_search` 工具来收集信息。
3. **管理上下文**：使用文件系统工具（[`write_file`](/oss/python/deepagents/harness#virtual-filesystem-access)、[`read_file`](/oss/python/deepagents/harness#virtual-filesystem-access)）来卸载大型搜索结果。
4. **按需生成子代理**：将复杂的子任务委派给专门的子代理。
5. **综合报告**：将发现编译成连贯的响应。

## 示例

有关你可以使用深度代理构建的代理、模式和应用，请参阅[示例](https://github.com/langchain-ai/deepagents/tree/main/examples)。

## 流式传输

深度代理内置了[流式传输](/oss/python/langchain/streaming/overview)功能，可通过 LangGraph 实现实时代理执行更新。
这允许你逐步观察输出，并审查和调试代理及子代理的工作，例如工具调用、工具结果和 LLM 响应。

## 后续步骤

现在你已经构建了第一个深度代理：

* **自定义你的代理**：了解[自定义选项](/oss/python/deepagents/customization)，包括自定义系统提示、工具和子代理。
* **添加长期记忆**：启用跨对话的[持久记忆](/oss/python/deepagents/memory)。
* **部署到生产环境**：了解深度代理的[部署选项](/oss/python/deepagents/deploy)。

***

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

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