本指南会引导你创建第一个具备 planning、file system tools 和 subagent capabilities 的 deep agent。你将构建一个可以开展研究并撰写报告的 research agent。
正在使用 AI coding assistant?

Prerequisites

开始前,请确保你已有 model provider(例如 Gemini、Anthropic、OpenAI)的 API key。
Deep Agents 需要支持 tool calling 的 model。请参阅 customization 了解如何配置 model。

Step 1: Install dependencies

npm install deepagents langchain @langchain/core @langchain/tavily
本指南使用 Tavily 作为示例 search provider,但你可以替换为任何 search API(例如 DuckDuckGo、SerpAPI、Brave Search)。

Step 2: Set up your API keys

export GOOGLE_API_KEY="your-api-key"
export TAVILY_API_KEY="your-tavily-api-key"

Step 3: Create a search tool

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: "Run a web search",
    schema: z.object({
      query: z.string().describe("The search query"),
      maxResults: z
        .number()
        .optional()
        .default(5)
        .describe("Maximum number of results to return"),
      topic: z
        .enum(["general", "news", "finance"])
        .optional()
        .default("general")
        .describe("Search topic category"),
      includeRawContent: z
        .boolean()
        .optional()
        .default(false)
        .describe("Whether to include raw content"),
    }),
  },
);

Step 4: Create a deep agent

传入 provider:model 格式的 model string,或传入 initialized model instance。请参阅 supported models 查看所有 providers,并参阅 suggested models 查看经过测试的推荐 models。
import { createDeepAgent } from "deepagents";

// System prompt to steer the agent to be an expert researcher
const researchInstructions = `You are an expert researcher. Your job is to conduct thorough research and then write a polished report.

You have access to an internet search tool as your primary means of gathering information.

## \`internet_search\`

Use this to run an internet search for a given query. You can specify the max number of results to return, the topic, and whether raw content should be included.
`;

const agent = createDeepAgent({
  model: "google-genai:gemini-3.5-flash",
  tools: [internetSearch],
  systemPrompt: researchInstructions,
});

Step 5: Run the agent

const result = await agent.invoke({
  messages: [{ role: "user", content: "What is langgraph?" }],
});

// Print the agent's response
console.log(result.messages[result.messages.length - 1].content);
使用 LangSmith trace 你的 agent 的 planning steps、tool calls 和 subagent delegation。按照 observability quickstart 完成设置。建议你同时设置 LangSmith Engine,它会监控 traces、检测问题,并提出修复建议。

How does it work?

你的 deep agent 会自动:
  1. 规划方法:使用 built-in write_todos tool 拆解 research task。
  2. 开展研究:调用 internet_search tool 收集信息。
  3. 管理 context:使用 file system tools(write_fileread_file)卸载大型 search results。
  4. 生成 subagents:按需将复杂 subtasks 委派给 specialized subagents。
  5. 综合报告:将 findings 汇总为连贯的 response。

Examples

有关可以用 Deep Agents 构建的 agents、patterns 和 applications,请参阅 Examples

Streaming

Deep Agents 具有 built-in streaming,可通过 LangGraph 获取 agent execution 的 real-time updates。 这让你可以逐步观察 output,并 review 和 debug agent 与 subagent 的工作,例如 tool calls、tool results 和 LLM responses。

Next steps

现在你已经构建了第一个 deep agent:
  • Customize your agent:了解 customization options,包括 custom system prompts、tools 和 subagents。
  • Add long-term memory:跨 conversations 启用 persistent memory
  • Deploy to production:使用 Managed Deep Agents 在 LangSmith 中创建、运行和运营 deep agents。