Sub topic 13
Building the augmented prompt
This chapter covers the step after retrieval: turning the top-k passages into a prompt the model can use. It assumes you can write basic Python, know how LLMs are prompted, and already have a retriever that returns a ranked list of passage strings.
What the augmented prompt does
The model only sees text. Your retrieved passages reach it as part of the prompt, next to the question and your instructions. A good augmented prompt has four parts:
- An instruction that limits the model to the supplied context.
- The numbered passages.
- The user's question.
- A rule for what to do when the context does not contain the answer.
The numbering makes citations possible. The refusal rule gives the model a safe way out. Without it, the model tends to fill gaps from its training data, and the answer is no longer grounded.
A prompt builder
This function numbers each passage starting at 1 and places the instructions before the context.
def build_prompt(question, passages):
context = "\n\n".join(f"[{i}] {p}" for i, p in enumerate(passages, start=1))
return (
"Answer using only the context below. "
"Cite passages like [1]. "
"If the context does not contain the answer, say you don't know.\n\n"
f"Context:\n{context}\n\n"
f"Question: {question}\nAnswer:"
)
passages = [
"Refunds are available within 30 days of purchase.",
"Gift cards cannot be refunded.",
]
print(build_prompt("Can I return a gift card?", passages))Running this prints a prompt with [1] and [2] before the passages. The model can then answer "No, gift cards cannot be refunded [2]."
Design choices
- Order. Models often pay less attention to material in the middle of a long prompt. Put the strongest passages first, or first and last, instead of burying them.
- Budget. Count tokens before sending. Context, question, instructions, and the expected answer must all fit in the model's window. Drop the lowest-ranked passages first.
- Source labels. If users need to verify answers, keep a map from each number to a document title or URL. Show that map next to the answer, not only the bare
[2]. - Duplicates. Overlapping chunks can repeat the same text. Remove near-duplicates so they do not use up your token budget.
Common errors
- Weak refusal rule. If the prompt does not say what to do when context is missing, the model guesses. Test with questions you know the documents cannot answer.
- Instructions inside passages. Retrieved text is data. A document may contain a line such as "ignore previous instructions". Keep the passages clearly separated from your instructions, and tell the model to treat the context as reference material only.
- Too many passages. Adding more context does not always help. Irrelevant passages distract the model. Raise k only if recall improves.
- Unstable numbering. If you renumber passages between calls, citations from earlier turns no longer match.
Exercise
A user asks a question that none of the retrieved passages answer, yet the model replies confidently with a plausible answer and no citation. Which part of the augmented prompt should you check first, and what would you change?
Show answer
Check the refusal rule and the "use only the context" instruction. Make them explicit, for example "If the context does not contain the answer, say you don't know", and place them where the model will notice them. Then retest with unanswerable questions.
Exercise
Your prompt overflows the model's context window with k=10 passages. Name two ways to fix it without changing the model.
Show answer
Lower k so only the top-ranked passages are included, or use smaller chunks. Removing near-duplicate passages also helps. Reranking before truncation keeps the most useful ones.