← GUIDES

GUIDE / SETUP NOTE

LangChain vs LlamaIndex: The Same Local RAG, Compared

LangChain vs LlamaIndex on the same 8 local files and 11 questions with qwen2.5:3b: the same retrieval, 28 against 16 lines of code, different answers.

17 Nov 2026Checked 2026-10-31
LangChain and LlamaIndex run the same local RAG over 8 policy files with qwen2.5:3b and nomic-embed-text: both found the right file for all 10 questions that had one, the LlamaIndex script was 16 lines against 28, and each gave one weak answer

Two Python 3.13.5 scripts, one using langchain-core 1.7.3, langchain-ollama 1.1.0 and langchain-text-splitters 1.1.3, one using llama-index-core 0.14.25, llama-index-llms-ollama 0.11.0 and llama-index-embeddings-ollama 0.10.0, against Ollama 0.40.13 (version checked after the run) on 127.0.0.1 with qwen2.5:3b and nomic-embed-text already on the machine, temperature 0: the same 8 short invented policy files and 11 labelled questions, top 2 chunks each, in one test.sh run on 2026-10-31 PYT, plus the LangChain RAG tutorial, the LlamaIndex local starter example and the LlamaIndex chat prompt source at v0.14.25 read the same day.

We built the same small RAG twice, once in LangChain and once in LlamaIndex, over the same 8 local files, with qwen2.5:3b for answers and nomic-embed-text for search. Both found the right file for all 10 questions that had one, and neither file set answers the eleventh. The LlamaIndex script was 16 lines of code against 28. The answers differed in tone, and each framework gave one answer we would not ship.

The human step: a person writes the source files, the labelled questions and the expected answers, and reads every reply. A keyword check in the script scored the answers first; we then read all 22 replies.

Based on LangChain, by the LangChain team and contributors, under the MIT licence, checked with langchain-core 1.7.3 (PyPI) and langchain-ollama 1.1.0. Based on LlamaIndex, by the LlamaIndex team and contributors, under the MIT licence, checked with llama-index-core 0.14.25 (PyPI). Checked 2026-10-31.

The set-up

Both scripts used qwen2.5:3b through Ollama 0.40.13 on the same machine, at temperature 0, and nomic-embed-text for embeddings.

The qwen2.5:3b weights are under the "Qwen RESEARCH LICENSE AGREEMENT", for non-commercial use only; check the model card before any commercial use.

The files are 8 short policy notes for an invented tea shop: returns, shipping, a subscription, storage, brewing, wholesale, gift cards and allergens. Each is under 330 characters, so each became one chunk in both frameworks. Ten questions have an answer in one file; one, about next-day delivery, has none. Both scripts take the top 2 chunks.

The LangChain script follows the parts of its RAG tutorial: a splitter, an in-memory vector store and a prompt. We gave it the same question prompt LlamaIndex uses by default.

python
import time
from pathlib import Path

from langchain_core.documents import Document
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.vectorstores import InMemoryVectorStore
from langchain_ollama import ChatOllama, OllamaEmbeddings
from langchain_text_splitters import RecursiveCharacterTextSplitter

from questions import report

PROMPT = ("Context information is below.\n---------------------\n{context}\n"
          "---------------------\nGiven the context information and not prior "
          "knowledge, answer the query.\nQuery: {question}\nAnswer: ")

t0 = time.time()
docs = [Document(page_content=p.read_text(), metadata={"source": p.name})
        for p in sorted(Path("docs").glob("*.txt"))]
splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
chunks = splitter.split_documents(docs)
embed = OllamaEmbeddings(model="nomic-embed-text")
store = InMemoryVectorStore.from_documents(chunks, embed)
retriever = store.as_retriever(search_kwargs={"k": 2})
chain = ChatPromptTemplate.from_messages([("user", PROMPT)]) | ChatOllama(
    model="qwen2.5:3b", temperature=0)
print(f"langchain: {len(chunks)} chunks indexed in {time.time() - t0:.1f}s")


def ask(question):
    found = retriever.invoke(question)
    context = "\n\n".join(d.page_content for d in found)
    reply = chain.invoke({"context": context, "question": question}).content
    return [d.metadata["source"] for d in found], reply


report("langchain", ask)

The LlamaIndex script follows its local starter example: read a folder, build an index, ask a query engine.

python
import time

from llama_index.core import Settings, SimpleDirectoryReader, VectorStoreIndex
from llama_index.embeddings.ollama import OllamaEmbedding
from llama_index.llms.ollama import Ollama

