构建 Deep Agents 研究快速入门
使用 AI 编码助手?
-
安装 LangChain 文档 MCP 服务器 以使您的智能体能够访问最新的 LangChain 文档和示例。
连接LangChain文档MCP服务器
-
安装 LangChain 技能 以提高您的智能体在 LangChain 生态系统任务上的性能。
安装 LangChain 技能
前置条件
开始之前,请确保您有一个模型提供程序的 API 密钥(例如 Gemini、Anthropic、OpenAI)。第 1 步:安装依赖项
:::pythonpip install deepagents
uv init
uv add deepagents
uv sync
npm install deepagents langchain @langchain/core
yarn add deepagents langchain @langchain/core
pnpm add deepagents langchain @langchain/core
Google、OpenAI 和 Anthropic 都提供内置网络搜索工具:无需额外包或 API 密钥。如果您使用不同的提供程序或更喜欢 Tavily 进行搜索,请安装 Tavily 包::::python::::::js:::
pip install tavily-python
npm install @langchain/tavily
第 2 步:设置您的 API 密钥
- Google
- OpenAI
- Anthropic
- OpenRouter
- Fireworks
- Baseten
- Ollama
- Other
export GOOGLE_API_KEY="your-api-key"
export OPENAI_API_KEY="your-api-key"
export ANTHROPIC_API_KEY="your-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"
# Local: Ollama must be running on your machine
# Cloud: Set your Ollama API key for hosted inference
export OLLAMA_API_KEY="your-api-key"
export TAVILY_API_KEY="your-tavily-api-key"
# Set the API key for your provider
export <PROVIDER>_API_KEY="your-api-key"
export TAVILY_API_KEY="your-tavily-api-key"
如果您正在使用 LangSmith 的 LLM 网关,则对提供程序的流量通过 LangSmith 进行路由。使用网关密钥作为 API 密钥。
第 3 步:创建一个搜索工具
Google、OpenAI 和 Anthropic 提供内置网络搜索工具,这些工具在服务器端运行:无需额外包或 API 密钥。将提供程序工具字典直接传递给create_deep_agent。
- Provider search (recommended)
- Tavily (any provider)
:::python::::::js:::
from deepagents import create_deep_agent
# Google's built-in search — no extra install or API key needed
internet_search = {"google_search": {}}
from deepagents import create_deep_agent
# OpenAI's built-in web search — no extra install or API key needed
internet_search = {"type": "web_search"}
from deepagents import create_deep_agent
# Anthropic's built-in web search — no extra install or API key needed
internet_search = {"type": "web_search_20260209", "name": "web_search"}
import { createDeepAgent } from "deepagents";
// Google's built-in search — no extra install or API key needed
const internetSearch = { googleSearch: {} };
import { createDeepAgent } from "deepagents";
// OpenAI's built-in web search — no extra install or API key needed
const internetSearch = { type: "web_search_preview" };
import { createDeepAgent } from "deepagents";
// Anthropic's built-in web search — no extra install or API key needed
const internetSearch = { type: "web_search_20250305", name: "web_search" };
:::python::::::js:::
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,
):
"""Run a web search"""
return tavily_client.search(
query,
max_results=max_results,
include_raw_content=include_raw_content,
topic=topic,
)
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 步:创建深度智能体
将您的搜索工具和模型传递给create_deep_agent。在 provider:model 格式中传递一个 model 字符串,或传递一个 初始化的模型实例。有关所有提供程序的支持模型,请参见 支持模型;有关经过测试的推荐模型,请参见 建议模型。
:::python
# System prompt to steer the agent to be an expert researcher
research_instructions = """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.
"""
agent = create_deep_agent(
model="google_genai:gemini-3.6-flash",
tools=[internet_search],
system_prompt=research_instructions,
)
# System prompt to steer the agent to be an expert researcher
research_instructions = """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.
"""
agent = create_deep_agent(
model="openai:gpt-5.5",
tools=[internet_search],
system_prompt=research_instructions,
)
# System prompt to steer the agent to be an expert researcher
research_instructions = """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.
"""
agent = create_deep_agent(
model="anthropic:claude-sonnet-5",
tools=[internet_search],
system_prompt=research_instructions,
)
# System prompt to steer the agent to be an expert researcher
research_instructions = """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.
"""
agent = create_deep_agent(
model="openrouter:z-ai/glm-5.2",
tools=[internet_search],
system_prompt=research_instructions,
)
# System prompt to steer the agent to be an expert researcher
research_instructions = """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.
"""
agent = create_deep_agent(
model="fireworks:accounts/fireworks/models/glm-5p2",
tools=[internet_search],
system_prompt=research_instructions,
)
# System prompt to steer the agent to be an expert researcher
research_instructions = """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.
"""
agent = create_deep_agent(
model="baseten:zai-org/GLM-5.2",
tools=[internet_search],
system_prompt=research_instructions,
)
# System prompt to steer the agent to be an expert researcher
research_instructions = """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.
"""
agent = create_deep_agent(
model="ollama:north-mini-code-1.0",
tools=[internet_search],
system_prompt=research_instructions,
)
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:gemini-3.6-flash",
tools: [internetSearch],
systemPrompt: researchInstructions,
});
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: "openai:gpt-5.5",
tools: [internetSearch],
systemPrompt: researchInstructions,
});
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: "anthropic:claude-sonnet-5",
tools: [internetSearch],
systemPrompt: researchInstructions,
});
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: "openrouter:z-ai/glm-5.2",
tools: [internetSearch],
systemPrompt: researchInstructions,
});
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: "fireworks:accounts/fireworks/models/glm-5p2",
tools: [internetSearch],
systemPrompt: researchInstructions,
});
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: "baseten:zai-org/GLM-5.2",
tools: [internetSearch],
systemPrompt: researchInstructions,
});
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: "ollama:north-mini-code-1.0",
tools: [internetSearch],
systemPrompt: researchInstructions,
});
第 5 步:设置 LangSmith 追踪
在一个 基于智能体的工作空间 中,在发送跟踪信息之前,先 创建一个智能体。使用 智能体寻址 来选择其标识符和环境。如果集成需要
project_name,请使用所选环境的 tracer_session_name 作为兼容性值。export LANGSMITH_AGENT_ID="<your-agent-id>"
export LANGSMITH_AGENT_ENVIRONMENT="production"
export LANGSMITH_TRACING=true
export LANGSMITH_API_KEY="your-langsmith-api-key"
第 6 步:运行智能体
:::pythonresult = agent.invoke({"messages": [{"role": "user", "content": "What is langgraph?"}]})
# Print the agent's response
print(result["messages"][-1].content)
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);
它是如何工作的?
您的深度智能体会自动:- 进行研究,通过调用
internet_search工具收集信息。 - 管理上下文,使用文件系统工具(
write_file、read_file)将大型搜索结果卸载到外部。 - 按需生成子智能体,将复杂的子任务委托给专业的子智能体。
- 综合报告,将发现结果汇总为连贯的响应。
write_todos 通过 @[TodoListMiddleware 进行选择。有关详细信息,请参见 任务规划。
示例
有关 Deep Agents 可构建的智能体和模式的示例,请参见 示例。流式输出
Deep Agents 具有内置的 流式输出,可使用 LangGraph 实时更新智能体执行结果。 这允许您逐步观察输出,并审查和调试智能体和子智能体的工作,例如工具调用、工具结果和 LLM 响应。下一步
现在你已经构建了你的第一个深度智能体:- 自定义你的智能体:了解 自定义选项,包括自定义系统提示词、工具和子智能体。
- 添加长期记忆:启用 持久记忆跨对话。
- 部署到生产环境:使用 托管深度智能体在 LangSmith 中创建、运行和操作深度智能体。
- 测试和评估:使用 LangSmith 评估运行自动化测试,并衡量你的智能体相对于数据集的性能。
官方原文:src/oss/deepagents/quickstart.mdx · 非官方翻译,译文由 AI 生成,以英文原文为准。