AWS Bedrock and Google VertexAINote that certain Anthropic models can also be accessed via AWS Bedrock and Google VertexAI. See the ChatBedrock and ChatVertexAI integrations to use Anthropic models via these services.
Overview
Integration details
Model features
Setup
To access Anthropic models you’ll need to create an Anthropic account, get an API key, and install thelangchain-anthropic integration package.
Credentials
Head to console.anthropic.com/ to sign up for Anthropic and generate an API key. Once you’ve done this set the ANTHROPIC_API_KEY environment variable:Installation
The LangChain Anthropic integration lives in thelangchain-anthropic package:
This guide requires
langchain-anthropic>=0.3.13Instantiation
Now we can instantiate our model object and generate chat completions:Invocation
Chaining
We can chain our model with a prompt template like so:Content blocks
When using tools, extended thinking, and other features, content from a single Anthropic AI message can either be a single string or a list of content blocks. For example, when an Anthropic model invokes a tool, the tool invocation is part of the message content (as well as being exposed in the standardizedAIMessage.tool_calls):
.content_blocks will render the content in a standard format that is
consistent across providers:
.tool_calls attribute:
Multimodal
Claude supports image and PDF inputs as content blocks, both in Anthropic’s native format (see docs for vision and PDF support) as well as LangChain’s standard format.Files API
Claude also supports interactions with files through its managed Files API. See examples below. The Files API can also be used to upload files to a container for use with Claude’s built-in code-execution tools. See the code execution section below, for details.Images
Images
PDFs
PDFs
Extended thinking
Some Claude models support an extended thinking feature, which will output the step-by-step reasoning process that led to its final answer. See applicable models in the Anthropic guide here. To use extended thinking, specify thethinking parameter when initializing ChatAnthropic. It can also be passed in as a kwarg during invocation.
You will need to specify a token budget to use this feature. See usage example below:
Prompt caching
Anthropic supports caching of elements of your prompts, including messages, tool definitions, tool results, images and documents. This allows you to re-use large documents, instructions, few-shot documents, and other data to reduce latency and costs. To enable caching on an element of a prompt, mark its associated content block using thecache_control key. See examples below:
Messages
Tools
Incremental caching in conversational applications
Prompt caching can be used in multi-turn conversations to maintain context from earlier messages without redundant processing. We can enable incremental caching by marking the final message withcache_control. Claude will automatically use the longest previously-cached prefix for follow-up messages.
Below, we implement a simple chatbot that incorporates this feature. We follow the LangChain chatbot tutorial, but add a custom reducer that automatically marks the last content block in each user message with cache_control. See below:
cache_control keys.
Token-efficient tool use
Anthropic supports a (beta) token-efficient tool use feature. To use it, specify the relevant beta-headers when instantiating the model.Citations
Anthropic supports a citations feature that lets Claude attach context to its answers based on source documents supplied by the user. When document orsearch result content blocks with "citations": {"enabled": True} are included in a query, Claude may generate citations in its response.
Simple 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.In tool results (agentic RAG)
Requires
langchain-anthropic>=0.3.17search_result content blocks in Anthropic’s native format. For example:
End to end example with LangGraph
End to end example with LangGraph
Here we demonstrate an end-to-end example in which we populate a LangChain vector store with sample documents and equip Claude with a tool that queries those documents.
The tool here takes a search query and a
category string literal, but any valid tool signature can be used.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 README (a markdown document) and pass it to Claude as context:Built-in tools
Anthropic supports a variety of built-in tools, which can be bound to the model in the usual way. Claude will generate tool calls adhering to its internal schema for the tool:Web search
Claude can use a web search tool to run searches and ground its responses with citations.Web search tool is supported since
langchain-anthropic>=0.3.13Web fetching
Claude can use a web fetching tool to run searches and ground its responses with citations. from langchain_anthropic import ChatAnthropicCode execution
Claude can use a code execution tool to execute Python code in a sandboxed environment.Code execution is supported since
langchain-anthropic>=0.3.14Use with Files API
Use with Files API
Using the Files API, Claude can write code to access files for data analysis and other purposes. See example below:Note that Claude may generate files as part of its code execution. You can access these files using the Files API:
Remote MCP
Claude can use a MCP connector tool for model-generated calls to remote MCP servers.Remote MCP is supported since
langchain-anthropic>=0.3.14