Skip to content

Research · Retrieval

How retrieval-augmented generation (RAG) works.

Updated

Retrieval-augmented generation (RAG) gives an AI model your documents to read before it answers. Colidal's RAG engine splits documents into passages, finds the ones that match a question by meaning and by exact words, puts the best ones first, and has the model answer from those passages only, with the source shown.

search = meaning + exact words · rerank = on · sources = shown

Retrieval-augmented generation (RAG) is a method where an AI model first looks up relevant passages in a set of documents and then answers using only what it found.

How it works

Two parts: prepare the documents, then answer the question.

The slow work happens once, before anyone asks anything. When a question arrives, the system only has to search, sort, and answer.

  1. The corpus is split into chunks.
  2. An LLM reads the whole document and writes a short context for every chunk.
  3. Each context plus chunk goes to an embedding model and to TF-IDF.
  4. Embeddings are stored in a vector database; TF-IDF encodings in a TF-IDF index.
  5. A query searches both stores.
  6. Rank fusion merges the results into the top N chunks.
  7. A reranker keeps the top K chunks.
  8. The generative model writes the response.
How RAG answers: documents are split into chunks, each chunk gets a line of context, and the chunks are indexed by meaning and by keyword. At runtime both indexes are searched, the results are merged and reranked, and the model answers from the best chunks.

Preprocessing

Before any question

  1. 01

    Split the documents into chunks.

    A chunk is a passage of a few hundred tokens: short enough to search precisely, long enough to mean something.

  2. 02

    Add context to every chunk.

    An LLM reads the whole document and writes a line or two that says where the chunk sits in it. This is contextual retrieval.

  3. 03

    Encode each chunk twice.

    An embedding model turns it into a vector that captures meaning. TF-IDF records the exact words in it.

  4. 04

    Store both.

    Vectors go into a vector database. Keyword encodings go into a TF-IDF index, searched with BM25.

Runtime

When a question arrives

  1. 01

    Search both indexes.

    The vector database finds chunks that mean the same thing. The keyword index finds chunks with the exact terms, such as an error code.

  2. 02

    Merge the results.

    Rank fusion combines the two lists and removes duplicates, leaving the top N chunks.

  3. 03

    Rerank.

    A reranker scores each chunk against the question and keeps the top K.

  4. 04

    Answer.

    The generative model reads the top K chunks and writes the response from them, with the sources.

What makes retrieval good

Four decisions that decide the answer.

A model can only answer from what it is handed. Most wrong answers from a RAG system are retrieval failures, not model failures.

chunking

Where to cut

Cut a document in the wrong place and a passage loses its meaning. Chunk size, boundaries, and overlap all change what can be found.

hybrid search

Meaning and exact words

Meaning search finds a passage that says the same thing in other words. Keyword search finds the exact part number. Good systems run both.

contextual retrieval

Context for every chunk

"Revenue grew by 3%" is useless alone. With a line saying which company and which quarter, the same chunk can be found and trusted.

reranking

Best passages first

Search casts a wide net. A reranker reads each candidate against the question and keeps only the strongest few.

Four ways to chunk a document

MethodHow it cutsTrade-off
Fixed-sizeEqual blocks by character or token count, with some overlapSimple and cheap. Cuts through sentences and ideas.
RecursiveBy paragraph, then sentence, then word, until each piece fitsMore coherent. Struggles when a document has no clear structure.
SemanticStarts a new chunk where the meaning shiftsFollows the topic. Needs more computing and a well-set threshold.
AgenticAn LLM groups small pieces into coherent chunks and gives each a title and summaryBest chunks for mixed documents. Costs a model call per chunk, and is still a young method.

Published results

How much each step helps.

In published tests, each addition cut the share of relevant passages the search failed to find. Lower is better.

Meaning search only5.7%baseline
+ context on each chunk3.7%35% fewer misses
+ contextual keyword search2.9%49% fewer misses
+ reranking1.9%67% fewer misses

Source: Anthropic, "Introducing Contextual Retrieval" (2024). Measure: share of relevant chunks missing from the top 20 retrieved, averaged across codebases, fiction, and research papers. Published figures, not Colidal measurements.

Settings behind those results

