The RAG Prompt
Context and question.
We can find the right chunks. Now we hand them to the model. The RAG prompt has three parts. An instruction, the context we retrieved, and the customer's question.

- Instruction
- Context
- Question
Building the prompt
We search the index for the three best chunks. We join their text into one context block. The prompt puts the instruction first, then the context, then the question. Run it. The model answers from the returns page, in one sentence.
from rag_kit import load_docs, make_chunks, Index
from llm import llm
index = Index(make_chunks(load_docs()))
question = "How long do refunds take?"
hits = index.search(question, k=3)
context = "\n\n".join(c["text"] for score, c in hits)
prompt = f"""Use the context to answer in one sentence.
Context:
{context}
Question: {question}"""
print(len(prompt), "characters sent")
print(llm.invoke(prompt).text.strip())602 characters sent Refunds go back to the original payment method within 7 working days of the item reaching the warehouse.
Also uses rag_kit.py
import warnings; warnings.filterwarnings("ignore")
from pathlib import Path
import numpy as np
from sentence_transformers import SentenceTransformer
import faiss # after torch, so the two libraries load safely together
faiss.omp_set_num_threads(1)
from langchain_text_splitters import RecursiveCharacterTextSplitter
def load_docs(folder="data/rag/northwind"):
return [{"source": p.name, "text": p.read_text()} for p in sorted(Path(folder).glob("*.md"))]
def make_chunks(docs, size=300, overlap=50):
splitter = RecursiveCharacterTextSplitter(chunk_size=size, chunk_overlap=overlap)
return [{"source": d["source"], "text": t.replace("\n", " ")}
for d in docs for t in splitter.split_text(d["text"])]
embedder = SentenceTransformer("all-MiniLM-L6-v2", device="cpu")
def embed(texts):
return embedder.encode(texts, normalize_embeddings=True)
class Index:
def __init__(self, chunks):
self.chunks = chunks
self.vectors = embed([c["text"] for c in chunks])
def search(self, query, k=3):
scores = self.vectors @ embed([query])[0]
top = np.argsort(-scores)[:k]
return [(round(float(scores[i]), 2), self.chunks[i]) for i in top]Also uses llm.py
from langchain.chat_models import init_chat_model
llm = init_chat_model("google_genai:gemini-flash-lite-latest", temperature=0)
PROMPT = """Answer the question using only the context below.
If the answer is not in the context, say "I don't know".
Context:
{context}
Question: {question}"""
def answer(question, chunks):
context = "\n\n".join(f"[{c['source']}] {c['text']}" for c in chunks)
return llm.invoke(PROMPT.format(context=context, question=question)).text.strip()The three parts
The instruction tells the model how to behave. The context gives it the facts. The question says what to answer. Keep the instruction short and clear, and put the question last, so it's fresh when the model starts writing.
| key | value |
|---|---|
| Instruction | how to behave |
| Context | the facts |
| Question | what to answer |
| Order | question last |
Summary
A RAG prompt is an instruction, the retrieved context, and the question. Next, keeping the model grounded in that context.
Key takeaways- Instruction
- Context
- Question last