> ## 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/javascript/langchain/models#tool-calling)的模型。有关如何配置模型，请参阅[自定义](/oss/javascript/deepagents/customization#model)。
</Note>

## 步骤 1：安装依赖

<CodeGroup>
  ```bash npm theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  npm install deepagents langchain @langchain/core @langchain/tavily
  ```

  ```bash yarn theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  yarn add deepagents langchain @langchain/core @langchain/tavily
  ```

  ```bash pnpm theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  pnpm add deepagents langchain @langchain/core @langchain/tavily
  ```
</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/javascript/deepagents/models#supported-models)。设置你的提供商的 API 密钥。
  </Tab>
</Tabs>

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

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import { tool } from "langchain";
import { TavilySearch } from "@langchain/tavily";
import { z } from "zod";

const internetSearch = tool(
  async ({
    query,
    maxResults = 5,
    topic = "general",
    includeRawContent = false,
  }: {
    query: string;
    maxResults?: number;
    topic?: "general" | "news" | "finance";
    includeRawContent?: boolean;
  }) => {
    const tavilySearch = new TavilySearch({
      maxResults,
      tavilyApiKey: process.env.TAVILY_API_KEY,
      includeRawContent,
      topic,
    });
    return await tavilySearch._call({ query });
  },
  {
    name: "internet_search",
    description: "运行网络搜索",
    schema: z.object({
      query: z.string().describe("搜索查询"),
      maxResults: z
        .number()
        .optional()
        .default(5)
        .describe("返回的最大结果数"),
      topic: z
        .enum(["general", "news", "finance"])
        .optional()
        .default("general")
        .describe("搜索主题类别"),
      includeRawContent: z
        .boolean()
        .optional()
        .default(false)
        .describe("是否包含原始内容"),
    }),
  },
);
```

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

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import { createDeepAgent } from "deepagents";

// 引导代理成为专家研究员的系统提示
const researchInstructions = `你是一名专家研究员。你的工作是进行彻底的研究，然后撰写一份精炼的报告。

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

## \`internet_search\`

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

从你的提供商中选择一个模型。默认情况下，`createDeepAgent` 使用 `claude-sonnet-4-6`。传递一个 `model` 字符串以使用不同的提供商——完整列表请参阅[推荐模型](/oss/javascript/deepagents/models#suggested-models)。

<Tabs>
  <Tab title="Google">
    ```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    const agent = createDeepAgent({
      model: "google-genai:gemini-3.1-pro-preview",
      tools: [internetSearch],
      systemPrompt: researchInstructions,
    });
    ```
  </Tab>

  <Tab title="OpenAI">
    ```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    const agent = createDeepAgent({
      model: "openai:gpt-5.4",
      tools: [internetSearch],
      systemPrompt: researchInstructions,
    });
    ```
  </Tab>

  <Tab title="Anthropic">
    ```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    const agent = createDeepAgent({
      model: "anthropic:claude-sonnet-4-6",
      tools: [internetSearch],
      systemPrompt: researchInstructions,
    });
    ```
  </Tab>

  <Tab title="OpenRouter">
    ```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    const agent = createDeepAgent({
      model: "openrouter:anthropic/claude-sonnet-4-6",
      tools: [internetSearch],
      systemPrompt: researchInstructions,
    });
    ```
  </Tab>

  <Tab title="Fireworks">
    ```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    const agent = createDeepAgent({
      model: "fireworks:accounts/fireworks/models/qwen3p5-397b-a17b",
      tools: [internetSearch],
      systemPrompt: researchInstructions,
    });
    ```
  </Tab>

  <Tab title="Ollama">
    ```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    const agent = createDeepAgent({
      model: "ollama:devstral-2",
      tools: [internetSearch],
      systemPrompt: researchInstructions,
    });
    ```
  </Tab>

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

    <CodeGroup>
      ```typescript 模型字符串 theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
      const agent = createDeepAgent({
        model: "provider:model-name",
        tools: [internetSearch],
        systemPrompt: researchInstructions,
      });
      ```

      ```typescript initChatModel theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
      import { initChatModel } from "langchain";

      const model = await initChatModel("provider:model-name");
      const agent = createDeepAgent({
        model,
        tools: [internetSearch],
        systemPrompt: researchInstructions,
      });
      ```

      ```typescript 模型类 theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
      import { Chat<Provider> } from "@langchain/<provider>";

      const model = new Chat<Provider>({ model: "model-name" });
      const agent = createDeepAgent({
        model,
        tools: [internetSearch],
        systemPrompt: researchInstructions,
      });
      ```
    </CodeGroup>
  </Tab>
</Tabs>

## 步骤 5：运行代理

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
const result = await agent.invoke({
  messages: [{ role: "user", content: "什么是 langgraph？" }],
});

// 打印代理的响应
console.log(result.messages[result.messages.length - 1].content);
```

## 工作原理

你的深度代理会自动：

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

## 示例

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

## 流式传输

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

## 后续步骤

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

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