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-anthropicVerified import paths — ran on the pinned version, not inferred.
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.
Migrate from `AnthropicLLM` to `ChatAnthropic` and adapt your code to use message-based input/output rather than string-in/string-out.
Explicitly set `max_tokens=1024` (or your desired value) when instantiating `ChatAnthropic` or `AnthropicLLM` if you need to maintain the old behavior.
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.
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.
Only use tools like 'web fetch' in trusted environments or when handling non-sensitive data. Implement strict input validation and access controls.
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-...")`).
Ensure you have installed the `langchain-anthropic` package (`pip install -U langchain-anthropic`) and use the correct import path: `from langchain_anthropic import ChatAnthropic`.
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.
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.
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.