> ## Documentation Index
> Fetch the complete documentation index at: https://cndoc-langchain.site/llms.txt
> Use this file to discover all available pages before exploring further.

# 快速入门

> 在几分钟内构建你的第一个代理

本快速入门指南将向你展示如何在短短几分钟内创建一个功能完备的 AI 代理。

<Tip>
  **正在使用 AI 编程助手？**

  * 安装 [LangChain 文档 MCP 服务器](/use-these-docs)，让你的代理能够访问最新的 LangChain 文档和示例。
  * 安装 [LangChain Skills](https://github.com/langchain-ai/langchain-skills)，以提升你的代理在 LangChain 生态系统任务上的性能。
</Tip>

## 安装依赖

安装以下包以跟随本教程：

<CodeGroup>
  ```bash uv theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  uv init
  uv add langchain deepagents
  uv sync
  ```

  ```bash pip theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  pip install -U langchain deepagents
  ```

  ```bash venv theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  python3 -m venv .venv
  source .venv/bin/activate
  # Windows: .venv\Scripts\activate
  pip install -U langchain deepagents
  ```
</CodeGroup>

## 设置 API 密钥

从[任何受支持的模型提供商](/oss/python/integrations/providers/overview)（例如，Google Gemini 或 OpenAI）获取 API 密钥。

设置 API 密钥，例如：

<Tabs>
  <Tab title="OpenAI">
    ```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    export OPENAI_API_KEY="your-api-key"
    ```
  </Tab>

  <Tab title="Google Gemini">
    ```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    export GOOGLE_API_KEY="your-api-key"
    ```
  </Tab>

  <Tab title="Claude (Anthropic)">
    ```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    export ANTHROPIC_API_KEY="your-api-key"
    ```
  </Tab>

  <Tab title="OpenRouter">
    ```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    export OPENROUTER_API_KEY="your-api-key"
    ```
  </Tab>

  <Tab title="Fireworks">
    ```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    export FIREWORKS_API_KEY="your-api-key"
    ```
  </Tab>

  <Tab title="Baseten">
    ```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    export BASETEN_API_KEY="your-api-key"
    ```
  </Tab>

  <Tab title="Ollama">
    ```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    # 本地：Ollama 必须正在运行 (https://ollama.com)
    # 云：为你托管的推理设置 Ollama API 密钥
    export OLLAMA_API_KEY="your-api-key"
    ```
  </Tab>

  <Tab title="Azure">
    ```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    export AZURE_OPENAI_API_KEY="your-api-key"
    export AZURE_OPENAI_ENDPOINT="https://your-resource.openai.azure.com"
    export AZURE_OPENAI_DEPLOYMENT_NAME="your-deployment"
    ```
  </Tab>

  <Tab title="AWS Bedrock">
    ```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    export AWS_ACCESS_KEY_ID="your-access-key"
    export AWS_SECRET_ACCESS_KEY="your-secret-key"
    export AWS_REGION="us-east-1"
    ```
  </Tab>

  <Tab title="HuggingFace">
    ```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    export HUGGINGFACEHUB_API_TOKEN="hf_..."
    ```
  </Tab>

  <Tab title="其他">
    查看完整的受支持[聊天模型集成](/oss/python/integrations/chat)列表。
  </Tab>
</Tabs>

## 构建基础代理

首先创建一个简单的代理，它能够回答问题并调用工具。本示例中的代理使用所选的语言模型、一个作为工具的基本天气函数，以及一个简单的提示来指导其行为：

<CodeGroup>
  ```python OpenAI theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  from langchain.agents import create_agent

  def get_weather(city: str) -> str:
      """获取给定城市的天气。"""
      return f"It's always sunny in {city}!"

  agent = create_agent(
      model="openai:gpt-5.4",
      tools=[get_weather],
      system_prompt="You are a helpful assistant",
  )

  result = agent.invoke(
      {"messages": [{"role": "user", "content": "What's the weather in San Francisco?"}]}
  )
  print(result["messages"][-1].content_blocks)
  ```

  ```python Google Gemini theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  from langchain.agents import create_agent

  def get_weather(city: str) -> str:
      """获取给定城市的天气。"""
      return f"It's always sunny in {city}!"

  agent = create_agent(
      model="google_genai:gemini-2.5-flash-lite",
      tools=[get_weather],
      system_prompt="You are a helpful assistant",
  )

  result = agent.invoke(
      {"messages": [{"role": "user", "content": "What's the weather in San Francisco?"}]}
  )
  print(result["messages"][-1].content_blocks)
  ```

  ```python Claude (Anthropic) theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  from langchain.agents import create_agent

  def get_weather(city: str) -> str:
      """获取给定城市的天气。"""
      return f"It's always sunny in {city}!"

  agent = create_agent(
      model="claude-sonnet-4-6",
      tools=[get_weather],
      system_prompt="You are a helpful assistant",
  )

  result = agent.invoke(
      {"messages": [{"role": "user", "content": "What's the weather in San Francisco?"}]}
  )
  print(result["messages"][-1].content_blocks)
  ```

  ```python OpenRouter theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  from langchain.agents import create_agent

  def get_weather(city: str) -> str:
      """获取给定城市的天气。"""
      return f"It's always sunny in {city}!"

  agent = create_agent(
      model="openrouter:anthropic/claude-sonnet-4-6",
      tools=[get_weather],
      system_prompt="You are a helpful assistant",
  )

  result = agent.invoke(
      {"messages": [{"role": "user", "content": "What's the weather in San Francisco?"}]}
  )
  print(result["messages"][-1].content_blocks)
  ```

  ```python Fireworks theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  from langchain.agents import create_agent

  def get_weather(city: str) -> str:
      """获取给定城市的天气。"""
      return f"It's always sunny in {city}!"

  agent = create_agent(
      model="fireworks:accounts/fireworks/models/qwen3p5-397b-a17b",
      tools=[get_weather],
      system_prompt="You are a helpful assistant",
  )

  result = agent.invoke(
      {"messages": [{"role": "user", "content": "What's the weather in San Francisco?"}]}
  )
  print(result["messages"][-1].content_blocks)
  ```

  ```python Baseten theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  from langchain.agents import create_agent

  def get_weather(city: str) -> str:
      """获取给定城市的天气。"""
      return f"It's always sunny in {city}!"

  agent = create_agent(
      model="baseten:zai-org/GLM-5",
      tools=[get_weather],
      system_prompt="You are a helpful assistant",
  )

  result = agent.invoke(
      {"messages": [{"role": "user", "content": "What's the weather in San Francisco?"}]}
  )
  print(result["messages"][-1].content_blocks)
  ```

  ```python Ollama theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  from langchain.agents import create_agent

  def get_weather(city: str) -> str:
      """获取给定城市的天气。"""
      return f"It's always sunny in {city}!"

  agent = create_agent(
      model="ollama:devstral-2",
      tools=[get_weather],
      system_prompt="You are a helpful assistant",
  )

  result = agent.invoke(
      {"messages": [{"role": "user", "content": "What's the weather in San Francisco?"}]}
  )
  print(result["messages"][-1].content_blocks)
  ```

  ```python Azure theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  import os
  from langchain.agents import create_agent

  def get_weather(city: str) -> str:
      """获取给定城市的天气。"""
      return f"It's always sunny in {city}!"

  agent = create_agent(
      model="azure_openai:gpt-5.4",
      tools=[get_weather],
      system_prompt="You are a helpful assistant",
      azure_deployment=os.environ["AZURE_OPENAI_DEPLOYMENT_NAME"],
  )

  result = agent.invoke(
      {"messages": [{"role": "user", "content": "What's the weather in San Francisco?"}]}
  )
  print(result["messages"][-1].content_blocks)
  ```

  ```python AWS Bedrock theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  from langchain.agents import create_agent

  def get_weather(city: str) -> str:
      """获取给定城市的天气。"""
      return f"It's always sunny in {city}!"

  agent = create_agent(
      model="anthropic.claude-3-5-sonnet-20240620-v1:0",
      model_provider="bedrock_converse",
      tools=[get_weather],
      system_prompt="You are a helpful assistant",
  )

  result = agent.invoke(
      {"messages": [{"role": "user", "content": "What's the weather in San Francisco?"}]}
  )
  print(result["messages"][-1].content_blocks)
  ```

  ```python HuggingFace theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  from langchain.agents import create_agent

  def get_weather(city: str) -> str:
      """获取给定城市的天气。"""
      return f"It's always sunny in {city}!"

  agent = create_agent(
      model="microsoft/Phi-3-mini-4k-instruct",
      model_provider="huggingface",
      tools=[get_weather],
      system_prompt="You are a helpful assistant",
      temperature=0.7,
      max_tokens=1024,
  )

  result = agent.invoke(
      {"messages": [{"role": "user", "content": "What's the weather in San Francisco?"}]}
  )
  print(result["messages"][-1].content_blocks)
  ```
</CodeGroup>

当你运行代码并提示代理告诉你旧金山的天气时，代理会使用该输入及其可用的上下文。
代理理解你是在询问旧金山市的天气，因此会使用提供的城市名称调用天气工具。

<Tip>
  你可以通过更改代码中的模型名称并设置相应的 API 密钥来使用[任何受支持的模型](/oss/python/integrations/providers/overview)。
</Tip>

## 构建真实场景代理

在接下来的示例中，你将构建一个研究代理，它可以回答关于文本文件的问题。
在此过程中，你将探索以下概念：

1. **详细的系统提示**，以改善代理行为
2. **创建工具**，与外部数据集成
3. **模型配置**，以获得一致的响应
4. **对话记忆**，实现类似聊天的交互
5. **深度代理**，提供内置功能
6. **测试**你的代理

<Steps>
  <Step title="定义系统提示">
    系统提示定义了你的代理的角色和行为。保持其具体且可操作：

    ```python wrap theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    SYSTEM_PROMPT = """You are a literary data assistant.

    ## Capabilities

    - `fetch_text_from_url`: loads document text from a URL into the conversation.
    Do not guess line counts or positions—ground them in tool results from the saved file."""
    ```
  </Step>

  <Step title="创建工具">
    [工具](/oss/python/langchain/tools)允许模型通过调用你定义的函数与外部系统交互。
    工具可以依赖于[运行时上下文](/oss/python/langchain/runtime)，也可以与[代理记忆](/oss/python/langchain/short-term-memory)交互。

    此示例使用一个工具从给定 URL 加载文档：

    ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    import urllib.error
    import urllib.request

    from langchain.tools import tool


    @tool
    def fetch_text_from_url(url: str) -> str:
        """从 URL 获取文档。
        """
        req = urllib.request.Request(
            url,
            headers={"User-Agent": "Mozilla/5.0 (compatible; quickstart-research/1.0)"},
        )
        try:
            with urllib.request.urlopen(req, timeout=120) as resp:
                raw = resp.read()
        except urllib.error.URLError as e:
            return f"Fetch failed: {e}"
        text = raw.decode("utf-8", errors="replace")
        return text
    ```

    <Tip>
      工具应有良好的文档：它们的名称、描述和参数名称会成为模型提示的一部分。
      LangChain 的 [`@tool` 装饰器](https://reference.langchain.com/python/langchain-core/tools/convert/tool)添加元数据，并通过 `ToolRuntime` 参数支持运行时注入。
      在[工具指南](/oss/python/langchain/tools)中了解更多。
    </Tip>
  </Step>

  <Step title="配置你的模型">
    为你的用例设置具有正确参数的[语言模型](/oss/python/langchain/models)。例如：

    <CodeGroup>
      ```python OpenAI theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
      from langchain.chat_models import init_chat_model

      model = init_chat_model(
          "openai:gpt-5.4",
          temperature=0.5,
          timeout=300,
          max_tokens=25000,
      )
      ```

      ```python Google Gemini theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
      from langchain.chat_models import init_chat_model

      model = init_chat_model(
          "gemini-3.1-pro-preview",
          model_provider="google-genai",
          temperature=0.5,
          timeout=600,
          max_tokens=25000,
          streaming=True,
      )
      ```

      ```python Claude (Anthropic) theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
      from langchain.chat_models import init_chat_model

      model = init_chat_model(
          "claude-sonnet-4-6",
          temperature=0.5,
          timeout=600,
          max_tokens=25000,
          streaming=True,
      )
      ```

      ```python OpenRouter theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
      from langchain.chat_models import init_chat_model

      model = init_chat_model(
          "openrouter:anthropic/claude-sonnet-4-6",
          temperature=0.5,
          timeout=300,
          max_tokens=25000,
      )
      ```

      ```python Fireworks theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
      from langchain.chat_models import init_chat_model

      model = init_chat_model(
          "fireworks:accounts/fireworks/models/qwen3p5-397b-a17b",
          temperature=0.5,
          timeout=300,
          max_tokens=25000,
      )
      ```

      ```python Baseten theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
      from langchain.chat_models import init_chat_model

      model = init_chat_model(
          "baseten:zai-org/GLM-5",
          temperature=0.5,
          timeout=300,
          max_tokens=25000,
      )
      ```

      ```python Ollama theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
      from langchain.chat_models import init_chat_model

      model = init_chat_model(
          "ollama:devstral-2",
          temperature=0.5,
          timeout=300,
          max_tokens=25000,
      )
      ```

      ```python Azure theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
      import os
      from langchain.chat_models import init_chat_model

      model = init_chat_model(
          "azure_openai:gpt-5.4",
          temperature=0.5,
          timeout=300,
          max_tokens=25000,
          azure_deployment=os.environ["AZURE_OPENAI_DEPLOYMENT_NAME"],
      )
      ```

      ```python AWS Bedrock theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
      from langchain.chat_models import init_chat_model

      model = init_chat_model(
          "anthropic.claude-3-5-sonnet-20240620-v1:0",
          model_provider="bedrock_converse",
          temperature=0.5,
          timeout=300,
          max_tokens=25000,
      )
      ```

      ```python HuggingFace theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
      from langchain.chat_models import init_chat_model

      model = init_chat_model(
          "microsoft/Phi-3-mini-4k-instruct",
          model_provider="huggingface",
          temperature=0.5,
          timeout=300,
          max_tokens=25000,
      )
      ```
    </CodeGroup>

    根据所选的模型和提供商，初始化参数可能有所不同；请参阅其参考页面了解详情。
  </Step>

  <Step title="添加记忆">
    为你的代理添加[记忆](/oss/python/langchain/short-term-memory)，以在交互中保持状态。这使得代理能够记住之前的对话和上下文。

    ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    from langgraph.checkpoint.memory import InMemorySaver

    checkpointer = InMemorySaver()
    ```

    <Info>
      在生产环境中，请使用将消息历史保存到数据库的持久化检查点。
      有关更多详细信息，请参阅[添加和管理记忆](/oss/python/langgraph/add-memory#manage-short-term-memory)。
    </Info>
  </Step>

  <Step title="创建并运行代理">
    现在，使用所有组件组装你的代理并运行它。

    创建代理有两种不同的框架：LangChain 代理和深度代理。
    LangChain 代理和深度代理都为你提供了对工具、记忆等的细粒度控制。
    两者的主要区别在于，深度代理内置了一系列常用功能，例如规划、文件系统工具和子代理。

    当你希望以最少的设置获得最大功能时，请使用深度代理；当你需要细粒度控制时，请选择 LangChain 代理。

    <Warning>
      由于代码使用了《了不起的盖茨比》的全部文本来调用模型，因此它使用了大量的 token。

      你可以在下一步中查看示例输出。
    </Warning>

    让我们尝试两种方式：

    ```python wrap theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    from langchain.agents import create_agent
    from deepagents import create_deep_agent

    agent = create_agent(
        model=model,
        tools=[fetch_text_from_url],
        system_prompt=SYSTEM_PROMPT,
        checkpointer=checkpointer,
    )

    deep_agent = create_deep_agent(
        model=model,
        tools=[fetch_text_from_url],
        system_prompt=SYSTEM_PROMPT,
        checkpointer=checkpointer,
    )

    content = f"""Project Gutenberg hosts a full plain-text copy of F. Scott Fitzgerald's The Great Gatsby.
    URL: https://www.gutenberg.org/files/64317/64317-0.txt

    Answer as much as you can:

    1) How many lines in the complete Gutenberg file contain the substring `Gatsby` (count lines, not occurrences within a line, each line ends with a line break).
    2) The 1-based line number of the first line in the file that contains `Daisy`.
    3) A two-sentence neutral synopsis.

    Do your best on (1) and (2). If at any point you realize you cannot **verify** an exact answer with
    your available tools and reasoning, do not fabricate numbers: use `null` for that field and spell out
    the limitation in `how_you_computed_counts`. If you encounter any errors please report what the error was and what the error message was."""

    agent_result = agent.invoke(
        {"messages": [{"role": "user", "content": content}]},
        config={"configurable": {"thread_id": "great-gatsby-lc"}},
    )
    deep_agent_result = deep_agent.invoke(
        {"messages": [{"role": "user", "content": content}]},
        config={"configurable": {"thread_id": "great-gatsby-da"}},
    )
    print(agent_result["messages"][-1].content_blocks)
    print("\n")
    print(deep_agent_result["messages"][-1].content_blocks)
    ```

    <Expandable title="完整示例代码">
      ```python wrap theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
      import urllib.error
      import urllib.request

      from langchain.agents import create_agent
      from deepagents import create_deep_agent
      from langchain.chat_models import init_chat_model
      from langchain.tools import tool
      from langgraph.checkpoint.memory import InMemorySaver

      SYSTEM_PROMPT = """You are a literary data assistant.

      ## Capabilities

      - `fetch_text_from_url`: loads document text from a URL into the conversation.
      Do not guess line counts or positions—ground them in tool results from the saved file."""


      @tool
      def fetch_text_from_url(url: str) -> str:
          """从 URL 获取文档。
          """
          req = urllib.request.Request(
              url,
              headers={"User-Agent": "Mozilla/5.0 (compatible; quickstart-research/1.0)"},
          )
          try:
              with urllib.request.urlopen(req, timeout=120) as resp:
                  raw = resp.read()
          except urllib.error.URLError as e:
              return f"Fetch failed: {e}"
          text = raw.decode("utf-8", errors="replace")
          return text


      model = init_chat_model(
          "gemini-3.1-pro-preview",
          model_provider="google-genai",
          temperature=0.5,
          timeout=600,
          max_tokens=25000,
          streaming=True,
      )

      checkpointer = InMemorySaver()

      agent = create_agent(
          model=model,
          tools=[fetch_text_from_url],
          system_prompt=SYSTEM_PROMPT,
          checkpointer=checkpointer,
      )

      deep_agent = create_deep_agent(
          model=model,
          tools=[fetch_text_from_url],
          system_prompt=SYSTEM_PROMPT,
          checkpointer=checkpointer,
      )

      content = f"""Project Gutenberg hosts a full plain-text copy of F. Scott Fitzgerald's The Great Gatsby.
      URL: https://www.gutenberg.org/files/64317/64317-0.txt

      Answer as much as you can:

      1) How many lines in the complete Gutenberg file contain the substring `Gatsby` (count lines, not occurrences within a line, each line ends with a line break).
      2) The 1-based line number of the first line in the file that contains `Daisy`.
      3) A two-sentence neutral synopsis.

      Do your best on (1) and (2). If at any point you realize you cannot **verify** an exact answer with
      your available tools and reasoning, do not fabricate numbers: use `null` for that field and spell out
      the limitation in `how_you_computed_counts`. If you encounter any errors please report what the error was and what the error message was."""

      agent_result = agent.invoke(
          {"messages": [{"role": "user", "content": content}]},
          config={"configurable": {"thread_id": "great-gatsby-lc"}},
      )
      deep_agent_result = deep_agent.invoke(
          {"messages": [{"role": "user", "content": content}]},
          config={"configurable": {"thread_id": "great-gatsby-da"}},
      )
      print(agent_result["messages"][-1].content_blocks)
      print("\n")
      print(deep_agent_result["messages"][-1].content_blocks)
      ```
    </Expandable>
  </Step>

  <Step title="查看结果">
    结果会因模型和执行情况而异。

    <Tabs default="LangChain agents">
      <Tab title="LangChain 代理">
        ```txt wrap expandable theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
        **1) 包含 `Gatsby` 的行数：** `null`

        **2) 包含 `Daisy` 的第一行：** `null`

        **3) 概要：**
        《了不起的盖茨比》讲述了神秘的百万富翁杰伊·盖茨比及其对与前情人黛西·布坎南重聚的痴迷，由他的邻居尼克·卡拉韦叙述。故事背景设定在长岛的咆哮二十年代，小说探讨了财富、阶级和美国梦的虚幻本质等主题。

        **how_you_computed_counts:**
        我成功使用 `fetch_text_from_url` 工具获取了电子书的全文。但是，由于我无法访问代码执行环境（如 Python）或文本处理工具（如 `grep`），我无法确定性地按换行符分割文本、遍历数千行并验证确切的行号或匹配计数。LLM 无法在其上下文窗口内可靠地对大量文本执行精确的行计数或索引，而无需外部计算工具。根据指示，我没有编造或猜测数字，而是为确切的计数和位置输出了 `null`。
        ```
      </Tab>

      <Tab title="深度代理">
        ```txt wrap expandable theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
        根据直接从 Gutenberg URL 获取并使用文件系统搜索工具分析的文本，以下是对您问题的回答：

        **1) 包含子字符串 `Gatsby` 的行**
        **258** 行包含精确子字符串 `Gatsby`。

        **2) 包含 `Daisy` 的第一行**
        第 **181** 行是文件中包含精确子字符串 `Daisy` 的第一行。
        *（供参考，该行内容为："Buchanans. Daisy was my second cousin once removed, and I’d known Tom"）*

        **3) 两句中性概要**
        *《了不起的盖茨比》* 讲述了神秘的百万富翁杰伊·盖茨比在 1920 年代的长岛痴迷地追求与前情人黛西·布坎南重聚的故事。故事由尼克·卡拉韦叙述，他观察到盖茨比不懈野心的悲剧后果以及那个时代富有的精英阶层的肤浅物质主义。

        ***

        **计数是如何计算的：**
        从 URL 获取文档时，文件太大无法通过标准输出显示，系统自动将其保存到本地文件系统（`/large_tool_results/x246ax2x`）。然后我使用 `grep` 工具在保存的文件中搜索精确的字面子字符串 `Gatsby` 和 `Daisy`。`grep` 工具返回了每个匹配行及其从 1 开始的行号。我手动计算了为 `Gatsby` 返回的精确行数（总计 258 行），并确定了为 `Daisy` 返回的第一个行号（即 181）。我还验证了没有大写变体（`GATSBY` 或 `DAISY`）会被遗漏。此过程中未遇到任何错误。
        ```
      </Tab>
    </Tabs>

    如果你查看两个标签页上的输出，你会注意到 LangChain 代理提供了答案，但它们是估计值。代理缺乏回答此问题的工具。你也可能收到提示太长的错误。

    另一方面，深度代理可以：

    1. **规划其方法**，使用内置的 [`write_todos`](/oss/python/deepagents/harness#planning-capabilities) 工具来分解研究任务。
    2. **加载文件**，通过调用 `fetch_text_from_url` 工具来收集信息。
    3. **管理上下文**，使用文件系统工具（[`grep`](/oss/python/deepagents/harness#virtual-filesystem-access) 和 [`read_file`](/oss/python/deepagents/harness#virtual-filesystem-access)）。
    4. **生成子代理**，根据需要将复杂的子任务委托给专门的子代理。

    对于 LangChain 代理，你必须实现更多功能才能获得类似水平的服务，并且可以根据需要随时自定义它们。
  </Step>
</Steps>

## 跟踪代理调用

你使用 LangChain 构建的大多数有趣的应用程序都会对 LLM 进行多次调用。随着这些应用程序变得越来越复杂，能够检查代理内部到底发生了什么变得非常重要。最好的方法是使用 [LangSmith](https://smith.langchain.com)。

注册一个 [LangSmith](https://smith.langchain.com) 账户并设置以下环境变量以开始记录跟踪：

```shell theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
export LANGSMITH_TRACING="true"
export LANGSMITH_API_KEY="..."
```

设置完成后，再次运行你的脚本，然后在 [LangSmith](https://smith.langchain.com) 上检查代理调用期间发生了什么。

<Tip>
  要了解更多关于使用 LangSmith 跟踪代理的信息，请参阅 [LangSmith 文档](/langsmith/trace-with-langchain)。
</Tip>

## 后续步骤

你现在拥有的代理可以：

* **理解上下文**并记住对话
* **智能地使用工具**
* **以一致的格式提供结构化响应**
* **通过上下文处理用户特定信息**
* **在交互中维护对话状态**
* **规划、研究和综合**（仅限深度代理）

继续学习：

* **LangChain 代理**：[添加和管理记忆](/oss/python/langgraph/add-memory#manage-short-term-memory)，[部署到生产环境](/oss/python/langgraph/deploy)
* **深度代理**：[自定义选项](/oss/python/deepagents/customization)，[持久化记忆](/oss/python/deepagents/long-term-memory)，[部署到生产环境](/oss/python/langgraph/deploy)

***

<div className="source-links">
  <Callout icon="terminal-2">
    [将这些文档连接](/use-these-docs)到 Claude、VSCode 等，通过 MCP 获取实时答案。
  </Callout>

  <Callout icon="edit">
    [在 GitHub 上编辑此页面](https://github.com/langchain-ai/docs/edit/main/src/oss/langchain/quickstart.mdx) 或 [提交问题](https://github.com/langchain-ai/docs/issues/new/choose)。
  </Callout>
</div>
