Search & Research

Retrieval-Augmented Generation in Search

How retrieval-augmented generation works, from splitting documents to writing a cited answer, and where it can fail.

Key takeaways

  • RAG pairs a retrieval system that finds passages with a language model that writes an answer from them.
  • Documents are usually split into chunks and indexed ahead of time, so questions can be matched quickly.
  • Answer quality depends heavily on retrieval, because the model can't use a passage it never received.
  • Grounding in sources reduces invented answers but doesn't eliminate them.
  • Good systems show sources, respect access permissions and say when they found nothing useful.

Retrieval-augmented generation, usually shortened to RAG, is a way of having a language model answer from documents fetched for the question at hand. A search step finds relevant passages, and those passages are placed into the model's input along with your question. The model then writes a reply based on what it was handed.

The idea matters because a model by itself only knows what it absorbed during training. Retrieval lets it use newer, private or specialized material, and lets the system show where an answer came from. It underlies many AI search features, help-center bots and tools that answer questions about a company's own files.

The problem it addresses

A large language model is trained on a huge body of text and then frozen. Ask it about something after the training cutoff, or about your company's vacation policy, and it has nothing reliable to draw on. It may still produce a fluent answer, which is the behavior known as hallucination. Why AI models hallucinate explains why.

You could retrain or adjust the model with new material, but that's costly, slow to update and hard to audit. RAG takes another route: leave the model alone and change what it reads. Update a document, re-index it, and the next question can use the new version. Fine-tuning vs. retrieval compares the two approaches in more depth.

Think of an open-book exam. The student still has to understand the question and write a good answer, but the facts can come from pages on the desk. If the wrong book is on the desk, the answer suffers.

Step one: preparing the documents

Before any question arrives, the system builds an index.

  1. Collect. Gather web pages, manuals, tickets or files.
  2. Clean. Strip navigation menus, repeated footers and formatting noise.
  3. Chunk. Split long documents into passages, often a few paragraphs each. Chunks that are too small lose context; chunks that are too large bury the relevant line and waste space.
  4. Index. Store each chunk in a keyword index, a vector index or both.
  5. Tag. Keep metadata such as title, date, author, product version and who is allowed to see it.

A vector index stores an embedding for each chunk: a list of numbers that represents its meaning, so that similar passages sit near each other. Semantic search and embeddings explains how that works. A keyword index, described in what is an inverted index, is better at exact terms such as part numbers and error codes.

Chunking deserves more care than it usually gets. A table split from its header, or a warning split from the instruction it qualifies, can lead to a confidently wrong answer later. Many systems overlap neighboring chunks or attach the document title and section heading to each one.

Step two: retrieving at question time

When you ask something, the system turns your question into one or more searches. In a conversation, it may first rewrite "does it support that on the older one?" into a self-contained question, using earlier messages.

It then fetches candidate chunks. A common design is hybrid retrieval: run both keyword and vector searches and merge the results. After that, a reranker, a second model that compares the question with each candidate more carefully, can reorder them. Finally the best few go forward, limited by how much text the model can read at once. That limit is the context window, covered in tokens and context windows.

A RAG request, message by message
You Application Search index Language model Ask a question 1 Search with rewritten query 2 Return matching passages 3 Send instructions, passages and question 4 Draft answer citing passages 5 Show answer with source links 6 You Application Search index Language model Ask a question 1 Search with rewritten query 2 Return matching passages 3 Send instructions, passages and question 4 Draft answer citing passages 5 Show answer with source links 6

Step three: assembling the prompt and writing

The application builds the model's input from several parts. A simplified, hypothetical version might look like this:

Instructions: Answer only from the sources below. Cite them by number.
If they don't contain the answer, say so.

[1] Title: Router Setup Guide, section "Guest network"
    ...passage text...
[2] Title: Release notes, version 3
    ...passage text...

Question: Can guests reach my printer?

The model generates a response from that input. The instruction to answer only from sources helps, but it's a request, not a guarantee. The model may still blend in general knowledge, or draw a conclusion the passages don't support.

Many systems add checks after generation: verifying that cited numbers exist, testing whether each sentence is supported, or filtering unsafe content. These reduce errors and add time and cost.

Where it can break

Most disappointing answers trace to a specific stage, which makes RAG easier to debug than a model answering from memory.

