快速起步
在几分钟内构建你的第一个 deep agent
本指南将带领你使用文件系统工具和子智能体能力创建第一个 deep agent。你将构建一个能够进行深入调研并撰写报告的研究型智能体。
构建 Deep Agents 研究快速起步案例:在工作目录中按照 Deep Agents 快速起步指引构建一个研究智能体。
步骤 1:阅读指南
检测项目使用的是 Python 还是 TypeScript/JavaScript。根据对应的页面进行操作,并将其作为包名、模型字符串、搜索工具配置和代码的准确来源:
- Python: https://docs.langchain.com/oss/python/deepagents/quickstart.md
- TypeScript: https://docs.langchain.com/oss/javascript/deepagents/quickstart.md
步骤 2:安装依赖
使用当前项目已有的包管理器安装 deepagents(在 TypeScript 中还包括 langchain / @langchain/core)。仅在用户未选择使用 Google、OpenAI 或 Anthropic 内置的提供商搜索工具时,才添加 Tavily。
步骤 3:配置模型凭据
检查受支持的提供商 API 密钥(例如 GOOGLE_API_KEY、OPENAI_API_KEY 或 ANTHROPIC_API_KEY)。如果尚未设置,请询问用户要使用哪家提供商,然后暂停等待他们在 Shell 或 .env 文件中设置密钥。请勿虚构、硬编码或提交 API 密钥。如果需要 Tavily,同样要求设置 TAVILY_API_KEY。
步骤 4:实现研究智能体
按顺序执行以下快速起步步骤:
- 创建网络搜索工具(所选模型支持时优先使用提供商内置搜索工具;否则使用 Tavily)。
- 调用
create_deep_agent,传入搜索工具、指南中推荐的provider:model字符串(或已初始化的模型实例)以及页面上展示的研究系统提示词。 - (可选)提示用户自行设置
LANGSMITH_TRACING=true和LANGSMITH_API_KEY以启用 LangSmith 追踪。 - 针对指南中的示例调研问题运行智能体,并打印最终响应。
规则
- 紧扣本快速起步的范围。不要混入 Managed Deep Agents 部署、评测或不相关的框架。
- 只要可用,优先使用提供商内置的网络搜索;仅在需要时使用 Tavily。
- 当凭证、提供商选择或项目约定不明确时,主动询问而不是凭空猜测。
TIP
正在使用 AI 编码助手?
安装 LangChain 文档 MCP 服务器,让你的智能体能够访问最新的 LangChain 文档和示例。
连接 LangChain 文档 MCP 服务器
将两个 LangChain 文档 MCP 服务器连接到编码智能体,以便其查阅当前的 LangChain、LangGraph 和 LangSmith 文档及 API 参考。
需添加的服务器:
docs-langchain: https://docs.langchain.com/mcpreference-langchain: https://reference.langchain.com/mcp
检测当前使用的智能体或编辑器(Claude Code、Cursor、Codex CLI、Claude Desktop、Deep Agents Code、VS Code、Antigravity 或其他兼容 MCP 的客户端)。参考 https://docs.langchain.com/use-these-docs.md 进行匹配配置:
- Claude Code:针对每个服务器执行
claude mcp add --transport http(默认项目级作用域;仅在需要全局访问时使用--scope user)。 - Codex CLI:使用每个服务器的 URL 执行
codex mcp add。 - Cursor、Deep Agents Code、VS Code 或 Antigravity:使用对应客户端所示字段将两项合并到 MCP 设置 JSON 中。
- Claude Desktop:在 Settings > Connectors 下添加这两个 URL。
请勿伪造备用 MCP URL。配置完成后,确认两个服务器均已列出且可正常访问。
安装 LangChain Skills,以提高智能体在 LangChain 生态系统任务上的表现。
安装 LangChain Skills
为编码智能体安装 LangChain Skills,以提升其在 LangChain、LangGraph 和 Deep Agents 任务上的执行能力。
使用来自 https://github.com/langchain-ai/langchain-skills 的 Agent Skills 安装器:
bashnpx skills add langchain-ai/langchain-skills --skill '*' --yes如果需要全局安装,请使用:
bashnpx skills add langchain-ai/langchain-skills --skill '*' --yes --global检测所使用的智能体或编辑器。如果使用 Claude Code 且偏好插件形式,请按照该仓库 README 中的市场安装指引操作(先
/plugin marketplace add再/plugin install)。请勿虚构替代技能包名称或安装 URL。安装完成后,确认技能对智能体可用。
前置准备
在开始之前,请确保你拥有来自模型提供商(如 Gemini、Anthropic、OpenAI)的 API 密钥。
NOTE
Deep Agents 需要支持工具调用(tool calling)的模型。关于如何配置模型,请参阅自定义。
步骤 1:安装依赖
npm install deepagents langchain @langchain/coreyarn add deepagents langchain @langchain/corepnpm add deepagents langchain @langchain/coreNOTE
Google、OpenAI 和 Anthropic 均提供内置的网络搜索工具:无需额外安装软件包或 API 密钥。如果你使用其他提供商或更偏好 Tavily 进行搜索,也请安装 Tavily 依赖包:
npm install @langchain/tavily步骤 2:设置 API 密钥
export GOOGLE_API_KEY="your-api-key"OpenAI
export OPENAI_API_KEY="your-api-key"Anthropic
export ANTHROPIC_API_KEY="your-api-key"OpenRouter
export OPENROUTER_API_KEY="your-api-key"
export TAVILY_API_KEY="your-tavily-api-key"Fireworks
export FIREWORKS_API_KEY="your-api-key"
export TAVILY_API_KEY="your-tavily-api-key"Baseten
export BASETEN_API_KEY="your-api-key"
export TAVILY_API_KEY="your-tavily-api-key"Ollama
# 本地:机器上必须运行 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"Deep Agents 可以与任何 LangChain 聊天模型配合使用。请为你所用的提供商设置 API 密钥。
TIP
使用 LangSmith Gateway
LangSmith Gateway 可以将大部分主流提供商通过 LangSmith 进行统一路由。你可以自带提供商密钥,或使用 Gateway Credits 在无需提供商密钥的情况下访问模型。
步骤 3:创建搜索工具
Google、OpenAI 和 Anthropic 提供了在服务端运行的内置网络搜索工具:无需额外安装包或 API 密钥。直接将提供商工具字典传递给 create_deep_agent。
提供商内置搜索(推荐)
import { createDeepAgent } from "deepagents";
// Google 内置搜索 — 无需额外安装或 API 密钥
const internetSearch = { googleSearch: {} };import { createDeepAgent } from "deepagents";
// OpenAI 内置网络搜索 — 无需额外安装或 API 密钥
const internetSearch = { type: "web_search_preview" };import { createDeepAgent } from "deepagents";
// Anthropic 内置网络搜索 — 无需额外安装或 API 密钥
const internetSearch = { type: "web_search_20250305", name: "web_search" };Tavily(支持任何提供商)
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:创建 Deep Agent
将你的搜索工具和模型传递给 create_deep_agent。传入形如 provider:model 格式的模型字符串,或传入已初始化的模型实例。请参阅受支持的模型查看所有提供商,并参阅推荐模型了解经过测试的建议。
import { createDeepAgent } from "deepagents";
// 引导智能体成为专家研究员的系统提示词
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:gemini-3.6-flash",
tools: [internetSearch],
systemPrompt: researchInstructions,
});import { createDeepAgent } from "deepagents";
// 引导智能体成为专家研究员的系统提示词
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.5",
tools: [internetSearch],
systemPrompt: researchInstructions,
});import { createDeepAgent } from "deepagents";
// 引导智能体成为专家研究员的系统提示词
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-5",
tools: [internetSearch],
systemPrompt: researchInstructions,
});import { createDeepAgent } from "deepagents";
// 引导智能体成为专家研究员的系统提示词
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:z-ai/glm-5.2",
tools: [internetSearch],
systemPrompt: researchInstructions,
});import { createDeepAgent } from "deepagents";
// 引导智能体成为专家研究员的系统提示词
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/glm-5p2",
tools: [internetSearch],
systemPrompt: researchInstructions,
});import { createDeepAgent } from "deepagents";
// 引导智能体成为专家研究员的系统提示词
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.2",
tools: [internetSearch],
systemPrompt: researchInstructions,
});import { createDeepAgent } from "deepagents";
// 引导智能体成为专家研究员的系统提示词
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:north-mini-code-1.0",
tools: [internetSearch],
systemPrompt: researchInstructions,
});步骤 5:配置 LangSmith 追踪
LangSmith 为你提供了智能体执行的可视化洞察,使你能够查看工具调用、子智能体委派和 LLM 响应。
在 smith.langchain.com 注册,创建 API 密钥,并设置以下环境变量:
export LANGSMITH_TRACING=true
export LANGSMITH_API_KEY="your-langsmith-api-key"步骤 6:运行智能体
const result = await agent.invoke({
messages: [{ role: "user", content: "What is langgraph?" }],
});
// 打印智能体的响应
console.log(result.messages[result.messages.length - 1].content);工作原理是什么?
你的 deep agent 会自动完成以下流程:
- 执行研究:通过调用
internet_search工具收集信息。 - 管理上下文:使用文件系统工具(
write_file、read_file)将大型搜索结果卸载保存。 - 衍生子智能体:根据需要将复杂的子任务委派给专门的子智能体。
- 综合撰写报告:将调研发现汇编为条理清晰的最终响应。
要使用 write_todos 添加结构化任务规划,可以通过 TodoListMiddleware 自主选择开启。参见任务规划。
示例
有关可以使用 Deep Agents 构建的智能体、模式和应用程序,请参阅示例仓库。
流式传输
Deep Agents 内置了使用 LangGraph 进行实时更新的流式传输支持。 这使你可以渐进式观察输出,并审查和调试智能体与子智能体的工作过程,例如工具调用、工具执行结果和 LLM 响应。
下一步
现在你已经构建了第一个 deep agent:
- 自定义你的智能体:了解自定义选项,包括自定义系统提示词、工具和子智能体。
- 添加长期记忆:启用跨会话的持久记忆。
- 部署到生产环境:使用 Managed Deep Agents 在 LangSmith 中创建、运行和运营 deep agents。
- 测试与评估:使用 LangSmith 评估运行自动化测试,并根据数据集衡量智能体的表现。