from questions import report

Settings.embed_model = OllamaEmbedding(model_name="nomic-embed-text")
Settings.llm = Ollama(model="qwen2.5:3b", temperature=0, request_timeout=120)

t0 = time.time()
docs = SimpleDirectoryReader("docs").load_data()
index = VectorStoreIndex.from_documents(docs)
engine = index.as_query_engine(similarity_top_k=2)
print(f"llamaindex: {len(index.docstore.docs)} chunks indexed in {time.time() - t0:.1f}s")


def ask(question):
    response = engine.query(question)
    return [n.node.metadata["file_name"] for n in response.source_nodes], str(response)


report("llamaindex", ask)

Both import the same questions and the same report function, which prints the files retrieved, whether the expected file was among them, and whether the reply contains an expected word.

sh
set -e
venv/bin/python rag_langchain.py
venv/bin/python rag_llamaindex.py
for f in rag_langchain.py rag_llamaindex.py; do
  echo "$f: $(grep -cv -e '^\s*$' -e '^\s*#' $f) lines of code"
done
venv/bin/pip list 2>/dev/null | grep -E '^(langchain-core|langchain-ollama|langchain-text-splitters|llama-index-core|llama-index-llms-ollama|llama-index-embeddings-ollama|ollama) '

Results

LangChainLlamaIndex
Lines of code (no blanks or comments)2816
Chunks indexed88
Right file in the top 2 (10 answerable questions)10 of 1010 of 10
Next-day question (no right file)Stated a policy no file hasSaid the files do not mention it
Keyword check passed10 of 1010 of 10
Answers we would ship, after reading10 of 1110 of 11
Time to index1.4 s0.3 s
Time for 11 questions46.0 s39.5 s

The whole run took 1 minute 29 seconds.

Retrieval was the same. Both used the same embedding model on the same chunks, and both put the right file first for every answerable question. The test script counted the next-day question as a hit because it has no right file; we leave it out of the retrieval score. Both returned the shipping and returns files for it. The second file differed once: for the wholesale question LangChain returned the returns file and LlamaIndex the shipping file.

The keyword check missed a wrong answer. For "Is the chai blend safe for someone with a nut allergy?" LlamaIndex said no, then added that the blend was "not specified as being made in a facility that is also used for handling nuts". The allergen file says the opposite. The reply contained "nuts", so the check passed it.

LangChain stated a policy the files do not have. For the next-day question it said "we do not offer next-day delivery". No file says that. LlamaIndex opened with "No," and then said the information "does not mention next-day delivery", which is what the files support.

The tone differed. LlamaIndex's default question prompt, in its source at v0.14.25, also has a system message with the rule "Never directly reference the given context in your answer." Its replies were shorter, such as "4.50 EUR" and "90 C". Two LangChain replies opened with "Based on the context information provided"; one LlamaIndex reply still said "the information provided".

The times favour LlamaIndex less than they look. LangChain ran first, so it appears to have paid for loading both models into memory. We did not run the order the other way round.

What this test does not show

  • Larger document sets. Eight one-chunk files test the plumbing, not the splitters. With long files, LangChain's splitter counts characters and LlamaIndex's default counts tokens, so the same number gives different chunks.
  • Tool calling and agents. Both frameworks wrap retrieval as an agent tool; we tested plain question answering only.
  • Repeat runs. One run. The reply wording can vary.
  • Other models. A bigger model may avoid both weak answers.

What to set up

  • Pick on code style, not retrieval, for a small local RAG: with the same embeddings and top-k, both found the same files.
  • Set top-k and chunk size yourself in both, so a comparison or an upgrade does not change them quietly.
  • Use a no-answer question in your test set; it caught the LangChain overclaim.
  • Read the replies, not only a keyword score; it missed the LlamaIndex mistake.

Takeaway

On the same files, model and settings, LangChain and LlamaIndex retrieved the same documents. LlamaIndex needed fewer lines and came with a stricter default prompt; LangChain showed every step in the script. Each gave one answer a person should catch before it reaches a customer.

Next step: write five questions your own files can answer and one they cannot, run them through your RAG, and read every reply.

How we check the tools we list is on our verification page.

Good setups, worth keeping. Request email updates on the home page, then confirm by email. No subscription starts until you confirm. Or find an agent for the job.

Your privacy choices

Choose optional tools. Rejecting them keeps the setup library available. Advertising consent stays off.

Choices stay in this browser for 90 days. You can change them using Privacy settings. Privacy and cookie details