Working with Generative AI
RAG: Giving a Model Access to Knowledge It Was Never Trained On
5 min read
The problem RAG solves
A foundation model's knowledge is frozen at the moment its training data was collected. Ask it about your company's internal wiki, a product you shipped last week, or a legal contract signed yesterday, and it will either say it doesn't know — or, worse, confidently make something up (a failure mode covered in Hallucinations and Inconsistency).
You could try to solve this with prompt engineering by pasting the relevant document directly into the prompt. That works — until your knowledge base is bigger than the model's context window, which it almost always is. Retrieval-augmented generation (RAG) automates that pasting: at the moment of the question, it searches a knowledge base, finds the most relevant pieces, and inserts just those pieces into the prompt.
How the pipeline actually works
RAG isn't one thing — it's a small system with several moving parts. Here's the typical flow from question to answer:
- User query — someone asks a question in plain language.
- Embed the query — the text is converted into a list of numbers (a vector) that captures its meaning. This is done by an embedding model, a smaller neural network trained specifically to map similar meanings to nearby points in vector space.
- Vector search — that query vector is compared against a pre-built index of vectors representing chunks of your documents, stored in a vector database. The system finds the chunks whose vectors are "closest" — i.e., most similar in meaning, not just in keywords.
- Top-k retrieval — the system pulls back a handful (commonly 3–10) of the most relevant chunks.
- Prompt augmentation — those chunks are inserted into a prompt template alongside the original question, typically with instructions like "Using only the context below, answer the question. If the answer isn't in the context, say so."
- Generation — the language model produces an answer, now grounded in real material rather than relying solely on what it memorized during training.
- Grounded response — ideally, one that can point back to where the information came from, which makes the system's answers checkable.
Why "vector" search instead of keyword search?
Traditional search matches keywords. Someone asking "How do I get my money back?" might not match a document titled "Refund Policy" if it never uses the word "money." Embedding-based vector search matches on meaning: both phrases land near each other in vector space, even though they share no words. This is the same idea behind the word-vector clustering covered in NLP Advancements — RAG just applies it to whole passages instead of single words, and uses it for retrieval instead of analysis.
A concrete walkthrough
Imagine a software company building an internal support assistant on top of its help-center articles.
- Without RAG: asked "What's our refund window for annual plans?", a general-purpose model might answer with an industry-typical guess — say, 30 days — even if the company's actual policy is 14 days for annual plans and 30 for monthly ones.
- With RAG: the system embeds the question, retrieves the company's actual Refund Policy article (and perhaps a related FAQ entry), inserts both into the prompt, and the model answers based on what's actually written — including the distinction between plan types that a generic guess would miss.
The model's underlying knowledge of language — how to read, summarize, and explain — hasn't changed at all. What changed is what it had in front of it at the moment it answered.
Where RAG shines, and where it struggles
RAG is a strong fit when:
- The gap is about facts, not skills — the model already knows how to write, summarize, or converse; it just lacks specific information.
- The underlying knowledge changes often. Updating a RAG system means re-indexing documents — much faster and cheaper than retraining a model.
- You need traceability. Because retrieved passages can be surfaced alongside the answer, users (and auditors) can check the source.
RAG runs into trouble when:
- Retrieval quality is poor. If the search step returns the wrong chunks — because the question is phrased ambiguously, the documents are poorly chunked, or the embedding model doesn't capture domain-specific meaning well — the model is then asked to reason from irrelevant material, and the final answer suffers no matter how good the model is. This is often the actual bottleneck in real RAG systems, more so than the language model itself.
- The task requires synthesis across many sources, beyond what fits in a context window.
- The task requires deep domain reasoning, not just lookup — for example, applying a tangled set of regulations to a novel situation, where a model would benefit from having internalized the patterns of that domain (which is closer to what fine-tuning provides).
If you want to go deeper on the mechanics, Pinecone's introduction to retrieval-augmented generation and the original RAG research paper from Meta AI (Lewis et al., "Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks," 2020) are both solid next steps.
Key takeaway
RAG closes the knowledge gap without touching the model's weights — by retrieving relevant material at the moment of the question and handing it to the model as context. It's more involved than prompt engineering (you now have a search system to build and maintain), but far cheaper than retraining, and it keeps your AI's knowledge current without ever needing to retrain the underlying model.