本指南会引导你创建第一个具备 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

pip install deepagents tavily-python
本指南使用 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 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,
    )

Step 4: Create a deep agent

传入 provider:model 格式的 model string,或传入 initialized model instance。请参阅 supported models 查看所有 providers,并参阅 suggested models 查看经过测试的推荐 models。
# 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.5-flash",
    tools=[internet_search],
    system_prompt=research_instructions,
)

Step 5: Run the agent

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

# Print the agent's response
print(result["messages"][-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。