Tutorial · 2026

LangChain Tutorial: Build LLM Apps in Python

LangChain is the most popular framework for building LLM applications — RAG pipelines, agents, chatbots, and more. This tutorial covers the core concepts and walks you through building a complete RAG app.

What is LangChain?

LangChain is an open-source Python framework for building applications powered by large language models. It provides abstractions for prompts, chains (sequences of LLM calls), retrievers (semantic search), agents (autonomous tool use), and memory (conversation history) — making it fast to build complex LLM pipelines without boilerplate.

Installation & Setup

pip install langchain langchain-openai

For local models with Ollama:

pip install langchain langchain-ollama

Core Concepts

ConceptWhat It Does
LLM/Chat modelsCall any LLM API (OpenAI, Anthropic, Gemini, Ollama) with a unified interface
PromptsReusable prompt templates with variables — build consistent, maintainable prompts
ChainsCombine LLMs, prompts, and tools into reusable processing pipelines
RetrieversFetch semantically relevant documents from vector stores for RAG
AgentsLLMs that decide which tools to use and in what order — autonomous reasoning
MemoryPersist and retrieve conversation history across turns

Your First LangChain Chain

Before building RAG, understand the core building block — a chain. This is the minimum working example:

from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
import os

os.environ["OPENAI_API_KEY"] = "your-api-key"

# 1. Define the prompt template
prompt = ChatPromptTemplate.from_template(
    "Explain {topic} in one sentence for a beginner."
)

# 2. Pick a model
model = ChatOpenAI(model="gpt-4o-mini", temperature=0)

# 3. Chain them together with LCEL (pipe syntax)
chain = prompt | model | StrOutputParser()

# 4. Run it
result = chain.invoke({"topic": "vector databases"})
print(result)
# → "A vector database stores data as numerical vectors so you
#    can find similar items by meaning, not just exact keywords."

The | pipe syntax is LCEL (LangChain Expression Language) — the modern way to chain steps. Old-style LLMChain is deprecated.

Build a Simple RAG App

RAG (Retrieval-Augmented Generation) lets you answer questions from your own documents. Here is the full pipeline — 6 steps with working code:

1. Load documents

Use LangChain document loaders (PyPDFLoader, WebBaseLoader, TextLoader) to ingest your data.

2. Split into chunks

RecursiveCharacterTextSplitter breaks documents into ~500-token chunks with overlap for better retrieval.

3. Generate embeddings

OpenAIEmbeddings or OllamaEmbeddings converts each chunk into a vector representation.

4. Store in vector DB

ChromaDB stores the vectors locally (free). Pinecone for managed production storage.

5. Retrieve relevant chunks

Given a user query, retrieve the top-k most similar chunks using cosine similarity.

6. Generate answer

Pass the retrieved chunks as context to the LLM and generate a grounded answer.

## Complete RAG pipeline — copy and run

from langchain_community.document_loaders import TextLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_openai import OpenAIEmbeddings, ChatOpenAI
from langchain_community.vectorstores import Chroma
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnablePassthrough

# Step 1: Load your document
loader = TextLoader("my_doc.txt")
docs = loader.load()

# Step 2: Split into chunks
splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
chunks = splitter.split_documents(docs)

# Step 3 + 4: Embed and store in ChromaDB
vectorstore = Chroma.from_documents(
    documents=chunks,
    embedding=OpenAIEmbeddings()
)

# Step 5: Create a retriever (top-3 chunks)
retriever = vectorstore.as_retriever(search_kwargs={"k": 3})

# Step 6: RAG chain with LCEL
prompt = ChatPromptTemplate.from_template("""
Answer based only on the context below.
Context: {context}
Question: {question}
""")

def format_docs(docs):
    return "\n\n".join(d.page_content for d in docs)

rag_chain = (
    {"context": retriever | format_docs, "question": RunnablePassthrough()}
    | prompt
    | ChatOpenAI(model="gpt-4o-mini")
    | StrOutputParser()
)

# Ask a question
answer = rag_chain.invoke("What is the main topic of this document?")
print(answer)

Common Errors and How to Fix Them

The LangChain docs skip these. Here are the errors every beginner hits:

ModuleNotFoundError: No module named 'langchain_openai'Install

Fix: LangChain split packages in v0.2. Install separately: pip install langchain-openai (not langchain alone)

ValidationError: 1 validation error for ChatOpenAI — openai_api_key field requiredAuth

Fix: Set the key before importing: os.environ['OPENAI_API_KEY'] = 'sk-...' or use python-dotenv to load from .env

NotImplementedError when calling chain.run()API change

Fix: chain.run() is deprecated in v0.2+. Use chain.invoke({'input': 'your text'}) instead

Empty retriever results — LLM says 'I don't know'RAG

Fix: Your chunks are too large or embeddings mismatch. Reduce chunk_size to 300, add chunk_overlap=50, and verify you used the same embedding model for indexing and querying

Rate limit / quota errors from OpenAI during embeddingRate limit

Fix: Add a sleep between batches or switch to a local embedding model: pip install langchain-ollama and use OllamaEmbeddings(model='nomic-embed-text')

LCEL — The Modern Way to Build Chains

LangChain Expression Language (LCEL) is the recommended way to compose pipelines in LangChain v0.2+. The old LLMChain, ConversationalRetrievalChain, and similar classes are deprecated.

Old way (deprecated)

chain = LLMChain(llm=model, prompt=prompt)
chain.run("my input")

New way (LCEL)

chain = prompt | model | StrOutputParser()
chain.invoke({"input": "my input"})

Why LCEL?

Streaming out of the box — chain.stream() works on any LCEL chain

Async support — chain.ainvoke() for FastAPI and async apps

Batch processing — chain.batch([input1, input2]) with parallel execution

Tracing with LangSmith — every step is observable automatically

LangChain vs Alternatives

LangChain

Best for agents + broad integrations

Most popular
LlamaIndex

Best for complex document RAG and indexing

RAG specialist
Raw API calls

Best for simple, performance-critical apps

No abstractions

Frequently Asked Questions

Is LangChain still worth learning in 2026?

Yes. LangChain remains the most widely-used framework for building LLM applications. While alternatives like LlamaIndex (for RAG) and raw API calls (for simplicity) have their place, LangChain has the largest ecosystem, most integrations, and best community support. Most AI engineering job descriptions mention it.

Do I need to know LangChain for AI jobs?

Knowing LangChain is a strong signal to employers. Most AI engineering teams use LangChain or LangGraph (its agent framework). That said, understanding the underlying concepts (RAG, chains, agents) matters more than knowing any specific library — if you understand the patterns, switching frameworks is straightforward.

LangChain vs LlamaIndex — which should I use?

LangChain for agent-heavy apps and when you need broad LLM integrations. LlamaIndex for complex document ingestion and multi-document RAG — it has more sophisticated indexing strategies and query engines. For beginners, start with LangChain; it has better documentation and a larger community.

Can I use LangChain with local models?

Yes. LangChain has first-class Ollama support via ChatOllama and OllamaEmbeddings. This lets you build fully local RAG pipelines with no API costs. See our Ollama guide for setup instructions.

Ready to build your first RAG app?

Follow our full step-by-step LangChain RAG tutorial — from document loading to a working Q&A chatbot.

Read the Full Tutorial →