LangChain Complete Guide 2026: Build Powerful AI Apps and Agents
If you want to build serious AI applications β not just call an API, but create complex pipelines, autonomous agents, and production-ready systems β LangChain is the framework you need to know.
LangChain is the most widely adopted framework for building LLM-powered applications. In 2026, itβs been downloaded over 200 million times and powers everything from chatbots to complex multi-agent research systems.
This guide will take you from zero to building real AI applications with LangChain.
Photo by John Barkiple on Unsplash
What Is LangChain?
LangChain is an open-source Python (and JavaScript) framework for building applications powered by large language models. It provides:
- Chains: Connect multiple LLM calls and tools into pipelines
- Agents: Let AI decide which tools to use and in what order
- RAG: Retrieval-Augmented Generation for knowledge-grounded responses
- Memory: Persistent conversation context
- Integrations: 600+ integrations with databases, APIs, and tools
The LangChain Ecosystem (2026)
LangChain Ecosystem:
βββ langchain β Core framework (chains, agents, prompts)
βββ langchain-core β Core abstractions and interfaces
βββ langchain-community β 600+ third-party integrations
βββ langchain-openai β OpenAI-specific integration
βββ langchain-anthropic β Anthropic/Claude integration
βββ langchain-aws β AWS Bedrock integration
βββ LangGraph β Agent workflow orchestration
βββ LangSmith β Observability and evaluation platform
βββ LangServe β Deploy LangChain apps as REST APIs
Installation & Setup
# Core installation
pip install langchain langchain-openai langchain-anthropic
# With all common dependencies
pip install langchain[all]
# For RAG (vector databases)
pip install langchain chromadb faiss-cpu
# For agents with tools
pip install langchain-community duckduckgo-search
Environment Setup
# .env file
OPENAI_API_KEY=sk-...
ANTHROPIC_API_KEY=sk-ant-...
LANGCHAIN_API_KEY=ls-... # Optional: LangSmith tracing
LANGCHAIN_TRACING_V2=true
# Load in Python
from dotenv import load_dotenv
load_dotenv()
Core Concepts with Code
1. Basic LLM Calls
from langchain_anthropic import ChatAnthropic
from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage, SystemMessage
# Use Claude
llm = ChatAnthropic(model="claude-3-5-sonnet-20241022")
# Use GPT-4o
llm = ChatOpenAI(model="gpt-4o")
# Simple call
response = llm.invoke("What is quantum computing?")
print(response.content)
# With system prompt
messages = [
SystemMessage(content="You are a Python expert. Be concise."),
HumanMessage(content="What's the difference between list and tuple?")
]
response = llm.invoke(messages)
2. Prompt Templates
from langchain_core.prompts import ChatPromptTemplate
# Create a reusable template
prompt = ChatPromptTemplate.from_messages([
("system", "You are a helpful assistant for {company_name}."),
("human", "{question}")
])
# Chain it with an LLM
chain = prompt | llm
# Invoke with variables
response = chain.invoke({
"company_name": "TechCorp",
"question": "What are your office hours?"
})
3. Output Parsers
from langchain_core.output_parsers import StrOutputParser, JsonOutputParser
from langchain_core.pydantic_v1 import BaseModel
from typing import List
# Parse structured output
class ProductReview(BaseModel):
rating: int
pros: List[str]
cons: List[str]
summary: str
parser = JsonOutputParser(pydantic_object=ProductReview)
prompt = ChatPromptTemplate.from_template(
"Analyze this product review: {review}\n\n{format_instructions}"
)
chain = prompt | llm | parser
result = chain.invoke({
"review": "Great laptop! Fast performance, good battery. Keyboard is a bit stiff.",
"format_instructions": parser.get_format_instructions()
})
# Returns: ProductReview(rating=4, pros=['Fast', 'Good battery'],
# cons=['Stiff keyboard'], summary='...')
Building a RAG System
RAG (Retrieval-Augmented Generation) lets your AI answer questions based on your own documents.
from langchain_community.document_loaders import PyPDFLoader, WebBaseLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_openai import OpenAIEmbeddings
from langchain_community.vectorstores import Chroma
from langchain.chains import RetrievalQA
# Step 1: Load documents
loader = PyPDFLoader("company_handbook.pdf")
documents = loader.load()
# Also works with URLs:
# loader = WebBaseLoader("https://docs.yourcompany.com")
# Step 2: Split into chunks
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=1000,
chunk_overlap=200
)
chunks = text_splitter.split_documents(documents)
# Step 3: Create embeddings and vector store
embeddings = OpenAIEmbeddings()
vectorstore = Chroma.from_documents(chunks, embeddings)
# Step 4: Create retrieval chain
llm = ChatOpenAI(model="gpt-4o")
qa_chain = RetrievalQA.from_chain_type(
llm=llm,
chain_type="stuff",
retriever=vectorstore.as_retriever(search_kwargs={"k": 4})
)
# Step 5: Ask questions about your documents
response = qa_chain.invoke({"query": "What is the vacation policy?"})
print(response["result"])
Building AI Agents with LangGraph
LangGraph is LangChainβs framework for building stateful, multi-step AI agents:
from langgraph.graph import StateGraph, END
from langgraph.prebuilt import create_react_agent
from langchain_community.tools import DuckDuckGoSearchRun
from langchain_core.tools import tool
import requests
# Define custom tools
@tool
def get_weather(city: str) -> str:
"""Get current weather for a city."""
# Call weather API
response = requests.get(f"https://api.weather.com/v1/{city}")
return response.json()["description"]
@tool
def calculate(expression: str) -> str:
"""Safely evaluate a mathematical expression."""
try:
result = eval(expression, {"__builtins__": {}}, {})
return str(result)
except Exception as e:
return f"Error: {e}"
# Built-in tools
search = DuckDuckGoSearchRun()
# Create agent with tools
tools = [search, get_weather, calculate]
agent = create_react_agent(
model=ChatAnthropic(model="claude-3-5-sonnet-20241022"),
tools=tools
)
# Run the agent
result = agent.invoke({
"messages": [{
"role": "user",
"content": "What's the weather in Seoul, and what's 15% of the temperature in Fahrenheit?"
}]
})
Real-World Application: Customer Support Bot
from langchain.memory import ConversationBufferWindowMemory
from langchain_core.prompts import MessagesPlaceholder
# Memory for conversation history
memory = ConversationBufferWindowMemory(
k=10, # Remember last 10 exchanges
memory_key="history",
return_messages=True
)
# Support bot prompt
prompt = ChatPromptTemplate.from_messages([
("system", """You are a helpful customer support agent for AcmeSoft.
Context about our products:
- AcmeSoft Pro: $99/month, unlimited users, cloud-based
- AcmeSoft Starter: $29/month, up to 5 users
- Free trial: 14 days, no credit card required
Always be polite, empathetic, and offer to escalate to human support if needed."""),
MessagesPlaceholder(variable_name="history"),
("human", "{input}")
])
# Create chain with memory
chain = prompt | llm | StrOutputParser()
class SupportBot:
def __init__(self):
self.memory = ConversationBufferWindowMemory(
k=10, memory_key="history", return_messages=True
)
def chat(self, user_input: str) -> str:
history = self.memory.load_memory_variables({})
response = chain.invoke({
"input": user_input,
"history": history.get("history", [])
})
self.memory.save_context(
{"input": user_input},
{"output": response}
)
return response
# Use it
bot = SupportBot()
print(bot.chat("What's the difference between Pro and Starter?"))
print(bot.chat("Can I try before buying?"))
print(bot.chat("How do I cancel my subscription?"))
LangSmith: Observability & Debugging
LangSmith lets you trace, debug, and evaluate your LangChain applications:
import os
os.environ["LANGCHAIN_TRACING_V2"] = "true"
os.environ["LANGCHAIN_PROJECT"] = "my-ai-app"
# Now ALL LangChain calls are automatically traced
# View them at: smith.langchain.com
# Run evaluation on your chain
from langsmith import Client
from langchain.smith import RunEvalConfig
client = Client()
eval_config = RunEvalConfig(
evaluators=["qa", "conciseness", "harmfulness"],
input_key="question",
eval_llm=ChatOpenAI(model="gpt-4o")
)
# This runs your chain on test cases and scores it
results = client.run_on_dataset(
dataset_name="my-test-questions",
llm_or_chain_factory=lambda: qa_chain,
evaluation=eval_config
)
LangChain Pricing
| Component | Cost |
|---|---|
| LangChain Framework | Free & Open Source |
| LangSmith (Developer) | Free (up to 5k traces/month) |
| LangSmith (Plus) | $39/month (50k traces) |
| LangSmith (Enterprise) | Custom |
| LangServe | Free & Open Source |
LangChain itself is free β you only pay for the LLM APIs you use.
Best Practices for Production
1. Use Async for Performance
import asyncio
async def process_batch(questions: list[str]):
tasks = [chain.ainvoke({"question": q}) for q in questions]
return await asyncio.gather(*tasks)
# Process 100 questions concurrently
results = asyncio.run(process_batch(questions))
2. Add Retry Logic
from langchain_core.runnables import RunnableWithFallbacks
# Primary: Claude, Fallback: GPT-4o
chain_with_fallback = (
prompt | claude_llm | parser
).with_fallbacks([prompt | gpt4_llm | parser])
3. Cache Expensive Calls
from langchain.globals import set_llm_cache
from langchain.cache import SQLiteCache
set_llm_cache(SQLiteCache(database_path=".langchain.db"))
# Now identical prompts use cached responses
Learning Resources
- Official Docs: python.langchain.com
- LangChain Academy: Free courses on building agents
- GitHub: github.com/langchain-ai/langchain β 90k+ stars
- LangGraph: langchain-ai.github.io/langgraph
- Community: Discord with 75k+ developers
The Verdict
LangChain Rating: 9.0/10
LangChain is the essential framework for any developer building serious AI applications in 2026. Its extensive ecosystem, strong community, and continuous improvement make it the go-to choice.
Whether youβre building a simple chatbot, a RAG system over your company documents, or a complex multi-agent workflow, LangChain provides the tools to do it right.
Start building at python.langchain.com