LangChain vs LlamaIndex: Which Should a Beginner Use for RAG?
By RAGStarter ยท 7th October 2026
If you are building your first RAG (retrieval-augmented generation) project, two names come up again and again: LangChain and LlamaIndex. Both can read your documents and answer questions about them. They just feel very different to use. This guide shows the same tiny project in both, so you can pick based on what you actually see.
Quick answer
Start with LlamaIndex if your project is “ask questions about my documents.” You can get a working app in about five lines of code. Start with LangChain if you expect to build more than document Q&A, such as an agent that calls tools, or a chain with several steps. LangChain takes a few more lines to set up but gives you more control over each part.
For a student’s first RAG project, LlamaIndex is usually the faster first win. You can always move to LangChain later, because the core ideas (chunking, embeddings, retrieval) are the same in both.
What is RAG, in one minute?
A language model only knows what it was trained on. RAG lets it answer from your files:
- Load your documents (PDFs, notes, web pages).
- Split them into small chunks.
- Embed each chunk, which turns text into numbers that capture meaning.
- Store those embeddings in a vector store.
- Retrieve the chunks most similar to a question, then pass them to the model as context.
LangChain and LlamaIndex are both toolkits that wire these five steps together for you.
What is LangChain?
LangChain is a general toolkit for building applications on top of language models. RAG is one thing you can build with it, next to agents, tool calling and multi-step workflows. It gives you separate building blocks (loaders, splitters, vector stores, prompts, models) and you connect them yourself.
Good for: projects that will grow beyond Q&A, and learners who want to see every step. Watch out for: more imports and more concepts to learn up front, and the library changes quickly, so older tutorials may not match current versions.
What is LlamaIndex?
LlamaIndex is built around one job: connecting your data to a language model. Its defaults handle loading, chunking, embedding and querying for you, so the simplest version is very short.
Good for: document Q&A, quick prototypes, and beginners who want a result first and detail later. Watch out for: the short version hides a lot of choices (chunk size, embedding model, retriever), so you will need to learn what the defaults are doing as soon as your answers look off.
The same project in both
Both examples answer a question about files in a folder called docs. They assume Python and an OpenAI API key in the OPENAI_API_KEY environment variable. Library versions change often, so if an import fails, check the current official docs.
LlamaIndex
pip install llama-indexfrom llama_index.core import VectorStoreIndex, SimpleDirectoryReader
# 1. Load every file in the docs folder
documents = SimpleDirectoryReader("docs").load_data()
# 2. Split, embed and store in one step
index = VectorStoreIndex.from_documents(documents)
# 3. Ask a question
query_engine = index.as_query_engine()
response = query_engine.query("What is the main topic of these documents?")
print(response)That is the whole app. Chunking, embedding and retrieval all happen behind from_documents and as_query_engine.
LangChain
pip install langchain langchain-community langchain-openai langchain-chroma langchain-text-splitters pypdffrom langchain_community.document_loaders import PyPDFLoader
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_openai import OpenAIEmbeddings, ChatOpenAI
from langchain_chroma import Chroma
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnablePassthrough
# 1. Load a PDF
docs = PyPDFLoader("docs/notes.pdf").load()
# 2. Split into chunks
splitter = RecursiveCharacterTextSplitter(chunk_size=800, chunk_overlap=100)
chunks = splitter.split_documents(docs)
# 3. Embed and store
vectorstore = Chroma.from_documents(chunks, OpenAIEmbeddings())
retriever = vectorstore.as_retriever(search_kwargs={"k": 3})
# 4. Build the chain: retrieve -> prompt -> model
prompt = ChatPromptTemplate.from_template(
"Answer using only this context:\n{context}\n\nQuestion: {question}"
)
llm = ChatOpenAI(model="gpt-4o-mini") # use any chat model you have access to
def format_docs(documents):
return "\n\n".join(d.page_content for d in documents)
chain = (
{"context": retriever | format_docs, "question": RunnablePassthrough()}
| prompt
| llm
| StrOutputParser()
)
print(chain.invoke("What is the main topic of this document?"))It is longer, but each step is visible. If you want to change the chunk size, the number of retrieved chunks or the prompt, the exact line is right there.
Side-by-side comparison
| LangChain | LlamaIndex | |
|---|---|---|
| Main focus | General LLM applications, agents, workflows | Connecting your data to an LLM |
| Lines for a basic RAG app | More (each step is explicit) | Fewer (sensible defaults) |
| Control over each step | High | Medium, increases as you customise |
| Learning curve | Steeper at the start | Gentler at the start |
| Best first project | Multi-step or tool-using apps | Document Q&A |
| Works with local or free models | Yes | Yes |
Which one should you pick?
Ask yourself two questions:
- Is my project mainly “chat with my PDFs or notes”? Pick LlamaIndex.
- Do I already know I want agents, tools or several chained steps? Pick LangChain.
If you are unsure, build the LlamaIndex version first. Once it works, rebuild it in LangChain. Doing both on the same documents is the fastest way to understand what RAG is actually doing, and it makes a good learning project in itself.
Common beginner mistakes
- Chunks that are too big or too small. Huge chunks bury the answer, and tiny chunks lose context. Start around 500 to 1000 characters with a small overlap and adjust.
- Not checking what was retrieved. If the answer is wrong, look at the retrieved chunks first. Most bad answers come from bad retrieval, not a bad model.
- Copying a tutorial that uses old imports. Both libraries move fast. Match the install commands to the version in the tutorial.
- Forgetting the API key. Both examples above fail without
OPENAI_API_KEYset.
Try it yourself
[Add your own screenshot here: your terminal output from running one of the examples on your own PDF.]
[Add 2 to 3 sentences about what you noticed: what was easy, what confused you, what you would change.]
FAQ
Can I use both together? Yes. Some projects use LlamaIndex for loading and indexing data and LangChain for the surrounding workflow.
Do I need a paid API? The examples above use OpenAI by default, but both libraries can work with other providers and with local models. Check each library’s documentation for the current setup.
Which one has better documentation for beginners? Both have quickstart guides. Try each quickstart for ten minutes and keep the one that makes sense to you.
Do I need a framework at all for RAG?
No. A basic RAG pipeline is just load, split, embed, search and prompt, which you can write by hand. A framework saves time and gives you ready-made parts, but understanding the steps first makes any framework easier to use. Our chatbot guide shows the steps.
Conclusion
For a first RAG project on your own documents, LlamaIndex gets you to a working result faster. LangChain is the better long-term choice if your idea is bigger than document Q&A. The concepts you learn in either one carry over to the other, so the worst outcome is learning RAG twice.