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.
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.
- Load and parse: PDFs, web pages, Word files, tickets, database rows → clean text with structure.
- Chunk: split on headings and paragraphs, with overlap (lesson 2.2).
- Embed: one vector per chunk.
- 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.
- Embed the query (with the same model).
- Retrieve: hybrid search plus filters (permissions, dates), then rerank (lesson 2.4).
- Assemble the prompt: instructions, the top chunks with their source labels, the question.
- Generate the answer with citations.
- 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)
Query pipeline (online)
Why RAG
Check your understanding
Score 75% or more to master this lesson and earn XP. Answer first, then check.