Lesson 3.1 · 8 min read · about 55 min to master at a very slow pace

The RAG Pipeline

Retrieval-augmented generation in two halves: an offline indexing pipeline (parse, chunk, embed, store) and an online query pipeline (retrieve, assemble the prompt, generate, cite). Every component, and a minimal implementation.

Not startedJump to quiz

The idea

Retrieval-augmented generation answers a question in two moves: first find the passages most likely to contain the answer, then ask the LLM to answer using only those passages. The model supplies the language; your documents supply the facts.

Offline: the indexing pipeline

Runs when documents are added or changed, not when users ask.

  1. Load and parse: PDFs, web pages, Word files, tickets, database rows → clean text with structure.
  2. Chunk: split on headings and paragraphs, with overlap (lesson 2.2).
  3. Embed: one vector per chunk.
  4. Store: vector, text and metadata (source, section, date, permissions) in the index.

Online: the query pipeline

Runs for every question, inside the user's wait.

  1. Embed the query (with the same model).
  2. Retrieve: hybrid search plus filters (permissions, dates), then rerank (lesson 2.4).
  3. Assemble the prompt: instructions, the top chunks with their source labels, the question.
  4. Generate the answer with citations.
  5. Post-process: check citations point to real chunks, log everything for evals.

The minimal version, in Python-style pseudo-code

# offline
for doc in load_documents("handbook/"):
    for chunk in split(doc, size=500, overlap=75):
        store.add(text=chunk.text, vector=embed(chunk.text),
                  meta={"source": doc.title, "section": chunk.heading})

# online
def answer(question):
    hits = store.search(embed(question), top_k=5)
    context = "\n\n".join(f"[{i+1}] ({h.meta['source']}) {h.text}"
                          for i, h in enumerate(hits))
    return llm(system=RAG_RULES,
               user=f"Sources:\n{context}\n\nQuestion: {question}")

About 15 lines gets a demo working. Everything else in this course is about making it work on the client's real documents and real questions.

Why RAG is the default for client work

  • Fresh: update a document and the next answer uses it. No retraining.
  • Private: documents stay in your index; the model only sees the few chunks per question.
  • Verifiable: answers cite sources the user can open.
  • Controllable: permissions and filters decide who sees what.

Stuck? Common doubts

Questions real learners asked while studying this lesson. They stay closed so they don't spoil your own thinking: open only the ones you need.

First passDo I need a framework such as LangChain or LlamaIndex?

No. They help you prototype quickly and provide loaders for many file types, but the core pipeline is a few functions you can write and debug yourself. Many teams start with a framework and later replace the parts they need to control. Understand the steps first; then choose.

Going deeperHow often should the index be updated?

As often as the source changes and the client needs. Track a content hash or modified date per document and re-index only what changed. Deleting is as important as adding: a removed policy that stays in the index will keep being quoted.

Cheatsheet

Indexing pipeline (offline)

Load and parse → chunk → embed → store vector, text and metadata.

Query pipeline (online)

Embed query → retrieve (hybrid + filters) → rerank → assemble prompt → generate with citations → log.

Why RAG

Fresh, private, verifiable (citations) and controllable (permissions), with no retraining.

See every cheatsheet by category →

Check your understanding

Score 75% or more to master this lesson and earn XP. Answer first, then check.

Q1conceptIn a RAG system, which steps run offline rather than for each user question?
Q2applyAn HR policy changes today. What must happen for the RAG assistant to answer with the new rule?
Q3conceptWhy does the online pipeline embed the query with the same model used for the documents?
Q4interpretA client asks why your RAG bot is "safer" than a plain chatbot for answering from their manuals. Which reason is accurate?
Q5diagnoseA removed product is still being recommended by the assistant months later. What is the most likely cause?