Installation
Copy
pip install langchain-anthropic
Code Examples
- Python
- Node.js
Copy
import os
from langchain_anthropic import ChatAnthropic
llm = ChatAnthropic(
model='claude-sonnet-4-5-20250929',
anthropic_api_key=os.getenv('PIPELLM_API_KEY'),
anthropic_api_url='https://api.pipellm.com'
)
response = llm.invoke([
('user', 'Why is the sky blue?')
])
print(response.content)
Copy
import { ChatAnthropic } from '@langchain/anthropic';
const llm = new ChatAnthropic({
model: 'claude-sonnet-4-5-20250929',
anthropicApiKey: process.env.PIPELLM_API_KEY,
anthropicApiUrl: 'https://api.pipellm.com'
});
const response = await llm.invoke([
{ role: 'user', content: 'Why is the sky blue?' }
]);
console.log(response.content);
Streaming
Copy
import os
from langchain_anthropic import ChatAnthropic
llm = ChatAnthropic(
model='claude-sonnet-4-5-20250929',
anthropic_api_key=os.getenv('PIPELLM_API_KEY'),
anthropic_api_url='https://api.pipellm.com',
streaming=True
)
for chunk in llm.stream('Tell me a story'):
print(chunk.content, end='', flush=True)
Function Calling
Copy
import os
from langchain_anthropic import ChatAnthropic
from langchain_core.tools import tool
@tool
def get_weather(location: str) -> str:
"""Get the weather for a location."""
return f"Weather in {location}: 22°C, Sunny"
llm = ChatAnthropic(
model='claude-sonnet-4-5-20250929',
anthropic_api_key=os.getenv('PIPELLM_API_KEY'),
anthropic_api_url='https://api.pipellm.com'
)
llm_with_tools = llm.bind_tools([get_weather])
response = llm_with_tools.invoke("What's the weather in Tokyo?")
print(response.tool_calls)