Agentic RAG

Retrieve only when it helps — and decide that yourself.

The agent decides autonomously whether, when, and how to retrieve — issuing and reformulating queries, selecting among sources, grading the relevance of returned passages, and iterating or skipping retrieval — instead of unconditionally retrieving once before generation. Named variants: Self-RAG (reflection tokens gate retrieval), Corrective RAG / CRAG (an evaluator scores evidence and triggers re-retrieval or a web fallback), GraphRAG (a knowledge graph for multi-hop questions), and RAPTOR (hierarchical tree-structured retrieval for long material).

In practiceA product-support agent re-queries its policy corpus with a refined term when the first retrieval scores below a relevance threshold, rather than answering from the weak hit.

Level
04 · Production
Subdomain
Tool Integration
Also known as
Agent-driven RAG, Autonomous Retrieval, Adaptive RAG, Self-RAG, Corrective RAG (CRAG), GraphRAG, RAPTOR, RAG-Based Agent
Trade-off
Higher relevance and recall are bought with extra LLM calls, added latency, and a larger failure surface (mis-decided retrieval, query-reformulation loops).
Native in
LangGraph, LlamaIndex
The mechanism

How it works.

Retrieval becomes a decision inside the loop. The agent decides whether to search, judges what came back, and searches again in different words when the passages do not answer the question.

1 · Decide

  1. Read the questionDoes it need outside knowledge?
  2. Answer directlyIf not, retrieval is skipped
  3. Choose a sourceIf so: which corpus or tool

2 · Retrieve and grade

  1. Write a queryNot always the user's wording
  2. RetrievePassages from the chosen source
  3. Grade the relevanceDo they answer the question?
  4. Answer or rewriteGood: answer. Weak: a new query, within a limit
Relevance grading
A check of the retrieved passages against the question before they are used for an answer.
Query rewriting
A second query in other words, built from what the first one failed to find.
Retrieval budget
The maximum number of rounds. Without it, a question the corpus cannot answer loops without end.
Walk it through

A real run, step by step.

TaskA support agent with a policy corpus receives four messages:
Illustrative run.
MessageWhat the agent doesRoundsOutcome
“Thanks, that helped!”Decides that no retrieval is needed0A direct reply
“What is the return period?”Retrieves once; the passages are relevant1An answer with its source
“Can I return a customised item?”The first passages cover returns in general and are graded weak. The query is rewritten to the exclusion for custom-made goods2An answer with its source
“Do you ship to Mars?”Two rounds, nothing relevant2“I could not find this.” No invented answer
What this shows: a fixed retrieve-then-answer pipeline would have searched for the thank-you as well, and it would have answered the third message from the weak general passages. The price is in the extra calls: deciding, grading and rewriting each cost a model call, so the third message took about four times as many calls as a single pass.
In code

The pattern in code.

Plain Pythonpython
def agentic_rag(question, max_rounds=2):
    if not needs_retrieval(question):  # model call: decide first
        return model(question)
    query = question
    for _ in range(max_rounds):
        passages = retrieve(choose_source(query), query)
        # model call: judge before using
        if grade(question, passages) == "relevant":
            return model(question, context=passages, cite=True)
        # model call: try a different wording
        query = rewrite(question, passages)
    return "I could not find this in the available sources."
Fit

Use it, or not.

When to reach for it

  • Some queries need external knowledge and others do not.
  • A single retrieval pass returns noisy or insufficient context.
  • The corpus spans multiple sources or tools that must be chosen per query.
  • Answer quality depends on iterative query refinement.

When it backfires

  • Every query needs the same single lookup — naive RAG is cheaper and more predictable.
  • Latency or cost budgets forbid the extra calls the retrieval decision adds.
  • The relevant knowledge already fits directly in the context window.
Pitfalls

Two ways this pattern will hurt you.

The retrieve-grade-rewrite loop never terminates

When no passage clears the relevance bar, the agent keeps reformulating and re-querying. Without a ceiling it burns tokens and latency chasing context that may not exist in the corpus.

Countermeasure

Bound the loop: cap reformulation rounds, and on exhaustion fall back to answering from what was retrieved (or declaring the gap) rather than looping again.

The relevance grader passes a similar-but-unsupporting passage

A passage scores high on embedding similarity yet does not actually contain the answer. The grader waves it through, and the agent grounds a confident answer on evidence that never supported it.

Countermeasure

Grade for answerability, not similarity: require the passage to actually support the claim (an entailment / does-it-contain-the-answer check), and cross-check across sources before generating.

Threat exposure

What this pattern exposes

Adopting this pattern opens these attack surfaces. Each links to its entry in the threat model.

Full threat model →
Framework support

Where Agentic RAG is native.

LangGraphretrieve → grade → rewrite graph (canonical)Native
LlamaIndexrouter + agentic query enginesNative
OpenAI Agents SDKretrieval exposed as a toolAdaptable
CrewAIas custom retrievalAdaptable
Microsoft Agent Frameworkas custom graphAdaptable

Search

Search patterns, frameworks, and pages.