Writing a grounded RAG prompt

Retrieval only matters if the prompt actually uses it. A well-designed RAG prompt tells the model to use only the provided context, to cite its sources, and to say it does not know when the answer is not in the context.

rag_utils.py
python
context_parts = []
for i, chunk in enumerate(relevant_chunks):
    context_parts.append(f"Source {i+1}:\n{chunk.content}")
context = "\n\n".join(context_parts)

prompt = f"""You are a helpful AI assistant for a website.
Answer the user's question using ONLY the context provided below.
If the answer is not in the context, say you don't know. Do not make up information.
Keep your answer professional, concise, and helpful.

Context:
{context}

Question: {question}

Answer:"""

async for chunk in llm_provider.generate_stream(prompt, temperature=0.3, max_tokens=1000):
    yield chunk

The "ONLY the context" instruction plus the explicit refusal clause are what keep the model honest. Temperature 0.3 stays grounded while still reading naturally.

Quiz: Quiz

Loading practice…

Hints: Hints

Loading practice…

AI prompt: Try it: a grounded RAG prompt

Loading practice…