Sub topic 3
Retrieval-augmented generation in plain terms
This chapter assumes you have used an AI chat or AI search product and know the basics of traditional search and SEO. You do not need to program. The short code sketch is there to make the pipeline concrete.
What retrieval-augmented generation is
Retrieval-augmented generation (RAG) is a design pattern. The system first retrieves documents that look relevant to a question. It then gives those documents to a language model and asks the model to write an answer based on them.
The earlier chapter on model limits explained the reason. A language model trained on a fixed snapshot of text can be out of date, and it can state things that sound right but are not grounded in any source. Retrieval gives the model fresh, specific material to work from. It also gives the system something it can point to as a source.
Think of an open-book exam. A model answering from memory is taking a closed-book exam. A RAG system hands the model a few pages from the library and says, "Answer using these."
The three stages
Most RAG systems can be described in three stages. Products differ in the details, and outsiders cannot see most of them, so treat this as a working model and not a specification.
- Retrieve. The system searches an index of documents, or the web, and pulls back candidate passages.
- Augment. The system places the best passages into the model's input, next to the user's question and any instructions.
- Generate. The model writes an answer. Many products also show links or citations to the sources used.
The important point for marketers is the order. Your content cannot be quoted or cited in the answer unless it is retrieved first. Retrieval acts as a gate in front of the writing.
A minimal sketch
The following Python code is a toy. It uses simple word overlap to retrieve, and it builds the prompt a model would receive. Real systems use far better retrieval, but the shape is the same.
def retrieve(question, passages, top_n=2):
"""Rank passages by how many question words they share."""
q_words = set(question.lower().split())
scored = []
for source, text in passages.items():
overlap = len(q_words & set(text.lower().split()))
scored.append((overlap, source))
scored.sort(reverse=True)
return [source for overlap, source in scored[:top_n] if overlap > 0]
def build_prompt(question, passages, chosen):
"""Augment: put the retrieved passages in front of the model."""
context = "\n".join(f"[{s}] {passages[s]}" for s in chosen)
return (
"Answer using only the sources below. Cite them by name.\n\n"
f"{context}\n\nQuestion: {question}"
)
passages = {
"shoe-guide": "Stability shoes add support for runners with flat feet.",
"brand-page": "Our company was founded in 1998 in Portland.",
"fit-guide": "Wide fit running shoes suit runners with flat feet.",
}
question = "which running shoes suit flat feet"
chosen = retrieve(question, passages)
print(chosen)
print(build_prompt(question, passages, chosen))Running it selects shoe-guide and fit-guide, and leaves out brand-page. The prompt that results contains only those two passages. The model never sees the brand page, so it cannot use it.
What the model does with retrieved text
The model does not copy the passages. It reads them as part of its input and writes new text. Three behaviours matter.
- Grounding. The instructions usually ask the model to rely on the supplied passages. This reduces, but does not remove, invented claims.
- Selection. The model has limited space, so only a few passages fit. Passages that did not make the cut play no part in the answer.
- Attribution. If the product shows citations, it links statements to some of the passages used. Which passages get linked is a design choice by the product, and a later chapter covers it.
Where RAG can go wrong
Knowing the failure modes helps you read AI answers critically and set realistic expectations for your content.
| Failure | What happens | Why it matters to you |
|---|---|---|
| Retrieval miss | The right page is never retrieved | Your page cannot be cited, however good it is |
| Wrong passage | A related but unhelpful passage is retrieved | The answer may lean on a weaker source |
| Misreading | The model summarizes a passage inaccurately | Your claim may be quoted wrongly |
| Unsupported addition | The model adds facts that no source states | Statements in the answer may not trace to any page |
| Stale index | The system retrieves from an old copy of a page | Recent changes may not appear |
A citation next to a sentence suggests support, but it does not guarantee that the cited page says what the sentence says. Checking is a manual job.
Retrieval is not one step
The three-stage picture hides detail. Inside "retrieve" there are usually several smaller steps, and each one is covered in its own chapter:
- understanding what the user wants,
- splitting one question into several narrower searches (query fan-out),
- matching by keywords, by meaning, or both,
- cutting pages into passages,
- merging result lists and reranking them.
The word "retrieval" in RAG therefore covers a whole pipeline. When someone says "the AI retrieved your page," that can mean any of several things, and the differences matter for how you diagnose a problem.
Differences from classic search
In classic search, the engine returns a ranked list of links and the user decides what to read. In RAG, the system reads for the user and writes one composed answer. Three consequences follow.
- The unit changes. Classic ranking scores whole pages. RAG tends to work with passages, because passages are what fit into a model's input.
- The result is shared. Several sources may feed one answer. Being the single top result matters less than supplying a useful passage.
- The output is not stable. The same question can produce different retrieved sets and different wording on different runs. A later chapter covers measurement under this variability.
What is documented and what is inferred
The general RAG pattern is well documented in published research and in vendor documentation for building such systems. What specific consumer AI search products do inside is mostly not public. Statements such as "the product always retrieves ten pages" or "it prefers pages of a certain length" should be treated as guesses unless the vendor states them. This tutorial marks such points as inferred when they come up.
Practical takeaways
- Treat retrieval as a precondition. A page that is not retrieved cannot be quoted or cited.
- Write passages that stand on their own, since passages are the unit that gets pulled into the prompt.
- Keep factual claims specific and clearly stated, so a misreading is less likely.
- Expect variation between runs, and do not read much into a single answer.
Exercise
In your own words, why can a very good page fail to appear in an AI-generated answer, even though the language model itself could have used it? Name the stage where this happens.
Show answer
The page was probably not retrieved. Retrieval comes before generation, and the model only sees the passages that retrieval selects and the system places in its prompt. If the page was missed, outranked, or cut for space, the model never reads it, so it cannot use or cite it.
Exercise
Change the toy retrieve function's input so that brand-page is selected for the question "when was the company founded in Portland". Which stage of RAG does your change illustrate, and what does it show about wording?
Show answer
Running retrieve("when was the company founded in Portland", passages) selects brand-page, because words such as "founded" and "Portland" overlap with its text. The change illustrates the retrieve stage. It shows that, with keyword matching, what gets retrieved depends on how the question and the passage are worded.