Chapter 12 · concept 3 of 4 · show all
2Tokens, cost and your first RAG
4Chunking
- 4.1Why we chunk, and the main chunking strategies
- 4.2Structure aware chunking and tables
- 4.3Chunk size, overlap and token based sizing
- 4.4Contextual chunking and late chunking
- 4.5Parent child and multi representation indexing
- 4.6Chunk metadata, provenance and the chunking debugging playbook
- Chapter overview
7Finding the right chunks
8When retrieval fails
Agentic RAG and RAG as an MCP tool
Instead of one fixed search, a model decides what to look up, reads, and searches again. Exposing retrieval as an MCP tool lets any agent use it. When the loop helps, what it costs, and how to keep it safe.
A real question
Some questions need two or three look-ups. Can the model do the searching itself?
Yes: give it a search tool and let it decide what to fetch, read the results and search again. That is agentic RAG. It handles multi-step questions, at the cost of more calls, more latency and new ways to fail.

The problem
Why this matters
Agents always beat a fixed pipeline.
Our one multi-document question, “What caused incident INC-77?”, is already answered by one hybrid search (answer at rank 2). An agent would add model calls for no gain. Agents pay off when questions really need several dependent steps.
Why not the simpler way
A fixed pipeline makes one search with the user's words. Some questions need more: compare two plans, find the latest status, combine a policy with a customer's own data, or calculate. Agents handle these by planning and calling tools, which is also how many assistants now consume RAG.
The idea
The idea in plain words
Agentic RAG: a model in a loop, with tools such as search, document fetch, database queries or calculators, deciding each next step from what it has read. RAG as a tool: retrieval is offered as a function an agent can call. MCP (Model Context Protocol, an open standard published by Anthropic in 2024) defines how tools and data sources are described to models, so one retrieval server can serve many clients.
A researcher with a library card: rather than handing over one search result, you let them search, read and search again until they can answer.
How it works
Step by step
- 1
Plan
The model reads the question and decides what to look up first: “find the bulletins about 3511, then the latest one”.
- 2
Retrieval as a tool
search_documents and get_document become tools, ideally behind MCP so any agent can use them with the same filters and access rules.
- 3
Cap and log
Limit steps and tokens, log every tool call in the trace, and keep the same grounding and verification rules for the final answer.
In detail
Loop. System rules, tools, the question → the model calls a tool → results are added → repeat until it answers or hits a limit.
Tool design. Few, clear tools: search_documents(query, filters) returning ids, titles, versions and snippets; get_document(id, section) returning text. Access filters come from the session, never from tool arguments the model chooses (RAG-46).
Guard rails. Step and token limits, timeouts, no write actions without confirmation, and the injection rules for anything a tool returns (RAG-45).
Evaluation. Score the final answer as before, and also the path: number of steps, tool errors, and whether the needed documents were fetched.
The code
Try it in Python
TOOLS = [
{"name": "search_documents",
"description": "Search company documents. Returns ids, titles, versions, dates and snippets.",
"input_schema": {"type": "object", "properties": {"query": {"type": "string"}}, "required": ["query"]}},
{"name": "get_document",
"description": "Fetch the text of one document section by id.",
"input_schema": {"type": "object", "properties": {"id": {"type": "string"}}, "required": ["id"]}},
]
def run_tool(name, args, session):
if name == "search_documents": # roles from the session, not from the model
return hybrid_search(args["query"], roles=session.roles)[:5]
if name == "get_document":
return fetch_if_allowed(args["id"], roles=session.roles)
MAX_STEPS = 4 # stop and answer with what was found, or abstain
See it
What we measured
Measured · questions that might need several steps
Polder corpus, 34 recursive 512 token chunks, hybrid search, with and without reranking.
Measured 2026-11-04 with corpus/scripts/measure-parts-4-7.mts. An agent needs model calls: not measured yet. Our golden set has few multi-step questions, so it cannot show where an agent pays off.
Try it on a real example
“Is the fibre outage in postcode 3511 fixed?” (asked on 2026-10-03)
Network bulletins 1, 2, 4 (INC-77)
The same outage at three moments. Only the latest is the answer.
Choosing
Use it when, avoid it when
- Agents for multi-step questions, comparisons and tool use
- Retrieval behind MCP with session-based access
- Step, token and time limits
- Agents for simple look-ups
- Tools that accept access roles from the model
What it costs · 2 to several model calls per question instead of 1, plus variable latency.
In production
Agent cost and latency vary per question: track p95 steps and tokens, and cap them. Cache tool results within a session. Keep a fixed pipeline for the common simple questions and route only complex ones to the agent (RAG-49). Log the whole loop in one trace (RAG-48), because failures are often a wrong tool choice early on.
Watch out
Common mistakes
- Agents remove the need for good retrieval. They call the same retrieval, more often.
- More steps mean better answers. They also mean more chances to go wrong.
- MCP is a model. It is a protocol for connecting tools and data to models.
Check yourself
Remember this
Remember these
- Agentic RAG lets the model search, read and search again.
- Use it for multi-step questions and tools; not for plain look-ups.
- Expose retrieval as an MCP tool with the same filters and access rules.
- Cap steps, log every call, and keep grounding and verification.
Say it in an interview
- 01Agentic RAG lets the model plan, search, read and search again with tools.
- 02I use it for multi-step questions; plain look-ups stay on the fixed pipeline.
- 03Retrieval is exposed as an MCP tool, with access roles taken from the session.
- 04I cap steps and tokens and log every tool call in one trace.
- 05On my corpus the one multi-document question was already answered by one hybrid search.
Quick quiz