The final step: hand the retrieved passages to the model and make it answer only from them — with citations, and an honest "I don’t know" when the context is silent.
Why: the model answers from what you put in the prompt, so the retrieved passages must be clearly delimited and labeled with their source. When: format each passage with an id the model can cite, separated so it never blurs two together. Where: this assembled prompt is the "augmented" in Retrieval-Augmented Generation.
def build_context(hits):
blocks = []
for h in hits:
src = h["meta"]["source"]
blocks.append(f"[source: {src}]\n{h['content']}")
return "\n\n---\n\n".join(blocks)
hits = retrieve("how long do refunds take?")
context = build_context(hits)Why: the entire point of RAG is answers grounded in retrieved text, not the model’s memory — the system prompt must forbid outside knowledge and require an "I don’t know" when the context lacks the answer. When: this single instruction is what prevents confident hallucinations. Where: pass the context as the user message and the rules as the system prompt.
import anthropic
client = anthropic.Anthropic()
SYSTEM = (
"Answer ONLY from the provided context. Cite the [source: ...] you used. "
"If the answer is not in the context, say 'I don't have that information.' "
"Never use outside knowledge."
)
def answer(question):
context = build_context(retrieve(question))
resp = client.messages.create(
model="claude-opus-4-8", max_tokens=512, system=SYSTEM,
messages=[{"role": "user",
"content": f"Context:\n{context}\n\nQuestion: {question}"}],
)
return resp.content[0].text
print(answer("How long do refunds take?"))Why: RAG is just these stages composed — embed the query, retrieve, (optionally rerank), build context, generate grounded. When: this is the loop you will optimize with the evaluation course; measure retrieval and faithfulness separately. Where: swapping the store, embedder, or model changes a single stage, never the shape.
question
|
embed query --------- same model as ingestion
|
retrieve top-k ------ vector (+ keyword) search
|
rerank (optional) ---- cross-encoder picks the best few
|
build context ------- delimited, source-labeled passages
|
generate ------------ grounded system prompt, cite or say "I don't know"