Understand the RAG pipeline
See why models need retrieval, trace a question through the pipeline, and know when RAG is the right tool.
- Explain the problems RAG solves: stale knowledge, private data and unverifiable answers
- Trace the indexing and query phases of a RAG system
- Choose between RAG, a long prompt and fine-tuning
A language model only knows what was in its training data. Ask it about your company’s return policy, yesterday’s release notes or a document it never saw, and it will either say it doesn’t know or - worse - make something up that sounds right.
Retrieval-Augmented Generation (RAG), named in a 2020 paper by Lewis et al., fixes this the way a student with an open book would: look up relevant passages first, then answer using them. That gives you:
- fresh and private knowledge without retraining the model,
- citations people can check,
- access control, because you decide which documents each user’s question can draw on.
Try it
Up to 15 XPFollow one question through a RAG system
Step through what happens when an employee asks the help-desk bot a question. Predict the marked steps.
Two phases
- Indexing (ahead of time): load documents → clean them → split into chunks → index each chunk for search (keywords and/or embeddings), keeping its source and permissions.
- Querying (per question): search the index → pick the best chunks → build a prompt → generate an answer with citations.
Each stage can fail on its own: the answer was never indexed, it was split badly, search missed it, or the model ignored it. A good RAG engineer checks each stage separately - which is what the rest of this track teaches.
Try it
Up to 15 XPRAG, a long prompt, or fine-tuning?
Pick the best fit for each situation.
“A support bot over 40,000 help articles that change weekly”
“Questions about one 20-page contract”
“Always answer in the company’s formal tone and JSON format”
“An HR assistant where each employee may only see their own country’s policies”
“Answers must link to the exact source paragraph”
Key takeaways
RAG retrieves relevant passages and asks the model to answer from them, with citations.
Indexing happens ahead of time; retrieval and generation happen per question.
Use RAG for large, changing or permissioned knowledge; small knowledge can go straight into the prompt.
Lesson quiz
6 questions · pass with 5 correct · up to 50 XP
Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.
Practice: write Python
Write Python in the editor and run it against sample inputs. Python runs locally in your browser using a WebAssembly runtime.
A tiny retriever
The first line is a question; each following line is id|passage. Score each passage by how many distinct question words (lowercase, from re.findall(r"[a-z0-9]+", ...), minus the STOP words) appear among its words.
Print id score for passages scoring above 0, highest first, ties by id. (Notice which word fails to match - real search engines handle that with stemming or embeddings.)
- Library hours
Python runs in a sandboxed browser worker with a 60 second time limit. Its runtime loads from the Pyodide CDN; your code stays in this browser.
Assemble a grounded prompt
The first line is the question; each following line is source|text, in rank order. Print exactly:
1Answer using only the sources below. If they don't contain the answer, say "I don't know".
2
3[1] (source) text
4[2] (source) text
5
6Question: QUESTION- Two sources
Python runs in a sandboxed browser worker with a 60 second time limit. Its runtime loads from the Pyodide CDN; your code stays in this browser.
Questions about this lesson
Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.
Loading posts…