Sub topic 2
The classic RAG pipeline
This chapter assumes you know basic Python, what a prompt and a completion are, and that embeddings let you find text with similar meaning through vector search. Here you will see the baseline that agentic RAG replaces.
What the pipeline does
Retrieval-augmented generation (RAG) gives a model facts it was not trained on. Instead of asking the model to answer from memory, you fetch relevant text first and place it in the prompt. The model then answers from that text.
The classic form is retrieve-then-generate. Retrieval runs exactly once, before every answer, and the model has no say in it.
The code
The function below takes embed, search and generate as arguments. You can plug in any embedding model, vector store and language model.
def answer_with_rag(question, embed, search, generate, k=4):
# Classic RAG: retrieval always runs once, with the user's words as the query.
query_vector = embed(question)
chunks = search(query_vector, k) # each chunk: {"id": str, "text": str}
context = "\n\n".join(f"[{c['id']}] {c['text']}" for c in chunks)
prompt = (
"Answer using only the context below. "
"If the context does not contain the answer, say so.\n\n"
f"Context:\n{context}\n\nQuestion: {question}"
)
return generate(prompt)How it works
- Embed.
embed(question)turns the question into a vector. - Search.
search(query_vector, k)returns thekchunks whose vectors are closest. Each chunk has anidand itstext. - Build the context. The chunks are joined into one string. Each is prefixed with its id, such as
[doc-12], so the model can refer to its source. - Prompt. The instruction limits the model to the context and gives it a way out: say so if the answer is missing.
- Generate.
generate(prompt)returns the answer.
Design choices that matter
- The query is the user's exact words. Nothing rewrites it, so a vague or multi-part question is searched as written.
kis fixed. Too small and you miss evidence. Too large and you add noise and cost.- Retrieval is unconditional. A greeting such as "hi" still triggers a search.
- The grounding instruction is the only safeguard. The code never checks whether the chunks are relevant. It relies on the model to notice.
Common errors
- Chunks that are too large or too small. Large chunks bury the relevant sentence. Small chunks lose the surrounding meaning.
- Dropping ids. Without ids in the context, you cannot ask the model to cite sources.
- Trusting the top result. Vector similarity measures closeness, not whether the chunk answers the question. The top
kcan all be irrelevant, and the model will still receive them. - Skipping the "say so" instruction. Without it, the model tends to fill gaps from memory and present them as if they came from the documents.
The next chapter looks at questions where this fixed design breaks down.
Exercise
In answer_with_rag, what happens if the user asks "Hello!"? Name one change in the design that would avoid it.
Show answer
The function still embeds the greeting, searches, and puts k unrelated chunks in the prompt, which wastes time and tokens and may confuse the model. The fix is to let something decide whether retrieval is needed at all. In agentic RAG, the model makes that decision by choosing whether to call a search tool.
Exercise
Why does the context string prefix each chunk with [{c['id']}]?
Show answer
The ids let the model refer to which passage supports each claim, so the answer can include citations that a reader or a test can check against the source chunks.