Start with the real problem
RAG first retrieves useful information from a chosen source and then gives that information to the model so it can answer with evidence.
Retrieval can find a useful passage, yet the model can ignore it, misread it, or cite the wrong sentence. RAG prompting must define how evidence controls the answer.
question → retrieve passages → label evidence → answer from evidence → cite → evaluate
What you will learn
- Separate retrieval from generation.
- Write grounded-answer rules for saying “I do not know”.
- Require claim-level evidence where needed.
- Distinguish retrieval failure from generation failure.
How this connects to current AI systems
GPT, Gemini, and Claude can consume retrieved context; the application remains responsible for retrieval quality, permissions, where the information came from, and prompt-injection defenses.
1. Why This Module Exists
Module 16 introduced retrieval as a context management strategy — pull back only what’s relevant, instead of keeping everything. RAG (Retrieval-Augmented Generation) is exactly that idea, applied to answering questions using an external knowledge source. This module covers how prompting specifically needs to change once retrieved documents enter the picture.
2. The Idea, in Plain Language
RAG means finding relevant information first, then handing it to the AI alongside the question — so the AI answers based on that specific material, instead of relying only on what it already “knows.”
User Question
↓
Retriever (searches a knowledge base for relevant documents)
↓
Relevant Documents
↓
Prompt Template (question + documents combined)
↓
LLM
↓
Answer
This is really how most real, knowledge-grounded AI assistants work — an internal company FAQ bot, a documentation search assistant, a customer support tool referencing a specific product catalog.
3. Why Prompting Changes for RAG
A normal prompt asks the AI to answer using its own general knowledge. A RAG prompt asks the AI to answer using specific, provided documents — and that shift requires new instructions that a normal prompt wouldn’t need.
A weak RAG prompt
"Answer this question: [question]
Here's some context: [retrieved documents]"
This just hands over the documents with no real guidance about how to use them — should the AI only use this information, or supplement it with its own general knowledge? What if the documents don’t actually contain the answer? Nothing here says.
A stronger RAG prompt
"Answer the question below using ONLY the information in the provided
context. Do not use any outside knowledge. If the context doesn't
contain enough information to answer, say so clearly instead of
guessing.
Context:
[retrieved documents]
Question: [question]"
This resolves the exact ambiguity the weak version left open: it’s explicit about the source restriction, and explicit about what to do when the answer really isn’t there.
4. Grounding — The Core RAG Instruction
Grounding means explicitly telling the AI to base its answer only on the provided material, not on its own general knowledge.
This matters enormously because of something you may already suspect: an AI can answer confidently using its own general training knowledge even when that’s not what you wanted — especially if the retrieved documents are incomplete or slightly off-topic. Grounding instructions directly address this by making the intended source of truth explicit.
"Base your answer strictly on the context provided below."
"Do not use any knowledge beyond what's given in the context."
"If the context does not contain the answer, respond with: 'I don't
have enough information to answer that.'"
5. Handling Missing Information
This is one of the most important, and most commonly mishandled, aspects of RAG prompting. What should the AI do if the retrieved documents simply don’t contain the answer?
Without guidance: the AI might confidently guess, drawing on
its own general knowledge -- exactly the
kind of answer RAG was meant to prevent
With explicit guidance: "If the context doesn't contain the
answer, say so clearly rather than
guessing."
Stating this explicitly is really one of the single highest-value additions to a RAG prompt — it directly prevents a common, real failure mode where an AI answers something confidently, even when the retrieved material never actually supported that answer.
6. Citations and Source Attribution
Many real RAG systems ask the AI to reference which document supported each part of its answer — really useful for letting a user verify the answer themselves.
"After each claim in your answer, cite which document it came from,
like this: [Source: Document 2]. Only make claims that are directly
supported by the provided context."
This connects directly to trustworthiness — a user (or a later in the workflow system) can check whether the cited source actually says what the AI claims it does, rather than having to simply trust the answer outright.
7. A Real Example From a Developer’s Perspective
Say you’re building an internal documentation assistant for a software company:
Weak version:
"Answer the engineer's question using this documentation: [docs]"
Production-grade version:
"You are a documentation assistant for [Company]'s internal
engineering docs. Answer the engineer's question using ONLY the
documentation provided below.
Rules:
1. If the documentation directly answers the question, answer clearly
and cite the relevant section.
2. If the documentation is only partially relevant, say what it does
cover, and note explicitly what it does NOT address.
3. If the documentation doesn't address the question at all, respond:
'I couldn't find this in the current documentation. You may want
to check with the platform team directly.'
4. Never guess or fill in gaps using general knowledge about similar
technologies.
Documentation:
[retrieved doc sections]
Question: [engineer's question]"
This is a really realistic, production-shaped RAG prompt — every rule resolves a specific, real failure mode (confident wrong answers, silent gaps, guessing) that a weaker version would leave open.
8. A Simple Agentic AI Example
RAG often feeds directly into an agent’s decision-making, not just a final answer to display:
"Before deciding how to respond to the customer, search the knowledge
base for relevant policy information. Base your response strictly on
what you find. If no relevant policy is found, do not make a decision
on your own — escalate to a human agent instead of guessing at
company policy."
Here, grounding isn’t just about answer quality — it’s a genuine safety mechanism, preventing the agent from confidently acting on a policy it invented rather than one that’s actually documented. Module 19 covers this connection between RAG and agent safety in more depth.
9. How Is This Used in AI?
🤖 How Is This Used in AI?
RAG prompting is foundational to nearly every AI system that needs to answer questions using a company’s specific, private, or frequently- updated knowledge — internal wikis, customer support knowledge bases, legal or medical document search, and product documentation assistants all rely on exactly this pattern: retrieve relevant material, then prompt the AI to answer strictly from it.
10. When Should You Use RAG-Style Prompting?
- The answer depends on specific, private, or frequently-changing information the AI wasn’t trained on (or shouldn’t rely on its training for)
- You need answers to be verifiable, ideally with citations back to source material
- You specifically want to prevent the AI from filling gaps with its own general knowledge
11. When Is Plain Prompting Enough?
- The question is about general, stable knowledge the AI already handles reliably (Module 3’s zero-shot territory)
- There’s no external, specific knowledge source the answer actually needs to be grounded in
Analogy: The Courtroom Witness & The Folder Think of prompting an LLM in a RAG system like questioning a witness on the stand in a court of law:
- General Questioning (Open-Book general knowledge): You ask: “What did the company do in 2024?” The witness answers from memory, potentially mixing up years or misremembering numbers (hallucination).
- Grounded Questioning (RAG): You hand the witness a specific folder (the retrieved documents) and instruct them:
- “You are to answer my questions using ONLY the contents of this folder. Do not guess. Do not pull in rumors you heard outside this room. If the folder does not address my question, you must say: ‘I do not have that information in the files’.”
- If you omit this instruction, the witness will naturally try to be helpful and fill in gaps by guessing, defeating the purpose of providing the folder.
📊 Visual Chart: RAG Prompt Assembly Layout
Here is how retrieved fragments are formatted into a grounded instruction prompt:
graph TD
classDef ground fill:#2ecc71,stroke:#333,stroke-width:1px,color:#fff;
classDef doc fill:#f1c40f,stroke:#333,stroke-width:1px,color:#fff;
classDef user fill:#3498db,stroke:#333,stroke-width:1px,color:#fff;
subgraph Payload ["Assembled Grounded Prompt Payload"]
Rules["1. Grounding Constraints:<br>'Answer ONLY using provided text. If missing, say 'NOT_FOUND'. Citing sources is mandatory.'"]:::ground
DocBlock["2. Reference Context:<br><context><br>[Doc 1: Returns policy is 30 days]<br>[Doc 2: Shipping costs are free above $50]<br></context>"]:::doc
Query["3. User Query:<br>'Can I return a product after 45 days?'"]:::user
end
Payload --> LLMModel["LLM Grounded Comprehension Engine"]
LLMModel --> Output["Output: 'No, returns must be completed within 30 days. [Source: Doc 1].'"]:::ground
12. Common Mistakes
Incorrect idea
Handing over documents without any grounding instruction.
Why it is incorrect
As shown directly, this leaves the AI free to blend in its own general knowledge, undermining the entire point of retrieving specific documents in the first place.
Incorrect idea
Not specifying what to do when the answer isn’t in the retrieved context.
Why it is incorrect
This is one of the most common, highest-impact gaps in real RAG prompts — without it, confident wrong answers on missing information become a real, frequent failure mode.
Incorrect idea
Including too much irrelevant retrieved context.
Why it is incorrect
Directly connects to Module 16 — dumping in every loosely-related document found by the retriever, rather than the most relevant ones, can confuse rather than help.
Incorrect idea
Not considering that retrieved documents themselves could contain malicious or misleading instructions.
Why it is incorrect
This is a genuine prompt injection risk specific to RAG — Module 23 covers this directly, but it’s worth flagging here: content that comes from outside your control (documents, search results) should be treated with the same delimiter/separation caution as any other untrusted input (Module 7).
13. Limitations
- RAG substantially reduces, but does not eliminate, hallucination — the AI can still misinterpret correctly retrieved context or generate slightly beyond what it actually supports (this connects directly to hallucination, covered fully in Module 22)
- The quality of a RAG answer depends heavily on retrieval quality — if the retriever fetches the wrong documents, no amount of good prompting can produce a correct answer from irrelevant material
- Citations improve trust but don’t guarantee accuracy — a citation only shows where a claim came from, not that the AI correctly represented what that source actually said
14. Quick Reference — The Whole Idea in One Diagram
User Question
↓
Retriever finds relevant documents
↓
Prompt: "Answer using ONLY this context. If the answer isn't here,
say so. Cite your sources."
↓
LLM answers -- GROUNDED in the retrieved material, not free-floating
general knowledge
15. Prompts in Code — Calling an LLM
Here’s how RAG prompting actually looks when calling an LLM through code — combining retrieved context with grounding instructions.
Example 1 — Simple
Retrieved documents (here, simulated as a plain string) handed to the AI with a basic grounding instruction.
import anthropic
client = anthropic.Anthropic()
retrieved_context = "Refunds are available within 30 days of purchase " \\
"with a valid receipt. Digital products are non-refundable."
question = "Can I get a refund after 45 days?"
prompt = f"""Answer the question using ONLY the context below. If the
context doesn't contain the answer, say so.
Context:
{retrieved_context}
Question: {question}"""
response = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=150,
messages=[{"role": "user", "content": prompt}]
)
print(response.content[0].text)
Example 2 — Intermediate
A reusable RAG prompt function, with retrieved documents passed as a structured list (simulating what a real retriever would return) and citation instructions added.
import anthropic
client = anthropic.Anthropic()
def build_rag_prompt(question: str, documents: list[dict]) -> str:
context_block = "\\n\\n".join(
f"[Document {i+1}]: {doc['text']}" for i, doc in enumerate(documents)
)
return f"""Answer the question using ONLY the context below. Cite
which document supports each claim, like [Source: Document 1]. If the
context doesn't contain enough information, say so clearly instead of
guessing.
Context:
{context_block}
Question: {question}"""
documents = [
{"text": "Refunds are available within 30 days of purchase with a valid receipt."},
{"text": "Digital products are non-refundable under any circumstances."},
]
prompt = build_rag_prompt("Can I get a refund on a digital product?", documents)
response = client.messages.create(
model="claude-sonnet-4-6", max_tokens=150,
messages=[{"role": "user", "content": prompt}]
)
print(response.content[0].text)
Example 3 — Production Grade
A full RAG function including a real retrieval step (simulated here, in practice a vector search call), a strict grounding prompt with explicit missing-information handling, and a check for whether the model’s answer indicates it couldn’t find the information.
import anthropic
client = anthropic.Anthropic()
NO_ANSWER_PHRASE = "I couldn't find this in the provided documentation."
def retrieve_relevant_docs(question: str) -> list[dict]:
# In a real system, this calls a vector database or search index.
# Simulated here with a small hardcoded knowledge base.
knowledge_base = [
{"text": "Refunds are available within 30 days of purchase with a valid receipt."},
{"text": "Digital products are non-refundable under any circumstances."},
{"text": "Shipping typically takes 5-7 business days."},
]
# Real retrieval would rank by relevance; here we just return all of them.
return knowledge_base
def answer_from_docs(question: str) -> dict:
documents = retrieve_relevant_docs(question)
context_block = "\\n\\n".join(
f"[Document {i+1}]: {doc['text']}" for i, doc in enumerate(documents)
)
prompt = f"""Answer the question using ONLY the context below. Cite
which document supports each claim. If the context does not contain
enough information to answer, respond with exactly:
"{NO_ANSWER_PHRASE}"
Context:
{context_block}
Question: {question}"""
response = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=200,
temperature=0,
messages=[{"role": "user", "content": prompt}],
)
answer = response.content[0].text
return {
"answer": answer,
"found_in_docs": NO_ANSWER_PHRASE not in answer,
"num_documents_used": len(documents),
}
result = answer_from_docs("What's your policy on returning opened electronics?")
print(result)
The found_in_docs check gives the application a clean, programmatic
signal for exactly the missing-information case Section 5 covered —
letting the app decide what to do next (offer to escalate, log the gap
for the documentation team) rather than just displaying a confident
guess.
When to use it—and when not to
Use it when:
- answers depend on private or changing documents.
- citations and traceable evidence matter.
Do not rely on it when:
- the model already knows stable general information.
- retrieved text is assumed safe or correct without validation.
16. Interview Questions
Q: Why does a RAG prompt need explicit grounding instructions, rather than just handing the AI retrieved documents alongside a question?
Ans: Without an explicit instruction to answer strictly from the provided context, the AI is free to blend in its own general training knowledge — which can undermine the entire purpose of retrieval in the first place, especially if that general knowledge is outdated, generic, or doesn’t match the specific policy or information the retrieved documents actually contain. Explicit grounding instructions (“answer using ONLY the context below”) make the intended source of truth unambiguous.
Q: Why is it important for a RAG prompt to specify what the AI should do when the retrieved context doesn’t contain the answer?
Ans: Without this instruction, a common and really problematic failure mode occurs: the AI answers confidently anyway, often by falling back on its own general knowledge, even though the retrieved material never actually supported that specific answer. Explicitly instructing the model to say so when information is missing, rather than guess, directly prevents this and makes gaps visible instead of silently papered over.
Q: Does RAG fully eliminate hallucination? Why or why not?
Ans: No — RAG substantially reduces hallucination risk by grounding answers in specific, retrieved material rather than relying solely on general training knowledge, but it doesn’t eliminate the risk entirely. The AI can still misinterpret or partially misuse correctly retrieved context, or generate content slightly beyond what the retrieved material actually supports. RAG improves the odds of an accurate, grounded answer; it isn’t a guarantee.
Q: Why does citation quality in a RAG system’s answer matter, and what does a citation actually guarantee (and not guarantee)?
Ans: Citations let a user or system that uses the result later verify a claim by checking the specific source document it’s attributed to, which builds trust and enables fact-checking. However, a citation only shows WHERE a claim supposedly came from — it doesn’t guarantee the AI accurately represented what that source actually says. A citation could still technically point to a real document while misquoting or misrepresenting its content, so citations are a useful verification tool, not an accuracy guarantee on their own.
17. What You Should Remember
- RAG prompts need explicit grounding instructions — answer only from the provided context, not general knowledge — verified directly with a weak vs. strong prompt comparison.
- Explicitly handling the missing-information case (“if the answer isn’t here, say so”) is one of the highest-value additions to any RAG prompt.
- RAG substantially reduces but does not eliminate hallucination — retrieval quality and prompt grounding both matter, and neither alone guarantees a correct answer.
18. Quick Practice
Write a full RAG prompt template for a medical clinic’s FAQ assistant, including grounding, missing-information handling, and a citation requirement. What’s the most important rule you’d add given the sensitivity of the topic?
19. Next Step
Next: Module 18 — Prompt Engineering for Tool Calling — how prompts influence when and how an AI decides to use a tool, connecting directly into how AI agents actually operate.
- Author
- TechByteByByte Editorial Team
- Reviewed by
- TechByteByByte Admin
- Published
- Last reviewed