> ## Documentation Index
> Fetch the complete documentation index at: https://langchain-5e9cc07a-preview-stream-1765228345-2909855.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# ChatAnthropic

> Get started using Anthropic [chat models](/oss/python/langchain/models) in LangChain.

You can find information about Anthropic's latest models, their costs, context windows, and supported input types in the [Claude](https://platform.claude.com/docs/en/about-claude/models/overview) docs.

<Tip>
  **API Reference**

  For detailed documentation of all features and configuration options, head to the [`ChatAnthropic`](https://reference.langchain.com/python/integrations/langchain_anthropic/ChatAnthropic) API reference.
</Tip>

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

  Note that certain Anthropic models can also be accessed via AWS Bedrock and Google VertexAI. See the [`ChatBedrock`](/oss/python/integrations/chat/bedrock/) and [`ChatVertexAI`](/oss/python/integrations/chat/google_vertex_ai/) integrations to use Anthropic models via these services.
</Info>

## Overview

### Integration details

| Class                                                                                                    | Package                                                                                          | <Tooltip tip="Can run on local hardware" cta="Learn more" href="/oss/python/langchain/models#local-models">Local</Tooltip> | Serializable |                             JS/TS Support                            |                                                                                                     Downloads                                                                                                    |                                                                                                                    Latest Version                                                                                                                    |
| :------------------------------------------------------------------------------------------------------- | :----------------------------------------------------------------------------------------------- | :------------------------------------------------------------------------------------------------------------------------: | :----------: | :------------------------------------------------------------------: | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: |
| [`ChatAnthropic`](https://reference.langchain.com/python/integrations/langchain_anthropic/ChatAnthropic) | [`langchain-anthropic`](https://reference.langchain.com/python/integrations/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="Downloads per month" 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 - Latest version" noZoom height="100" class="rounded" /></a> |

### Model features

| [Tool calling](/oss/python/langchain/tools) | [Structured output](/oss/python/langchain/structured-output) | JSON mode | [Image input](/oss/python/langchain/messages#multimodal) | Audio input | Video input | [Token-level streaming](/oss/python/langchain/streaming/) | Native async | [Token usage](/oss/python/langchain/models#token-usage) | [Logprobs](/oss/python/langchain/models#log-probabilities) |
| :-----------------------------------------: | :----------------------------------------------------------: | :-------: | :------------------------------------------------------: | :---------: | :---------: | :-------------------------------------------------------: | :----------: | :-----------------------------------------------------: | :--------------------------------------------------------: |
|                      ✅                      |                               ✅                              |     ✅     |                             ✅                            |      ❌      |      ❌      |                             ✅                             |       ✅      |                            ✅                            |                              ❌                             |

## Setup

To access Anthropic (Claude) models you'll need to install the `langchain-anthropic` integration package and acquire a [Claude](https://platform.claude.com/docs/en/get-started#prerequisites) API key.

### Installation

<CodeGroup>
  ```bash pip theme={null}
  pip install -U langchain-anthropic
  ```

  ```bash uv theme={null}
  uv add langchain-anthropic
  ```
</CodeGroup>

### Credentials

Head to the [Claude console](https://console.anthropic.com) to sign up and generate a Claude API key. Once you've done this set the `ANTHROPIC_API_KEY` environment variable:

```python theme={null}
import getpass
import os

if "ANTHROPIC_API_KEY" not in os.environ:
    os.environ["ANTHROPIC_API_KEY"] = getpass.getpass("Enter your Anthropic API key: ")
```

To enable automated tracing of your model calls, set your [LangSmith](https://docs.smith.langchain.com/) API key:

```python theme={null}
os.environ["LANGSMITH_API_KEY"] = getpass.getpass("Enter your LangSmith API key: ")
os.environ["LANGSMITH_TRACING"] = "true"
```

## Instantiation

Now we can instantiate our model object and generate chat completions:

```python theme={null}
from langchain_anthropic import ChatAnthropic

model = ChatAnthropic(
    model="claude-haiku-4-5-20251001",
    temperature=0,
    max_tokens=1024,
    timeout=None,
    max_retries=2,
    # other params...
)
```

## Invocation

```python theme={null}
messages = [
    (
        "system",
        "You are a helpful assistant that translates English to French. Translate the user sentence.",
    ),
    ("human", "I love programming."),
]
ai_msg = model.invoke(messages)
ai_msg
```

```output theme={null}
AIMessage(content="J'adore la programmation.", response_metadata={'id': 'msg_018Nnu76krRPq8HvgKLW4F8T', 'model': 'claude-3-5-sonnet-20240620', 'stop_reason': 'end_turn', 'stop_sequence': None, 'usage': {'input_tokens': 29, 'output_tokens': 11}}, id='run-57e9295f-db8a-48dc-9619-babd2bedd891-0', usage_metadata={'input_tokens': 29, 'output_tokens': 11, 'total_tokens': 40})
```

```python theme={null}
print(ai_msg.text)
```

```output theme={null}
J'adore la programmation.
```

## Token counting

You can count tokens in messages before sending them to the model using the `get_num_tokens_from_messages()` method. This uses Anthropic's official [token counting API](https://platform.claude.com/docs/en/build-with-claude/token-counting).

```python theme={null}
from langchain_anthropic import ChatAnthropic
from langchain.messages import HumanMessage, SystemMessage

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

messages = [
    SystemMessage(content="You are a scientist"),
    HumanMessage(content="Hello, Claude"),
]

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

```output theme={null}
14
```

You can also count tokens when using tools:

```python theme={null}
from langchain.tools import tool

@tool(parse_docstring=True)
def get_weather(location: str) -> str:
    """Get the current weather in a given location

    Args:
        location: The city and state, e.g. San Francisco, CA
    """
    return "Sunny"

messages = [
    HumanMessage(content="What's the weather like in San Francisco?"),
]

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

```output theme={null}
586
```

## Content blocks

When using tools, [extended thinking](#extended-thinking), and other features, content from a single Anthropic [`AIMessage`](https://reference.langchain.com/python/langchain/messages/#langchain.messages.AIMessage) can either be a single string or a list of Anthropic 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 standardized `AIMessage.tool_calls`):

```python theme={null}
from langchain_anthropic import ChatAnthropic
from typing_extensions import Annotated

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


def get_weather(
    location: Annotated[str, ..., "Location as city and state."]
) -> str:
    """Get the weather at a location."""
    return "It's sunny."


model_with_tools = model.bind_tools([get_weather])
response = model_with_tools.invoke("Which city is hotter today: LA or NY?")
response.content
```

```output theme={null}
[{'text': "I'll help you compare the temperatures of Los Angeles and New York by checking their current weather. I'll retrieve the weather for both cities.",
  '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'}]
```

Using `content_blocks` will render the content in LangChain's standard format that is consistent across other model providers. Read more about [content blocks](/oss/python/langchain/messages#standard-content-blocks).

```python theme={null}
response.content_blocks
```

You can also access tool calls specifically in a standard format using the
`tool_calls` attribute:

```python theme={null}
ai_msg.tool_calls
```

```output theme={null}
[{'name': 'GetWeather',
  'args': {'location': 'Los Angeles, CA'},
  'id': 'toolu_01Ddzj5PkuZkrjF4tafzu54A'},
 {'name': 'GetWeather',
  'args': {'location': 'New York, NY'},
  'id': 'toolu_012kz4qHZQqD4qg8sFPeKqpP'}]
```

## Multimodal

Claude supports image and PDF inputs as content blocks, both in Anthropic's native format (see docs for [vision](https://platform.claude.com/docs/en/build-with-claude/vision#base64-encoded-image-example) and [PDF support](https://platform.claude.com/docs/en/build-with-claude/pdf-support) as well as LangChain's [standard format](/oss/python/langchain/messages#multimodal).

### Files API

In addition to base64 data, Claude supports interactions with files through its managed [Files API](https://platform.claude.com/docs/en/build-with-claude/files).

The Files API can be used to upload files to a container for use with Claude's built-in code-execution tools. See the [code execution](#code-execution) section below, for details.

<Accordion title="Upload images">
  ```python theme={null}
  import anthropic
  from langchain_anthropic import ChatAnthropic


  client = anthropic.Anthropic()
  file = client.beta.files.upload(
      # Supports image/jpeg, image/png, image/gif, image/webp
      file=("image.png", open("/path/to/image.png", "rb"), "image/png"),
  )
  image_file_id = file.id


  # Run inference
  model = ChatAnthropic(
      model="claude-sonnet-4-5-20250929",
      betas=["files-api-2025-04-14"],
  )

  input_message = {
      "role": "user",
      "content": [
          {
              "type": "text",
              "text": "Describe this image.",
          },
          {
              "type": "image",
              "file_id": image_file_id,
          },
      ],
  }
  model.invoke([input_message])
  ```
</Accordion>

<Accordion title="Upload PDFs">
  ```python theme={null}
  import anthropic
  from langchain_anthropic import ChatAnthropic


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


  # Run inference
  model = ChatAnthropic(
      model="claude-sonnet-4-5-20250929",
      betas=["files-api-2025-04-14"],
  )

  input_message = {
      "role": "user",
      "content": [
          {"type": "text", "text": "Describe this document."},
          {"type": "file", "file_id": pdf_file_id}
      ],
  }
  model.invoke([input_message])
  ```
</Accordion>

## Extended thinking

Some Claude models support an [extended thinking](https://platform.claude.com/docs/en/build-with-claude/extended-thinking) feature, which will output the step-by-step reasoning process that led to its final answer.

See compatible models in the [Claude documentation](https://platform.claude.com/docs/en/build-with-claude/extended-thinking#supported-models).

To use extended thinking, specify the `thinking` parameter when initializing [`ChatAnthropic`](https://reference.langchain.com/python/integrations/langchain_anthropic/ChatAnthropic). If needed, it can also be passed in as a parameter during invocation.

You will need to specify a token budget to use this feature. See usage example below:

<CodeGroup>
  ```python Init param theme={null}
  import json
  from langchain_anthropic import ChatAnthropic

  model = ChatAnthropic(
      model="claude-sonnet-4-5-20250929",
      max_tokens=5000,
      thinking={"type": "enabled", "budget_tokens": 2000},
  )

  response = model.invoke("What is the cube root of 50.653?")
  print(json.dumps(response.content_blocks, indent=2))
  ```

  ```python Invocation param theme={null}
  import json
  from langchain_anthropic import ChatAnthropic

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

  response = model.invoke(
      "What is the cube root of 50.653?",
      max_tokens=5000,
      thinking={"type": "enabled", "budget_tokens": 2000}
  )
  print(json.dumps(response.content_blocks, indent=2))
  ```
</CodeGroup>

```output theme={null}
[
  {
    "type": "reasoning",
    "reasoning": "To find the cube root of 50.653, I need to find the value of $x$ such that $x^3 = 50.653$.\n\nI can try to estimate this first. \n$3^3 = 27$\n$4^3 = 64$\n\nSo the cube root of 50.653 will be somewhere between 3 and 4, but closer to 4.\n\nLet me try to compute this more precisely. I can use the cube root function:\n\ncube root of 50.653 = 50.653^(1/3)\n\nLet me calculate this:\n50.653^(1/3) \u2248 3.6998\n\nLet me verify:\n3.6998^3 \u2248 50.6533\n\nThat's very close to 50.653, so I'm confident that the cube root of 50.653 is approximately 3.6998.\n\nActually, let me compute this more precisely:\n50.653^(1/3) \u2248 3.69981\n\nLet me verify once more:\n3.69981^3 \u2248 50.652998\n\nThat's extremely close to 50.653, so I'll say that the cube root of 50.653 is approximately 3.69981.",
    "extras": {"signature": "ErUBCkYIBxgCIkB0UjV..."}
  },
  {
    "type": "text"
    "text": "The cube root of 50.653 is approximately 3.6998.\n\nTo verify: 3.6998\u00b3 = 50.6530, which is very close to our original number.",
  }
]
```

## Prompt caching

Anthropic supports [caching](https://platform.claude.com/docs/en/build-with-claude/prompt-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](/langsmith/create-few-shot-evaluators), and other data to reduce latency and costs.

To enable caching on an element of a prompt, mark its associated content block using the `cache_control` key. See examples below:

### Messages

```python expandable theme={null}
import requests
from langchain_anthropic import ChatAnthropic


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

# Pull 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": "You are a technology expert.",
            },
            {
                "type": "text",
                "text": f"{readme}",
                "cache_control": {"type": "ephemeral"},  # [!code highlight]
            },
        ],
    },
    {
        "role": "user",
        "content": "What's LangChain, according to its README?",
    },
]

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"First invocation:\n{usage_1}")
print(f"\nSecond:\n{usage_2}")
```

```output theme={null}
First invocation:
{'cache_read': 0, 'cache_creation': 1458}

Second:
{'cache_read': 1458, 'cache_creation': 0}
```

<Tip>
  **Extended caching**

  The cache lifetime is 5 minutes by default. If this is too short, you can apply one hour caching by enabling the `"extended-cache-ttl-2025-04-11"` beta header and specifying `"cache_control": {"type": "ephemeral", "ttl": "1h"}`:

  ```python theme={null}
  model = ChatAnthropic(
      model="claude-sonnet-4-5-20250929",
      betas=["extended-cache-ttl-2025-04-11"],  # [!code highlight]
  )
  ```

  Details of cached token counts will be included on the [`InputTokenDetails`](https://reference.langchain.com/python/langchain/messages/#langchain.messages.InputTokenDetails) of response's [`usage_metadata`](https://reference.langchain.com/python/langchain/messages/#langchain.messages.UsageMetadata):

  ```python theme={null}
  response = model.invoke(messages)
  response.usage_metadata
  ```

  ```json theme={null}
  {
      "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,
      }
  }
  ```
</Tip>

### Tools

```python expandable theme={null}
from langchain_anthropic import convert_to_anthropic_tool
from langchain.tools import tool


# For demonstration purposes, we artificially expand the
# tool description.
description = (
    "Get the weather at a location. "
    f"By the way, check out this readme: {readme}"
)


@tool(description=description)
def get_weather(location: str) -> str:
    return "It's sunny."


# Enable caching on the tool
weather_tool = convert_to_anthropic_tool(get_weather)  # [!code highlight]
weather_tool["cache_control"] = {"type": "ephemeral"}  # [!code highlight]

model = ChatAnthropic(model="claude-sonnet-4-5-20250929")
model_with_tools = model.bind_tools([weather_tool])
query = "What's the weather in San Francisco?"

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"First invocation:\n{usage_1}")
print(f"\nSecond:\n{usage_2}")
```

```output theme={null}
First invocation:
{'cache_read': 0, 'cache_creation': 1809}

Second:
{'cache_read': 1809, 'cache_creation': 0}
```

### Incremental caching in conversational applications

Prompt caching can be used in [multi-turn conversations](https://platform.claude.com/docs/en/build-with-claude/prompt-caching#continuing-a-multi-turn-conversation) to maintain context from earlier messages without redundant processing.

We can enable incremental caching by marking the final message with `cache_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](/oss/python/langchain/quickstart), but add a custom [reducer](/oss/python/langgraph/graph-api#reducers) that automatically marks the last content block in each user message with `cache_control`:

<Accordion title="Chatbot with incremental prompt caching">
  ```python expandable theme={null}
  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-5-20250929")

  # Pull 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:
      # Update last user message
      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)


  # Define the function that calls the model
  def call_model(state: State):
      response = model.invoke(state["messages"])
      return {"messages": [response]}


  # Define the (single) node in the graph
  workflow.add_edge(START, "model")
  workflow.add_node("model", call_model)

  # Add memory
  memory = MemorySaver()
  app = workflow.compile(checkpointer=memory)
  ```

  ```python theme={null}
  from langchain.messages import HumanMessage

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

  query = "Hi! I'm Bob."

  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']}")
  ```

  ```output theme={null}
  ================================== Ai Message ==================================

  Hello, Bob! It's nice to meet you. How are you doing today? Is there something I can help you with?

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

  ```python theme={null}
  query = f"Check out this 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']}")
  ```

  ```output theme={null}
  ================================== Ai Message ==================================

  I can see you've shared the README from the LangChain GitHub repository. This is the documentation for LangChain, which is a popular framework for building applications powered by Large Language Models (LLMs). Here's a summary of what the README contains:

  LangChain is:
  - A framework for developing LLM-powered applications
  - Helps chain together components and integrations to simplify AI application development
  - Provides a standard interface for models, embeddings, vector stores, etc.

  Key features/benefits:
  - Real-time data augmentation (connect LLMs to diverse data sources)
  - Model interoperability (swap models easily as needed)
  - Large ecosystem of integrations

  The LangChain ecosystem includes:
  - LangSmith - For evaluations and observability
  - LangGraph - For building complex agents with customizable architecture
  - LangSmith - For deployment and scaling of agents

  The README also mentions installation instructions (`pip install -U langchain`) and links to various resources including tutorials, how-to guides, conceptual guides, and API references.

  Is there anything specific about LangChain you'd like to know more about, Bob?

  {'cache_read': 0, 'cache_creation': 1498}
  ```

  ```python theme={null}
  query = "What was my name again?"

  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']}")
  ```

  ```output theme={null}
  ================================== Ai Message ==================================

  Your name is Bob. You introduced yourself at the beginning of our conversation.

  {'cache_read': 1498, 'cache_creation': 269}
  ```

  In the [LangSmith trace](https://smith.langchain.com/public/4d0584d8-5f9e-4b91-8704-93ba2ccf416a/r), toggling "raw output" will show exactly what messages are sent to the chat model, including `cache_control` keys.
</Accordion>

## Strict tool use

<Info>
  Strict tool use requires:

  * Claude Sonnet 4.5 or Opus 4.1.
  * `langchain-anthropic>=1.1.0`
</Info>

Anthropic supports opt-in [strict schema adherence to tool calls](https://platform.claude.com/docs/en/build-with-claude/structured-outputs). This guarantees that tool names and arguments are validated and correctly typed.

To enable strict tool use:

1. Specify the `structured-outputs-2025-11-13` beta header
2. Specify `strict=True` when calling [bind\_tools](/oss/python/langchain/models#tool-calling).

```python theme={null}
from langchain_anthropic import ChatAnthropic

model = ChatAnthropic(
    model="claude-sonnet-4-5",
    betas=["structured-outputs-2025-11-13"],  # [!code highlight]
)

def get_weather(location: str) -> str:
    """Get the weather at a location."""
    return "It's sunny."

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

## Token-efficient tool use

Anthropic supports a [token-efficient tool use](https://platform.claude.com/docs/en/agents-and-tools/tool-use/token-efficient-tool-use) feature. It is supported by default on all Claude 4 models.

To use it with Claude 3.7, specify the `token-efficient-tools-2025-02-19` beta-header when instantiating the model, as shown below:

<Accordion title="Enabling token-efficient tool use with Claude 3.7">
  ```python theme={null}
  from langchain_anthropic import ChatAnthropic
  from langchain.tools import tool

  model = ChatAnthropic(
      model="claude-3-7-sonnet-20250219",
      betas=["token-efficient-tools-2025-02-19"],  # [!code highlight]
  )


  @tool
  def get_weather(location: str) -> str:
      """Get the weather at a location."""
      return "It's sunny."


  model_with_tools = model.bind_tools([get_weather])
  response = model_with_tools.invoke("What's the weather in San Francisco?")
  print(response.tool_calls)
  print(f"\nTotal tokens: {response.usage_metadata['total_tokens']}")
  ```

  ```output theme={null}
  [{'name': 'get_weather', 'args': {'location': 'San Francisco'}, 'id': 'toolu_01EoeE1qYaePcmNbUvMsWtmA', 'type': 'tool_call'}]

  Total tokens: 408
  ```
</Accordion>

## Citations

Anthropic supports a [citations](https://platform.claude.com/docs/en/build-with-claude/citations) feature that lets Claude attach context to its answers based on source documents supplied by the user.

When [document](https://platform.claude.com/docs/en/build-with-claude/citations#document-types) or `search_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](https://platform.claude.com/docs/en/build-with-claude/citations#plain-text-documents). In the background, Claude [automatically chunks](https://platform.claude.com/docs/en/build-with-claude/citations#plain-text-documents) the input text into sentences, which are used when generating citations.

```python theme={null}
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": "The grass is green. The sky is blue.",
                },
                "title": "My Document",
                "context": "This is a trustworthy document.",
                "citations": {"enabled": True},
            },
            {"type": "text", "text": "What color is the grass and sky?"},
        ],
    }
]
response = model.invoke(messages)
response.content
```

```output theme={null}
[{'text': 'Based on the document, ', 'type': 'text'},
 {'text': 'the grass is green',
  'type': 'text',
  'citations': [{'type': 'char_location',
    'cited_text': 'The grass is green. ',
    'document_index': 0,
    'document_title': 'My Document',
    'start_char_index': 0,
    'end_char_index': 20}]},
 {'text': ', and ', 'type': 'text'},
 {'text': 'the sky is blue',
  'type': 'text',
  'citations': [{'type': 'char_location',
    'cited_text': 'The sky is blue.',
    'document_index': 0,
    'document_title': 'My Document',
    'start_char_index': 20,
    'end_char_index': 36}]},
 {'text': '.', 'type': 'text'}]
```

### In tool results (agentic RAG)

Claude supports a [search\_result](https://platform.claude.com/docs/en/build-with-claude/search-results) content block representing citable results from queries against a knowledge base or other custom source. These content blocks can be passed to claude both top-line (as in the above example) and within a tool result. This allows Claude to cite elements of its response using the result of a tool call.

To pass search results in response to tool calls, define a tool that returns a list of `search_result` content blocks in Anthropic's native format. For example:

```python theme={null}
def retrieval_tool(query: str) -> list[dict]:
    """Access my knowledge base."""

    # Run a search (e.g., with a LangChain vector store)
    results = vector_store.similarity_search(query=query, k=2)

    # Package results into search_result blocks
    return [
        {
            "type": "search_result",
            # Customize fields as desired, using document metadata or otherwise
            "title": "My Document Title",
            "source": "Source description or provenance",
            "citations": {"enabled": True},
            "content": [{"type": "text", "text": doc.page_content}],
        }
        for doc in results
    ]
```

<Accordion title="End to end example with LangGraph">
  Here we demonstrate an end-to-end example in which we populate a LangChain [vector store](/oss/python/integrations/vectorstores/) 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.

  ```python theme={null}
  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


  # Set up vector store
  embeddings = init_embeddings("openai:text-embedding-3-small")
  vector_store = InMemoryVectorStore(embeddings)

  document_1 = Document(
      id="1",
      page_content=(
          "To request vacation days, submit a leave request form through the "
          "HR portal. Approval will be sent by email."
      ),
      metadata={
          "category": "HR Policy",
          "doc_title": "Leave Policy",
          "provenance": "Leave Policy - page 1",
      },
  )
  document_2 = Document(
      id="2",
      page_content="Managers will review vacation requests within 3 business days.",
      metadata={
          "category": "HR Policy",
          "doc_title": "Leave Policy",
          "provenance": "Leave Policy - page 2",
      },
  )
  document_3 = Document(
      id="3",
      page_content=(
          "Employees with over 6 months tenure are eligible for 20 paid vacation days "
          "per year."
      ),
      metadata={
          "category": "Benefits Policy",
          "doc_title": "Benefits Guide 2025",
          "provenance": "Benefits Policy - page 1",
      },
  )

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


  # Define tool
  async def retrieval_tool(
      query: str, category: Literal["HR Policy", "Benefits Policy"]
  ) -> list[dict]:
      """Access my knowledge base."""

      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
      ]



  # Create agent
  model = init_chat_model("claude-haiku-4-5-20251001")

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


  # Invoke on a query
  config = {"configurable": {"thread_id": "session_1"}}

  input_message = {
      "role": "user",
      "content": "How do I request vacation days?",
  }
  async for step in agent.astream(
      {"messages": [input_message]},
      config,
      stream_mode="values",
  ):
      step["messages"][-1].pretty_print()
  ```
</Accordion>

### Using with text splitters

Anthropic also lets you specify your own splits using [custom document](https://platform.claude.com/docs/en/build-with-claude/citations#custom-content-documents) types. LangChain [text splitters](/oss/python/integrations/splitters/) can be used to generate meaningful splits for this purpose. See the below example, where we split the LangChain `README.md` (a markdown document) and pass it to Claude as context:

```python expandable theme={null}
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},
    }


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

# Split into chunks
splitter = MarkdownTextSplitter(
    chunk_overlap=0,
    chunk_size=50,
)
documents = splitter.split_text(readme)

# Construct message
message = {
    "role": "user",
    "content": [
        format_to_anthropic_documents(documents),
        {"type": "text", "text": "Give me a link to LangChain's tutorials."},
    ],
}

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

## Context management

Anthropic supports a context editing feature that will automatically manage the model's context window (e.g., by clearing tool results).

See the [Claude documentation](https://platform.claude.com/docs/en/build-with-claude/context-editing) for details and configuration options.

<Info>
  **Context management is supported since `langchain-anthropic>=0.3.21`**
</Info>

```python theme={null}
from langchain_anthropic import ChatAnthropic


model = ChatAnthropic(
    model="claude-sonnet-4-5-20250929",
    betas=["context-management-2025-06-27"],
    context_management={"edits": [{"type": "clear_tool_uses_20250919"}]},
)
model_with_tools = model.bind_tools([{"type": "web_search_20250305", "name": "web_search"}])
response = model_with_tools.invoke("Search for recent developments in AI")
```

## Structured output

<Info>
  Structured output requires:

  * Claude Sonnet 4.5 or Opus 4.1.
  * `langchain-anthropic>=1.1.0`
</Info>

Anthropic supports a native [structured output feature](https://platform.claude.com/docs/en/build-with-claude/structured-outputs), which guarantees that its responses adhere to a given schema.

You can access this feature in individual model calls, or by specifying the [response format](/oss/python/langchain/structured-output) of a LangChain [agent](/oss/python/langchain/agents). See below for examples.

<Accordion title="Individual model calls">
  Use the [`with_structured_output`](/oss/python/langchain/models#structured-output) method to generate a structured model response. Specify `method="json_schema"` to enable Anthropic's native structured output feature; otherwise the method defaults to using function calling.

  Note that we set the `structured-outputs-2025-11-13` beta header.

  ```python theme={null}
  from langchain_anthropic import ChatAnthropic
  from pydantic import BaseModel, Field

  model = ChatAnthropic(
      model="claude-sonnet-4-5",
      betas=["structured-outputs-2025-11-13"],  # [!code highlight]
  )

  class Movie(BaseModel):
      """A movie with details."""
      title: str = Field(..., description="The title of the movie")
      year: int = Field(..., description="The year the movie was released")
      director: str = Field(..., description="The director of the movie")
      rating: float = Field(..., description="The movie's rating out of 10")

  model_with_structure = model.with_structured_output(Movie, method="json_schema")  # [!code highlight]
  response = model_with_structure.invoke("Provide details about the movie Inception")
  print(response)  # Movie(title="Inception", year=2010, director="Christopher Nolan", rating=8.8)
  ```
</Accordion>

<Accordion title="Agent response format">
  Specify `response_format` with [`ProviderStrategy`](/oss/python/langchain/agents#providerstrategy) to engage Anthropic's structured output feature when generating its final response.

  ```python theme={null}
  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:
      """Get the weather at a location."""
      return "Sunny and 75 degrees F."

  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": "What's the weather in SF?"}]
  })

  print(result["structured_response"])
  # Weather(temperature=75.0, condition='Sunny')
  ```
</Accordion>

## Built-in tools

Anthropic supports a variety of [built-in tools](https://platform.claude.com/docs/en/agents-and-tools/tool-use/text-editor-tool), which can be bound to the model in the [usual way](/oss/python/langchain/tools/). Claude will generate tool calls adhering to its internal schema for the tool.

### Web search

Claude can use a [web search tool](https://platform.claude.com/docs/en/agents-and-tools/tool-use/web-search-tool) to run searches and ground its responses with citations.

<Info>
  **Web search tool is supported since `langchain-anthropic>=0.3.13`**
</Info>

```python theme={null}
from langchain_anthropic import ChatAnthropic

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

tool = {"type": "web_search_20250305", "name": "web_search", "max_uses": 3}
model_with_tools = model.bind_tools([tool])

response = model_with_tools.invoke("How do I update a web app to TypeScript 5.5?")
```

### Web fetching

Claude can use a [web fetching tool](https://platform.claude.com/docs/en/agents-and-tools/tool-use/web-fetch-tool) to run searches and ground its responses with citations.

```python theme={null}
from langchain_anthropic import ChatAnthropic

model = ChatAnthropic(
    model="claude-haiku-4-5-20251001",
    betas=["web-fetch-2025-09-10"],  # Enable web fetch beta
)

tool = {"type": "web_fetch_20250910", "name": "web_fetch", "max_uses": 3}
model_with_tools = model.bind_tools([tool])

response = model_with_tools.invoke(
    "Please analyze the content at https://example.com/article"
)
```

<Warning>
  You must add the `'web-fetch-2025-09-10'` beta header to use web fetching.
</Warning>

### Code execution

Claude can use a [code execution tool](https://platform.claude.com/docs/en/agents-and-tools/tool-use/code-execution-tool) to execute code in a sandboxed environment.

<Info>
  Anthropic's 2025-08-25 code execution tools are supported since `langchain-anthropic>=1.0.3`.

  The legacy [2025-05-22](https://platform.claude.com/docs/en/agents-and-tools/tool-use/code-execution-tool#upgrade-to-latest-tool-version) tool is supported since `langchain-anthropic>=0.3.14`.
</Info>

<Note>
  The code sandbox does not have internet access, thus you may only use packages that are pre-installed in the environment. See the [Claude docs](https://platform.claude.com/docs/en/agents-and-tools/tool-use/code-execution-tool#networking-and-security) for more info.
</Note>

```python theme={null}
from langchain_anthropic import ChatAnthropic

model = ChatAnthropic(
    model="claude-sonnet-4-5-20250929",
    betas=["code-execution-2025-08-25"],
)

tool = {"type": "code_execution_20250825", "name": "code_execution"}
model_with_tools = model.bind_tools([tool])

response = model_with_tools.invoke(
    "Calculate the mean and standard deviation of [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]"
)
```

<Accordion title="Use with Files API">
  Using the Files API, Claude can write code to access files for data analysis and other purposes. See example below:

  ```python theme={null}
  import anthropic
  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


  # Run inference
  model = ChatAnthropic(
      model="claude-sonnet-4-5-20250929",
      betas=["code-execution-2025-08-25"],
  )

  tool = {"type": "code_execution_20250825", "name": "code_execution"}
  model_with_tools = model.bind_tools([tool])

  input_message = {
      "role": "user",
      "content": [
          {
              "type": "text",
              "text": "Please plot these data and tell me what you see.",
          },
          {
              "type": "container_upload",
              "file_id": file_id,
          },
      ]
  }
  response = model_with_tools.invoke([input_message])
  ```

  Note that Claude may generate files as part of its code execution. You can access these files using the Files API:

  ```python theme={null}
  # Take all file outputs for demonstration purposes
  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")
  ```
</Accordion>

### Memory tool

Claude supports a memory tool for client-side storage and retrieval of context across conversational threads. See docs [here](https://platform.claude.com/docs/en/agents-and-tools/tool-use/memory-tool) for details.

<Info>
  **Anthropic's built-in memory tool is supported since `langchain-anthropic>=0.3.21`**
</Info>

```python theme={null}
from langchain_anthropic import ChatAnthropic

model = ChatAnthropic(
    model="claude-sonnet-4-5-20250929",
    betas=["context-management-2025-06-27"],
)
model_with_tools = model.bind_tools([{"type": "memory_20250818", "name": "memory"}])

response = model_with_tools.invoke("What are my interests?")
```

### Remote MCP

Claude can use a [MCP connector tool](https://platform.claude.com/docs/en/agents-and-tools/mcp-connector) for model-generated calls to remote MCP servers.

<Info>
  **Remote MCP is supported since `langchain-anthropic>=0.3.14`**
</Info>

```python theme={null}
from langchain_anthropic import ChatAnthropic

mcp_servers = [
    {
        "type": "url",
        "url": "https://mcp.deepwiki.com/mcp",
        "name": "deepwiki",
        "tool_configuration": {  # optional configuration
            "enabled": True,
            "allowed_tools": ["ask_question"],
        },
        "authorization_token": "PLACEHOLDER",  # optional authorization
    }
]

model = ChatAnthropic(
    model="claude-sonnet-4-5-20250929",
    betas=["mcp-client-2025-04-04"],
    mcp_servers=mcp_servers,
)

response = model.invoke(
    "What transport protocols does the 2025-03-26 version of the MCP "
    "spec (modelcontextprotocol/modelcontextprotocol) support?"
)
```

### Text editor

The text editor tool can be used to view and modify text files. See docs [here](https://platform.claude.com/docs/en/agents-and-tools/tool-use/text-editor-tool) for details.

```python theme={null}
from langchain_anthropic import ChatAnthropic

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

tool = {"type": "text_editor_20250124", "name": "str_replace_editor"}
model_with_tools = model.bind_tools([tool])

response = model_with_tools.invoke(
    "There's a syntax error in my primes.py file. Can you help me fix it?"
)
print(response.text)
response.tool_calls
```

```output theme={null}
I'd be happy to help you fix the syntax error in your primes.py file. First, let's look at the current content of the file to identify the error.
```

```output theme={null}
[{'name': 'str_replace_editor',
  'args': {'command': 'view', 'path': '/repo/primes.py'},
  'id': 'toolu_01VdNgt1YV7kGfj9LFLm6HyQ',
  'type': 'tool_call'}]
```

***

## API reference

For detailed documentation of all features and configuration options, head to the [`ChatAnthropic`](https://reference.langchain.com/python/integrations/langchain_anthropic/ChatAnthropic) API reference.

***

<Callout icon="pen-to-square" iconType="regular">
  [Edit the source of this page on GitHub.](https://github.com/langchain-ai/docs/edit/main/src/oss/python/integrations/chat/anthropic.mdx)
</Callout>

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