> ## 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.

# ChatAnthropic 集成

> 使用 LangChain Python 集成 ChatAnthropic 聊天模型。

您可以在 [Claude](https://platform.claude.com/docs/en/about-claude/models/overview) 文档中找到有关 Anthropic 最新模型、其成本、上下文窗口和支持的输入类型的信息。

<Tip>
  **API 参考**

  有关所有功能和配置选项的详细文档，请访问 [`ChatAnthropic`](https://reference.langchain.com/python/langchain-anthropic/chat_models/ChatAnthropic) API 参考。
</Tip>

<Info>
  **AWS Bedrock 和 Google VertexAI**

  请注意，某些 Anthropic 模型也可以通过 AWS Bedrock 和 Google VertexAI 访问。请参阅 [`ChatBedrock`](/oss/python/integrations/chat/bedrock/) 和 [`ChatVertexAI`](/oss/python/integrations/chat/google_vertex_ai) 集成，以通过这些服务使用 Anthropic 模型。

  对于在 AWS Bedrock 上使用与 `ChatAnthropic` 相同 API 的 Anthropic 模型，请使用 `langchain-aws` 中的 [`ChatAnthropicBedrock`](/oss/python/integrations/chat/bedrock#chatanthropicbedrock)。
</Info>

## 概述

### 集成详情

| 类                                                                                                       | 包                                                                                    | 可序列化 |                               JS/TS 支持                               |                                                                                                 下载量                                                                                                |                                                                                                                    最新版本                                                                                                                    |
| :------------------------------------------------------------------------------------------------------ | :----------------------------------------------------------------------------------- | :--: | :------------------------------------------------------------------: | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: |
| [`ChatAnthropic`](https://reference.langchain.com/python/langchain-anthropic/chat_models/ChatAnthropic) | [`langchain-anthropic`](https://reference.langchain.com/python/langchain-anthropic/) | beta | ✅ [(npm)](https://js.langchain.com/docs/integrations/chat/anthropic) | <a href="https://pypi.org/project/langchain-anthropic/" target="_blank"><img src="https://static.pepy.tech/badge/langchain-anthropic/month" alt="每月下载量" noZoom height="100" class="rounded" /></a> | <a href="https://pypi.org/project/langchain-anthropic/" target="_blank"><img src="https://img.shields.io/pypi/v/langchain-anthropic?style=flat-square&label=%20&color=orange" alt="PyPI - 最新版本" noZoom height="100" class="rounded" /></a> |

### 模型特性

| [工具调用](/oss/python/langchain/tools) | [结构化输出](/oss/python/langchain/structured-output) | [图像输入](/oss/python/langchain/messages#multimodal) | 音频输入 | 视频输入 | [令牌级流式传输](/oss/python/langchain/streaming/) | 原生异步 | [令牌使用量](/oss/python/langchain/models#token-usage) | [对数概率](/oss/python/langchain/models#log-probabilities) |
| :---------------------------------: | :----------------------------------------------: | :-----------------------------------------------: | :--: | :--: | :-----------------------------------------: | :--: | :-----------------------------------------------: | :----------------------------------------------------: |
|                  ✅                  |                         ✅                        |                         ✅                         |   ❌  |   ❌  |                      ✅                      |   ✅  |                         ✅                         |                            ❌                           |

## 设置

要访问 Anthropic (Claude) 模型，您需要安装 `langchain-anthropic` 集成包并获取 [Claude](https://platform.claude.com/docs/en/get-started#prerequisites) API 密钥。

### 安装

<CodeGroup>
  ```bash pip theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  pip install -U langchain-anthropic
  ```

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

### 凭证

前往 [Claude 控制台](https://console.anthropic.com) 注册并生成 Claude API 密钥。完成此操作后，设置 `ANTHROPIC_API_KEY` 环境变量：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import getpass
import os

if "ANTHROPIC_API_KEY" not in os.environ:
    os.environ["ANTHROPIC_API_KEY"] = getpass.getpass("输入您的 Anthropic API 密钥：")
```

要启用模型调用的自动跟踪，请设置您的 [LangSmith](/langsmith/home) API 密钥：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
os.environ["LANGSMITH_API_KEY"] = getpass.getpass("输入您的 LangSmith API 密钥：")
os.environ["LANGSMITH_TRACING"] = "true"
```

## 实例化

现在我们可以实例化模型对象并生成聊天补全：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_anthropic import ChatAnthropic

model = ChatAnthropic(
    model="claude-haiku-4-5-20251001",
    # temperature=,
    # max_tokens=,
    # timeout=,
    # max_retries=,
    # ...
)
```

有关所有可用实例化参数的详细信息，请参阅 [`ChatAnthropic`](https://reference.langchain.com/python/langchain-anthropic/chat_models/ChatAnthropic) API 参考。

<Note>
  **推理地理位置**

  要控制模型推理运行的位置以实现[数据驻留](https://platform.claude.com/docs/en/build-with-claude/data-residency)，请在创建 [`ChatAnthropic`](https://reference.langchain.com/python/langchain-anthropic/chat_models/ChatAnthropic) 时传递 `inference_geo`。有关支持的值，请参阅 Anthropic 文档。
</Note>

## 调用

<AccordionGroup>
  <Accordion title="调用">
    ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    messages = [
        (
            "system",
            "你是一个有用的翻译员。将用户句子翻译成法语。",
        ),
        (
            "human",
            "我喜欢编程。",
        ),
    ]
    ai_msg = model.invoke(messages)
    ```

    ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    print(ai_msg.text)
    ```

    ```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    J'adore la programmation.
    ```
  </Accordion>

  <Accordion title="流式传输">
    ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    for chunk in model.stream(messages):
        print(chunk.text, end="")
    ```

    ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    AIMessageChunk(content="J", id="run-272ff5f9-8485-402c-b90d-eac8babc5b25")
    AIMessageChunk(content="'", id="run-272ff5f9-8485-402c-b90d-eac8babc5b25")
    AIMessageChunk(content="a", id="run-272ff5f9-8485-402c-b90d-eac8babc5b25")
    AIMessageChunk(content="ime", id="run-272ff5f9-8485-402c-b90d-eac8babc5b25")
    AIMessageChunk(content=" la", id="run-272ff5f9-8485-402c-b90d-eac8babc5b25")
    AIMessageChunk(content=" programm", id="run-272ff5f9-8485-402c-b90d-eac8babc5b25")
    AIMessageChunk(content="ation", id="run-272ff5f9-8485-402c-b90d-eac8babc5b25")
    AIMessageChunk(content=".", id="run-272ff5f9-8485-402c-b90d-eac8babc5b25")
    ```

    要从流中聚合完整消息：

    ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    stream = model.stream(messages)
    full = next(stream)
    for chunk in stream:
        full += chunk
    full
    ```

    ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    AIMessageChunk(content="J'aime la programmation.", id="run-b34faef0-882f-4869-a19c-ed2b856e6361")
    ```
  </Accordion>

  <Accordion title="异步">
    ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    ai_msg = await model.ainvoke(messages)

    # 流式传输
    async for chunk in model.astream(messages):
        print(chunk.text, end="")

    # 批处理
    await model.abatch([messages])
    ```

    ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    AIMessage(
        content="J'aime la programmation.",
        response_metadata={
            "id": "msg_01Trik66aiQ9Z1higrD5XFx3",
            "model": "claude-sonnet-4-6",
            "stop_reason": "end_turn",
            "stop_sequence": None,
            "usage": {"input_tokens": 25, "output_tokens": 11},
        },
        id="run-5886ac5f-3c2e-49f5-8a44-b1e92808c929-0",
        usage_metadata={
            "input_tokens": 25,
            "output_tokens": 11,
            "total_tokens": 36,
        },
    )
    ```
  </Accordion>
</AccordionGroup>

在我们的[模型](/oss/python/langchain/models#invocation)指南中了解更多关于支持的调用方法。

## 内容块

使用工具、[扩展思考](#扩展思考)和其他功能时，来自单个 Anthropic [`AIMessage`](https://reference.langchain.com/python/langchain-core/messages/ai/AIMessage) 的内容可以是单个字符串，也可以是 Anthropic 内容块的列表。

例如，当 Anthropic 模型调用工具时，工具调用是消息内容的一部分（同时也在标准化的 [`AIMessage.tool_calls`](https://reference.langchain.com/python/langchain/messages/#langchain.messages.AIMessage.tool_calls) 中暴露）：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_anthropic import ChatAnthropic
from typing_extensions import Annotated

model = ChatAnthropic(model="claude-haiku-4-5-20251001")


def get_weather(
    location: Annotated[str, ..., "位置，格式为城市和州。"]
) -> str:
    """获取某个位置的天气。"""
    return "今天是晴天。"


model_with_tools = model.bind_tools([get_weather])
response = model_with_tools.invoke("今天哪个城市更热：洛杉矶还是纽约？")
response.content
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
[{'text': "我将通过检查洛杉矶和纽约的当前天气来帮助您比较它们的温度。我将获取两个城市的天气。",
  'type': 'text'},
 {'id': 'toolu_01CkMaXrgmsNjTso7so94RJq',
  'input': {'location': 'Los Angeles, CA'},
  'name': 'get_weather',
  'type': 'tool_use'},
 {'id': 'toolu_01SKaTBk9wHjsBTw5mrPVSQf',
  'input': {'location': 'New York, NY'},
  'name': 'get_weather',
  'type': 'tool_use'}]
```

使用 `content_blocks` 将以 LangChain 的标准格式呈现内容，该格式与其他模型提供商保持一致。阅读更多关于[内容块](/oss/python/langchain/messages#standard-content-blocks)的信息。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
response.content_blocks
```

您也可以使用 `tool_calls` 属性以标准格式专门访问工具调用：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
response.tool_calls
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
[{'name': 'GetWeather',
  'args': {'location': 'Los Angeles, CA'},
  'id': 'toolu_01Ddzj5PkuZkrjF4tafzu54A'},
 {'name': 'GetWeather',
  'args': {'location': 'New York, NY'},
  'id': 'toolu_012kz4qHZQqD4qg8sFPeKqpP'}]
```

## 工具

Anthropic 的工具使用功能允许您定义 Claude 在对话期间可以调用的外部函数。这支持动态信息检索、计算以及与外部系统的交互。

有关如何将工具绑定到模型实例的详细信息，请参阅 [`ChatAnthropic.bind_tools`](https://reference.langchain.com/python/langchain-anthropic/chat_models/ChatAnthropic/bind_tools)。

<Note>
  有关 Claude 内置工具（代码执行、网页浏览、文件 API 等）的信息，请参阅[内置工具](#内置工具)。
</Note>

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from pydantic import BaseModel, Field


class GetWeather(BaseModel):
    '''获取给定位置的当前天气'''

    location: str = Field(description="城市和州，例如旧金山，加利福尼亚州")


class GetPopulation(BaseModel):
    '''获取给定位置的当前人口'''

    location: str = Field(description="城市和州，例如旧金山，加利福尼亚州")


model_with_tools = model.bind_tools([GetWeather, GetPopulation]) # [!code highlight]
ai_msg = model_with_tools.invoke("今天哪个城市更热，哪个更大：洛杉矶还是纽约？")
ai_msg.tool_calls
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
[
    {
        "name": "GetWeather",
        "args": {"location": "Los Angeles, CA"},
        "id": "toolu_01KzpPEAgzura7hpBqwHbWdo",
    },
    {
        "name": "GetWeather",
        "args": {"location": "New York, NY"},
        "id": "toolu_01JtgbVGVJbiSwtZk3Uycezx",
    },
    {
        "name": "GetPopulation",
        "args": {"location": "Los Angeles, CA"},
        "id": "toolu_01429aygngesudV9nTbCKGuw",
    },
    {
        "name": "GetPopulation",
        "args": {"location": "New York, NY"},
        "id": "toolu_01JPktyd44tVMeBcPPnFSEJG",
    },
]
```

### 严格工具使用

<Info>
  严格工具使用需要 `langchain-anthropic>=1.1.0`。有关支持的模型，请参阅 [Claude 文档](https://platform.claude.com/docs/en/agents-and-tools/tool-use/strict-tool-use)。
</Info>

Anthropic 支持选择加入[对工具调用的严格模式架构遵循](https://platform.claude.com/docs/en/build-with-claude/structured-outputs)。这通过约束解码保证工具名称和参数经过验证且类型正确。

没有严格模式时，Claude 偶尔会生成无效的工具输入，从而破坏您的应用程序：

* **类型不匹配**：`passengers: "2"` 而不是 `passengers: 2`
* **缺少必填字段**：省略函数期望的字段
* **无效的枚举值**：超出允许集合的值
* **架构违规**：嵌套对象与预期结构不匹配

严格工具使用保证符合架构的工具调用：

* 工具输入严格遵循您的 `input_schema`
* 保证字段类型和必填字段
* 消除对格式错误输入的错误处理
* 使用的工具 `name` 始终来自提供的工具

| 使用严格工具使用                    | 使用标准工具调用  |
| --------------------------- | --------- |
| 构建可靠性至关重要的智能体工作流            | 简单的单轮工具调用 |
| 具有许多参数或嵌套对象的工具              | 原型设计和实验   |
| 需要特定类型（例如 `int` 与 `str`）的函数 |           |

要启用严格工具使用，请在调用 [`bind_tools`](https://reference.langchain.com/python/langchain-anthropic/chat_models/ChatAnthropic/bind_tools) 时指定 `strict=True`。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_anthropic import ChatAnthropic

model = ChatAnthropic(model="claude-sonnet-4-6")

def get_weather(location: str) -> str:
    """获取某个位置的天气。"""
    return "今天是晴天。"

model_with_tools = model.bind_tools([get_weather], strict=True)  # [!code highlight]
```

<Accordion title="示例：类型安全的预订系统">
  考虑一个预订系统，其中 `passengers` 必须是整数：

  ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  from langchain_anthropic import ChatAnthropic
  from typing import Literal

  model = ChatAnthropic(model="claude-sonnet-4-6")

  def book_flight(
      destination: str,
      departure_date: str,
      passengers: int, # [!code highlight]
      cabin_class: Literal["economy", "business", "first"]
  ) -> str:
      """预订飞往目的地的航班。

      参数:
          destination: 目的地城市
          departure_date: 格式为 YYYY-MM-DD 的日期
          passengers: 乘客数量（必须是整数）
          cabin_class: 航班的舱位等级
      """
      return f"已预订 {passengers} 名乘客前往 {destination}"

  model_with_tools = model.bind_tools(
      [book_flight],
      strict=True, # [!code highlight]
      tool_choice="any",
  )
  response = model_with_tools.invoke("预订 2 名乘客前往东京，商务舱，2025-01-15")

  # 使用 strict=True，passengers 保证是 int，而不是 "2" 或 "two"
  print(response.tool_calls[0]["args"]["passengers"])
  ```

  ```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  2
  ```
</Accordion>

严格工具使用有一些需要了解的 JSON 架构限制。有关更多详细信息，请参阅 [Claude 文档](https://platform.claude.com/docs/en/build-with-claude/structured-outputs#json-schema-limitations)。

如果您的工具架构使用了不支持的功能，您将收到 400 错误。在这些情况下，请简化架构或使用标准（非严格）工具调用。

### 输入示例

对于复杂工具，您可以提供使用示例以帮助 Claude 正确理解如何使用它们。这是通过在工具的 `extras` 参数中设置 `input_examples` 来完成的。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_anthropic import ChatAnthropic
from langchain.tools import tool

@tool(
    extras={ # [!code highlight]
        "input_examples": [ # [!code highlight]
            { # [!code highlight]
                "query": "天气报告", # [!code highlight]
                "location": "旧金山", # [!code highlight]
                "format": "详细" # [!code highlight]
            }, # [!code highlight]
            { # [!code highlight]
                "query": "温度", # [!code highlight]
                "location": "纽约", # [!code highlight]
                "format": "简要" # [!code highlight]
            } # [!code highlight]
        ] # [!code highlight]
    } # [!code highlight]
)
def search_weather_data(query: str, location: str, format: str = "brief") -> str:
    """使用特定查询和格式偏好搜索天气数据库。

    参数:
        query: 要检索的天气信息类型
        location: 要搜索的城市或地区
        format: 输出格式，'brief' 或 'detailed'
    """
    return f"{format.title()} {query} for {location}: 找到数据"

model = ChatAnthropic(model="claude-sonnet-4-6")
model_with_tools = model.bind_tools([search_weather_data])

response = model_with_tools.invoke(
    "获取西雅图的详细天气报告"
)
```

`extras` 参数还支持：

* `defer_loading` (bool)：按需加载工具以用于[工具搜索](#工具搜索)
* `cache_control` (dict)：为工具启用[提示缓存](#缓存工具)
* `eager_input_streaming` (bool)：为该工具启用[细粒度工具流式传输](#细粒度工具流式传输)

### 细粒度工具流式传输

Anthropic 支持[细粒度工具流式传输](https://platform.claude.com/docs/en/agents-and-tools/tool-use/fine-grained-tool-streaming)，这可以减少流式传输具有大参数的工具调用时的延迟。

细粒度流式传输不是在传输前缓冲整个参数值，而是在参数数据可用时立即发送。对于大型工具参数，这可以将初始延迟从 15 秒减少到大约 3 秒。

<Warning>
  细粒度流式传输可能会返回无效或部分的 JSON 输入，特别是如果响应在完成之前达到 `max_tokens`。请为不完整的 JSON 数据实现适当的错误处理。
</Warning>

要为应该增量流式传输工具参数的工具启用细粒度工具流式传输，请在工具上设置 `extras={"eager_input_streaming": True}`。该值会传递到工具定义中的 Anthropic API。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_anthropic import ChatAnthropic
from langchain.tools import tool

model = ChatAnthropic(model="claude-sonnet-4-6")

@tool(extras={"eager_input_streaming": True})
def write_document(title: str, content: str) -> str:
    """使用给定的标题和内容编写文档。"""
    return f"文档 '{title}' 编写成功"

model_with_tools = model.bind_tools([write_document])

# 以降低的延迟流式传输工具调用
for chunk in model_with_tools.stream(
    "编写一份关于流式 API 优势的详细技术文档"
):
    print(chunk.content)
```

流式数据以 `input_json_delta` 块的形式到达 `chunk.content`。您可以累积这些块以构建完整的工具参数：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import json

from langchain_anthropic import ChatAnthropic
from langchain.tools import tool

model = ChatAnthropic(model="claude-sonnet-4-6")

@tool(extras={"eager_input_streaming": True})
def write_document(title: str, content: str) -> str:
    """使用给定的标题和内容编写文档。"""
    return f"文档 '{title}' 编写成功"

model_with_tools = model.bind_tools([write_document])

accumulated_json = ""

for chunk in model_with_tools.stream("编写一份关于 AI 的文档"):
    for block in chunk.content:
        if isinstance(block, dict) and block.get("type") == "input_json_delta":
            accumulated_json += block.get("partial_json", "")
            try:
                # 尝试解析累积的 JSON
                parsed = json.loads(accumulated_json)
                print(f"完整参数: {parsed}")
            except json.JSONDecodeError:
                # JSON 仍不完整，继续累积
                pass
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
完整参数: {'title': '人工智能：概述', 'content': '# 人工智能：概述...
```

### 编程式工具调用

<Info>
  编程式工具调用需要 `langchain-anthropic>=1.3.0`。有关支持的模型，请参阅 [Claude 文档](https://platform.claude.com/docs/en/agents-and-tools/tool-use/programmatic-tool-calling)。
</Info>

工具可以配置为可从 Claude 的[代码执行](#代码执行)环境调用，从而减少涉及大数据处理或多工具工作流的上下文中的延迟和令牌消耗。

有关详细信息，请参阅 Claude 的[编程式工具调用指南](https://platform.claude.com/docs/en/agents-and-tools/tool-use/programmatic-tool-calling)。要使用此功能：

* 在您的工具集中包含[代码执行](#代码执行)内置工具
* 在您希望编程式调用的工具上指定 `extras={"allowed_callers": ["code_execution_20250825"]}`

有关使用 [`create_agent`](/oss/python/langchain/agents) 的完整示例，请参见下文。

<Tip>
  您可以在初始化时指定 `reuse_last_container` 以自动重用先前模型响应的代码执行容器。
</Tip>

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


@tool(extras={"allowed_callers": ["code_execution_20250825"]}) # [!code highlight]
def get_weather(location: str) -> str:
    """获取某个位置的天气。"""
    return "今天是晴天。"

tools = [
    {"type": "code_execution_20250825", "name": "code_execution"}, # [!code highlight]
    get_weather,
]

model = ChatAnthropic(
    model="claude-sonnet-4-6",
    reuse_last_container=True,  # [!code highlight]
)

agent = create_agent(model, tools=tools)

input_query = {
    "role": "user",
    "content": "波士顿的天气怎么样？",
}

result = agent.invoke({"messages": [input_query]})
```

## 多模态

Claude 支持图像和 PDF 输入作为内容块，既支持 Anthropic 的原生格式（参见 [视觉](https://platform.claude.com/docs/en/build-with-claude/vision) 和 [PDF 支持](https://platform.claude.com/docs/en/build-with-claude/pdf-support) 文档），也支持 LangChain 的[标准格式](/oss/python/langchain/messages#multimodal)。

### 支持的输入方法

| 方法                                                                    |  图像 | PDF |
| --------------------------------------------------------------------- | :-: | :-: |
| Base64 内联数据                                                           |  ✅  |  ✅  |
| HTTP/HTTPS URL                                                        |  ✅  |  ✅  |
| [文件 API](https://platform.claude.com/docs/en/build-with-claude/files) |  ✅  |  ✅  |

<Tip>
  文件 API 也可用于将文件上传到容器，以供 Claude 的内置代码执行工具使用。有关详细信息，请参阅[代码执行](#代码执行)部分。
</Tip>

### 图像输入

使用具有列表内容格式的 [`HumanMessage`](https://reference.langchain.com/python/langchain-core/messages/human/HumanMessage) 提供图像输入和文本。

<CodeGroup>
  ```python URL theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  from langchain_anthropic import ChatAnthropic
  from langchain.messages import HumanMessage

  model = ChatAnthropic(model="claude-sonnet-4-6")

  message = HumanMessage(
      content=[
          {"type": "text", "text": "描述 URL 处的图像。"},
          {
              "type": "image",
              "url": "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg",
          },
      ]
  )
  response = model.invoke([message])
  ```

  ```python Base64 编码 theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  import base64
  import httpx
  from langchain_anthropic import ChatAnthropic
  from langchain.messages import HumanMessage

  model = ChatAnthropic(model="claude-sonnet-4-6")

  image_url = "https://picsum.photos/id/237/200/300"
  image_data = base64.b64encode(httpx.get(image_url, follow_redirects=True).content).decode("utf-8")

  message = HumanMessage(
      content=[
          {"type": "text", "text": "描述图像。"},
          { # [!code highlight]
              "type": "image", # [!code highlight]
              "base64": image_data, # [!code highlight]
              "mime_type": "image/jpeg", # [!code highlight]
          }, # [!code highlight]
      ]
  )
  response = model.invoke([message])
  ```

  ```python 文件 API theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  import anthropic
  from langchain_anthropic import ChatAnthropic
  from langchain.messages import HumanMessage

  client = anthropic.Anthropic()
  file = client.beta.files.upload(
      file=("image.png", open("/path/to/image.png", "rb"), "image/png"),
  )

  model = ChatAnthropic(
      model="claude-sonnet-4-6",
      betas=["files-api-2025-04-14"], # [!code highlight]
  )

  message = HumanMessage(
      content=[
          {"type": "text", "text": "描述此图像。"},
          {
              "type": "image",
              "file_id": file.id, # [!code highlight]
          },
      ]
  )
  response = model.invoke([message])
  ```
</CodeGroup>

### PDF 输入

提供 PDF 文件输入和文本。

<CodeGroup>
  ```python URL theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  from langchain_anthropic import ChatAnthropic
  from langchain.messages import HumanMessage

  model = ChatAnthropic(model="claude-sonnet-4-6")

  message = HumanMessage(
      content=[
          {"type": "text", "text": "总结此文档。"},
          {
              "type": "file",
              "url": "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf",
              "mime_type": "application/pdf",
          },
      ]
  )
  response = model.invoke([message])
  ```

  ```python Base64 编码 theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  import base64
  import httpx
  from langchain_anthropic import ChatAnthropic
  from langchain.messages import HumanMessage

  model = ChatAnthropic(model="claude-sonnet-4-6")

  pdf_url = "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf"
  pdf_data = base64.b64encode(httpx.get(pdf_url).content).decode("utf-8")

  message = HumanMessage(
      content=[
          {"type": "text", "text": "总结此文档。"},
          {
              "type": "file",
              "base64": pdf_data, # [!code highlight]
              "mime_type": "application/pdf", # [!code highlight]
          },
      ]
  )
  response = model.invoke([message])
  ```

  ```python 文件 API theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  import anthropic
  from langchain_anthropic import ChatAnthropic
  from langchain.messages import HumanMessage

  client = anthropic.Anthropic()
  file = client.beta.files.upload(
      file=("document.pdf", open("/path/to/document.pdf", "rb"), "application/pdf"),
  )

  model = ChatAnthropic(
      model="claude-sonnet-4-6",
      betas=["files-api-2025-04-14"], # [!code highlight]
  )

  message = HumanMessage(
      content=[
          {"type": "text", "text": "总结此文档。"},
          {
              "type": "file",
              "file_id": file.id, # [!code highlight]
          },
      ]
  )
  response = model.invoke([message])
  ```
</CodeGroup>

## 扩展思考

某些 Claude 模型支持[扩展思考](https://platform.claude.com/docs/en/build-with-claude/extended-thinking)功能，该功能将输出导致其最终答案的逐步推理过程。

有关兼容模型，请参阅 [Claude 文档](https://platform.claude.com/docs/en/build-with-claude/extended-thinking#supported-models)。

要使用扩展思考，请在初始化 [`ChatAnthropic`](https://reference.langchain.com/python/langchain-anthropic/chat_models/ChatAnthropic) 时指定 `thinking` 参数。如果需要，也可以在调用时作为参数传递。

对于 Claude Sonnet 及更早的模型，您需要指定令牌预算。对于 Claude Opus 4.6+，您可以使用自适应思考，它会自动确定预算。

<CodeGroup>
  ```python 使用令牌预算 theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  import json
  from langchain_anthropic import ChatAnthropic

  model = ChatAnthropic(
      model="claude-sonnet-4-6",
      max_tokens=5000,
      thinking={"type": "enabled", "budget_tokens": 2000}, # [!code highlight]
  )

  response = model.invoke("50.653 的立方根是多少？")
  print(json.dumps(response.content_blocks, indent=2))
  ```

  ```python 自适应 (Opus 4.6+) theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  import json
  from langchain_anthropic import ChatAnthropic

  model = ChatAnthropic(
      model="claude-opus-4-6",
      max_tokens=5000,
      thinking={"type": "adaptive"}, # [!code highlight]
  )

  response = model.invoke("50.653 的立方根是多少？")
  print(json.dumps(response.content_blocks, indent=2))
  ```

  ```python 调用参数 theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  import json
  from langchain_anthropic import ChatAnthropic

  model = ChatAnthropic(model="claude-sonnet-4-6")

  response = model.invoke(
      "50.653 的立方根是多少？",
      max_tokens=5000,
      thinking={"type": "enabled", "budget_tokens": 2000} # [!code highlight]
  )
  print(json.dumps(response.content_blocks, indent=2))
  ```
</CodeGroup>

```json theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
[
  {
    "type": "reasoning",
    "reasoning": "要找到 50.653 的立方根，我需要找到满足 $x^3 = 50.653$ 的 $x$ 值。\n\n我可以先尝试估算一下。\n$3^3 = 27$\n$4^3 = 64$\n\n所以 50.653 的立方根将在 3 到 4 之间，但更接近 4。\n\n让我尝试更精确地计算。我可以使用立方根函数：\n\n50.653 的立方根 = 50.653^(1/3)\n\n让我计算一下：\n50.653^(1/3) ≈ 3.6998\n\n让我验证一下：\n3.6998^3 ≈ 50.6533\n\n这非常接近 50.653，所以我确信 50.653 的立方根大约是 3.6998。\n\n实际上，让我更精确地计算一下：\n50.653^(1/3) ≈ 3.69981\n\n让我再验证一次：\n3.69981^3 ≈ 50.652998\n\n这极其接近 50.653，所以我会说 50.653 的立方根大约是 3.69981。",
    "extras": {"signature": "ErUBCkYIBxgCIkB0UjV..."}
  },
  {
    "type": "text",
    "text": "50.653 的立方根大约是 3.6998。\n\n验证：3.6998³ = 50.6530，这非常接近我们的原始数字。",
  }
]
```

<Warning>
  Claude Messages API 在 Claude Sonnet 3.7 和 Claude 4 模型之间对思考的处理方式不同。

  有关更多信息，请参阅 [Claude 文档](https://platform.claude.com/docs/en/build-with-claude/extended-thinking#differences-in-thinking-across-model-versions)。
</Warning>

## 努力程度

某些 Claude 模型支持[努力程度](https://platform.claude.com/docs/en/build-with-claude/effort)功能，该功能控制 Claude 响应时使用多少令牌。这对于平衡响应质量与延迟和成本非常有用。

<Info>
  **模型支持**

  努力程度在 Claude Opus 4.6 和 Claude Opus 4.5 上普遍可用。`max` 努力程度仅在 Claude Opus 4.6 上受支持。`xhigh` 努力程度在 Claude Opus 4.7 上受支持。Anthropic 可能会随时间增加或调整模型支持——请使用 [Claude 努力程度文档](https://platform.claude.com/docs/en/build-with-claude/effort)作为权威来源。
</Info>

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_anthropic import ChatAnthropic

model = ChatAnthropic(
    model="claude-opus-4-5-20251101",
    effort="medium",  # 选项："max"、"xhigh"、"high"、"medium"、"low" [!code highlight]
)

response = model.invoke("分析微服务与单体架构之间的权衡")
```

<Note>
  将 `effort` 设置为 `"high"` 产生的行为与完全省略该参数完全相同。
</Note>

有关何时使用不同努力程度以及查看支持模型的详细信息，请参阅 [Claude 文档](https://platform.claude.com/docs/en/build-with-claude/effort)。

## 任务预算

Claude Opus 4.7 支持[任务预算](https://platform.claude.com/docs/en/build-with-claude/task-budgets)，这是智能体循环（思考、工具调用、工具结果和最终输出）的建议令牌目标。模型会看到一个运行中的倒计时，并使用它来优先处理工作并优雅地完成。与 `max_tokens` 不同，任务预算不是硬性上限。

<Info>
  任务预算需要 `langchain-anthropic>=1.4.1`，目前处于 beta 阶段。
</Info>

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_anthropic import ChatAnthropic

model = ChatAnthropic(
    model="claude-opus-4-7",
    output_config={  # [!code highlight]
        "effort": "high",  # [!code highlight]
        "task_budget": {"type": "tokens", "total": 128_000},  # [!code highlight]
    },  # [!code highlight]
)
```

## 引用

Anthropic 支持[引用](https://platform.claude.com/docs/en/build-with-claude/citations)功能，该功能允许 Claude 根据用户提供的源文档为其答案附加上下文。

当查询中包含带有 `"citations": {"enabled": True}` 的[文档](https://platform.claude.com/docs/en/build-with-claude/citations#document-types)或 `search_result` 内容块时，Claude 可能会在其响应中生成引用。

### 简单示例

在此示例中，我们传递一个[纯文本文档](https://platform.claude.com/docs/en/build-with-claude/citations#plain-text-documents)。在后台，Claude [自动将](https://platform.claude.com/docs/en/build-with-claude/citations#plain-text-documents)输入文本分块为句子，这些句子在生成引用时使用。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_anthropic import ChatAnthropic

model = ChatAnthropic(model="claude-haiku-4-5-20251001")

messages = [
    {
        "role": "user",
        "content": [
            {
                "type": "document",
                "source": {
                    "type": "text",
                    "media_type": "text/plain",
                    "data": "草是绿色的。天空是蓝色的。",
                },
                "title": "我的文档",
                "context": "这是一份可靠的文档。",
                "citations": {"enabled": True},
            },
            {"type": "text", "text": "草和天空是什么颜色的？"},
        ],
    }
]
response = model.invoke(messages)
response.content
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
[{'text': '根据文档，', 'type': 'text'},
 {'text': '草是绿色的',
  'type': 'text',
  'citations': [{'type': 'char_location',
    'cited_text': '草是绿色的。',
    'document_index': 0,
    'document_title': '我的文档',
    'start_char_index': 0,
    'end_char_index': 20}]},
 {'text': '，而', 'type': 'text'},
 {'text': '天空是蓝色的',
  'type': 'text',
  'citations': [{'type': 'char_location',
    'cited_text': '天空是蓝色的。',
    'document_index': 0,
    'document_title': '我的文档',
    'start_char_index': 20,
    'end_char_index': 36}]},
 {'text': '。', 'type': 'text'}]
```

### 在工具结果中（智能体 RAG）

Claude 支持 [search\_result](https://platform.claude.com/docs/en/build-with-claude/search-results) 内容块，表示针对知识库或其他自定义源查询的可引用结果。这些内容块可以传递给 Claude，既可以作为顶级内容（如上例所示），也可以在工具结果中。这允许 Claude 使用工具调用的结果来引用其响应的元素。

要传递工具调用的响应搜索结果，请定义一个返回 Anthropic 原生格式的 `search_result` 内容块列表的工具。例如：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
def retrieval_tool(query: str) -> list[dict]:
    """访问我的知识库。"""

    # 运行搜索（例如，使用 LangChain 向量存储）
    results = vector_store.similarity_search(query=query, k=2)

    # 将结果打包成 search_result 块
    return [
        {
            "type": "search_result",
            # 根据需要自定义字段，使用文档元数据或其他方式
            "title": "我的文档标题",
            "source": "来源描述或出处",
            "citations": {"enabled": True},
            "content": [{"type": "text", "text": doc.page_content}],
        }
        for doc in results
    ]
```

<Accordion title="使用 LangGraph 的端到端示例">
  这里我们演示一个端到端示例，其中我们用示例文档填充 LangChain [向量存储](/oss/python/integrations/vectorstores/)，并为 Claude 配备一个查询这些文档的工具。

  这里的工具接受一个搜索查询和一个 `category` 字符串字面量，但可以使用任何有效的工具签名。

  此示例需要安装 `langchain-openai` 和 `numpy`：

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

  ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  from typing import Literal

  from langchain.chat_models import init_chat_model
  from langchain.embeddings import init_embeddings
  from langchain_core.documents import Document
  from langchain_core.vectorstores import InMemoryVectorStore
  from langgraph.checkpoint.memory import InMemorySaver
  from langchain.agents import create_agent


  # 设置向量存储
  # 确保您设置了 OPENAI_API_KEY 环境变量
  embeddings = init_embeddings("openai:text-embedding-3-small")
  vector_store = InMemoryVectorStore(embeddings)

  document_1 = Document(
      id="1",
      page_content=(
          "要申请休假天数，请通过 HR 门户提交休假申请表。"
          "批准将通过电子邮件发送。"
      ),
      metadata={
          "category": "HR 政策",
          "doc_title": "休假政策",
          "provenance": "休假政策 - 第 1 页",
      },
  )
  document_2 = Document(
      id="2",
      page_content="经理将在 3 个工作日内审核休假请求。",
      metadata={
          "category": "HR 政策",
          "doc_title": "休假政策",
          "provenance": "休假政策 - 第 2 页",
      },
  )
  document_3 = Document(
      id="3",
      page_content=(
          "任期超过 6 个月的员工每年有资格获得 20 天带薪休假。"
      ),
      metadata={
          "category": "福利政策",
          "doc_title": "2025 年福利指南",
          "provenance": "福利政策 - 第 1 页",
      },
  )

  documents = [document_1, document_2, document_3]
  vector_store.add_documents(documents=documents)


  # 定义工具
  async def retrieval_tool(
      query: str, category: Literal["HR 政策", "福利政策"]
  ) -> list[dict]:
      """访问我的知识库。"""

      def _filter_function(doc: Document) -> bool:
          return doc.metadata.get("category") == category

      results = vector_store.similarity_search(
          query=query, k=2, filter=_filter_function
      )

      return [
          {
              "type": "search_result",
              "title": doc.metadata["doc_title"],
              "source": doc.metadata["provenance"],
              "citations": {"enabled": True},
              "content": [{"type": "text", "text": doc.page_content}],
          }
          for doc in results
      ]



  # 创建智能体
  model = init_chat_model("claude-haiku-4-5-20251001")

  checkpointer = InMemorySaver()
  agent = create_agent(model, [retrieval_tool], checkpointer=checkpointer)


  # 在查询上调用
  config = {"configurable": {"thread_id": "session_1"}}

  input_message = {
      "role": "user",
      "content": "我如何申请休假天数？",
  }
  async for step in agent.astream(
      {"messages": [input_message]},
      config,
      stream_mode="values",
  ):
      step["messages"][-1].pretty_print()
  ```
</Accordion>

### 与文本分割器一起使用

Anthropic 还允许您使用[自定义文档](https://platform.claude.com/docs/en/build-with-claude/citations#custom-content-documents)类型指定自己的分割。LangChain [文本分割器](/oss/python/integrations/splitters/)可用于为此目的生成有意义的分割。参见下面的示例，我们分割 LangChain 的 `README.md`（一个 markdown 文档）并将其作为上下文传递给 Claude：

此示例需要安装 [`langchain-text-splitters`](https://reference.langchain.com/python/langchain-text-splitters/)：

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

```python expandable theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import requests
from langchain_anthropic import ChatAnthropic
from langchain_text_splitters import MarkdownTextSplitter


def format_to_anthropic_documents(documents: list[str]):
    return {
        "type": "document",
        "source": {
            "type": "content",
            "content": [{"type": "text", "text": document} for document in documents],
        },
        "citations": {"enabled": True},
    }


# 拉取 readme
get_response = requests.get(
    "https://raw.githubusercontent.com/langchain-ai/langchain/master/README.md"
)
readme = get_response.text

# 分割成块
splitter = MarkdownTextSplitter(
    chunk_overlap=0,
    chunk_size=50,
)
documents = splitter.split_text(readme)

# 构造消息
message = {
    "role": "user",
    "content": [
        format_to_anthropic_documents(documents),
        {"type": "text", "text": "给我一个 LangChain 教程的链接。"},
    ],
}

# 查询模型
model = ChatAnthropic(model="claude-haiku-4-5-20251001")
response = model.invoke([message])
```

## 提示缓存

Anthropic 支持对提示中的元素进行[缓存](https://platform.claude.com/docs/en/build-with-claude/prompt-caching)，包括消息、工具定义、工具结果、图像和文档。这允许您重用大型文档、指令、[少样本文档](/langsmith/create-few-shot-evaluators)和其他数据，以减少延迟和成本。

有两种启用提示缓存的方法：

* **[自动缓存](#自动缓存)**：在调用时传递 `cache_control`。系统自动将缓存断点应用于最后一个可缓存块，并在对话增长时向前移动。最适合多轮对话。
* **[显式缓存断点](#显式缓存断点)**：直接在单个内容块上放置 `cache_control`，以精确控制缓存的内容。

<Warning>
  只有某些 Claude 模型支持提示缓存。有关详细信息，请参阅 [Claude 文档](https://platform.claude.com/docs/en/build-with-claude/prompt-caching#supported-models)。
</Warning>

### 自动缓存

<Info>
  自动缓存需要 `langchain-anthropic>=1.4.0`。
</Info>

传递 `cache_control` 作为调用参数，以自动缓存所有内容，直到并包括最后一个可缓存块。在具有相同前缀的后续请求中，缓存的内容会自动重用。随着对话的增长，缓存断点会向前移动，因此您无需管理单个 `cache_control` 标记。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_anthropic import ChatAnthropic
import requests

model = ChatAnthropic(model="claude-sonnet-4-5")

# 拉取 LangChain readme
get_response = requests.get(
    "https://raw.githubusercontent.com/langchain-ai/langchain/master/README.md"
)
readme = get_response.text

messages = [
    {
        "role": "system",
        "content": [
            {
                "type": "text",
                "text": "你是一位技术专家。",
            },
            {
                "type": "text",
                "text": f"{readme}"
            },
        ],
    },
    {
        "role": "user",
        "content": "根据其 README，LangChain 是什么？",
    },
]

response = model.invoke(
    messages,
    cache_control={"type": "ephemeral"},  # [!code highlight]
)

usage = response.usage_metadata["input_token_details"]
print(f"使用情况:\n{usage}")

```

要进行 1 小时缓存，请指定 `ttl` 字段：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
response = model.invoke(
    messages,
    cache_control={"type": "ephemeral", "ttl": "1h"},  # [!code highlight]
)
```

### 显式缓存断点

要进行细粒度控制，请使用 `cache_control` 标记单个内容块。当您需要缓存以不同频率更改的不同部分时，这非常有用。

#### 消息

```python expandable theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import requests
from langchain_anthropic import ChatAnthropic


model = ChatAnthropic(model="claude-sonnet-4-5")

# 拉取 LangChain readme
get_response = requests.get(
    "https://raw.githubusercontent.com/langchain-ai/langchain/master/README.md"
)
readme = get_response.text

messages = [
    {
        "role": "system",
        "content": [
            {
                "type": "text",
                "text": "你是一位技术专家。",
            },
            {
                "type": "text",
                "text": f"{readme}",
                "cache_control": {"type": "ephemeral"},  # [!code highlight]
            },
        ],
    },
    {
        "role": "user",
        "content": "根据其 README，LangChain 是什么？",
    },
]

response_1 = model.invoke(messages)
response_2 = model.invoke(messages)

usage_1 = response_1.usage_metadata["input_token_details"]
usage_2 = response_2.usage_metadata["input_token_details"]

print(f"第一次调用:\n{usage_1}")
print(f"\n第二次:\n{usage_2}")
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
第一次调用:
{'cache_read': 0, 'cache_creation': 0, 'ephemeral_5m_input_tokens': 1569, 'ephemeral_1h_input_tokens': 0}

第二次:
{'cache_read': 1569, 'cache_creation': 0, 'ephemeral_5m_input_tokens': 0, 'ephemeral_1h_input_tokens': 0}
```

<Tip>
  **1 小时缓存**

  缓存生命周期默认为 5 分钟。要进行更长时间的缓存，请在 `cache_control` 字段中指定 `"ttl": "1h"`。1 小时缓存写入的成本是基础输入令牌价格的 2 倍（而 5 分钟缓存是 1.25 倍）。

  <Accordion title="示例">
    ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    model = ChatAnthropic(model="claude-sonnet-4-6")

    messages = [
        {
            "role": "user",
            "content": [
                {
                    "type": "text",
                    "text": f"{long_text}",
                    "cache_control": {"type": "ephemeral", "ttl": "1h"}, # [!code highlight]
                },
            ],
        }
    ]
    ```

    缓存令牌计数的详细信息将包含在响应的 [`usage_metadata`](https://reference.langchain.com/python/langchain-core/messages/ai/UsageMetadata) 的 [`InputTokenDetails`](https://reference.langchain.com/python/langchain-core/messages/ai/InputTokenDetails) 中：

    ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    response = model.invoke(messages)
    response.usage_metadata
    ```

    ```json theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    {
        "input_tokens": 1500,
        "output_tokens": 200,
        "total_tokens": 1700,
        "input_token_details": {
            "cache_read": 0,
            "cache_creation": 1000,
            "ephemeral_1h_input_tokens": 750,
            "ephemeral_5m_input_tokens": 250,
        }
    }
    ```
  </Accordion>
</Tip>

#### 缓存工具

```python expandable theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import requests

from langchain_anthropic import ChatAnthropic
from langchain.tools import tool


# 为演示目的，我们使用 LangChain README 文本人为地扩展了
# 工具描述。
get_response = requests.get(
    "https://raw.githubusercontent.com/langchain-ai/langchain/master/README.md"
)
readme = get_response.text
description = (
    "获取某个位置的天气。"
    f"顺便看看这个 readme：{readme}"
)


@tool(description=description, extras={"cache_control": {"type": "ephemeral"}})  # [!code highlight]
def get_weather(location: str) -> str:
    return "今天是晴天。"


model = ChatAnthropic(model="claude-sonnet-4-6")
model_with_tools = model.bind_tools([get_weather])
query = "旧金山的天气怎么样？"

response_1 = model_with_tools.invoke(query)
response_2 = model_with_tools.invoke(query)

usage_1 = response_1.usage_metadata["input_token_details"]
usage_2 = response_2.usage_metadata["input_token_details"]

print(f"第一次调用:\n{usage_1}")
print(f"\n第二次:\n{usage_2}")
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
第一次调用:
{'cache_read': 0, 'cache_creation': 1809}

第二次:
{'cache_read': 1809, 'cache_creation': 0}
```

#### 对话应用程序中的增量缓存

提示缓存可用于[多轮对话](https://platform.claude.com/docs/en/build-with-claude/prompt-caching#continuing-a-multi-turn-conversation)，以维护来自早期消息的上下文，而无需冗余处理。

我们可以通过用 `cache_control` 标记最后一条消息来启用增量缓存。Claude 将自动使用之前缓存的最长前缀来处理后续消息。

下面，我们实现一个包含此功能的简单聊天机器人。我们遵循 LangChain [聊天机器人教程](/oss/python/langchain/quickstart)，但添加了一个自定义[归约器](/oss/python/langgraph/graph-api#reducers)，该归约器自动用 `cache_control` 标记每个用户消息中的最后一个内容块：

<Accordion title="具有增量提示缓存的聊天机器人">
  ```python expandable theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  import requests
  from langchain_anthropic import ChatAnthropic
  from langgraph.checkpoint.memory import MemorySaver
  from langgraph.graph import START, StateGraph, add_messages
  from typing_extensions import Annotated, TypedDict


  model = ChatAnthropic(model="claude-sonnet-4-6")

  # 拉取 LangChain readme
  get_response = requests.get(
      "https://raw.githubusercontent.com/langchain-ai/langchain/master/README.md"
  )
  readme = get_response.text


  def messages_reducer(left: list, right: list) -> list:
      # 更新最后一条用户消息
      for i in range(len(right) - 1, -1, -1):
          if right[i].type == "human":
              right[i].content[-1]["cache_control"] = {"type": "ephemeral"}
              break

      return add_messages(left, right)


  class State(TypedDict):
      messages: Annotated[list, messages_reducer]


  workflow = StateGraph(state_schema=State)


  # 定义调用模型的函数
  def call_model(state: State):
      response = model.invoke(state["messages"])
      return {"messages": [response]}


  # 定义图中的（单个）节点
  workflow.add_edge(START, "model")
  workflow.add_node("model", call_model)

  # 添加内存
  memory = MemorySaver()
  app = workflow.compile(checkpointer=memory)
  ```

  ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  from langchain.messages import HumanMessage

  config = {"configurable": {"thread_id": "abc123"}}

  query = "你好！我是鲍勃。"

  input_message = HumanMessage([{"type": "text", "text": query}])
  output = app.invoke({"messages": [input_message]}, config)
  output["messages"][-1].pretty_print()
  print(f"\n{output['messages'][-1].usage_metadata['input_token_details']}")
  ```

  ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  ================================== AI 消息 ==================================

  你好，鲍勃！很高兴认识你。你今天过得怎么样？有什么我可以帮你的吗？

  {'cache_read': 0, 'cache_creation': 0, 'ephemeral_5m_input_tokens': 0, 'ephemeral_1h_input_tokens': 0}
  ```

  ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  query = f"看看这个 readme：{readme}"

  input_message = HumanMessage([{"type": "text", "text": query}])
  output = app.invoke({"messages": [input_message]}, config)
  output["messages"][-1].pretty_print()
  print(f"\n{output['messages'][-1].usage_metadata['input_token_details']}")
  ```

  ```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  ================================== AI 消息 ==================================

  我看到你分享了 LangChain GitHub 仓库的 README。这是 LangChain 的文档，LangChain 是一个用于构建由大型语言模型 (LLM) 驱动的应用程序的流行框架。以下是 README 内容的摘要：

  LangChain 是：
  - 一个用于开发 LLM 驱动应用程序的框架
  - 帮助链接组件和集成以简化 AI 应用程序开发
  - 为模型、嵌入、向量存储等提供标准接口

  主要功能/优势：
  - 实时数据增强（将 LLM 连接到各种数据源）
  - 模型互操作性（根据需要轻松交换模型）
  - 大型集成生态系统

  LangChain 生态系统包括：
  - LangSmith - 用于评估和可观察性
  - LangGraph - 用于构建具有可定制架构的复杂智能体
  - LangSmith - 用于智能体的部署和扩展

  README 还提到了安装说明（`pip install -U langchain`）并链接到各种资源，包括教程、操作指南、概念指南和 API 参考。

  鲍勃，关于 LangChain 有什么你想了解的吗？

  {'cache_read': 0, 'cache_creation': 1846, 'ephemeral_5m_input_tokens': 1846, 'ephemeral_1h_input_tokens': 0}
  ```

  ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  query = "我叫什么名字来着？"

  input_message = HumanMessage([{"type": "text", "text": query}])
  output = app.invoke({"messages": [input_message]}, config)
  output["messages"][-1].pretty_print()
  print(f"\n{output['messages'][-1].usage_metadata['input_token_details']}")
  ```

  ```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  ================================== AI 消息 ==================================

  你叫鲍勃。你在对话开始时做了自我介绍。

  {'cache_read': 1846, 'cache_creation': 278, 'ephemeral_5m_input_tokens': 278, 'ephemeral_1h_input_tokens': 0}
  ```

  在 [LangSmith 跟踪](https://smith.langchain.com/public/4d0584d8-5f9e-4b91-8704-93ba2ccf416a/r)中，切换“原始输出”将显示发送给聊天模型的确切消息，包括 `cache_control` 键。
</Accordion>

## 令牌计数

您可以在将消息发送给模型之前使用 [`get_num_tokens_from_messages()`](https://reference.langchain.com/python/langchain-anthropic/chat_models/ChatAnthropic/get_num_tokens_from_messages) 计算消息中的令牌数。这使用 Anthropic 的官方[令牌计数 API](https://platform.claude.com/docs/en/build-with-claude/token-counting)。

<AccordionGroup>
  <Accordion title="消息令牌计数">
    ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    from langchain_anthropic import ChatAnthropic
    from langchain.messages import HumanMessage, SystemMessage

    model = ChatAnthropic(model="claude-sonnet-4-6")

    messages = [
        SystemMessage(content="你是一位科学家"),
        HumanMessage(content="你好，Claude"),
    ]

    token_count = model.get_num_tokens_from_messages(messages)
    print(token_count)
    ```

    ```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    14
    ```
  </Accordion>

  <Accordion title="工具令牌计数">
    使用工具时也可以计算令牌：

    ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    from langchain.tools import tool

    @tool(parse_docstring=True)
    def get_weather(location: str) -> str:
        """获取给定位置的当前天气

        参数:
            location: 城市和州，例如旧金山，加利福尼亚州
        """
        return "晴天"

    messages = [
        HumanMessage(content="旧金山的天气怎么样？"),
    ]

    token_count = model.get_num_tokens_from_messages(messages, tools=[get_weather])
    print(token_count)
    ```

    ```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    586
    ```
  </Accordion>
</AccordionGroup>

## 上下文管理

Anthropic 支持上下文管理功能，可自动管理模型的上下文窗口以优化性能和成本。

有关更多详细信息和配置选项，请参阅 [Claude 文档](https://platform.claude.com/docs/en/build-with-claude/context-editing)。

### 清除工具使用

从上下文中清除工具结果以减少令牌使用，同时保留对话流程。

<Info>
  **上下文管理自 `langchain-anthropic>=0.3.21` 起受支持**

  您必须指定 `context-management-2025-06-27` beta 头才能将上下文管理应用于您的模型调用。
</Info>

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_anthropic import ChatAnthropic

model = ChatAnthropic(
    model="claude-sonnet-4-6",
    betas=["context-management-2025-06-27"], # [!code highlight]
    context_management={"edits": [{"type": "clear_tool_uses_20250919"}]}, # [!code highlight]
)
model_with_tools = model.bind_tools([{"type": "web_search_20250305", "name": "web_search"}])
response = model_with_tools.invoke("搜索 AI 的最新进展")
```

### 自动压缩

Claude Opus 4.6 支持自动[服务器端压缩](https://platform.claude.com/docs/en/build-with-claude/compaction)，当上下文窗口接近其限制时，它会智能地压缩对话历史记录。这允许进行更长的对话，而无需手动管理上下文。

<Info>
  **自动压缩要求：**

  * Claude Opus 4.6
  * `langchain-anthropic>=1.3.0`
  * `compact-2026-01-12` beta 头
</Info>

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_anthropic import ChatAnthropic

model = ChatAnthropic(
    model="claude-opus-4-6",
    betas=["compact-2026-01-12"], # [!code highlight]
    max_tokens=4096,
    context_management={ # [!code highlight]
        "edits": [ # [!code highlight]
            { # [!code highlight]
                "type": "compact_20260112", # [!code highlight]
                "trigger": {"type": "input_tokens", "value": 50000}, # [!code highlight]
            } # [!code highlight]
        ] # [!code highlight]
    }, # [!code highlight]
)
```

有关[触发器配置](https://platform.claude.com/docs/en/build-with-claude/compaction#trigger-configuration)的详细信息，请参阅 Anthropic 的文档。

当触发压缩事件时，`ChatAnthropic` 将返回表示提示状态的[压缩块](https://platform.claude.com/docs/en/build-with-claude/compaction#working-with-compaction-blocks)。在多轮应用程序中，这些块应保留在传递回模型的消息历史记录中。

## 结构化输出

<Info>
  结构化输出需要 `langchain-anthropic>=1.1.0`。有关支持的模型，请参阅 [Claude 文档](https://platform.claude.com/docs/en/build-with-claude/structured-outputs)。
</Info>

Anthropic 支持原生[结构化输出功能](https://platform.claude.com/docs/en/build-with-claude/structured-outputs)，该功能保证其响应遵循给定的架构。

您可以在单个模型调用中访问此功能，也可以通过指定 LangChain [智能体](/oss/python/langchain/agents)的[响应格式](/oss/python/langchain/structured-output)来访问。有关示例，请参见下文。

<Accordion title="单个模型调用">
  使用 [`with_structured_output`](/oss/python/langchain/models#structured-output) 方法生成结构化的模型响应。指定 `method="json_schema"` 以启用 Anthropic 的原生结构化输出功能；否则该方法默认使用函数调用。

  ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  from langchain_anthropic import ChatAnthropic
  from pydantic import BaseModel, Field

  model = ChatAnthropic(model="claude-sonnet-4-6")

  class Movie(BaseModel):
      """一部包含详情的电影。"""
      title: str = Field(description="电影标题")
      year: int = Field(description="电影发行年份")
      director: str = Field(description="电影导演")
      rating: float = Field(description="电影评分，满分 10 分")

  model_with_structure = model.with_structured_output(Movie, method="json_schema")  # [!code highlight]
  response = model_with_structure.invoke("提供关于电影《盗梦空间》的详情")
  response
  ```

  ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  Movie(title='盗梦空间', year=2010, director='克里斯托弗·诺兰', rating=8.8)
  ```
</Accordion>

<Accordion title="智能体响应格式">
  使用 [`ProviderStrategy`](/oss/python/langchain/agents#providerstrategy) 指定 `response_format`，以在生成最终响应时启用 Anthropic 的结构化输出功能。

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

  class Weather(BaseModel):
      temperature: float
      condition: str

  def weather_tool(location: str) -> str:
      """获取某个位置的天气。"""
      return "晴天，75 华氏度。"

  agent = create_agent(
      model="anthropic:claude-sonnet-4-5",
      tools=[weather_tool],
      response_format=ProviderStrategy(Weather),  # [!code highlight]
  )

  result = agent.invoke({
      "messages": [{"role": "user", "content": "旧金山的天气怎么样？"}]
  })

  result["structured_response"]
  ```

  ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  Weather(temperature=75.0, condition='晴天')
  ```
</Accordion>

## 内置工具

Anthropic 支持多种内置客户端和服务器端[工具](/oss/python/langchain/tools/)。

服务器端工具（例如 [web search](#web-search)）传递给模型并由 Anthropic 执行。客户端工具（例如 [bash tool](#bash-tool)）需要您在应用程序中实现回调执行逻辑并将结果返回给模型。

在任何一种情况下，您都可以通过在模型实例上使用 [`bind_tools`](https://reference.langchain.com/python/langchain-anthropic/chat_models/ChatAnthropic/bind_tools) 使工具可被聊天模型访问。

重要的是，客户端工具需要您实现执行逻辑。有关示例，请参阅下面的相关部分。

<Info>
  **中间件与工具**

  对于客户端工具（例如 [bash](#bash-tool)、[text editor](#text-editor)、[memory](#memory-tool)），您可以选择使用[中间件](/oss/python/integrations/middleware/anthropic)，它们提供生产就绪的实现，包含内置执行、状态管理和安全策略。

  当您想要一个开箱即用的解决方案时使用中间件；当您需要自定义执行逻辑或想要直接使用 [`bind_tools`](https://reference.langchain.com/python/langchain-anthropic/chat_models/ChatAnthropic/bind_tools) 时使用下面记录的工具。
</Info>

<Note>
  **Beta 工具**

  如果将 beta 工具绑定到您的聊天模型，LangChain 将自动为您添加所需的 beta 头。
</Note>

### Bash 工具

Claude 支持客户端 [bash 工具](https://platform.claude.com/docs/en/agents-and-tools/tool-use/bash-tool)，允许它在持久的 bash 会话中执行 shell 命令。这支持系统操作、脚本执行和命令行自动化。

<Note>
  **重要：您必须提供执行环境**

  LangChain 处理 API 集成（发送/接收工具调用），但**您负责**：

  * 设置沙盒计算环境（Docker、VM 等）
  * 实现命令执行和输出捕获
  * 在智能体循环中将结果传递回 Claude

  有关实现指导，请参阅 [Claude bash 工具文档](https://platform.claude.com/docs/en/agents-and-tools/tool-use/bash-tool)。
</Note>

<Info>
  **要求：**

  * Claude 4 模型或 Claude Sonnet 3.7
</Info>

<Tabs>
  <Tab title="Anthropic 类型">
    ```python expandable theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    import subprocess

    from anthropic.types.beta import BetaToolBash20250124Param  # [!code highlight]
    from langchain_anthropic import ChatAnthropic
    from langchain.messages import HumanMessage, ToolMessage
    from langchain.tools import tool

    tool_spec = BetaToolBash20250124Param(  # [!code highlight]
        name="bash",  # [!code highlight]
        type="bash_20250124",  # [!code highlight]
    )  # [!code highlight]


    @tool(extras={"provider_tool_definition": tool_spec})  # [!code highlight]
    def bash(*, command: str, restart: bool = False, **kw):
        """执行 bash 命令。"""
        if restart:
            return "Bash 会话已重启"
        try:
            result = subprocess.run(
                command,
                shell=True,
                capture_output=True,
                text=True,
                timeout=30,
            )
            return result.stdout + result.stderr
        except Exception as e:
            return f"错误: {e}"


    model = ChatAnthropic(model="claude-sonnet-4-6")
    model_with_bash = model.bind_tools([bash]) # [!code highlight]

    # 初始请求
    messages = [HumanMessage("列出当前目录中的所有文件")]
    response = model_with_bash.invoke(messages)
    print(response.content_blocks)

    # 工具执行循环
    while response.tool_calls:
        # 执行每个工具调用
        tool_messages = []
        for tool_call in response.tool_calls:
            result = bash.invoke(tool_call)
            tool_messages.append(result)

        # 使用工具结果继续对话
        messages = [*messages, response, *tool_messages]
        response = model_with_bash.invoke(messages)
        print(response.content_blocks)
    ```
  </Tab>

  <Tab title="create_agent">
    ```python expandable theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    import subprocess

    from anthropic.types.beta import BetaToolBash20250124Param # [!code highlight]
    from langchain.agents import create_agent
    from langchain_anthropic import ChatAnthropic
    from langchain.tools import tool

    tool_spec = BetaToolBash20250124Param( # [!code highlight]
        name="bash", # [!code highlight]
        type="bash_20250124", # [!code highlight]
    ) # [!code highlight]


    @tool(extras={"provider_tool_definition": tool_spec}) # [!code highlight]
    def bash(*, command: str, restart: bool = False, **kw):
        """执行 bash 命令。"""
        if restart:
            return "Bash 会话已重启"
        result = subprocess.run(
            command,
            shell=True,
            capture_output=True,
            text=True,
        )
        return result.stdout + result.stderr


    agent = create_agent(
        model=ChatAnthropic(model="claude-sonnet-4-6"),
        tools=[bash],  # [!code highlight]
    )

    result = agent.invoke({"messages": [{"role": "user", "content": "列出文件"}]})

    for message in result["messages"]:
        message.pretty_print()
    ```
  </Tab>

  <Tab title="字典">
    ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    from langchain_anthropic import ChatAnthropic

    model = ChatAnthropic(model="claude-sonnet-4-6")

    bash_tool = { # [!code highlight]
        "type": "bash_20250124", # [!code highlight]
        "name": "bash", # [!code highlight]
    } # [!code highlight]

    model_with_bash = model.bind_tools([bash_tool]) # [!code highlight]
    response = model_with_bash.invoke(
        "列出当前目录中的所有 Python 文件"
    )
    # 您必须通过工具执行循环在 response.tool_calls 中处理 bash 命令的执行
    ```

    使用 [`create_agent`](https://reference.langchain.com/python/langchain/agents/factory/create_agent) 会自动处理工具执行循环。

    `response.tool_calls` 将包含 Claude 想要执行的 bash 命令。您必须在您的环境中运行此命令并将结果传递回去。

    ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    [{'type': 'text',
    'text': "我将为您列出当前目录中的 Python 文件。"},
    {'type': 'tool_call',
    'name': 'bash',
    'args': {'command': 'ls -la *.py'},
    'id': 'toolu_01ABC123...'}]
    ```
  </Tab>
</Tabs>

bash 工具支持两个参数：

* `command`（必需）：要执行的 bash 命令
* `restart`（可选）：设置为 `true` 以重启 bash 会话

<Tip>
  要获得“开箱即用”的实现，请考虑使用 [`ClaudeBashToolMiddleware`](/oss/python/integrations/middleware/anthropic#bash-tool)，它提供持久会话、Docker 隔离、输出编辑和开箱即用的启动/关闭命令。
</Tip>

### 代码执行

Claude 可以使用服务器端[代码执行工具](https://platform.claude.com/docs/en/agents-and-tools/tool-use/code-execution-tool)在沙盒环境中执行代码。

<Info>
  Anthropic 的 `2025-08-25` 代码执行工具自 `langchain-anthropic>=1.0.3` 起受支持。

  旧版 [`2025-05-22`](https://platform.claude.com/docs/en/agents-and-tools/tool-use/code-execution-tool#upgrade-to-latest-tool-version) 工具自 `langchain-anthropic>=0.3.14` 起受支持。
</Info>

<Note>
  代码沙盒没有互联网访问权限，因此您只能使用环境中预安装的包。有关更多信息，请参阅 [Claude 文档](https://platform.claude.com/docs/en/agents-and-tools/tool-use/code-execution-tool#networking-and-security)。
</Note>

<Tabs>
  <Tab title="Anthropic 类型">
    ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    from anthropic.types.beta import BetaCodeExecutionTool20250825Param # [!code highlight]
    from langchain_anthropic import ChatAnthropic

    model = ChatAnthropic(
        model="claude-sonnet-4-6",
        # （可选）启用下面的参数以自动
        # 从先前的响应中传回容器 ID
        reuse_last_container=True,
    )

    code_tool = BetaCodeExecutionTool20250825Param( # [!code highlight]
        name="code_execution", # [!code highlight]
        type="code_execution_20250825", # [!code highlight]
    ) # [!code highlight]
    model_with_tools = model.bind_tools([code_tool]) # [!code highlight]

    response = model_with_tools.invoke(
        "计算 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 的平均值和标准差"
    )
    ```
  </Tab>

  <Tab title="create_agent">
    ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    from anthropic.types.beta import BetaCodeExecutionTool20250825Param # [!code highlight]
    from langchain.agents import create_agent
    from langchain_anthropic import ChatAnthropic

    code_tool = BetaCodeExecutionTool20250825Param( # [!code highlight]
        name="code_execution", # [!code highlight]
        type="code_execution_20250825", # [!code highlight]
    ) # [!code highlight]

    agent = create_agent(
        model=ChatAnthropic(model="claude-sonnet-4-6"),
        tools=[code_tool],  # [!code highlight]
    )

    result = agent.invoke({
        "messages": [{"role": "user", "content": "计算 [1,2,3,4,5] 的平均值和标准差"}]
    })

    for message in result["messages"]:
        message.pretty_print()
    ```
  </Tab>

  <Tab title="字典">
    ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    from langchain_anthropic import ChatAnthropic

    model = ChatAnthropic(
        model="claude-sonnet-4-6",
    )

    code_tool = {"type": "code_execution_20250825", "name": "code_execution"} # [!code highlight]
    model_with_tools = model.bind_tools([code_tool])

    response = model_with_tools.invoke(
        "计算 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 的平均值和标准差"
    )
    ```
  </Tab>
</Tabs>

<Accordion title="与文件 API 一起使用">
  使用文件 API，Claude 可以编写代码来访问文件以进行数据分析和其他目的。请参见下面的示例：

  ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  import anthropic
  from anthropic.types.beta import BetaCodeExecutionTool20250825Param # [!code highlight]
  from langchain_anthropic import ChatAnthropic


  client = anthropic.Anthropic()
  file = client.beta.files.upload(
      file=open("/path/to/sample_data.csv", "rb")
  )
  file_id = file.id


  # 运行推理
  model = ChatAnthropic(
      model="claude-sonnet-4-6",
  )

  code_tool = BetaCodeExecutionTool20250825Param( # [!code highlight]
      name="code_execution", # [!code highlight]
      type="code_execution_20250825", # [!code highlight]
  ) # [!code highlight]
  model_with_tools = model.bind_tools([code_tool])

  input_message = {
      "role": "user",
      "content": [
          {
              "type": "text",
              "text": "请绘制这些数据并告诉我你看到了什么。",
          },
          {
              "type": "container_upload",
              "file_id": file_id,
          },
      ]
  }
  response = model_with_tools.invoke([input_message])
  ```

  请注意，Claude 可能会在其代码执行过程中生成文件。您可以使用文件 API 访问这些文件：

  ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  # 为演示目的获取所有文件输出
  file_ids = []
  for block in response.content:
      if block["type"] == "bash_code_execution_tool_result":
          file_ids.extend(
              content["file_id"]
              for content in block.get("content", {}).get("content", [])
              if "file_id" in content
          )

  for i, file_id in enumerate(file_ids):
      file_content = client.beta.files.download(file_id)
      file_content.write_to_file(f"/path/to/file_{i}.png")
  ```

  <Note>
    **可用工具版本：**

    * `code_execution_20250522`（旧版）
    * `code_execution_20250825`（推荐）
  </Note>
</Accordion>

### 计算机使用

Claude 支持客户端[计算机使用](https://platform.claude.com/docs/en/agents-and-tools/tool-use/computer-use-tool)功能，允许它通过屏幕截图、鼠标控制和键盘输入与桌面环境交互。

<Note>
  **重要：您必须提供执行环境**

  LangChain 处理 API 集成（发送/接收工具调用），但**您负责**：

  * 设置沙盒计算环境（Linux VM、Docker 容器等）
  * 实现虚拟显示（例如 Xvfb）
  * 执行 Claude 的工具调用（屏幕截图、鼠标点击、键盘输入）
  * 在智能体循环中将结果传递回 Claude

  Anthropic 提供了一个[参考实现](https://github.com/anthropics/anthropic-quickstarts/tree/main/computer-use-demo)来帮助您入门。
</Note>

<Info>
  **要求：**

  * Claude Opus 4.5、Claude 4 或 Claude Sonnet 3.7
</Info>

<Tabs>
  <Tab title="Anthropic 类型">
    ```python expandable theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    import base64
    from typing import Literal

    from anthropic.types.beta import BetaToolComputerUse20250124Param # [!code highlight]
    from langchain_anthropic import ChatAnthropic
    from langchain.messages import HumanMessage, ToolMessage
    from langchain.tools import tool

    DISPLAY_WIDTH = 1024
    DISPLAY_HEIGHT = 768

    tool_spec = BetaToolComputerUse20250124Param( # [!code highlight]
        name="computer", # [!code highlight]
        type="computer_20250124", # [!code highlight]
        display_width_px=DISPLAY_WIDTH, # [!code highlight]
        display_height_px=DISPLAY_HEIGHT, # [!code highlight]
        display_number=1, # [!code highlight]
    ) # [!code highlight]

    @tool(extras={"provider_tool_definition": tool_spec}) # [!code highlight]
    def computer(
        *,
        action: Literal[
            "key", "type", "mouse_move", "left_click", "left_click_drag",
            "right_click", "middle_click", "double_click", "screenshot",
            "cursor_position", "scroll"
        ],
        coordinate: list[int] | None = None,
        text: str | None = None,
        **kw
    ):
        """控制计算机显示。"""
        if action == "screenshot":
            # 截取屏幕截图并返回 base64 编码的图像
            # 实现取决于您的显示设置（例如 Xvfb、pyautogui）
            return {"type": "image", "data": "base64_screenshot_data..."}
        elif action == "left_click" and coordinate:
            # 在坐标处执行点击
            return f"点击了 {coordinate}"
        elif action == "type" and text:
            # 输入文本
            return f"输入了: {text}"
        # ... 实现其他操作
        return f"执行了 {action}"

    model = ChatAnthropic(model="claude-sonnet-4-6")
    model_with_computer = model.bind_tools([computer]) # [!code highlight]

    # 初始请求
    messages = [HumanMessage("截取屏幕截图以查看屏幕上的内容")]
    response = model_with_computer.invoke(messages)
    print(response.content_blocks)

    # 工具执行循环
    while response.tool_calls:
        tool_messages = []
        for tool_call in response.tool_calls:
            result = computer.invoke(tool_call)
            tool_messages.append(
                ToolMessage(content=str(result), tool_call_id=tool_call["id"])
            )

        messages = [*messages, response, *tool_messages]
        response = model_with_computer.invoke(messages)
        print(response.content_blocks)
    ```
  </Tab>

  <Tab title="create_agent">
    ```python expandable theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    from typing import Literal

    from anthropic.types.beta import BetaToolComputerUse20250124Param # [!code highlight]
    from langchain.agents import create_agent
    from langchain_anthropic import ChatAnthropic
    from langchain.tools import tool

    tool_spec = BetaToolComputerUse20250124Param( # [!code highlight]
        name="computer", # [!code highlight]
        type="computer_20250124", # [!code highlight]
        display_width_px=1024, # [!code highlight]
        display_height_px=768, # [!code highlight]
    ) # [!code highlight]


    @tool(extras={"provider_tool_definition": tool_spec}) # [!code highlight]
    def computer(
        *,
        action: Literal[
            "key", "type", "mouse_move", "left_click", "left_click_drag",
            "right_click", "middle_click", "double_click", "screenshot",
            "cursor_position", "scroll"
        ],
        coordinate: list[int] | None = None,
        text: str | None = None,
        **kw
    ):
        """控制计算机显示。"""
        if action == "screenshot":
            return {"type": "image", "data": "base64_screenshot_data..."}
        elif action == "left_click" and coordinate:
            return f"点击了 {coordinate}"
        elif action == "type" and text:
            return f"输入了: {text}"
        return f"执行了 {action}"


    agent = create_agent(
        model=ChatAnthropic(model="claude-sonnet-4-6"),
        tools=[computer],  # [!code highlight]
    )

    result = agent.invoke({
        "messages": [{"role": "user", "content": "截取屏幕截图"}]
    })

    for message in result["messages"]:
        message.pretty_print()
    ```
  </Tab>

  <Tab title="字典">
    ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    from langchain_anthropic import ChatAnthropic

    model = ChatAnthropic(model="claude-sonnet-4-6")

    computer_tool = {
        "type": "computer_20250124",
        "name": "computer",
        "display_width_px": 1024,
        "display_height_px": 768,
        "display_number": 1,
    }

    model_with_computer = model.bind_tools([computer_tool]) # [!code highlight]
    response = model_with_computer.invoke(
        "截取屏幕截图以查看屏幕上的内容"
    )
    # 您必须通过工具执行循环在 response.tool_calls 中处理计算机操作的执行
    ```

    使用 [`create_agent`](https://reference.langchain.com/python/langchain/agents/factory/create_agent) 会自动处理工具执行循环。

    `response.tool_calls` 将包含 Claude 想要执行的计算机操作。您必须在您的环境中执行此操作并将结果传递回去。

    ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    [{'type': 'text',
    'text': "我将截取屏幕截图以查看当前屏幕上的内容。"},
    {'type': 'tool_call',
    'name': 'computer',
    'args': {'action': 'screenshot'},
    'id': 'toolu_01RNsqAE7dDZujELtacNeYv9'}]
    ```
  </Tab>
</Tabs>

<Note>
  **可用工具版本：**

  * `computer_20250124`（适用于 Claude 4 和 Claude Sonnet 3.7）
  * `computer_20251124`（适用于 Claude Opus 4.5）
</Note>

### 远程 MCP

Claude 可以使用服务器端 [MCP 连接器工具](https://platform.claude.com/docs/en/agents-and-tools/mcp-connector)进行模型生成的对远程 MCP 服务器的调用。

<Info>
  **远程 MCP 自 `langchain-anthropic>=0.3.14` 起受支持**
</Info>

<Tabs>
  <Tab title="Anthropic 类型">
    ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    from anthropic.types.beta import BetaMCPToolsetParam # [!code highlight]
    from langchain_anthropic import ChatAnthropic

    mcp_servers = [
        {
            "type": "url",
            "url": "https://docs.langchain.com/mcp",
            "name": "LangChain 文档",
        }
    ]

    model = ChatAnthropic(
        model="claude-sonnet-4-6",
        mcp_servers=mcp_servers, # [!code highlight]
    )

    mcp_tool = BetaMCPToolsetParam( # [!code highlight]
        type="mcp_toolset", # [!code highlight]
        mcp_server_name="LangChain 文档", # [!code highlight]
    ) # [!code highlight]

    response = model.invoke(
        "什么是 LangChain 内容块？",
        tools=[mcp_tool], # [!code highlight]
    )
    ```
  </Tab>

  <Tab title="create_agent">
    ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    from anthropic.types.beta import BetaMCPToolsetParam # [!code highlight]
    from langchain.agents import create_agent
    from langchain_anthropic import ChatAnthropic

    mcp_servers = [
        {
            "type": "url",
            "url": "https://docs.langchain.com/mcp",
            "name": "LangChain 文档",
        }
    ]

    mcp_tool = BetaMCPToolsetParam( # [!code highlight]
        type="mcp_toolset", # [!code highlight]
        mcp_server_name="LangChain 文档", # [!code highlight]
    ) # [!code highlight]

    agent = create_agent(
        model=ChatAnthropic(
            model="claude-sonnet-4-6",
            mcp_servers=mcp_servers,  # [!code highlight]
        ),
        tools=[mcp_tool],  # [!code highlight]
    )

    result = agent.invoke({
        "messages": [{"role": "user", "content": "什么是 LangChain 内容块？"}]
    })

    for message in result["messages"]:
        message.pretty_print()
    ```
  </Tab>

  <Tab title="字典">
    ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    from langchain_anthropic import ChatAnthropic

    mcp_servers = [
        {
            "type": "url",
            "url": "https://docs.langchain.com/mcp",
            "name": "LangChain 文档",
            # "tool_configuration": {  # 可选配置
            #     "enabled": True,
            #     "allowed_tools": ["ask_question"],
            # },
            # "authorization_token": "PLACEHOLDER",  # 如果需要，可选授权
        }
    ]

    model = ChatAnthropic(
        model="claude-sonnet-4-6",
        mcp_servers=mcp_servers, # [!code highlight]
    )

    response = model.invoke(
        "什么是 LangChain 内容块？",
        tools=[{"type": "mcp_toolset", "mcp_server_name": "LangChain 文档"}], # [!code highlight]
    )
    response.content_blocks
    ```
  </Tab>
</Tabs>

### 文本编辑器

Claude 支持客户端文本编辑器工具，可用于查看和修改本地文本文件。有关详细信息，请参阅[文本编辑器工具文档](https://platform.claude.com/docs/en/agents-and-tools/tool-use/text-editor-tool)。

<Tabs>
  <Tab title="Anthropic 类型">
    ```python expandable theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    from typing import Literal

    from anthropic.types.beta import BetaToolTextEditor20250728Param # [!code highlight]
    from langchain_anthropic import ChatAnthropic
    from langchain.messages import HumanMessage, ToolMessage
    from langchain.tools import tool

    tool_spec = BetaToolTextEditor20250728Param( # [!code highlight]
        name="str_replace_based_edit_tool", # [!code highlight]
        type="text_editor_20250728", # [!code highlight]
    ) # [!code highlight]

    # 用于演示的简单内存文件存储
    files: dict[str, str] = {
        "/workspace/primes.py": "def is_prime(n):\n    if n < 2\n        return False\n    return True"
    }

    @tool(extras={"provider_tool_definition": tool_spec}) # [!code highlight]
    def str_replace_based_edit_tool(
        *,
        command: Literal["view", "create", "str_replace", "insert", "undo_edit"],
        path: str,
        file_text: str | None = None,
        old_str: str | None = None,
        new_str: str | None = None,
        insert_line: int | None = None,
        view_range: list[int] | None = None,
        **kw
    ):
        """查看和编辑文本文件。"""
        if command == "view":
            if path not in files:
                return f"错误: 文件 {path} 未找到"
            content = files[path]
            if view_range:
                lines = content.splitlines()
                start, end = view_range[0] - 1, view_range[1]
                return "\n".join(lines[start:end])
            return content
        elif command == "create":
            files[path] = file_text or ""
            return f"已创建 {path}"
        elif command == "str_replace" and old_str is not None:
            if path not in files:
                return f"错误: 文件 {path} 未找到"
            files[path] = files[path].replace(old_str, new_str or "", 1)
            return f"已在 {path} 中替换"
        # ... 实现其他命令
        return f"已在 {path} 上执行 {command}"

    model = ChatAnthropic(model="claude-sonnet-4-6")
    model_with_tools = model.bind_tools([str_replace_based_edit_tool]) # [!code highlight]

    # 初始请求
    messages = [HumanMessage("我的 primes.py 文件中有语法错误。你能修复它吗？")]
    response = model_with_tools.invoke(messages)
    print(response.content_blocks)

    # 工具执行循环
    while response.tool_calls:
        tool_messages = []
        for tool_call in response.tool_calls:
            result = str_replace_based_edit_tool.invoke(tool_call)
            tool_messages.append(
                ToolMessage(content=result, tool_call_id=tool_call["id"])
            )

        messages = [*messages, response, *tool_messages]
        response = model_with_tools.invoke(messages)
        print(response.content_blocks)
    ```
  </Tab>

  <Tab title="create_agent">
    ```python expandable theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    from typing import Literal

    from anthropic.types.beta import BetaToolTextEditor20250728Param # [!code highlight]
    from langchain.agents import create_agent
    from langchain_anthropic import ChatAnthropic
    from langchain.tools import tool

    # 简单的内存文件存储
    files: dict[str, str] = {
        "/workspace/primes.py": "def is_prime(n):\n    if n < 2\n        return False\n    return True"
    }

    tool_spec = BetaToolTextEditor20250728Param( # [!code highlight]
        name="str_replace_based_edit_tool", # [!code highlight]
        type="text_editor_20250728", # [!code highlight]
    ) # [!code highlight]


    @tool(extras={"provider_tool_definition": tool_spec}) # [!code highlight]
    def str_replace_based_edit_tool(
        *,
        command: Literal["view", "create", "str_replace", "insert", "undo_edit"],
        path: str,
        file_text: str | None = None,
        old_str: str | None = None,
        new_str: str | None = None,
        **kw
    ):
        """查看和编辑文本文件。"""
        if command == "view":
            return files.get(path, f"错误: 文件 {path} 未找到")
        elif command == "create":
            files[path] = file_text or ""
            return f"已创建 {path}"
        elif command == "str_replace" and old_str is not None:
            if path not in files:
                return f"错误: 文件 {path} 未找到"
            files[path] = files[path].replace(old_str, new_str or "", 1)
            return f"已在 {path} 中替换"
        return f"已在 {path} 上执行 {command}"


    agent = create_agent(
        model=ChatAnthropic(model="claude-sonnet-4-6"),
        tools=[str_replace_based_edit_tool],  # [!code highlight]
    )

    result = agent.invoke({
        "messages": [{"role": "user", "content": "修复 /workspace/primes.py 中的语法错误"}]
    })

    for message in result["messages"]:
        message.pretty_print()
    ```
  </Tab>

  <Tab title="字典">
    ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    from langchain_anthropic import ChatAnthropic

    model = ChatAnthropic(model="claude-sonnet-4-6")

    editor_tool = {"type": "text_editor_20250728", "name": "str_replace_based_edit_tool"} # [!code highlight]

    model_with_tools = model.bind_tools([editor_tool]) # [!code highlight]

    response = model_with_tools.invoke(
        "我的 primes.py 文件中有语法错误。你能帮我修复它吗？"
    )
    # 您必须通过工具执行循环在 response.tool_calls 中处理文本编辑器命令的执行
    ```

    使用 [`create_agent`](https://reference.langchain.com/python/langchain/agents/factory/create_agent) 会自动处理工具执行循环。

    ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    [{'name': 'str_replace_based_edit_tool',
    'args': {'command': 'view', 'path': '/root'},
    'id': 'toolu_011BG5RbqnfBYkD8qQonS9k9',
    'type': 'tool_call'}]
    ```
  </Tab>
</Tabs>

<Note>
  **可用工具版本：**

  * `text_editor_20250124`（旧版）
  * `text_editor_20250728`（推荐）
</Note>

<Tip>
  要获得“开箱即用”的实现，请考虑使用 [`StateClaudeTextEditorMiddleware`](/oss/python/integrations/middleware/anthropic#text-editor) 或 [`FilesystemClaudeTextEditorMiddleware`](/oss/python/integrations/middleware/anthropic#text-editor)，它们提供 LangGraph 状态集成或文件系统持久化、路径验证和其他功能。
</Tip>

### 网页抓取

Claude 可以使用服务器端[网页抓取工具](https://platform.claude.com/docs/en/agents-and-tools/tool-use/web-fetch-tool)从指定的网页和 PDF 文档中检索完整内容，并通过引用来支持其响应。

<Tabs>
  <Tab title="Anthropic 类型">
    ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    from anthropic.types.beta import BetaWebFetchTool20250910Param # [!code highlight]
    from langchain_anthropic import ChatAnthropic

    model = ChatAnthropic(model="claude-haiku-4-5-20251001")

    fetch_tool = BetaWebFetchTool20250910Param( # [!code highlight]
        name="web_fetch", # [!code highlight]
        type="web_fetch_20250910", # [!code highlight]
        max_uses=3, # [!code highlight]
    ) # [!code highlight]

    model_with_tools = model.bind_tools([fetch_tool]) # [!code highlight]

    response = model_with_tools.invoke(
        "请分析 https://docs.langchain.com/ 处的内容"
    )
    ```
  </Tab>

  <Tab title="create_agent">
    ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    from anthropic.types.beta import BetaWebFetchTool20250910Param # [!code highlight]
    from langchain.agents import create_agent
    from langchain_anthropic import ChatAnthropic

    fetch_tool = BetaWebFetchTool20250910Param( # [!code highlight]
        name="web_fetch", # [!code highlight]
        type="web_fetch_20250910", # [!code highlight]
        max_uses=3, # [!code highlight]
    ) # [!code highlight]

    agent = create_agent(
        model=ChatAnthropic(model="claude-haiku-4-5-20251001"),
        tools=[fetch_tool],  # [!code highlight]
    )

    result = agent.invoke({
        "messages": [{"role": "user", "content": "分析 https://docs.langchain.com/"}]
    })

    for message in result["messages"]:
        message.pretty_print()
    ```
  </Tab>

  <Tab title="字典">
    ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    from langchain_anthropic import ChatAnthropic

    model = ChatAnthropic(model="claude-haiku-4-5-20251001")

    fetch_tool = {"type": "web_fetch_20250910", "name": "web_fetch", "max_uses": 3} # [!code highlight]

    model_with_tools = model.bind_tools([fetch_tool]) # [!code highlight]

    response = model_with_tools.invoke(
        "请分析 https://docs.langchain.com/ 处的内容"
    )
    ```
  </Tab>
</Tabs>

### 网页搜索

Claude 可以使用服务器端[网页搜索工具](https://platform.claude.com/docs/en/agents-and-tools/tool-use/web-search-tool)运行搜索并通过引用来支持其响应。

<Info>
  **网页搜索工具自 `langchain-anthropic>=0.3.13` 起受支持**
</Info>

<Tabs>
  <Tab title="Anthropic 类型">
    ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    from anthropic.types.beta import BetaWebSearchTool20250305Param # [!code highlight]
    from langchain_anthropic import ChatAnthropic

    model = ChatAnthropic(model="claude-sonnet-4-6")

    search_tool = BetaWebSearchTool20250305Param( # [!code highlight]
        name="web_search", # [!code highlight]
        type="web_search_20250305", # [!code highlight]
        max_uses=3, # [!code highlight]
    ) # [!code highlight]

    model_with_tools = model.bind_tools([search_tool]) # [!code highlight]

    response = model_with_tools.invoke("如何将 Web 应用更新到 TypeScript 5.5？")
    ```
  </Tab>

  <Tab title="create_agent">
    ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    from anthropic.types.beta import BetaWebSearchTool20250305Param # [!code highlight]
    from langchain.agents import create_agent
    from langchain_anthropic import ChatAnthropic

    search_tool = BetaWebSearchTool20250305Param( # [!code highlight]
        name="web_search", # [!code highlight]
        type="web_search_20250305", # [!code highlight]
        max_uses=3, # [!code highlight]
    ) # [!code highlight]

    agent = create_agent(
        model=ChatAnthropic(model="claude-sonnet-4-6"),
        tools=[search_tool],  # [!code highlight]
    )

    result = agent.invoke({
        "messages": [{"role": "user", "content": "如何将 Web 应用更新到 TypeScript 5.5？"}]
    })

    for message in result["messages"]:
        message.pretty_print()
    ```
  </Tab>

  <Tab title="字典">
    ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    from langchain_anthropic import ChatAnthropic

    model = ChatAnthropic(model="claude-sonnet-4-6")

    search_tool = {"type": "web_search_20250305", "name": "web_search", "max_uses": 3} # [!code highlight]

    model_with_tools = model.bind_tools([search_tool]) # [!code highlight]

    response = model_with_tools.invoke("如何将 Web 应用更新到 TypeScript 5.5？")
    ```
  </Tab>
</Tabs>

### 记忆工具

Claude 支持用于跨对话线程进行客户端上下文存储和检索的记忆工具。有关详细信息，请参阅[记忆工具文档](https://platform.claude.com/docs/en/agents-and-tools/tool-use/memory-tool)。

<Info>
  **Anthropic 的内置记忆工具自 `langchain-anthropic>=0.3.21` 起受支持**
</Info>

<Tabs>
  <Tab title="Anthropic 类型">
    ```python expandable theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    from typing import Literal

    from anthropic.types.beta import BetaMemoryTool20250818Param  # [!code highlight]
    from langchain_anthropic import ChatAnthropic
    from langchain.messages import HumanMessage, ToolMessage
    from langchain.tools import tool

    tool_spec = BetaMemoryTool20250818Param(  # [!code highlight]
        name="memory",  # [!code highlight]
        type="memory_20250818",  # [!code highlight]
    )  # [!code highlight]

    # 用于演示的简单内存存储
    memory_store: dict[str, str] = {
        "/memories/interests": "用户喜欢 Python 编程和徒步旅行"
    }


    @tool(extras={"provider_tool_definition": tool_spec})  # [!code highlight]
    def memory(
        *,
        command: Literal["view", "create", "str_replace", "insert", "delete", "rename"],
        path: str,
        content: str | None = None,
        old_str: str | None = None,
        new_str: str | None = None,
        insert_line: int | None = None,
        new_path: str | None = None,
        **kw,
    ):
        """管理跨对话的持久记忆。"""
        if command == "view":
            if path == "/memories":
                # 列出所有记忆
                return "\n".join(memory_store.keys()) or "未存储记忆"
            return memory_store.get(path, f"{path} 处无记忆")
        elif command == "create":
            memory_store[path] = content or ""
            return f"已在 {path} 创建记忆"
        elif command == "str_replace" and old_str is not None:
            if path in memory_store:
                memory_store[path] = memory_store[path].replace(old_str, new_str or "", 1)
            return f"已更新 {path}"
        elif command == "delete":
            memory_store.pop(path, None)
            return f"已删除 {path}"
        # ... 实现其他命令
        return f"已在 {path} 上执行 {command}"


    model = ChatAnthropic(model="claude-sonnet-4-6")
    model_with_tools = model.bind_tools([memory]) # [!code highlight]

    # 初始请求
    messages = [HumanMessage("我的兴趣是什么？")]
    response = model_with_tools.invoke(messages)
    print(response.content_blocks)

    # 工具执行循环
    while response.tool_calls:
        tool_messages = []
        for tool_call in response.tool_calls:
            result = memory.invoke(tool_call)
            tool_messages.append(ToolMessage(content=result, tool_call_id=tool_call["id"]))

        messages = [*messages, response, *tool_messages]
        response = model_with_tools.invoke(messages)
        print(response.content_blocks)
    ```

    ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    [{'type': 'text',
    'text': "我将检查我的记忆，看看我有关于您兴趣的哪些信息。"},
    {'type': 'tool_call',
    'name': 'memory',
    'args': {'command': 'view', 'path': '/memories'},
    'id': 'toolu_01XeP9sxx44rcZHFNqXSaKqh'}]
    ```
  </Tab>

  <Tab title="create_agent">
    ```python expandable theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    from typing import Literal

    from anthropic.types.beta import BetaMemoryTool20250818Param # [!code highlight]
    from langchain.agents import create_agent
    from langchain_anthropic import ChatAnthropic
    from langchain.tools import tool

    # 简单的内存存储
    memory_store: dict[str, str] = {
        "/memories/interests": "用户喜欢 Python 编程和徒步旅行"
    }

    tool_spec = BetaMemoryTool20250818Param( # [!code highlight]
        name="memory", # [!code highlight]
        type="memory_20250818", # [!code highlight]
    ) # [!code highlight]


    @tool(extras={"provider_tool_definition": tool_spec}) # [!code highlight]
    def memory(
        *,
        command: Literal["view", "create", "str_replace", "insert", "delete", "rename"],
        path: str,
        content: str | None = None,
        old_str: str | None = None,
        new_str: str | None = None,
        **kw
    ):
        """管理跨对话的持久记忆。"""
        if command == "view":
            if path == "/memories":
                return "\n".join(memory_store.keys()) or "未存储记忆"
            return memory_store.get(path, f"{path} 处无记忆")
        elif command == "create":
            memory_store[path] = content or ""
            return f"已在 {path} 创建记忆"
        elif command == "str_replace" and old_str is not None:
            if path in memory_store:
                memory_store[path] = memory_store[path].replace(old_str, new_str or "", 1)
            return f"已更新 {path}"
        elif command == "delete":
            memory_store.pop(path, None)
            return f"已删除 {path}"
        return f"已在 {path} 上执行 {command}"


    agent = create_agent(
        model=ChatAnthropic(model="claude-sonnet-4-6"),
        tools=[memory],  # [!code highlight]
    )

    result = agent.invoke({
        "messages": [{"role": "user", "content": "我的兴趣是什么？"}]
    })

    for message in result["messages"]:
        message.pretty_print()
    ```

    使用 [`create_agent`](https://reference.langchain.com/python/langchain/agents/factory/create_agent) 会自动处理工具执行循环。
  </Tab>

  <Tab title="字典">
    ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    from langchain_anthropic import ChatAnthropic

    model = ChatAnthropic(
        model="claude-sonnet-4-6",
    )
    model_with_tools = model.bind_tools([{"type": "memory_20250818", "name": "memory"}]) # [!code highlight]

    response = model_with_tools.invoke("我的兴趣是什么？")
    response.content_blocks
    # 您必须通过工具执行循环在 response.tool_calls 中处理记忆命令的执行
    ```

    ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    [{'type': 'text',
    'text': "我将检查我的记忆，看看我有关于您兴趣的哪些信息。"},
    {'type': 'tool_call',
    'name': 'memory',
    'args': {'command': 'view', 'path': '/memories'},
    'id': 'toolu_01XeP9sxx44rcZHFNqXSaKqh'}]
    ```
  </Tab>
</Tabs>

<Tip>
  要获得“开箱即用”的实现，请考虑使用 [`StateClaudeMemoryMiddleware`](/oss/python/integrations/middleware/anthropic#memory) 或 [`FilesystemClaudeMemoryMiddleware`](/oss/python/integrations/middleware/anthropic#memory)，它们提供 LangGraph 状态集成或文件系统持久化、自动系统提示注入和其他功能。
</Tip>

### 工具搜索

Claude 支持服务器端[工具搜索](https://platform.claude.com/docs/en/agents-and-tools/tool-use/tool-search-tool)功能，支持动态工具发现和加载。Claude 不是将所有工具定义预先加载到上下文窗口中，而是可以搜索您的工具目录并仅加载其需要的工具。

这在以下情况下很有用：

* 您的系统中有 10 多个可用工具
* 工具定义消耗大量令牌
* 您在大型工具集中遇到工具选择准确性问题

有两种工具搜索变体：

* **正则表达式** (`tool_search_tool_regex_20251119`)：Claude 构造正则表达式模式来搜索工具
* **BM25** (`tool_search_tool_bm25_20251119`)：Claude 使用自然语言查询来搜索工具

使用 `extras` 参数在 LangChain 工具上指定 `defer_loading`：

<Tabs>
  <Tab title="Anthropic 类型">
    ```python expandable theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    from anthropic.types.beta import BetaToolSearchToolRegex20251119Param # [!code highlight]
    from langchain_anthropic import ChatAnthropic
    from langchain.tools import tool

    @tool(extras={"defer_loading": True}) # [!code highlight]
    def get_weather(location: str, unit: str = "fahrenheit") -> str:
        """获取某个位置的当前天气。

        参数:
            location: 城市名称
            unit: 温度单位（摄氏度或华氏度）
        """
        return f"{location} 的天气：晴天"

    @tool(extras={"defer_loading": True}) # [!code highlight]
    def search_files(query: str) -> str:
        """搜索工作区中的文件。

        参数:
            query: 搜索查询
        """
        return f"找到匹配 '{query}' 的文件"

    model = ChatAnthropic(model="claude-sonnet-4-6")

    tool_search = BetaToolSearchToolRegex20251119Param( # [!code highlight]
        name="tool_search_tool_regex", # [!code highlight]
        type="tool_search_tool_regex_20251119", # [!code highlight]
    ) # [!code highlight]

    model_with_tools = model.bind_tools([
        tool_search, # [!code highlight]
        get_weather,
        search_files,
    ])
    response = model_with_tools.invoke("旧金山的天气怎么样？")
    ```
  </Tab>

  <Tab title="create_agent">
    ```python expandable theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    from anthropic.types.beta import BetaToolSearchToolRegex20251119Param # [!code highlight]
    from langchain.agents import create_agent
    from langchain_anthropic import ChatAnthropic
    from langchain.tools import tool

    tool_search = BetaToolSearchToolRegex20251119Param( # [!code highlight]
        name="tool_search_tool_regex", # [!code highlight]
        type="tool_search_tool_regex_20251119", # [!code highlight]
    ) # [!code highlight]


    @tool(extras={"defer_loading": True}) # [!code highlight]
    def get_weather(location: str, unit: str = "fahrenheit") -> str:
        """获取某个位置的当前天气。

        参数:
            location: 城市名称
            unit: 温度单位（摄氏度或华氏度）
        """
        return f"{location} 的天气：晴天"


    @tool(extras={"defer_loading": True}) # [!code highlight]
    def search_files(query: str) -> str:
        """搜索工作区中的文件。

        参数:
            query: 搜索查询
        """
        return f"找到匹配 '{query}' 的文件"


    agent = create_agent(
        model=ChatAnthropic(model="claude-sonnet-4-6"),
        tools=[
            tool_search,  # [!code highlight]
            get_weather,
            search_files,
        ],
    )

    result = agent.invoke({
        "messages": [{"role": "user", "content": "旧金山的天气怎么样？"}]
    })

    for message in result["messages"]:
        message.pretty_print()
    ```
  </Tab>

  <Tab title="字典">
    ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    from langchain_anthropic import ChatAnthropic
    from langchain.tools import tool

    @tool(extras={"defer_loading": True})  # [!code highlight]
    def get_weather(location: str, unit: str = "fahrenheit") -> str:
        """获取某个位置的当前天气。

        参数:
            location: 城市名称
            unit: 温度单位（摄氏度或华氏度）
        """
        return f"{location} 的天气：晴天"

    @tool(extras={"defer_loading": True})  # [!code highlight]
    def search_files(query: str) -> str:
        """搜索工作区中的文件。

        参数:
            query: 搜索查询
        """
        return f"找到匹配 '{query}' 的文件"

    model = ChatAnthropic(model="claude-sonnet-4-6")

    model_with_tools = model.bind_tools([
        {"type": "tool_search_tool_regex_20251119", "name": "tool_search_tool_regex"}, # [!code highlight]
        get_weather,
        search_files,
    ])
    response = model_with_tools.invoke("旧金山的天气怎么样？")
    ```
  </Tab>
</Tabs>

```mermaid theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
sequenceDiagram
    participant 用户
    participant 模型
    participant 工具搜索 as 工具搜索
    participant 工具 as get_weather

    用户->>模型: "旧金山的天气怎么样？"
    模型->>工具搜索: tool_search_tool_regex(pattern="weather")
    工具搜索-->>模型: tool_references: [get_weather]
    模型->>工具: get_weather(location="San Francisco")
    工具-->>模型: 天气结果
    模型->>用户: 包含天气信息的响应
```

**关键点：**

* 设置了 `defer_loading: True` 的工具仅在 Claude 通过搜索发现它们时才加载
* 将您最常用的 3-5 个工具保持为非延迟加载以获得最佳性能
* 两种变体都搜索工具名称、描述、参数名称和参数描述

有关工具搜索的更多详细信息，包括与 MCP 服务器和客户端实现一起使用，请参阅 [Claude 文档](https://platform.claude.com/docs/en/agents-and-tools/tool-use/tool-search-tool)。

## 响应元数据

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
ai_msg = model.invoke(messages)
ai_msg.response_metadata
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
{
    "id": "msg_013xU6FHEGEq76aP4RgFerVT",
    "model": "claude-sonnet-4-6",
    "stop_reason": "end_turn",
    "stop_sequence": None,
    "usage": {"input_tokens": 25, "output_tokens": 11},
}
```

## 令牌使用量元数据

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
ai_msg = model.invoke(messages)
ai_msg.usage_metadata
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
{"input_tokens": 25, "output_tokens": 11, "total_tokens": 36}
```

默认情况下，包含令牌使用量的消息块将在流式传输期间包含：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
stream = model.stream(messages)
full = next(stream)
for chunk in stream:
    full += chunk
full.usage_metadata
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
{"input_tokens": 25, "output_tokens": 11, "total_tokens": 36}
```

可以通过在流方法中设置 `stream_usage=False` 或在初始化 `ChatAnthropic` 时禁用这些。

***

## API 参考

有关所有功能和配置选项的详细文档，请访问 [`ChatAnthropic`](https://reference.langchain.com/python/langchain-anthropic/chat_models/ChatAnthropic) API 参考。

***

<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/python/integrations/chat/anthropic.mdx) 或 [提交问题](https://github.com/langchain-ai/docs/issues/new/choose)。
  </Callout>
</div>
