DS DevShelfHub Projects · AI tools
Tutorials / LangChain / Reference / Classes / ChatAnthropic
Chat Model langchain-anthropic Beginner

ChatAnthropic: Reference Guide

By DevShelfHub

Interact with Anthropic's Claude models.

What is ChatAnthropic?

ChatAnthropic is the LangChain chat model for Anthropic Claude. It speaks the Anthropic Messages API and exposes the same Runnable interface as ChatOpenAI — invoke, ainvoke, stream, astream, and batch — so you can switch between Claude and other providers without rewriting your chains. It takes a list of BaseMessage objects and returns an AIMessage carrying the response text, token usage, stop reason, and any tool calls.

The class covers the full modern Claude feature set: streaming over server-sent events, tool calling through bind_tools, structured output through with_structured_output, vision inputs for images and PDFs, prompt caching to cut cost on repeated context, and extended thinking on the reasoning-capable models where the model exposes a separate thinking block before its answer. Token usage comes back on response_metadata and usage_metadata, so you can track cost without a callback. Claude has a large context window (200K tokens on current models), which makes it a natural fit for long-document RAG and multi-turn agents.

Configuration is straightforward: set ANTHROPIC_API_KEY in the environment or pass api_key directly, and choose a model such as claude-3-5-sonnet, claude-3-5-haiku, or a newer release. The class lives in the langchain-anthropic package, which you install separately from langchain-core. Pin a dated model string in production rather than a floating alias so a provider-side model update never silently changes your output.

When to Use

You need Claude's superior reasoning or longer context windows. Use ChatAnthropic for complex reasoning tasks.

Use Cases

  • Reasoning-heavy tasks
  • Long context processing
  • Vision understanding
  • Code analysis
  • Content generation
  • Multi-turn reasoning

Key Features

  • All Claude versions
  • Extended thinking
  • Long context (200K)
  • Tool use
  • Vision support
  • Streaming

When NOT to Use

For fastest/cheapest models—use GPT-3.5-turbo.

Notes

Streaming yields AIMessageChunk objects

model.stream() returns AIMessageChunk pieces, not plain strings. The text is on .content, but chunks can also carry tool_call_chunks during tool use. Merge chunks with chunk1 + chunk2 (the class implements __add__) rather than string concatenation so tool calls and usage metadata survive into the final message.

Tool calls return normalized AIMessage.tool_calls

bind_tools() targets the Anthropic tools API and returns results in the same normalized AIMessage.tool_calls shape that ChatOpenAI uses. That cross-provider parity is why you can prototype an agent on GPT-4o and switch to Claude without touching the tool-dispatch loop.

Prompt caching cuts cost on repeated context

For agents and RAG that resend a large system prompt or document on every turn, mark that block with cache_control so Anthropic serves it from cache. Cached input tokens are billed at a fraction of normal price, which is the single biggest cost lever on long-context Claude workloads.

Pin a dated model string

Use an explicit version like claude-3-5-sonnet-20241022 rather than a floating alias in production. Aliases can be repointed to a newer snapshot, which can shift output, token counts, and latency without any change on your side. Pinning keeps evaluations reproducible.

Import

python
from langchain_anthropic import ChatAnthropic

Initialization Parameters

Parameter Type Default Purpose
model str claude-3-5-sonnet-20241022 Claude model version

Code Examples

Basic invocation

python
from langchain_anthropic import ChatAnthropic
from langchain_core.messages import HumanMessage

model = ChatAnthropic(model='claude-3-5-sonnet-20241022', temperature=0)
result = model.invoke([HumanMessage(content='Explain quantum computing in one sentence')])
print(result.content)

Streaming tokens

python
from langchain_anthropic import ChatAnthropic

model = ChatAnthropic(model='claude-3-5-sonnet-20241022')
for chunk in model.stream('Write a haiku about retrieval augmented generation'):
    print(chunk.content, end='', flush=True)

Structured output with a Pydantic schema

python
from langchain_anthropic import ChatAnthropic
from pydantic import BaseModel, Field

class Ticket(BaseModel):
    priority: str = Field(description='low, medium, or high')
    summary: str = Field(description='one-line summary')

model = ChatAnthropic(model='claude-3-5-sonnet-20241022')
structured = model.with_structured_output(Ticket)
ticket = structured.invoke('The checkout page returns a 500 on every purchase')
print(ticket.priority, ticket.summary)

Common Mistakes

❌ Forget to set ANTHROPIC_API_KEY

✅ export ANTHROPIC_API_KEY='sk-ant-...'

Alternative Models

Model When to Use
ChatOpenAI For faster, cheaper GPT models

Browse the full LangChain API reference index to explore more classes, methods, and decorators, or start with the LangChain introduction tutorial for end-to-end context on building with ChatAnthropic and the wider framework.

ChatAnthropic FAQ

What is ChatAnthropic in LangChain?

Interact with Anthropic's Claude models. ChatAnthropic is the LangChain chat model for Anthropic Claude. It speaks the Anthropic Messages API and exposes the same Runnable interface as ChatOpenAI — invoke, ainvoke, stream, astream, and batch — so you can switch between Claude and other providers without rewriting your chains. It takes a list of BaseMessage objects and returns an AIMessage carrying the response text, token usage, stop reason, and any tool calls. The class covers the full modern Claude feature set: s…

Which package provides ChatAnthropic?

DevShelfHub documents ChatAnthropic from the langchain-anthropic package. Pin your installed LangChain version and match imports to the snippet on this page.

When should I use ChatAnthropic?

You need Claude's superior reasoning or longer context windows. Use ChatAnthropic for complex reasoning tasks.

When should I avoid using ChatAnthropic?

For fastest/cheapest models—use GPT-3.5-turbo.

How do I import ChatAnthropic in Python?

from langchain_anthropic import ChatAnthropic

Where can I explore more LangChain API reference pages?

Open the LangChain API reference index on DevShelfHub to browse classes, methods, and decorators, each with runnable examples, parameters, common mistakes, and cross-links.