正在使用 AI 编程助手?
- 安装 LangChain 文档 MCP 服务器,让你的 Agent 能够访问最新的 LangChain 文档和示例。
- 安装 LangChain Skills,提升你的 Agent 在 LangChain 生态系统任务上的表现。
前置条件
开始之前,请确保你有一个模型提供商的 API 密钥(例如 Gemini、Anthropic、OpenAI)。步骤 1:安装依赖
npm install deepagents langchain @langchain/core @langchain/tavily
yarn add deepagents langchain @langchain/core @langchain/tavily
pnpm add deepagents langchain @langchain/core @langchain/tavily
本指南使用 Tavily 作为示例搜索提供商,但你可以替换为任何搜索 API(例如 DuckDuckGo、SerpAPI、Brave Search)。
步骤 2:设置 API 密钥
- Google
- OpenAI
- Anthropic
- OpenRouter
- Fireworks
- Baseten
- Ollama
- 其他
export GOOGLE_API_KEY="your-api-key"
export TAVILY_API_KEY="your-tavily-api-key"
export OPENAI_API_KEY="your-api-key"
export TAVILY_API_KEY="your-tavily-api-key"
export ANTHROPIC_API_KEY="your-api-key"
export TAVILY_API_KEY="your-tavily-api-key"
export OPENROUTER_API_KEY="your-api-key"
export TAVILY_API_KEY="your-tavily-api-key"
export FIREWORKS_API_KEY="your-api-key"
export TAVILY_API_KEY="your-tavily-api-key"
export BASETEN_API_KEY="your-api-key"
export TAVILY_API_KEY="your-tavily-api-key"
# 本地运行:Ollama 必须在你的机器上运行
# 云端运行:设置你的 Ollama API 密钥用于托管推理
export OLLAMA_API_KEY="your-api-key"
export TAVILY_API_KEY="your-tavily-api-key"
# 设置你的提供商的 API 密钥
export <PROVIDER>_API_KEY="your-api-key"
export TAVILY_API_KEY="your-tavily-api-key"
步骤 3:创建搜索工具
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"),
}),
},
);
步骤 4:创建 Deep Agent
传入provider:model 格式的 model 字符串,或一个已初始化的模型实例。所有提供商请参阅支持的模型,经过测试的推荐请参阅推荐模型。
import { createDeepAgent } from "deepagents";
// 通过 system prompt 引导 Agent 成为专业研究员
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.1-pro-preview",
tools: [internetSearch],
systemPrompt: researchInstructions,
});
import { createDeepAgent } from "deepagents";
// 通过 system prompt 引导 Agent 成为专业研究员
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: "openai:gpt-5.4",
tools: [internetSearch],
systemPrompt: researchInstructions,
});
import { createDeepAgent } from "deepagents";
// 通过 system prompt 引导 Agent 成为专业研究员
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: "anthropic:claude-sonnet-4-6",
tools: [internetSearch],
systemPrompt: researchInstructions,
});
import { createDeepAgent } from "deepagents";
// 通过 system prompt 引导 Agent 成为专业研究员
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: "openrouter:anthropic/claude-sonnet-4-6",
tools: [internetSearch],
systemPrompt: researchInstructions,
});
import { createDeepAgent } from "deepagents";
// 通过 system prompt 引导 Agent 成为专业研究员
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: "fireworks:accounts/fireworks/models/qwen3p5-397b-a17b",
tools: [internetSearch],
systemPrompt: researchInstructions,
});
import { createDeepAgent } from "deepagents";
// 通过 system prompt 引导 Agent 成为专业研究员
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: "baseten:zai-org/GLM-5",
tools: [internetSearch],
systemPrompt: researchInstructions,
});
import { createDeepAgent } from "deepagents";
// 通过 system prompt 引导 Agent 成为专业研究员
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: "ollama:devstral-2",
tools: [internetSearch],
systemPrompt: researchInstructions,
});
步骤 5:运行 Agent
const result = await agent.invoke({
messages: [{ role: "user", content: "What is langgraph?" }],
});
// 打印 Agent 的响应
console.log(result.messages[result.messages.length - 1].content);
它是如何工作的?
你的 Deep Agent 会自动执行以下操作:- 规划方法,使用内置的
write_todos工具将研究任务分解。 - 进行研究,通过调用
internet_search工具收集信息。 - 管理上下文,使用文件系统工具(
write_file、read_file)来卸载大型搜索结果。 - 生成子 Agent,根据需要将复杂子任务委托给专门的子 Agent。
- 合成报告,将研究发现整合为连贯的响应。
示例
有关使用 Deep Agents 构建的 Agent、模式和应用,请参阅示例。Streaming
Deep Agents 内置了 Streaming 功能,可以使用 LangGraph 从 Agent 执行中获取实时更新。 这让你可以渐进式地观察输出,并审查和调试 Agent 及子 Agent 的工作,例如工具调用、工具结果和 LLM 响应。后续步骤
现在你已经构建了第一个 Deep Agent:- 自定义你的 Agent:了解自定义选项,包括自定义系统提示词、工具和子 Agent。
- 添加长期记忆:启用跨对话的持久化记忆。
- 部署到生产环境:使用托管 Deep Agents 在 LangSmith 中创建、运行和管理 Deep Agents。
将这些文档连接到 Claude、VSCode 等工具,通过 MCP 获取实时答案。

