Retrieval-Augmented Generation
Instead of trusting a language model's memory, you search your own documents first and paste the relevant passages into the prompt. It turns an unverifiable fluency problem into a search problem you can measure.
Prerequisites: Large Language Models, Word Embeddings and Word2Vec
Ask a language model what a company guided to last quarter and you will get an answer. You will get an answer whether or not the model has ever seen that filing, because a next-token predictor's job is to produce plausible text, not to abstain. It has no mechanism for saying "I don't know", and the fabricated version reads exactly like the real one.
The fix is embarrassingly simple and it is the difference between a demo and a system: stop asking from memory, and hand over the document. A closed-book exam rewards recall and punishes anyone who studied a different chapter. An open-book exam rewards finding the right page. Retrieval-augmented generation, or RAG, turns every question into an open-book exam — search your own corpus for the passages that matter, staple them into the prompt, and require the answer to come from them.
RAG does not make the model smarter. It changes what the model is being asked to do — from recall ("what do you remember about this filing?") to reading comprehension ("here is the paragraph, answer from it"). Models are far better at the second, and the second is auditable.
The pipeline
There are two halves, one that runs ahead of time and one that runs per question.
Offline, you take every document — filings, transcripts, research notes, internal wikis — and cut them into chunks of a few hundred tokens. Each chunk is turned into an embedding vector by a model that places semantically similar text nearby, the same geometry described in Word Embeddings and Word2Vec. Those vectors go into an index built for fast nearest-neighbour lookup, described in Vector Databases and Embedding Search.
Online, the user's question is embedded with the same model, the index returns the closest chunks, and those chunks are pasted into the prompt above the question with an instruction like "answer only from the passages below, and quote the sentence you used." The model generates, and because the source text is right there, every claim can be checked against it.
Worked example: ranking the chunks
Retrieval is cosine similarity, nothing more. Suppose a question embeds to , which has length , and three candidate chunks embed to:
- — a paragraph on margin guidance
- — a paragraph on share buybacks
- — a boilerplate legal disclaimer
For : the dot product is and , so
For : dot product , length , giving . For : dot product , length , giving .
Ranking is , then , then . With the first two chunks go into the prompt and the disclaimer is correctly left out. In plain English: the index is voting on which paragraphs the question is about, using nothing but angles between vectors.
Worked example: where the accuracy goes
RAG accuracy is a product of two independent stages, and knowing which one is failing is most of the job.
Say your retriever puts the correct passage somewhere in the top for out of questions — recall at of . And say that when the right passage is present, the model extracts the answer correctly times out of . The end-to-end ceiling is:
About one answer in five is wrong, and three-quarters of that error is retrieval's fault. Buying a larger or cleverer language model addresses the and can win you at most a couple of points. Fixing chunking, adding keyword search alongside the embeddings, or re-ranking the top 50 down to the top 5 addresses the , where the real damage is.
The other constraint is arithmetic. With an -token context, reserving for the answer and for instructions leaves for retrieved text. At tokens per chunk that is chunks, and no more. Bigger chunks mean fewer of them; smaller chunks mean more of them but each one may be cut mid-argument. A common compromise is to retrieve small chunks for precise ranking, then expand each hit to the surrounding paragraph before it goes into the prompt, so the model sees enough context to interpret what it matched on.
Two cheap upgrades usually pay for themselves before anything exotic. Running a keyword search alongside the embedding search catches exact matches — tickers, section headings, defined terms — that vector similarity blurs away. And re-ranking a generous candidate set down to a handful with a slower, more accurate model shifts effort to the stage where the accuracy is actually leaking.
Retrieval does not eliminate hallucination, it bounds it. A model handed irrelevant passages will still answer, blending the retrieved text with whatever it half-remembers, and the result is more convincing than an ungrounded answer because it now cites something. Always measure grounding separately: what fraction of claims in the output appear verbatim in the retrieved chunks? If you are not measuring that, you do not know whether RAG is working. See Hallucination and Groundedness.
In finance the corpus must be time-aware. If a chunk from a March filing can be retrieved when answering a January question, your backtest has look-ahead built into the index itself. Stamp every chunk with its publication timestamp and filter the search by it before ranking, not after.
Related concepts
Practice in interviews
Further reading
- Lewis et al., Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks (2020)
- Gao et al., Retrieval-Augmented Generation for Large Language Models: A Survey (2024)