SettingValue in the testsWhy it matters
Chunk sizeA few hundred tokensSize, boundaries, and overlap all change what is found. Test them on your own documents.
Context per chunk50 to 100 tokens, written by an LLM that sees the whole documentIt must be specific to the chunk. A generic summary of the document on every chunk gave very little gain.
Keyword searchBM25, which builds on TF-IDFIt adjusts for document length and for a word repeated many times, and it finds exact strings such as "error code TS-999" that meaning search can miss.
Candidates retrievedThe top 150 chunksA wide first pass, so the right passage is somewhere in the pile.
Chunks given to the modelThe top 20 after rerankingPassing 20 chunks gave better answers than passing 10 or 5.
Reranking costA small delay on every questionReranking more candidates is more accurate and slower. The right number is a measured trade-off.
Cost of adding contextThe document is cached once, then reused for each of its chunksWithout caching, the whole document is paid for again with every chunk.

Fit

When RAG fits, and when it doesn't.

Use it when

  • The answer is written down somewhere in your documents.
  • People ask specific questions: a policy, a past case, a product detail.
  • Every answer has to show where it came from.
  • The documents change, and the answers must change with them.

Skip it when

  • The whole knowledge base fits in one prompt. Under about 500 pages, hand the model all of it.
  • The question needs facts joined across many documents. GraphRAG suits that.
  • The question takes several steps or live systems. Agentic RAG suits that.
  • Nothing is written down yet. Retrieval cannot find what does not exist.

Compare

RAG, GraphRAG, or agentic RAG?

They are not rivals. Most systems start with RAG, and add a graph or an agent only where the questions need one.

RAGGraphRAGAgentic RAG
What it retrievesPassages that match the questionConnected facts, and summaries of whole topicsWhatever the agent decides the task needs
A question it suitsWhat does the returns policy say about opened items?Which suppliers are linked to the late orders this year?Compare this month's returns with the policy and draft a reply.
Built before any questionChunks, embeddings, a keyword indexAll of that, plus a knowledge graph and community summariesNothing new: it uses the other indexes as tools
Cost to set upLowestHighest: an LLM reads every chunkMedium: tools, checks, and tests
Cost per answerLowest: one search, one model callLow for specific questions, higher for broad onesHighest: several model calls per task
How it shows its workThe passage and page it usedThe path it followed through the graphEvery step, tool call, and source
Start here whenThe answer is written down in one placeThe answer is spread across many connected documentsThe answer takes several steps or several systems

How Colidal builds it

A RAG engine built into your workflows.

Colidal's custom RAG engine does more than wait for a question. It is embedded in the work, so the relevant policy or past case appears beside a request when it arrives.

  • Tuned to your documents. Chunking, search, and reranking are tested on your own files and questions before anyone relies on them.
  • Sources on every answer. Each answer shows the page it came from.
  • Declines when unsure. When the documents do not support an answer, it says so.
  • Sanitized both ways. Sensitive data is masked before the model sees it, and the output is checked.
  • In your cloud account. Your documents and indexes stay where you control them.

Used in: Your documents · Repeat questions · colidal/rag

FAQ

Common questions.

What is RAG, in one sentence?

Retrieval-augmented generation is a method where an AI model looks up relevant passages in your documents and answers from those passages, instead of from memory.

Why not just paste the documents into the prompt?

For a small set, do exactly that. Once the documents outgrow what a model can read at once, about 500 pages, retrieval picks the few passages that matter.

Does RAG stop an AI model from making things up?

It reduces it, because the model answers from text it was handed. It works best with two more rules: show the source, and decline when the documents are silent.

What is the difference between meaning search and keyword search?

Meaning search matches ideas, so "refund" finds "money back". Keyword search matches exact terms, so an order number finds that order. RAG needs both.

Keep reading

More from Colidal Research.

All research →

Next step

Request a 30-minute call.

Tell us what you need. We reply within one business day with times for a call, and you'll get a short agenda before we talk.

  1. 01

    Request a call

    Thirty minutes, by video. No preparation needed.

  2. 02

    Get times and a short agenda

    Within one business day, so the call is spent on your problem.

  3. 03

    Leave with a next step

    Usually Colidal AI Assessment, or an honest "AI isn't the answer here".