How to build a RAG pipeline.

A practical, step-by-step guide to building a retrieval-augmented generation pipeline that answers from your own documents, with a Python example you can read.

Sculptural head crowned by a glowing slab of violet light

To build a RAG pipeline, you ingest and chunk your documents, embed those chunks into a vector database, retrieve the most relevant ones for each question, and have a language model generate an answer grounded in them with citations. The steps below walk through the whole flow, from raw documents to a system that answers from your own knowledge.

What is a RAG pipeline?

RAG stands for retrieval-augmented generation. A RAG pipeline is the sequence of steps that lets a language model answer from your own documents instead of guessing from what it memorised in training. It retrieves the passages relevant to a question, then asks the model to answer using only those passages, with citations back to the source. That grounding is what makes the answers accurate and verifiable.

Before you start

A RAG pipeline is a set of small, swappable parts rather than one big model. Before building, get these in place:

How to build a RAG pipeline, step by step

  1. Collect and prepare your documents

    Gather the sources and clean them: strip boilerplate, extract text from PDFs, keep useful metadata like title, section and URL. Quality here decides quality everywhere downstream.

  2. Chunk the documents

    Split each document into passages of a few hundred tokens, with a little overlap so context is not cut mid-thought. Chunk size is the single biggest lever on retrieval quality, so it is worth tuning.

  3. Embed the chunks

    Run every chunk through the embedding model to get a vector. Chunks about the same topic end up near each other in vector space, which is what makes semantic search work.

  4. Store them in a vector database

    Index the vectors, together with their text and metadata, in a vector database. This is what you query at answer time, and it is cheap to rebuild when documents change.

  5. Retrieve the right chunks

    For each question, embed the query and pull the top matching chunks. Add re-ranking or hybrid keyword search when plain vector search misses. Good retrieval is where most of the quality comes from.

  6. Generate a grounded answer

    Put the retrieved chunks into the prompt and instruct the model to answer only from them, and to cite sources. If the context does not contain the answer, the model should say so rather than invent one.

  7. Evaluate and iterate

    Build a small evaluation set of questions and expected sources. Measure whether retrieval finds the right passages and whether answers are faithful, then tune chunking, retrieval and prompts.

A minimal RAG pipeline in Python

Here is the core loop in a few lines: embed the chunks, retrieve the closest ones for a question, and ask a model to answer from them. Real systems add a vector database, re-ranking, citations and evaluation, but the shape is the same.

Minimal RAG loop · Python
from sentence_transformers import SentenceTransformer
import numpy as np

embedder = SentenceTransformer("all-MiniLM-L6-v2")

# 1. chunk your documents, then embed and index them
chunks = chunk_documents(docs, size=500, overlap=50)
index  = embedder.encode(chunks, normalize_embeddings=True)

def answer(question):
    # 2. retrieve the top-k most relevant chunks
    q = embedder.encode([question], normalize_embeddings=True)
    scores = index @ q.T
    top = np.argsort(scores.ravel())[-4:][::-1]
    context = "\n\n".join(chunks[i] for i in top)

    # 3. generate an answer grounded in the retrieved context
    prompt = (
        "Answer using ONLY the context. Cite sources. "
        "If the answer is not in the context, say you do not know.\n\n"
        f"Context:\n{context}\n\nQuestion: {question}"
    )
    return llm(prompt)

Swap the in-memory index for a real vector database once you have more than a few thousand chunks, and add citations by carrying each chunk's source metadata through to the answer.

Common mistakes to avoid

Want it built for production?

A prototype is a weekend; a reliable, private RAG system with good retrieval, citations and evaluation is real engineering. That is exactly our RAG development service. If you also need the model tuned to your domain, see custom LLM development, or explore the full AI hub.

Frequently asked

What is a RAG pipeline?

A RAG pipeline is the sequence of steps that lets a language model answer from your own documents: ingest and chunk the documents, embed them into a vector database, retrieve the most relevant chunks for a question, and generate an answer grounded in those chunks with citations.

What are the steps in a RAG pipeline?

Collect and prepare documents, chunk them into passages, embed the chunks, store them in a vector database, retrieve the top matches for a query, generate a grounded answer with citations, and evaluate and iterate on quality.

Which embedding model and vector database should I use?

Start with a strong general embedding model and a simple vector database such as pgvector or Qdrant. The right choice depends on your data volume, languages and privacy needs, and can be swapped without rewriting the whole pipeline.

How do I reduce hallucinations in a RAG system?

Improve retrieval so the right passages are found, instruct the model to answer only from the retrieved context, show citations so answers are verifiable, and add evaluation to catch regressions. Most hallucinations in RAG come from weak retrieval, not the model.

Do I need to fine-tune a model to build a RAG pipeline?

Usually no. RAG gives an off-the-shelf model your knowledge at answer time, so most projects need no fine-tuning. Fine-tuning helps when you also need a specific tone, format or domain skill, and the two can be combined. See custom LLM development.

From prototype to production.

Bring your documents. We build a private RAG system that answers with citations.

RAG development Book a call