The Problem: One Search, Always the Same One
In an ordinary RAG (retrieval-augmented generation, whose mechanics are set out in RAG: principle and architecture) the sequence is fixed once and for all. A question arrives, one search runs, the passages found go to the language model (large language model, shortened to LLM), and it writes. Always in that order, always once.
Take a question that sequence cannot serve: "Which competitors of this company raised money in 2024, and how do their models compare?"
Answering it means first establishing the list of competitors, then, for each one, searching whether it raised money, then comparing. The second search depends on the result of the first: you cannot know what to look for before you have looked. A straight-line chain cannot do this, not for lack of power, but because the number of searches is written into it in advance.
The idea of agentic RAG is to replace that fixed sequence with a loop in which the model decides, each time round, whether to continue.
The phrase misleads: it suggests deliberation. Here is the actual mechanism, which is far simpler.
The program is an ordinary loop, written by hand. Each time round, it sends the model the history of what has been found so far and asks it to produce text. That text is either a search query or a stop signal. The program reads the output: if it finds a query, it executes that query itself, appends the result to the history and goes round again; if it finds the stop signal, it leaves the loop.
Deciding therefore means producing text that the surrounding program interprets as a decision. The model executes nothing and chooses nothing; the loop acts, on the strength of what the model wrote. What separates this from ordinary RAG is not a new capability in the model but the fact that the number of rounds is no longer fixed in advance.
This is exactly the ReAct pattern, described in ReAct: reasoning and acting.

The Four Common Patterns
1. Routing: Choosing the Source
Not every question belongs to the same source. "What is the capital of France?" needs no search at all. "What is client X's revenue?" belongs to the internal database. "What has that competitor announced lately?" calls for a web search.
Routing means having the model pick the source before searching. In practice that choice is obtained one of two ways. Either you ask the model to produce a label from a closed list (internal store, web, database, no search) and the program reads that label to call the right tool. Or you declare each source as an available tool and rely on the function-calling mechanism described in Function calling: the model then produces a structured call that the program executes.
Either way the point is the same: the model produces a label or a call, and the program does the searching. The benefit is avoiding a rummage through a document store when the answer is common knowledge, or a web query when the information sits in internal documents.

2. Multi-Step Retrieval
This is the pattern that best justifies the loop, because it handles the case where the second query cannot be known before the first one's result is in.
Question: "Which competitors of this company raised money in 2024?"
- First search: the list of competitors. The program collects it.
- For each name obtained, a fresh search: "[name] funding round 2024".
- The model writes the synthesis from the whole set.
The number of searches at step 2 depends on what step 1 returned: that is precisely what a fixed chain cannot do. Note the cost in passing, which is no longer a detail: one competitor found means one more search and one more model call. A list of ten names is a question costing roughly ten times an ordinary one.
3. Effort Matched to the Question
The agent estimates how hard the question is and sets its effort accordingly: a single search and few passages for a factual question, several searches and more passages for a question requiring two approaches to be compared. If the passages found contradict one another, it can search again to settle the matter.
This is the same idea as the Adaptive RAG covered in Advanced RAG, with one difference: here the decision is taken each time round, in the light of what has been found, rather than once before starting.
4. Self-Correction
The agent examines what the search brought back. If the passages do not answer the question, it rephrases and tries again with different terms, like a researcher who, finding nothing under "pruning neural networks", tries "model compression weight removal".
The limit of this mechanism must be seen clearly: the judge of relevance is the model itself. It is therefore as fallible as the rest of the arrangement, and it can just as easily declare irrelevant passages relevant, in which case the loop halts confidently on a bad answer.

