By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
Some answers do not live in the model's weights: this week's shipping policy, a customer-specific contract, the help-centre page you updated yesterday. Retrieval-augmented generation (RAG) fetches the relevant passages first and puts them in the prompt, so the answer is grounded in text you can point at. Grounding is not a truth guarantee — but it turns "the model remembered something" into "the answer cites this passage".
from langchain_core.documents import Document
from langchain_core.embeddings import DeterministicFakeEmbedding
from langchain_core.vectorstores import InMemoryVectorStore
from langchain_text_splitters import RecursiveCharacterTextSplitter
raw_docs = [
Document(
page_content=(
"Harbor Supply refunds: damaged items are refunded or replaced "
"within 30 days of delivery. Keep the original packaging."
),
metadata={"source": "refund-policy"},
),
Document(
page_content=(
"Harbor Supply shipping: standard delivery takes 3-5 business "
"days. Tracking numbers are emailed when an order ships."
),
metadata={"source": "shipping-faq"},
),
]
splitter = RecursiveCharacterTextSplitter(chunk_size=200, chunk_overlap=20)
chunks = splitter.split_documents(raw_docs)
# Deterministic stand-in: same vectors every run, no key and no network.
store = InMemoryVectorStore(embedding=DeterministicFakeEmbedding(size=64))
store.add_documents(chunks)
hits = store.similarity_search("How long do I have to refund a damaged item?", k=1)
print(hits[0].metadata["source"])search_docs(query) tool and let the agent decide when to search.source metadata to every passage and show it to the customer or log it. A citation-free answer is not grounded.In an agent, retrieval is usually one more read-only tool. That keeps the loop simple: the model asks for a search, gets chunks in a ToolMessage, and writes an answer that cites them. Build the retriever once at startup — embedding every chunk on every request is the classic RAG performance bug.
Key takeaways
RAG grounds answers in retrieved passages instead of the model's memory.
The pipeline is load → split → embed → index → retrieve → grounded answer.
Retrieval quality depends on chunking, the embedding model and top-k, not on the prompt alone.
Cite sources, say when the docs do not cover the question, and treat retrieved text as untrusted data.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.