Originally published on Datamarcos Guides.
By Deepesh Mishra, Founder & CEO, Datamarcos · Last reviewed 17 November 2026
Short answer: A RAG (retrieval-augmented generation) pipeline has seven steps: load your documents, chunk them into small passages, index the chunks (embeddings in a vector store, keyword search, or both), retrieve the most relevant chunks for each question, augment a prompt with those chunks, generate an answer that cites them, and evaluate retrieval and answers on a fixed test set. Start with the simplest version that works end to end (the short Python script below), measure it, and only then add embeddings, hybrid search, reranking or an agent.
Not sure RAG is the right approach for your problem? Read RAG vs fine-tuning first.
What a RAG pipeline is (and why it exists)
A language model only knows what was in its training data. It doesn't know your company policies, last week's product changes or the PDF your customer uploaded. RAG fixes that by fetching relevant text at question time and putting it in front of the model.
The term comes from a 2020 research paper by Lewis and colleagues, which described models that "combine pre-trained parametric and non-parametric memory for language generation" (Lewis et al., arXiv:2005.11401). In plain words: the model's own weights are the parametric memory; your searchable document store is the non-parametric memory.
Today "RAG pipeline" usually means the whole system around that idea. LlamaIndex, for example, describes "five key stages within RAG": loading, indexing, storing, querying and evaluation (LlamaIndex, Introduction to RAG). This guide splits querying into its parts (retrieve, augment, generate) so you can see where things go wrong.
Do you even need RAG?
Before you build anything, check whether a simpler option works:
- Small knowledge base: Anthropic notes that "if your knowledge base is smaller than 200,000 tokens (about 500 pages of material), you can just include the entire knowledge base in the prompt" (Anthropic, Introducing Contextual Retrieval). Context window sizes vary by model, so check yours.
- Style or format problem: if answers are factually fine but in the wrong tone or structure, better prompts or fine-tuning fit better than retrieval.
- Structured data: if the answer lives in a database table, a text-to-SQL tool is often more accurate than embedding rows as text.
RAG is the right call when the knowledge is large, changes often, must be cited, or must respect who is allowed to see what.
The RAG pipeline architecture at a glance
| Step | What happens | Simple first choice | Upgrade when needed |
|---|---|---|---|
| 1. Load | Read PDFs, docs, web pages, tickets, database rows | Plain text files | Document parsers for PDFs, tables and scans |
| 2. Chunk | Split into passages and keep metadata (source, page, date) | Paragraph splits | Section-aware or overlapping chunks |
| 3. Index | Turn chunks into something searchable | TF-IDF or BM25 keyword search | Embeddings in a vector store, or hybrid search |
| 4. Retrieve | Find the top-k chunks for a question | Top 2–5 above a score threshold | Hybrid search + reranking, metadata filters |
| 5. Augment | Put the chunks into a prompt with clear rules | "Answer only from context, cite ids" | Query rewriting, context compression |
| 6. Generate | Call the model | One model call | Streaming, structured output, guardrails |
| 7. Evaluate | Check retrieval and answer quality | Hit rate on 20–50 test questions | Faithfulness and context metrics, regression tests in CI |
Step-by-step: build a working RAG pipeline in Python
The script below is deliberately small. It uses TF-IDF from scikit-learn for retrieval so you can run it on any laptop without a GPU, a vector database or an API key, and see every step. Once it works, you swap pieces out (see "Upgrading each step" below).
Setup: create a folder with a docs/ subfolder holding a few .txt files (we used two short HR policy files, with paragraphs separated by blank lines), then run pip install scikit-learn.
"""A minimal, runnable RAG pipeline: load -> chunk -> index -> retrieve -> prompt -> generate -> evaluate. pip install scikit-learn (optional: pip install openai, then set OPENAI_API_KEY and MODEL to a current model name from your provider's models page to call a real model)""" import os, pathlib from sklearn.feature_extraction.text import TfidfVectorizer from sklearn.metrics.pairwise import cosine_similarity # 1. Load: read every .txt file in ./docs def load_docs(folder="docs"): return {p.name: p.read_text(encoding="utf-8") for p in sorted(pathlib.Path(folder).glob("*.txt"))} # 2. Chunk: split on blank lines (paragraphs) and keep the source name with each chunk def chunk(docs): chunks = [] for name, text in docs.items(): for i, para in enumerate(p.strip() for p in text.split("\n\n")): if para: chunks.append({"id": f"{name}#{i+1}", "text": para}) return chunks # 3. Index: TF-IDF vectors here; swap in an embedding model + vector store later def build_index(chunks): vec = TfidfVectorizer(stop_words="english") matrix = vec.fit_transform([c["text"] for c in chunks]) return vec, matrix # 4. Retrieve: top-k chunks above a minimum similarity score def retrieve(question, chunks, vec, matrix, k=2, min_score=0.1): scores = cosine_similarity(vec.transform([question]), matrix)[0] ranked = sorted(zip(scores, chunks), key=lambda x: x[0], reverse=True)[:k] return [(round(float(s), 3), c) for s, c in ranked if s >= min_score] # 5. Augment: build a grounded prompt that forces citations and allows "I don't know" def build_prompt(question, hits): context = "\n".join(f"[{c['id']}] {c['text']}" for _, c in hits) return ("Answer using ONLY the context below. Cite chunk ids in square brackets. " "If the context does not contain the answer, say \"I don't know\".\n\n" f"Context:\n{context}\n\nQuestion: {question}\nAnswer:") # 6. Generate: call a model if a key is set, otherwise show the prompt (useful for testing) def generate(prompt): if not os.getenv("OPENAI_API_KEY"): return "(no API key set; prompt that would be sent)\n" + prompt from openai import OpenAI resp = OpenAI().responses.create(model=os.environ["MODEL"], input=prompt) return resp.output_text def answer(question, chunks, vec, matrix): hits = retrieve(question, chunks, vec, matrix) if not hits: # refuse before calling the model when retrieval finds nothing return "I don't know: nothing relevant was found in the documents.", hits return generate(build_prompt(question, hits)), hits # 7. Evaluate retrieval: does the right chunk come back for each test question? TESTS = [("How many days of leave can I carry forward?", "leave-policy.txt#1"), ("Do I need a medical certificate for sick leave?", "leave-policy.txt#2"), ("Can I fly business class?", "expense-policy.txt#2"), ("When must travel expenses be filed?", "expense-policy.txt#1")] if __name__ == "__main__": chunks = chunk(load_docs()) vec, matrix = build_index(chunks) hit = sum(any(c["id"] == want for _, c in retrieve(q, chunks, vec, matrix)) for q, want in TESTS) print(f"{len(chunks)} chunks indexed | retrieval hit rate: {hit}/{len(TESTS)}") for q in ["Can I fly business class?", "What is the office Wi-Fi password?"]: text, hits = answer(q, chunks, vec, matrix) print("\nQ:", q, "| retrieved:", [(s, c["id"]) for s, c in hits]) print(text) What you should see (output from our test run on 17 November 2026, with no API key set):
5 chunks indexed | retrieval hit rate: 4/4 Q: Can I fly business class? | retrieved: [(0.574, 'expense-policy.txt#2')] (no API key set; prompt that would be sent) Answer using ONLY the context below. Cite chunk ids in square brackets. If the context does not contain the answer, say "I don't know". ... Q: What is the office Wi-Fi password? | retrieved: [] I don't know: nothing relevant was found in the documents. To call a real model, pip install openai, set OPENAI_API_KEY, and set MODEL to a current model name from your provider's models page. Model names change and older ones get deprecated, which is why the script doesn't hard-code one.
What each step teaches you
- Load. Real projects spend more time here than anywhere else: scanned PDFs, tables, headers and footers repeated on every page. Garbage in, garbage retrieved.
- Chunk. Each chunk keeps an
idwith its source, so the answer can cite it. Chunks that are too large dilute the match; chunks that are too small lose meaning. Paragraphs or document sections are a sensible first unit. - Index. TF-IDF matches words, not meaning. That's a feature while learning, because you can see exactly why a chunk matched, and a limitation in production, where users phrase things differently from your documents.
- Retrieve. Note the
min_scorethreshold. Without it, the pipeline always returns something, and the model will happily answer from irrelevant text. - Augment. The prompt sets three rules: use only the context, cite chunk ids, and say "I don't know" otherwise. Those three lines prevent a large share of beginner RAG failures.
- Generate. The model call is the smallest part of the code. Notice the pipeline refuses before calling the model when retrieval finds nothing, which saves cost and avoids invented answers.
- Evaluate. Four test questions are enough to prove the loop works and to catch a broken change instantly. Real projects need dozens of questions written by people who know the documents.
Upgrading each step for real-world RAG
Better indexing: embeddings, vector stores and hybrid search
- Embeddings turn text into vectors that capture meaning. A common small open model, all-MiniLM-L6-v2, "maps sentences & paragraphs to a 384 dimensional dense vector space". The Sentence Transformers library makes it a few lines of code.
- Vector stores hold those vectors and find the nearest ones fast. Options range from the FAISS library ("a library for efficient similarity search and clustering of dense vectors") to pgvector ("Open-source vector similarity search for Postgres"), which is a good fit if your data already lives in Postgres.
- Hybrid search combines embeddings with keyword ranking such as BM25, "a bag-of-words retrieval function". Keywords catch exact terms such as product codes, error messages and names that embeddings can miss. pgvector's README shows hybrid search with Postgres full-text search.
Better retrieval: reranking and context
A reranker re-scores the top 20–50 candidates with a more precise (and slower) model and keeps the best few. Anthropic's write-up on Contextual Retrieval, which adds a short explanation of each chunk's place in its document before indexing, reports that the method "can reduce the number of failed retrievals by 49% and, when combined with reranking, by 67%" on their benchmarks (Anthropic). Your numbers will differ; the point is that retrieval quality, not the model, is usually the bottleneck.
Better chunking
- Split on document structure (headings, sections, table rows) rather than a fixed character count where you can.
- Store metadata with every chunk: source, page, date, department, access level. You'll need it for citations, filters and permissions.
- Re-index when documents change, and delete chunks for removed documents, or the assistant will quote outdated policy.
Evaluation that goes beyond "looks right"
Measure retrieval and generation separately:
- Retrieval: did the right chunk appear in the top-k (hit rate), and how high (rank)?
- Generation: is the answer supported by the retrieved text, and does it answer the question?
Libraries such as Ragas provide metrics including Context Precision, Context Recall, Faithfulness and Response Relevancy. Keep a fixed test set and re-run it after every change to chunking, embeddings, prompts or models.
Security and access control
The OWASP Top 10 for LLM Applications 2025 includes LLM01 Prompt Injection, LLM02 Sensitive Information Disclosure and LLM08 Vector and Embedding Weaknesses, all directly relevant to RAG. In practice:
- Filter retrieval by the user's permissions before chunks reach the prompt.
- Treat retrieved text as untrusted: a document can contain instructions aimed at the model.
- Mask personal data you don't need, and log what was retrieved for each answer.
Should you use a framework (LangChain, LlamaIndex)?
Frameworks such as LangChain and LlamaIndex give you document loaders, splitters, vector store integrations and retrievers out of the box. They save time once you know what each step does. Build the plain-Python version above first; then you'll know which part of the framework to tune when answers go wrong.
When retrieval becomes one tool among several (search, SQL, calculators, APIs), you are moving from RAG to an agent. Our agentic AI roadmap shows where RAG fits in that path, and how to build an MCP server shows how to expose a retriever as a reusable tool.
Common RAG pipeline mistakes
- No "I don't know" path. Without a score threshold and a refusal rule, the model answers from whatever it gets.
- Judging by demo questions. Five questions you wrote yourself will always pass. Test with real user questions.
- Only measuring the final answer. When an answer is wrong, you need to know whether retrieval or generation failed.
- Losing metadata. No source or page means no citations, no filters and no permission checks.
- Jumping to fine-tuning. Most "the model doesn't know our data" problems are retrieval problems.
- Ignoring updates. A RAG system over stale documents gives confident, outdated answers.
RAG project ideas to practise
- Policy assistant: answers HR or travel policy questions with citations (extend the script above).
- Course syllabus Q&A: students ask about modules and deadlines; the bot says "I don't know" for anything off-syllabus.
- Support ticket helper: retrieves similar past tickets and drafts a reply for a human to approve.
- Hybrid search comparison: run the same 30 questions through keyword, embedding and hybrid retrieval and publish the hit rates in your README.
For more ideas organised by level, see agentic AI projects for beginners.
The full guide, with an FAQ, is on Datamarcos Guides. More free guides on GenAI, agentic AI and data engineering are on the Datamarcos guides hub.
Sources (fetched 17 Nov 2026; re-checked 27 Nov 2026, all HTTP 200)
- Lewis et al., "Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks" (arXiv:2005.11401): https://arxiv.org/abs/2005.11401
- LlamaIndex, Introduction to RAG: https://developers.llamaindex.ai/python/framework/understanding/rag/
- Anthropic, "Introducing Contextual Retrieval": https://www.anthropic.com/research/contextual-retrieval
- LangChain, Retrieval Augmented Generation (RAG) with Deep Agents: https://docs.langchain.com/oss/python/deepagents/rag
- Sentence Transformers: https://www.sbert.net/
- all-MiniLM-L6-v2 model card: https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2
- FAISS: https://github.com/facebookresearch/faiss
- pgvector: https://github.com/pgvector/pgvector
- Okapi BM25 (Wikipedia): https://en.wikipedia.org/wiki/Okapi_BM25
- Ragas documentation: https://docs.ragas.io/
- OWASP Top 10 for LLM Applications 2025: https://genai.owasp.org/llm-top-10/