Skip to content
Simon Place
FR EN
Book a Meet call

Module 7 of 10, lesson 1 of 5

Agentic RAG

Know what an agent adds to RAG, and when it only weighs the system down.

Classic RAG follows a fixed route. The question is turned into a vector, nearby passages are retrieved, then added to the prompt. The LLM is called once, to generate the answer.

Agentic RAG also uses the model to decide: which source to query, how to rephrase, whether the result is good enough.

The chain, from document to answer

The whole chain

A RAG has two halves. The preparation runs once, then again when documents change. The answer runs for every question. Everything the agent can find was decided on the left.

The full RAG chainLeft, the preparation: documents, chunking, embeddings, vector store. Right, for each question: agent, search, reranking, check, answer. The agent starts again if the context is not enough. Prepare, onceAnswer, each questionDocumentsPDF, pages, ticketsChunkingshort passagesEmbeddingeach passage becomes numbersVector storevectors, text and access rightsQuestionAgentdecides what to searchSearchwords and meaningRerankingkeeps the best passagesEnough to answer?Answerwith its sourcesno: search againThe same embedding modelturns the question into a vector.
Most RAG failures start on the left, long before the model writes a word.

1. Chunking

A model cannot read a whole library for each question, so documents are cut into passages. Too long, and one passage mixes several subjects. Too short, and it loses its meaning.

Chunking a documentLeft, a policy with two articles. Right, the three passages cut from it. The line about the photo appears in passages one and two, and passage two gets a copy of the title of article 12. The documentArticle 12: returnsA damaged parcel is refundedwithin 30 days of delivery.Attach a photo of the damage.Refunds leave within 5 days.Article 13: warrantyDevices: 2-year warranty.The passages Passage 1 Article 12: returnsA damaged parcel is refundedwithin 30 days of delivery.Attach a photo of the damage. Passage 2 Article 12: returnsAttach a photo of the damage.Refunds leave within 5 days. Passage 3 Article 13: warrantyDevices: 2-year warranty.Shared line: nothing is lostat the cutTPassage 2 repeats the title:it makes sense on its own.
Cut where the document cuts. A fixed size could have split “within 30 days” from the rule it belongs to.

2. Embedding

An embedding model turns each passage into a vector, a list of numbers that captures its meaning. Texts that say the same thing get close vectors, even with different words.

From text to vectorsThree passages go through an embedding model and come out as lists of numbers. The two passages about damage get close numbers, the warranty passage gets very different ones. PassageVectorEmbeddingmodelDamaged parcel, refund[ 0.81, -0.32, … ]Photo of the damage[ 0.74, -0.28, … ]Two-year warranty[ -0.15, 0.66, … ]
The model places sentences by meaning, not by spelling. These values are illustrative, and real vectors hold hundreds or thousands of numbers.

3. The vector store

The store keeps each vector with its text, its source and its access rights. It answers one question quickly: which passages are nearest to this vector? Keep access rights here, so you can filter before ranking.

How the store finds the nearest passagesPassages are placed on a map by subject. The question lands in the refund area, and the store returns the three nearest passages. The warranty passage is far away and is left out. “My parcel is broken, can I get a refund?” Returns and refunds Delivery Warranty Damaged parcel, refund Photo of the damage Refund within 5 days Delivery time Parcel tracking Two-year warranty Device fault your question far What the store returns, nearest first1 Damaged parcel, refund 0.922 Photo of the damage 0.843 Refund within 5 days 0.714 Two-year warranty 0.18
The three nearest passages go on to the next step, and the warranty is left out. This 2D map is a sketch of a space with far more dimensions, and the scores are illustrative: closeness is usually measured by cosine similarity.

4. The types of search

A vector store answers one kind of question. Others need exact words, a database query, a graph of relations or a look inside the files. For an agent, each of these is a separate tool, which is why choosing the source matters.

Which search for which questionSeven questions, each matched to a search type and its limit: words, meaning, hybrid, structured, graph, files and web. Filtering on metadata and rights comes first on your own data. The questionThe search, and its limit“Error ERR-4512”Words (BM25)misses synonyms“My parcel is in pieces”Meaning (vectors)misses exact codes“Return after ERR-4512?”Hybridruns two searches“March orders above 100”Structured (SQL)cannot read free text“Who supplies this product?”Graphcostly to build and maintain“Where is refund_limit set?”Files (grep)slow on large volumes“Carrier rates today”Webuntrusted contentFilter on metadata and access rights firstdate, language, team: it narrows every search on your own data
Cyan searches documents, violet searches structured data, green reaches a source directly. Pick from the question, not from the tool you already have.

5. Combining searches and reranking

The hybrid search above is the usual default for documents. Run both searches, merge their rankings, then let a slower model reread only the best candidates.

From two searches to a few passagesA keyword ranking and a vector ranking are merged by rank, then a reranker rereads the best candidates and only the top passages reach the prompt. Words (BM25)1 delivery-faq2 article-123 returns-guideMeaning (vectors)1 warranty2 article-123 returns-guideRank fusion (RRF)1 article-12 2 returns-guide 3 delivery-faqRerankinga slower model rereads the question with each candidateThe prompta few passages, each with its source
Each search ranks a different document first. Article 12 wins because both found it, and BM25 catches exact codes that vectors miss.

6. The agent

So far the path never changes. The agent turns search into a tool it can use, skip or try again. That is the only difference between a classic RAG and an agentic one.

What the agent addsThe agent chooses a source, reads the results and decides. If they are enough, it answers with sources. If not, it rewrites the search a limited number of times, then says it does not know. Parcel damaged 20 days ago?Agentwhich source?PolicychosenOrdersnot neededWebout of scopeEnough to answer?the agent judges the resultno: rewritecapped triesyeslimit reachedAnswer with sources“Yes, within 30 days. Article 12”“I can’t find this”better than a confident invention
Without an agent the path is fixed. With one, each decision adds a model call, so cap the tries.

Four common roles

What matters most

The trade-off

Each model decision adds a call, so cost and latency. For a simple question on one source, classic RAG stays faster and cheaper. And neither one fully removes hallucinations.

Fix retrieval before adding an agent.

Worked example: Merging two rankings

Keyword search finds the exact article number and vector search finds the meaning. Reciprocal rank fusion merges them using ranks only, so the two kinds of score never need comparing.

function fuse(rankings, k = 60) {
  const scores = new Map();
  for (const ranking of rankings) {
    ranking.forEach((id, rank) => {
      scores.set(id, (scores.get(id) ?? 0) + 1 / (k + rank + 1));
    });
  }
  return [...scores].sort((a, b) => b[1] - a[1]).map(([id]) => id);
}

const keywords = ['refund-art-12', 'shipping-faq', 'returns-guide'];
const vectors = ['returns-guide', 'refund-art-12', 'warranty'];

fuse([keywords, vectors]);

['refund-art-12', 'returns-guide', 'shipping-faq', 'warranty']

Article 12 comes first because both searches rank it well, even though neither is sure of it alone.

Try it in the editor

function rrf(rankings, { k = 60, limit = 10 } = {}) {  // TODO  return [];}module.exports = { rrf };
Give the agent a skillMerge rankings with RRF
Open in the editor

Legal notice and privacy