Stage What goes wrong Typical remedy
Source collection Documents are missing, outdated or contradictory Curate and date the content
Chunking Key context is cut apart Split on headings, keep tables whole
Retrieval Right passage isn't found Combine keyword and vector search
Ranking Right passage found but dropped Add a reranker, pass more candidates
Prompt Model ignores or misreads sources Clearer instructions, less clutter
Generation Claim goes beyond the source Verify support, show quotes
Diagnosing a wrong RAG answer
Is the correct information in any source document? YES Check the next question. If not, fix the content first. 1 NO Was the correct passage missing from the retrieved set? YES Improve chunking, search method or filters. 2 NO Was it retrieved but the answer still disagrees? YES Tighten instructions and check support of each claim. 3 NO Check the question itself It may be ambiguous, or the content missing entirely. Is the correct information in any source document? 1 IF YES Check the next question. If not, fix the content first. NO Was the correct passage missing from the retrieved set? 2 IF YES Improve chunking, search method or filters. NO Was it retrieved but the answer still disagrees? 3 IF YES Tighten instructions and check support of each claim. NO Check the question itself It may be ambiguous, or the content missing entirely.

A worked example

Say a small software company builds a help assistant over its documentation. A customer asks, "Why does export fail on large projects?" The application searches the index and gets back four chunks: a troubleshooting page about export time limits, a release note that raised a limit in a newer version, an old forum reply suggesting a workaround and a marketing page that mentions export.

A reranker should push the troubleshooting page and release note to the top and drop the marketing page. The model then needs to notice that the two good passages depend on version. A strong answer says that older versions have a time limit on export, that the newer release raised it, and asks which version the customer runs, citing both passages. A weak answer picks one passage and states it as universal.

The difference often comes down to preparation. If each chunk carries its product version and date, the system can prefer current material and the model can see that a condition applies. If the old forum reply is labeled as unofficial, it can be ranked lower or left out. Little of this is glamorous, but it decides whether people trust the tool.

Keeping the index current

An index is a copy, and copies go stale. Decide how changes reach it: a scheduled re-crawl, a notification when a document is saved or a full rebuild. Deletions matter most. If a retracted policy or removed file stays in the index, the assistant can keep quoting it with a link that no longer works.

Contradictions deserve attention too. When two documents disagree, retrieval may return either one depending on how the question is phrased. Marking one source as authoritative, retiring superseded pages and showing dates beside citations all help the reader notice. Logging which passages were used for each answer makes it possible to trace a complaint back to the document that caused it. Treat those logs as sensitive, since they contain user questions.

Security and permissions

Retrieval widens what a model can reveal. If a system indexes internal files, it must apply each user's access rights at search time. Filtering only what's displayed is too late, because a passage the model has read can leak into its wording.

Retrieved text can also carry instructions. A web page or shared document might contain hidden text telling an assistant to ignore its rules or send information somewhere. This is called prompt injection. Defenses include treating retrieved text as data, limiting what the system can do on its own and requiring confirmation for consequential actions. Prompt injection explained covers the risk.

Poor or manipulated sources are a quieter threat. Anyone who can get content into the index can influence answers, so decide who may add material and review it.

How to judge a RAG system

If you're choosing or building one, test with real questions, including some whose answers aren't in the documents. Check:

  • Does it find the right passage? Measure this separately from the writing.
  • Do the cited passages support each claim?
  • Does it admit when nothing relevant was found?
  • Does it handle exact identifiers like model numbers?
  • Does it prefer the current version over an old one?
  • Can a user see a document they shouldn't?

Keep a set of test questions with known answers and rerun it after changing chunk sizes, models or content. Without that, improvements in one area can silently damage another.

What it means for you as a searcher

When a search page or assistant answers with links, you're often looking at some form of RAG. That tells you where to look for errors. If the answer seems off, open the sources: either they're weak, or the model misread them. Rephrasing your question with exact terms can change which passages are retrieved, and therefore the answer.

Adding specifics helps the retrieval step directly. A model number, version or error string gives the search something distinctive to match, the same way it would in a classic engine. Checking the sources behind an AI answer shows how to confirm the result.

Common questions

Does RAG need a vector database?

No. Retrieval can use an ordinary keyword search, a database query, an API call or a combination. Vector search is popular because it handles differences in wording.

Is a long context window a replacement?

A larger window lets you paste more material in directly, which suits a handful of documents. For large or changing collections, searching first is still cheaper and usually easier to check.

Written for 4Internet with AI writing tools and not reviewed line by line by a person. Check anything you rely on with the maker, provider or standard concerned. Report an error.

Keep reading

All 45 articles in Search & Research →

Latest news on this topic