Registry / llm-agents / langchain-anthropic

langchain-anthropic

JSON →
library1.6.1pypypi✓ verified 26d ago

LangChain Anthropic is an integration package that connects Anthropic's generative AI models (like Claude) with the LangChain framework. It allows developers to easily incorporate Anthropic's powerful chat models and (legacy) text completion models into their LangChain-based applications. As part of the broader LangChain ecosystem, it follows a frequent release cadence, often aligning with updates to `langchain-core` and the main `langchain` library. The current version is 1.4.0.

pip install langchain-anthropic
INSTALL
IMPORT
SIG · LANGCHAIN-ANTHROPI
L
langchain-anthropic
llm-agentspythonv1.6.1
Install
8.4s avg
Import
4304ms
Disk
88MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.6.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 4.484s · 85.1MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 8.4s · import 4.124s · 93MB
88MB installed
● package 88MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

ChatAnthropic
from langchain_anthropic import ChatAnthropic
This is the recommended class for interacting with Anthropic's chat models (e.g., Claude 3, Claude 2.1).
AnthropicLLM
from langchain_anthropic import AnthropicLLM
from langchain.llms import Anthropic
AnthropicLLM is a legacy class for text completion models (e.g., older Claude 2 models) and is generally deprecated in favor of ChatAnthropic. Older LangChain versions used 'from langchain.llms import Anthropic'.

This quickstart demonstrates how to instantiate and use the `ChatAnthropic` model to get a response from a Claude chat model. It highlights the importance of setting the `ANTHROPIC_API_KEY` environment variable.

import os from langchain_anthropic import ChatAnthropic from langchain_core.messages import HumanMessage, SystemMessage # Set your Anthropic API key as an environment variable # os.environ["ANTHROPIC_API_KEY"] = "YOUR_ANTHROPIC_API_KEY" # Ensure the API key is set if not os.environ.get("ANTHROPIC_API_KEY"): raise ValueError("ANTHROPIC_API_KEY environment variable not set.") model = ChatAnthropic(model="claude-3-sonnet-20240229", temperature=0) messages = [ SystemMessage(content="You are a helpful AI assistant."), HumanMessage(content="What is the capital of France?"), ] response = model.invoke(messages) print(response.content)
Debug
Known issues
deprecatedThe `AnthropicLLM` class for text completion models is considered legacy. For modern Anthropic models like Claude 3, it's strongly recommended to use `ChatAnthropic` instead, which supports the chat-based API.
fix
Migrate from `AnthropicLLM` to `ChatAnthropic` and adapt your code to use message-based input/output rather than string-in/string-out.
affects: >=1.0.0
breakingThe default value for the `max_tokens` parameter in `langchain-anthropic` changed with LangChain v1. Previously, it defaulted to 1024. Now, it defaults to higher values based on the specific model's `max_output_tokens` profile. If your application relied on the old default, it might now consume more tokens or exhibit different truncation behavior.
fix
Explicitly set `max_tokens=1024` (or your desired value) when instantiating `ChatAnthropic` or `AnthropicLLM` if you need to maintain the old behavior.
affects: >=1.0.0
gotchaIt is crucial to set the `ANTHROPIC_API_KEY` environment variable for authentication. Without it, your application will fail when trying to connect to Anthropic models.
fix
Ensure `os.environ["ANTHROPIC_API_KEY"]` is set to your valid Anthropic API key before initializing any Anthropic model classes. Consider using a `.env` file and `python-dotenv` for local development.
affects: All
breakingLangChain v1 introduced significant architectural changes, including a simplified `langchain` package namespace, revised import paths, and new patterns for agents and tools. Code written for `langchain==0.x` might require substantial modifications.
fix
Refer to the official LangChain v1 migration guides and update your imports and component usage according to the new patterns, focusing on partner packages like `langchain-anthropic` for model integrations.
affects: >=1.0.0
gotchaWhen using built-in Anthropic tools like the 'web fetch' tool, be aware of security implications. Enabling web fetching in environments where Claude processes untrusted input alongside sensitive data can pose data exfiltration risks.
fix
Only use tools like 'web fetch' in trusted environments or when handling non-sensitive data. Implement strict input validation and access controls.
affects: All
Errors
Common errors & fixes
anthropic.AuthenticationError: Invalid x-api-key.
The Anthropic API key is either missing, incorrectly formatted, or invalid, preventing successful authentication with the Anthropic API.
fix
Set the `ANTHROPIC_API_KEY` environment variable with your valid Anthropic API key (e.g., `os.environ["ANTHROPIC_API_KEY"] = "sk-ant-..."`) or pass it directly when initializing `ChatAnthropic` (e.g., `llm = ChatAnthropic(api_key="sk-ant-...")`).
ModuleNotFoundError: No module named 'langchain.llms' or ImportError: cannot import name 'AnthropicLLM'
The import path for Anthropic models has changed with the modularization of LangChain, or the `langchain-anthropic` package is not installed.
fix
Ensure you have installed the `langchain-anthropic` package (`pip install -U langchain-anthropic`) and use the correct import path: `from langchain_anthropic import ChatAnthropic`.
anthropic.RateLimitError: Number of concurrent connections has exceeded your rate limit.
Too many requests are being sent to the Anthropic API within a short period, exceeding the rate limits imposed by Anthropic for your account.
fix
Implement retry logic with exponential backoff using `tenacity`, or leverage LangChain's built-in rate limiting features. Consider requesting a rate limit increase from Anthropic if necessary.
anthropic.BadRequestError: Error code: 400 - {'type': 'error', 'error': {'type': 'invalid_request_error', 'message': 'messages: Unexpected role "ai". Allowed roles are "user" or "assistant"'}}
The message list sent to the Anthropic API includes a message with an unsupported role (e.g., 'ai' when Anthropic expects 'assistant') or an incorrectly ordered system message.
fix
Ensure that `SystemMessage` is the first message in the list, and other roles are correctly mapped to 'user' (`HumanMessage`) or 'assistant' (`AIMessage`) as expected by the Anthropic API, or construct messages carefully following Anthropic's message API format.
anthropic.BadRequestError: Error code: 400 - {'type': 'error', 'error': {'type': 'invalid_request_error', 'message': 'This model does not support assistant message prefill. The conversation must end with a user message.'}}
Certain newer Anthropic models (e.g., Claude Opus) do not support starting a new turn with an `AIMessage` (assistant message prefill). If the message history passed to `ChatAnthropic` ends with an AI message, it will cause this error.
fix
Ensure that the final message in the input list to `ChatAnthropic.invoke()` is always a `HumanMessage`, or explicitly manage the conversation history to avoid ending with an AI message for models that don't support prefill.
Upgrade
Version history
1.6.1latest on PyPI · released Aug 20, 2026
Audit
Dependencies
anthropicrequiredRequired for interacting with Anthropic's API.
pydanticrequiredUsed for data validation and settings management within LangChain components.
langchain-corerequiredCore abstractions and foundational components of LangChain.
Agent activity
30 hits · last 30 days
node
26
OpenAI (training)
1
Resources