Skip to content

Grokem · https://www.grokem.com/ai-and-machine-learning/generative-ai/large-language-models/retrieval-augmented-generation/retrieval-augmented-generation-explained

Retrieval-augmented generation explained: look it up, then answer

How a language model answers from your own documents: split, embed, retrieve, then ask it to answer from what was found. And where this goes wrong.

Grokem Editorial

Editorial desk

Published
  • Intermediate
  • 4 min read

What you will learn

  • Describe the steps of a RAG pipeline
  • Explain what embeddings and vector search do
  • List the main design choices: chunking, retrieval, prompting
  • Name the common failure modes and how to evaluate a system

A large language model knows what was in its training data and nothing since. It does not know your product documentation, last week's policy change or your customer records, and when asked about them it may produce an answer that sounds right and is not. Retrieval-augmented generation (RAG) addresses that: before the model answers, the system looks up relevant passages in your own documents and gives them to the model together with the question.

The idea in one sentence: retrieve first, then generate from what was retrieved.

The two phases

Preparing the documents (done ahead of time):

  1. Collect and clean the source documents: web pages, PDFs, tickets, wiki pages.
  2. Split them into chunks, pieces of a few hundred words, so that each holds one idea.
  3. Create an embedding for each chunk. An embedding model turns text into a list of numbers such that texts with similar meaning end up close together.
  4. Store the embeddings with the text and its metadata (source, title, date, access rights) in a vector index or database.

Answering a question (done for each question):

  1. Embed the question with the same model.
  2. Retrieve the chunks whose embeddings are nearest to the question's. Many systems combine this with ordinary keyword search, because exact terms (an error code, a product name) are matched better that way.
  3. Optionally re-rank the candidates with a more precise model.
  4. Build a prompt that contains the question, the best chunks and instructions such as "answer only from the sources; say so if they do not contain the answer; cite the source".
  5. Generate the answer, and show the sources so the reader can check.

What makes it work or not

Chunking. Chunks that are too large dilute the meaning and waste the context window. Chunks that are too small lose the surrounding context. Splitting at natural boundaries (headings, paragraphs) and keeping a little overlap helps, and including the document title in each chunk is a cheap improvement.

Retrieval quality. If the right passage is not retrieved, no model can answer correctly. Retrieval is usually the weakest link, so test it on its own: for a set of questions, is the passage that contains the answer in the top five results?

The prompt. Tell the model to rely on the sources, to say when it does not know, and to quote or cite. Keep instructions and retrieved text clearly separated.

Freshness and permissions. Re-index when documents change. Store who may read each chunk and filter by it at retrieval time, so one user's question never surfaces another's confidential document.

How it goes wrong

Failure What you see What helps
The answer was never retrieved A fluent but wrong or evasive answer Better chunking, hybrid search, re-ranking, a larger top-k
Retrieved text is irrelevant The model builds an answer from the wrong passage Filters by metadata, re-ranking, a minimum similarity
The model ignores the sources An answer from its own memory, contradicting the documents A stricter prompt, and checking answers against the cited text
Outdated or conflicting documents Contradictory answers Version and date the sources, retire old ones
Injected instructions in a document The model follows text that was in the data, not from you Treat retrieved text as untrusted, and limit what the model can do
Sensitive data exposed Someone sees what they should not Permission filtering at retrieval, and careful choice of what to index

Evaluating it

Judge retrieval and answers separately, so you know where a failure comes from.

  1. Write a set of realistic questions, with the document and passage that answers each.
  2. Retrieval: how often is the right passage in the top results?
  3. Answers: is the answer correct, complete and supported by the cited text? Have people grade a sample, and be careful when using a model as a grader, because it has its own biases.
  4. Include questions the documents cannot answer. A good system says it does not know.
  5. Re-run the set whenever you change the chunking, the models or the prompt.

When RAG is the right tool

It suits question answering over a body of text that changes: documentation, policies, support history. It is a poor fit when the answer needs calculation across many records (use a database query), or when you need a specific writing style or behaviour (consider adjusting the prompt or, later, fine-tuning). Start small, with a few documents and honest tests, before building infrastructure around it.

Glossary

Embedding
A list of numbers that represents the meaning of a piece of text, so similar meanings are close together.
Vector database
A store that finds the stored embeddings nearest to a query embedding, quickly.
Chunk
A piece of a document, small enough to embed and to fit into a prompt.
Context window
The amount of text a model can read in one request.
Grounding
Making the model base its answer on supplied sources instead of only on what it learned in training.
Hallucination
A confident answer that is not supported by the sources or by fact.

Frequently asked questions

Is RAG better than fine-tuning?

They solve different problems. RAG supplies facts at question time and is easy to update. Fine-tuning changes how a model behaves or writes. For answering from changing documents, RAG is usually the first thing to try.

Does RAG stop hallucinations?

It reduces them by giving the model sources, but does not remove them. The model can still misread the sources, or answer when the sources do not contain the answer, so you have to design for that and test it.

Next steps

  • Build a small pipeline over a handful of your own documents
  • Write 30 realistic questions with known answers and measure retrieval and answers separately

References

From Grokem: https://www.grokem.com/ai-and-machine-learning/generative-ai/large-language-models/retrieval-augmented-generation/retrieval-augmented-generation-explained

How did this strike you?

Be the first to react – it is anonymous.

Community

Questions about this?

Ask in Questions and help and get help from other readers.

Ask a questionBrowse Questions and help

Comments

No comments yet. Share what worked for you, or ask a question.

Choose what we may use. You can change this at any time with “Privacy choices” at the foot of every page. Details are in the Cookie policy and the Privacy policy.

Strictly necessary

Remembers your theme, the lessons you marked as done and this choice. It stays on your device, tracks nothing and cannot be switched off.

Always on
What exactly is collected, and why

Grokem’s own counts

Collects
The pages you open and how long you stay, how far you scroll, where you came from, your device type, browser, screen size, language and time zone. Your IP address is turned into a code that changes every day; it is never stored as it is.
Why
To see how long lessons are read and where readers leave. Not shared with anyone.
Stored
A random visitor ID, a visit ID and the time of your last page view in your browser’s storage (gr_vid, gr_sid, gr_last). No name or email is attached.