Implementation:Langchain ai Langchain AnthropicLLM
| Knowledge Sources | |
|---|---|
| Domains | LLM Framework, Anthropic, Text Completion |
| Last Updated | 2026-02-11 00:00 GMT |
Overview
The AnthropicLLM class provides a deprecated legacy text-completion wrapper around the Anthropic Messages API, supporting synchronous, asynchronous, and streaming text generation.
Description
This module defines the AnthropicLLM class and its base class _AnthropicCommon in the langchain-anthropic partner package. AnthropicLLM extends LangChain's LLM base class to provide a simple text-in/text-out interface to the Anthropic API. It is explicitly marked as deprecated; users should use ChatAnthropic instead. The class internally converts text prompts to the Anthropic Messages API format, handling both legacy prompt markers (Human:/Assistant:) and plain text.
Usage
This class is deprecated. Users should import ChatAnthropic from langchain_anthropic instead. The legacy import remains available for backward compatibility.
Code Reference
Source Location
- Repository: Langchain_ai_Langchain
- File:
libs/partners/anthropic/langchain_anthropic/llms.py - Lines: 1-433
Signature
class _AnthropicCommon(BaseLanguageModel):
client: Any = None
async_client: Any = None
model: str = Field(default="claude-sonnet-4-5", alias="model_name")
max_tokens: int = Field(default=1024, alias="max_tokens_to_sample")
temperature: float | None = None
top_k: int | None = None
top_p: float | None = None
streaming: bool = False
default_request_timeout: float | None = None
max_retries: int = 2
anthropic_api_url: str | None = Field(alias="base_url", ...)
anthropic_api_key: SecretStr = Field(alias="api_key", ...)
...
class AnthropicLLM(LLM, _AnthropicCommon):
def _call(self, prompt: str, stop: list[str] | None = None, ...) -> str: ...
async def _acall(self, prompt: str, stop: list[str] | None = None, ...) -> str: ...
def _stream(self, prompt: str, stop: list[str] | None = None, ...) -> Iterator[GenerationChunk]: ...
async def _astream(self, prompt: str, stop: list[str] | None = None, ...) -> AsyncIterator[GenerationChunk]: ...
Import
from langchain_anthropic import AnthropicLLM # Deprecated
# Recommended replacement:
from langchain_anthropic import ChatAnthropic
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| model | str | No | Model name to use (default: "claude-sonnet-4-5"). Alias: model_name
|
| max_tokens | int | No | Maximum tokens to predict per generation (default: 1024). Alias: max_tokens_to_sample
|
| temperature | float or None | No | Randomness in generation (default: None) |
| top_k | int or None | No | Number of most likely tokens to consider (default: None) |
| top_p | float or None | No | Nucleus sampling threshold (default: None) |
| streaming | bool | No | Whether to stream results (default: False) |
| default_request_timeout | float or None | No | Timeout for requests in seconds (default: None, Anthropic default is 600s) |
| max_retries | int | No | Number of retries for failed requests (default: 2) |
| anthropic_api_url | str or None | No | Base URL for API requests. Alias: base_url. Env: ANTHROPIC_API_URL
|
| anthropic_api_key | SecretStr | No | API key. Alias: api_key. Env: ANTHROPIC_API_KEY
|
Outputs
| Name | Type | Description |
|---|---|---|
| result | str | Generated text from the Anthropic model (for _call and _acall)
|
| chunk | GenerationChunk | Individual token chunks (for _stream and _astream)
|
Key Mechanisms
Prompt Formatting
The _format_messages method converts text prompts into the Anthropic Messages API format:
- Legacy prompts: Prompts containing
Human:/Assistant:markers are split and converted to alternating user/assistant messages - Plain text: Wrapped in a single user message, with
Human:/Assistant:markers stripped if present
Environment Validation
The validate_environment model validator initializes both synchronous (anthropic.Anthropic) and asynchronous (anthropic.AsyncAnthropic) clients using the configured API key, base URL, timeout, and retry settings.
Deprecation Warning
A model_validator on AnthropicLLM emits a deprecation warning on every instantiation, directing users to ChatAnthropic.
Usage Examples
Basic Usage
# Deprecated - use ChatAnthropic instead
from langchain_anthropic import AnthropicLLM
model = AnthropicLLM(model="claude-sonnet-4-5")
# Synchronous call
response = model.invoke("What is the meaning of life?")
print(response)
Streaming Usage
from langchain_anthropic import AnthropicLLM
model = AnthropicLLM(model="claude-sonnet-4-5", streaming=True)
for chunk in model.stream("Write a haiku about Python."):
print(chunk, end="")