Sub topic 2
The Retrieve-Then-Generate Pipeline
Assume you understand basic web crawling, HTML, HTTP requests, and the general role of a large language model. This chapter uses those ideas to explain how an answer engine finds sources before writing an answer.
What “retrieve then generate” means
An AI answer engine often uses a retrieve-then-generate pipeline. It first retrieves relevant content from a web index or another search system. It then gives selected passages to a language model, which generates an answer from that context.
This pattern is also called retrieval-augmented generation (RAG). Retrieval supplies current or specific information. Generation turns that information into readable prose.
The language model does not normally search every page on the web while writing. A separate retrieval process selects a smaller set of candidate pages and passages.
The pipeline stages
| Stage | What happens | Common output |
|---|---|---|
| Query rewriting | The system expands or changes the question | Several focused searches |
| Retrieval | Search systems find candidate documents | URLs, titles, snippets |
| Ranking | Candidates are ordered by relevance and quality | A smaller source set |
| Passage extraction | Useful sections are selected from pages | Text chunks |
| Generation | The model writes from the selected context | An answer with claims |
| Citation attachment | Claims are linked to supporting sources | Citations or source links |
Query rewriting
A user’s question may be incomplete or conversational. The engine can rewrite it into multiple searches. For example, “Does this work with older versions?” might become searches that include the product name, version range, and compatibility terms.
This step helps cover different interpretations, but it can also introduce errors. A poor rewrite may search for the wrong product or assume a requirement that the user did not state.
Retrieval and ranking
The retrieval system searches an index for pages that match the rewritten queries. It may use keywords, semantic similarity, link signals, freshness, source quality, or a combination of these factors.
Ranking does not identify the one true page. It creates a shortlist under time and relevance constraints. A page can be accurate but absent from the shortlist because it is new, blocked, poorly indexed, or less relevant to the rewritten query.
Passage extraction and context building
The engine usually passes selected passages rather than entire pages to the model. It may remove navigation, advertisements, repeated text, and unrelated sections. It then combines passages into a context window.
A passage is easier to use when it states one claim clearly, includes necessary definitions, and identifies its scope. Ambiguous pronouns, missing dates, and facts separated across many sections make extraction less reliable.
> callout note > Retrieval supplies evidence, but it does not guarantee that the model will use every retrieved passage correctly. The model can misunderstand context, combine incompatible sources, or make a claim that the sources do not support.
Grounded generation and citations
The model generates an answer using the retrieved context. A citation system then associates claims with source pages, either during generation or in a later step.
A citation shows that a page was selected as supporting evidence. It does not prove that every sentence in the answer appears on that page. Check whether the cited passage actually supports the specific claim.
Common errors
- Treating a citation as proof that the whole page was read.
- Assuming one query produces one fixed set of sources.
- Confusing a search index with the model’s training data.
- Writing a page that requires readers to combine facts from several distant sections.
- Expecting retrieval to correct an inaccurate or outdated source.
Exercise
Why can a page be relevant to a user’s question but still not appear as a citation?
Show answer
The query rewrite, index coverage, ranking, passage extraction, or context limit may exclude it. Retrieval is selective and does not guarantee inclusion.