Use Claude with LangChain in Python. Install langchain-anthropic, build chains, use LCEL, add memory, and stream responses — working 2026 examples.
LangChain's langchain-anthropic package wraps the Anthropic SDK so Claude fits natively into any LangChain pipeline — chains, agents, retrievers, and memory.
pip install langchain langchain-anthropic
from langchain_anthropic import ChatAnthropic
from langchain_core.messages import HumanMessage
llm = ChatAnthropic(model="claude-sonnet-4-6") # reads ANTHROPIC_API_KEY from env
response = llm.invoke([HumanMessage(content="Explain LangChain in two sentences.")])
print(response.content)
from langchain_anthropic import ChatAnthropic
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
prompt = ChatPromptTemplate.from_messages([
("system", "You are a concise technical writer."),
("human", "{question}")
])
llm = ChatAnthropic(model="claude-sonnet-4-6", max_tokens=512)
chain = prompt | llm | StrOutputParser()
answer = chain.invoke({"question": "What is prompt caching?"})
print(answer)
for chunk in chain.stream({"question": "Explain agentic AI step by step."}):
print(chunk, end="", flush=True)
from langchain.memory import ConversationBufferMemory
from langchain.chains import ConversationChain
llm = ChatAnthropic(model="claude-sonnet-4-6", max_tokens=1024)
memory = ConversationBufferMemory()
conversation = ConversationChain(llm=llm, memory=memory)
print(conversation.predict(input="Hi, my name is Alex."))
print(conversation.predict(input="What's my name?")) # Claude remembers "Alex"
from langchain_anthropic import ChatAnthropic
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnablePassthrough
# Assume 'retriever' is any LangChain retriever (Chroma, FAISS, Pinecone, etc.)
prompt = ChatPromptTemplate.from_template("""
Answer the question based only on the following context:
{context}
Question: {question}
""")
llm = ChatAnthropic(model="claude-sonnet-4-6")
rag_chain = (
{"context": retriever, "question": RunnablePassthrough()}
| prompt
| llm
| StrOutputParser()
)
answer = rag_chain.invoke("What are Claude's context window limits?")
| Criterion | Raw Anthropic SDK | LangChain + langchain-anthropic |
|---|---|---|
| Setup complexity | Minimal (1 package) | 2+ packages, more concepts |
| Switching models/providers | Manual refactor | Swap one class name |
| Retrieval (RAG) | DIY | Built-in retrievers + vectorstores |
| Agents | Custom loop needed | LangGraph / AgentExecutor |
| Observability | SDK callbacks | LangSmith built in |
| Streaming | Native SDK stream() | .stream() on any chain |
| When to use | Direct API, minimal deps | RAG, multi-provider, agent graphs |
Use the raw SDK when you want minimal dependencies and direct control. Use LangChain when you need retrievers, multi-provider portability, or LangSmith tracing. Estimate costs for either approach at Claude API Cost Calculator.