TodayAI

GuidesAgents

用 Deep Agents 做第一次研究型 Agent

按 Deep Agents Quickstart:安装 deepagents、配置搜索工具与模型,用 create_deep_agent 跑通第一个研究型 Agent。

基于 LangChain Docs 整理 · 官方资料 ↗

Deep Agents 面向需要多步研究、写报告的任务:自带文件系统工具与子代理委派,不只是单次 tool call。

这篇跟着官方 Quickstart,做出第一个研究型 Agent。若你接下来要做文档 RAG,可对照 用 Deep Agents 做文档 RAG Agent

安装与 API Key

官方安装

bash
pip install deepagents

Deep Agents 需要支持 tool calling 的模型。按你选的 provider 导出对应 Key,例如:

按 provider 配置(择一)

bash
export GOOGLE_API_KEY="your-api-key"
# 或
export OPENAI_API_KEY="your-api-key"
# 或
export ANTHROPIC_API_KEY="your-api-key"

Google / OpenAI / Anthropic 提供内置网页搜索,一般不必另装包。若改用 Tavily,再装 tavily-python 并设置 TAVILY_API_KEY。

准备搜索工具

内置搜索可直接传 provider tool dict。以 Google 为例:

Google 内置搜索

python
from deepagents import create_deep_agent

# Google's built-in search — no extra install or API key needed
internet_search = {"google_search": {}}

OpenAI 用 {"type": "web_search"};Anthropic 用 {"type": "web_search_20260209", "name": "web_search"}。

创建研究型 Deep Agent

把模型、tools、system_prompt 传给 create_deep_agent。model 用 provider:model 字符串。

官方 Google 示例

python
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,
)

跑第一次研究任务

官方 invoke 示例

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

print(result["messages"][-1].content)

运行时它会:调用搜索、用文件系统工具卸载过长中间结果、必要时派生子代理,最后合成报告。

可选开启 LangSmith tracing(LANGSMITH_TRACING / LANGSMITH_API_KEY)观察 tool call 与子代理委派。需要文档索引式 RAG 时,继续看 用 Deep Agents 做文档 RAG Agent

容易踩的坑

模型不支持 tool calling

换成官方文档列出的支持 tool calling 的 chat model,并检查对应 API Key。

搜索工具不可用

确认用了 provider 内置 web search 字典;若走 Tavily,检查 TAVILY_API_KEY 与 tavily-python。

上下文被长搜索结果撑爆

这正是 Deep Agents 文件系统卸载的用途;观察是否出现 write_file / read_file,不要关掉相关能力。

官方资料

LangChain Docs

Quickstart