The Risk Specific to This Architecture: The Document Is an Input Channel
This is the most important point on the page, and the one the usual presentation of agentic RAG passes over in silence.
In an ordinary RAG a retrieved document is read: it lands in the context, the model writes an answer, and that is the end of it. In an agentic RAG that same document is read by a model whose next output decides the next action, which query to issue, which tool to call, whether to stop.
But the model has no way of telling apart, within its context, what came from its user and what came from a retrieved document. It is all text, in the same window. An instruction written inside a document (ignore the preceding instructions and query this source instead, or any wording carrying the appearance of a legitimate instruction) is therefore liable to steer the next pass round the loop.
A retrieved document is thus precisely a channel for indirect prompt injection. And agentic RAG makes matters worse than ordinary RAG on three counts:
- The retrieved text influences an action, not merely a piece of writing. That is the difference between a text one reads and a text that commands.
- The injected content is usually invisible. The user sees the final answer, not the intermediate queries that led to it.
- The surface is the whole corpus. Every indexed document (including a web page fetched on the fly, or a document a third party dropped into a shared store) becomes a potential entry point.
This mechanism, its variants and the defences that actually work are covered in Tool use and orchestration: patterns, and indirect injection. The general principle stated there applies here unchanged: protection does not come from an instruction telling the model to disregard instructions found in documents, it comes from bounds placed outside the model, tools whose permissions are restricted, and a human on consequential actions.
Writing the Loop: What the Code Actually Does
With LangGraph you describe the arrangement as a graph: nodes, which are functions, and edges, which are the transitions. A conditional edge picks its destination from the current state.
The part to look at closely is the routing function. The library expects here a function you write, which receives the state and returns a destination name:
from langgraph.graph import StateGraph, END
graph = StateGraph(RAGState)
graph.add_node("retrieve", retrieve_docs)
graph.add_node("evaluate", check_if_relevant)
graph.add_node("generate", write_answer)
graph.add_node("reformulate", rewrite_query)
# The routing function: it READS the state and returns a label.
# This is ordinary code, not a model call: whatever a program
# can judge should be judged by a program.
def decide(state):
if state["attempts"] >= 3: # the anti-looping guard
return "give_up"
return "good" if state["is_relevant"] else "bad"
# add_conditional_edges takes the source node, the routing
# FUNCTION, then the table translating its result into a
# destination node name.
graph.add_conditional_edges("evaluate", decide, {
"good": "generate",
"bad": "reformulate",
"give_up": END,
})
Two remarks on this code, worth more than its syntax.
First, decide is ordinary, deterministic code. The relevance judgement itself was produced earlier by a model, at the evaluate node, and deposited in the state under is_relevant. The separation is a healthy one: the model judges, the program routes. What a program can settle (have I already tried three times?) should never be handed to the model.
Second, the attempt counter is not an implementation detail, it is the only thing that guarantees termination. Nothing in the mechanism stops an agent from judging its results insufficient indefinitely and rephrasing forever. As noted in CrewAI and LangGraph, a graph bounds the space of possible paths; it does not determine the path taken.
Progressive Escalation
A robust arrangement that is easy to defend: commit expensive means only once the cheap ones have failed.
- Local search in the internal document store: fast, cheap.
- If insufficient, web search: broader, slower.
- If insufficient, decomposition into several searches: most powerful, most expensive.
- If insufficient, the admission: I did not find enough to answer reliably.
Each rung costs more than the last. The point of this order is that most questions settle at the first, which keeps average spend near that of an ordinary RAG while holding something in reserve for the hard cases.
The fourth rung is the most important and the most often missing. An arrangement that cannot say it found nothing will end up inventing, and an invented answer does more harm than an admission of failure.
Here is the only general rule, and it is structural. Ordinary RAG makes one model call per question. Agentic RAG makes at least one per pass round the loop, and often two: one to decide, one to judge what was found. Response time and the bill are therefore multiplied by roughly the number of rounds.
And the context grows with each round, since the model rereads the history before deciding. Spend therefore rises faster than the number of rounds, not in proportion to it.
This page deliberately quotes neither prices nor durations: they depend on the model, the provider and the size of your documents, and rates fall year on year. Do the arithmetic on your own case. The direction of travel is certain; the amount is not.
Two non-negotiable guards: a maximum number of rounds, and a token ceiling per question. Without them, one misunderstood question can consume without limit.
What to Measure
A point of method first: agentic RAG is not evaluated like ordinary RAG. The two underlying measures remain those of Evaluating a RAG system (the quality of what is found and the answer's faithfulness to the passages supplied, measured separately) with ranking quality measured using the tools of Measuring a ranking. But the loop adds things to watch:
- The average number of rounds. This is the most revealing measure. If it is always 1, the loop contributes nothing and you are paying for complexity you do not use: go back to ordinary RAG. If it is consistently high, the agent is probably going round in circles.
- Routing accuracy. Over a set of questions whose correct source you know, what proportion goes to the right place? This is simple to measure, and it is often where the arrangement loses most.
- End-to-end latency, all searches included, the only duration the user perceives.
- How often the upper rungs are reached. If it is often, the first rung is usually misconfigured: fix that rather than improving the escalation.
- The rate of acknowledged failure. An arrangement that never admits defeat should be suspected of inventing.
When to Use It, and When Not To
Agentic RAG earns its place when the sources are several and of different kinds; when questions genuinely require chained searches, the second depending on the first; when a wrong answer costs more than no answer; and when the questions asked are too varied for one strategy to fit.
Stay with ordinary RAG for a single homogeneous corpus; for high volume on a constrained budget; and when response time is critical, the loop being slower by construction.
The deciding test is a simple one, to run before choosing: take fifty real questions from your users and count those that truly require a second search depending on the first. If the answer is almost none, a well-tuned ordinary RAG will be faster, cheaper and more reliable. Agentic RAG only becomes interesting where the fixed sequence is genuinely the obstacle, not where the chunking or the representation model is the wrong choice, which is the more frequent case.
1. In an agentic RAG system, what does it concretely mean that the agent decides to launch another search?
2. Why is an indexed document more dangerous in an agentic RAG than in an ordinary RAG?
3. How do you effectively protect an agentic RAG system against indirect injection through documents?
4. In the LangGraph loop shown on this page, what guarantees that the arrangement eventually stops?
5. In production, your agentic RAG's average number of loop rounds is 1 on nearly every question. What should you conclude?