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

# Anthropic 集成

> 使用 LangChain JavaScript 与 Anthropic 中间件集成。

专为 Anthropic 的 Claude 模型设计的中间件。了解更多关于[中间件](/oss/javascript/langchain/middleware/overview)的信息。

| 中间件                      | 描述                |
| ------------------------ | ----------------- |
| [提示词缓存](#prompt-caching) | 通过缓存重复的提示词前缀来降低成本 |

## 提示词缓存

通过在 Anthropic 服务器上缓存静态或重复的提示内容（如系统提示、工具定义和对话历史），来降低成本和延迟。此中间件实现了**对话式缓存策略**，该策略会在最新消息后设置缓存断点，从而允许整个对话历史（包括最新的用户消息）被缓存并在后续 API 调用中重复使用。

提示词缓存适用于以下场景：

* 系统提示词较长且静态，在请求之间不发生变化的应用
* 拥有许多工具定义，且这些定义在多次调用中保持不变的智能体
* 早期对话历史在多个轮次中被重复使用的对话
* 高流量部署，其中降低 API 成本和延迟至关重要

<Info>
  了解更多关于 [Anthropic 提示词缓存](https://platform.claude.com/docs/en/build-with-claude/prompt-caching#cache-limitations)的策略和限制。
</Info>

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import { createAgent, anthropicPromptCachingMiddleware } from "langchain";

const agent = createAgent({
  model: "claude-sonnet-4-6",
  prompt: "<你的长系统提示词放在这里>",
  middleware: [anthropicPromptCachingMiddleware({ ttl: "5m" })],
});
```

<Accordion title="配置选项">
  <ParamField body="ttl" type="string" default="5m">
    缓存内容的生存时间。有效值：`'5m'` 或 `'1h'`
  </ParamField>
</Accordion>

<Accordion title="完整示例">
  该中间件会缓存每个请求中直到最新消息（包括该消息）的内容。在 TTL 窗口（5 分钟或 1 小时）内的后续请求中，之前已见的内容将从缓存中检索，而不是重新处理，从而显著降低成本和延迟。

  **工作原理：**

  1. 第一次请求：系统提示词、工具和用户消息 *"Hi, my name is Bob"* 被发送到 API 并缓存
  2. 第二次请求：缓存的内容（系统提示词、工具和第一条消息）从缓存中检索。只有新消息 *"What's my name?"* 需要处理，再加上模型对第一次请求的响应
  3. 此模式在每个轮次中持续进行，每个请求都重复使用缓存的对话历史

  ```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  import { createAgent, HumanMessage, anthropicPromptCachingMiddleware } from "langchain";

  const LONG_PROMPT = `
  请做一个乐于助人的助手。

  <更多上下文...>
  `;

  const agent = createAgent({
    model: "claude-sonnet-4-6",
    prompt: LONG_PROMPT,
    middleware: [anthropicPromptCachingMiddleware({ ttl: "5m" })],
  });

  // 第一次调用：创建包含系统提示词、工具和 "Hi, my name is Bob" 的缓存
  await agent.invoke({
    messages: [new HumanMessage("Hi, my name is Bob")]
  });

  // 第二次调用：重复使用缓存的系统提示词、工具和之前的消息
  // 仅处理新消息 "What's my name?" 和之前 AI 的响应
  const result = await agent.invoke({
    messages: [new HumanMessage("What's my name?")]
  });
  ```
</Accordion>

***

<div className="source-links">
  <Callout icon="edit">
    [Edit this page on GitHub](https://github.com/langchain-ai/docs/edit/main/src/i18n\zh-CN\oss\javascript\integrations\middleware\anthropic.mdx) or [file an issue](https://github.com/langchain-ai/docs/issues/new/choose).
  </Callout>

  <Callout icon="terminal-2">
    [Connect these docs](/use-these-docs) to Claude, VSCode, and more via MCP for real-time answers.
  </Callout>
</div>
