Skip to main content
Anthropic 是一家 AI 安全与研究公司,是 Claude 的创造者。 This will help you getting started with ChatAnthropic chat models. For detailed documentation of all ChatAnthropic features and configurations head to the API reference.

概述

集成详情

模型功能

请参阅下表标题中的链接,了解如何使用特定功能。

设置

你需要注册并获取 Anthropic API key,并安装 @langchain/anthropic 集成包。

凭证

前往 Anthropic’s website 注册 Anthropic 并生成 API 密钥。完成后设置 ANTHROPIC_API_KEY 环境变量:
如果你想要自动追踪模型调用,还可以设置你的 LangSmith API 密钥,取消注释以下内容:

安装

LangChain 的 ChatAnthropic 集成位于 @langchain/anthropic 包中:

实例化

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

调用

Content blocks

One key difference to note between Anthropic models and most others is that the contents of a single Anthropic AIMessage can either be a single string or a list of content blocks. For example when an Anthropic model calls a tool, the tool invocation is part of the message content (as well as being exposed in the standardized AIMessage.tool_calls field):

自定义请求头

You can pass custom headers in your requests like this:

提示词缓存

Anthropic supports caching parts of your prompt in order to reduce costs for use-cases that require long context. You can cache tools and both entire messages and individual blocks. The initial request containing one or more blocks or tool definitions with a "cache_control": { "type": "ephemeral" } field will automatically cache that part of the prompt. This initial caching step will cost extra, but subsequent requests will be billed at a reduced rate. The cache has a lifetime of 5 minutes, but this is refreshed each time the cache is hit. For longer caching, specify "ttl": "1h" in the cache_control field. There is a minimum cacheable prompt length, which varies according to model. For more information, see prompt caching details. Here’s an example of caching part of a system message that contains the LangChain conceptual docs:
We can see that there’s a new field called cache_creation_input_tokens in the raw usage field returned from Anthropic. If we use the same messages again, we can see that the long text’s input tokens are read from the cache:

Tool caching

You can also cache tools by setting the same "cache_control": { "type": "ephemeral" } within a tool definition. This currently requires you to bind a tool in Anthropic’s raw tool format Here’s an example:
For more on how prompt caching works, see Anthropic’s docs.

Custom clients

Anthropic models may be hosted on cloud services such as Google Vertex that rely on a different underlying client with the same interface as the primary Anthropic client. You can access these services by providing a createClient method that returns an initialized instance of an Anthropic client. Here’s an example:

Citations

Anthropic supports a citations feature that lets Claude attach context to its answers based on source material supplied by the user. This source material can be provided either as document content blocks, which describe full documents, or as search results, which describe relevant passages or snippets returned from a retrieval system. When "citations": { "enabled": true } is included in a query, Claude may generate direct citations to the provided material in its response.

Document example

In this example we pass a plain text document. In the background, Claude automatically chunks the input text into sentences, which are used when generating citations.

Search results example

In this example, we pass in search results as part of our message content. This allows Claude to cite specific passages or snippets from your own retrieval system in its response. This approach is helpful when you want Claude to cite information from a specific set of knowledge, but you want to bring your own pre-fetched/cached content directly rather than having the model search or retrieve them automatically.

Search results from a tool

You can also use a tool to provide search results that the model can cite in its responses. This is well suited for RAG (or Retrieval-Augmented Generation) workflows where Claude can decide when and where to retrieve information from. When returning this information as search results, it gives Claude the ability to create citations from the material returned from the tool. Here’s how you can create a tool that returns search results in the format expected by Anthropic’s citations API:
Learn more about how RAG works in LangChain Learn more about tool calling

Using with text splitters

Anthropic also lets you specify your own splits using custom document types. LangChain text splitters can be used to generate meaningful splits for this purpose. See the below example, where we split the LangChain.js README (a markdown document) and pass it to Claude as context:

Context management

Anthropic supports a context editing feature that will automatically manage the model’s context window (e.g., by clearing tool results). See Anthropic documentation for details and configuration options.
Context management is supported since @langchain/anthropic@0.3.29

API 参考

有关所有 ChatAnthropic 功能和配置的详细文档,请前往 API 参考