Skip to main content
This integration connects Anthropic’s Claude models to LangChain.

Installation

pip install -U langchain-anthropic

Setup

Set your Anthropic API key as an environment variable:
export ANTHROPIC_API_KEY="your-api-key"

Usage

from langchain_anthropic import ChatAnthropic

model = ChatAnthropic(
    model="claude-sonnet-4-5-20250929",
    temperature=0,
    max_tokens=1024,
    timeout=None,
    max_retries=2,
)

messages = [
    ("system", "You are a helpful assistant."),
    ("human", "What is the capital of France?"),
]

response = model.invoke(messages)
print(response.content)

Streaming

for chunk in model.stream(messages):
    print(chunk.content, end="")

API Reference

ChatAnthropic

model
str
required
Model name to use (e.g., claude-sonnet-4-5-20250929, claude-opus-4-6-20250929).
max_tokens
int | None
default:"None"
Maximum number of tokens to generate. If not specified, uses the model’s max_output_tokens from its profile.
temperature
float | None
default:"None"
Sampling temperature. A non-negative float that controls randomness in generation.
top_k
int | None
default:"None"
Number of most likely tokens to consider at each step.
top_p
float | None
default:"None"
Total probability mass of tokens to consider at each step (nucleus sampling).
timeout
float | None
default:"None"
Timeout for requests to Claude API in seconds.
max_retries
int
default:"2"
Number of retries allowed for requests sent to the Claude API.
stop_sequences
list[str] | None
default:"None"
Custom stop sequences to end generation.
api_key
SecretStr
default:"from env"
Anthropic API key. Automatically read from ANTHROPIC_API_KEY environment variable if not provided.
base_url
str
default:"https://api.anthropic.com"
Base URL for API requests. Only specify if using a proxy or service emulator. Reads from ANTHROPIC_API_URL or ANTHROPIC_BASE_URL if not provided.
betas
list[str] | None
default:"None"
List of beta features to enable (e.g., ["token-efficient-tools-2025-02-19"]).
thinking
dict | None
default:"None"
Parameters for Claude reasoning. For Claude Opus 4.5, use {"type": "enabled", "budget_tokens": 10_000}. For Claude Opus 4.6, use {"type": "adaptive"}.
effort
'max' | 'high' | 'medium' | 'low' | None
default:"None"
Control how many tokens Claude uses when responding. Available on Claude Opus 4.6 and 4.5. The max effort level is only supported by Claude Opus 4.6.
streaming
bool
default:"False"
Whether to use streaming or not.
stream_usage
bool
default:"True"
Whether to include usage metadata in streaming output.

Supported Models

  • Claude Opus 4.6: Most capable model with advanced reasoning
  • Claude Sonnet 4.5: Balanced performance and speed
  • Claude Haiku 4: Fast, cost-effective model
See the Claude Platform docs for the latest models and pricing.

Features

  • Text generation
  • Tool calling
  • Vision (multimodal input)
  • Streaming
  • Async support
  • Prompt caching
  • Extended thinking/reasoning mode
  • Built-in tools (computer use, bash, web search, etc.)

Build docs developers (and LLMs) love