Getting Started with LangChain: A Practical Guide
Learn how to build your first LLM-powered application using LangChain. From chains to agents, this hands-on guide covers everything you need to get started.
Why LangChain?
LangChain has become the de facto standard for building LLM-powered applications in Python. It provides a modular framework that abstracts away the complexity of working with language models, allowing you to focus on building your application logic.
In this guide, we'll build a simple Q&A bot that can answer questions about your own documents.
Setting Up Your Environment
First, create a new Python project and install the dependencies:
pip install langchain langchain-openai chromadb python-dotenv
Create a .env file with your OpenAI API key:
OPENAI_API_KEY=your-key-here
Building Your First Chain
The simplest LangChain application is a chain — a sequence of operations that process input and produce output.
from langchain_openai import ChatOpenAI
from langchain.prompts import ChatPromptTemplatellm = ChatOpenAI(model="gpt-4o", temperature=0)
prompt = ChatPromptTemplate.from_messages([
("system", "You are a helpful AI assistant."),
("user", "{input}")
])
chain = prompt | llm
response = chain.invoke({"input": "What is LangChain?"})
print(response.content)
Working with Documents
LangChain's real power comes from its document handling capabilities. Let's load a PDF and ask questions about it:
from langchain_community.document_loaders import PyPDFLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_openai import OpenAIEmbeddings
from langchain_community.vectorstores import Chroma# Load and split the document
loader = PyPDFLoader("your-document.pdf")
documents = loader.load()
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=1000,
chunk_overlap=200
)
chunks = text_splitter.split_documents(documents)
# Create vector store
embeddings = OpenAIEmbeddings()
vectorstore = Chroma.from_documents(chunks, embeddings)
Building a RAG Pipeline
Now let's combine everything into a Retrieval-Augmented Generation (RAG) pipeline:
from langchain.chains import create_retrieval_chain
from langchain.chains.combine_documents import create_stuff_documents_chainretriever = vectorstore.as_retriever()
qa_prompt = ChatPromptTemplate.from_messages([
("system", "Answer based on the provided context."),
("user", "Context: {context}\n\nQuestion: {input}")
])
document_chain = create_stuff_documents_chain(llm, qa_prompt)
retrieval_chain = create_retrieval_chain(retriever, document_chain)
response = retrieval_chain.invoke({
"input": "What are the key findings?"
})
Next Steps
Now that you've built your first RAG application, try:
- Adding memory for multi-turn conversations - Using different document loaders (web pages, databases, APIs) - Implementing streaming responses for better UX - Adding error handling and fallbacks
LangChain's ecosystem is vast, but starting with these fundamentals will give you a solid foundation for building more complex applications.
Related Articles
LlamaIndex vs LangChain in 2026: Which RAG Framework Should You Use?
Head-to-head comparison of LlamaIndex and LangChain for building RAG applications in 2026. We compare data ingestion, retrieval quality, agent capabilities, and production readiness with real benchmarks.
LangChain in Production: Building a Reliable RAG Pipeline
Go beyond the LangChain hello-world tutorial. Learn how to build a production-ready RAG pipeline with proper chunking strategies, retrieval optimization, evaluation, and monitoring.
Complete Guide to Fine-Tuning LLMs in 2026: From LoRA to Full Fine-Tuning
A practical guide to fine-tuning LLMs in 2026. Compare LoRA, QLoRA, full fine-tuning, and DPO. Includes GPU requirements, cost estimates, step-by-step tutorials, and when to choose each approach.