RAG & Memory
Grounding models in real, current knowledge.
We've hit the model's two deepest limitations over and over in this series:
- It only knows what was in its training data, frozen at the knowledge cutoff (post #5). It doesn't know last week's news, or anything about your private documents.
- It can hallucinate — produce fluent, confident, false statements — because it's reconstructing patterns, not retrieving facts (post #2).
Retrieval-Augmented Generation (RAG) is the dominant technique for fixing both at once. The idea is simple and powerful: instead of relying on the model's frozen memory, fetch the relevant information at question time and put it in the context. The model then answers from material right in front of it.
The core idea
Don't ask the model to recall. Ask it to read.
Without RAG:
user question ──► model answers from frozen memory (may be stale/wrong)
With RAG:
user question ──► find relevant documents ──► put them in the context
──► model answers FROM those documents (current, grounded)It's the difference between a closed-book exam (recall everything from memory) and an open-book one (look it up, then answer). Open book is more reliable — and it lets the model work with information it was never trained on.
This is really just tool use (post #11) aimed at knowledge: a "search" tool whose results get fed back into the context. The reason it gets its own name and post is that how you do the search turns out to be subtle and important.
How retrieval actually works
The naïve approach — keyword search — misses a lot. Someone asks "how do I reset my password?" but your document says "credential recovery procedure." No keywords overlap, yet they mean the same thing. We need search by meaning, not just words.
This is where embeddings come back (post #3). Recall that embeddings turn text into vectors where meaning becomes geometry — similar meanings land near each other in space. RAG uses this for semantic search:
1. AHEAD OF TIME (indexing):
chop your documents into chunks
→ turn each chunk into an embedding vector (post #3)
→ store all vectors in a database
2. AT QUESTION TIME (retrieval):
turn the user's question into a vector too
→ find the stored chunks whose vectors are NEAREST to it
→ those are the most semantically relevant chunks
3. GENERATION:
put the retrieved chunks into the context alongside the question
→ the model answers using them"Reset my password" and "credential recovery procedure" end up close together in vector space even with no shared words, so the right document gets found. The specialized database that stores these vectors and finds nearest neighbors quickly is called a vector database, and it's the workhorse of most RAG systems.
Why RAG is everywhere
RAG hits a sweet spot that pure model approaches can't:
- Current and private knowledge. It connects the model to information that isn't in its weights — your company wiki, today's data, a customer's records — without retraining anything.
- Fewer hallucinations. Grounding answers in retrieved text makes the model far more reliable on facts. The information is right there; it doesn't have to guess.
- Citations and trust. Because you know which chunks were retrieved, you can show sources. The answer becomes checkable — a huge deal for any serious use.
- Cheaper than retraining. Updating knowledge means updating the document index, not running a multimillion-dollar training job (post #5). New info is available the moment you index it.
- Access control. You can retrieve only the documents a given user is allowed to see, so the model answers within each user's permissions.
This combination is why RAG became the default architecture for "AI over your own data" — support bots, internal knowledge assistants, document Q&A, and more.
RAG is harder than it looks
The diagram is clean; the reality has sharp edges, and most RAG quality problems live in the retrieval step, not the model:
- Chunking is finicky. Split documents too small and each chunk loses its context; too big and you waste the window and dilute relevance. How you cut matters a lot.
- Retrieval can miss. If the right chunk doesn't surface in the top results, the model never sees it — and will often answer anyway, from its flawed memory, with no idea it's missing the key fact. Retrieval failures are silent.
- Garbage in, garbage out. If your documents are outdated, contradictory, or wrong, the model faithfully repeats the errors. RAG grounds the model in your data — including your data's mistakes.
- More isn't better. Stuffing in twenty marginal chunks can bury the one that mattered and confuse the model (the "lost in the middle" effect, post #9). Good systems often re-rank retrieved results and keep only the best few.
Serious RAG systems layer on fixes: combining keyword and semantic search, re-ranking results for relevance, and sometimes letting an agent (post #12) iterate — search, judge whether the results are sufficient, refine the query, search again. That last move is agentic retrieval, and it's where RAG and agents merge.
Memory: RAG pointed at the past
The same machinery solves another problem we keep running into: an agent's limited context window and lack of long-term memory (post #12).
Instead of indexing documents, index the conversation history and the agent's own notes. When something becomes relevant again, retrieve it back into context.
long-term memory = a vector store of past interactions, facts, and notes
─► agent writes important things to it as it goes
─► agent retrieves relevant pieces when the current task needs themThis gives an agent an effective memory far larger than its context window: it keeps only what's relevant right now in the window and pulls the rest back on demand. It's how an assistant can "remember" your preferences across sessions or an agent can recall what it learned forty steps ago — not by holding it all in context, but by retrieving it when needed. Memory, in this sense, is just RAG aimed at the past instead of at a document store.
The takeaway
RAG fixes the model's frozen, fallible memory by fetching relevant information at question time and putting it in the context — open-book instead of closed-book. Semantic search over embeddings (meaning as geometry, post #3) finds the right material even without keyword overlap, which makes answers current, grounded, citable, and cheap to update. The hard part is retrieval quality, and the same technique, pointed at an agent's own history, becomes long-term memory.
We've built the entire stack now — model, engine, harness, tools, agents, and the knowledge to ground them. The last question is the one that decides whether any of it is actually good: how do we measure quality, manage the risks, and where is all this heading?