Corrective, Self and Adaptive RAG

Study RAG systems that evaluate and adapt their own retrieval: corrective RAG, self-reflective RAG, adaptive routing, quality checks and retry loops.

25 minAdvancedCode Examples
The Core Idea: A standard RAG pipeline never asks whether retrieval worked. It passes along whatever it found and trusts the model. Corrective, self-reflective and adaptive RAG add judgment to the loop: grade what was retrieved, repair or replace it when it is poor, decide how much retrieval a question needs, and check the draft answer before showing it.

1. Intermediate: RAG That Checks Itself

In a plain pipeline, a bad retrieval produces a bad answer, silently. The ideas in this lesson add three kinds of control.
Corrective
Grade the retrieved passages. If they are poor, fix the retrieval or look somewhere else.
Self-reflective
The system critiques its own retrieval decisions and its own draft answer, and retries if the critique fails.
Adaptive
Choose the amount of retrieval effort per question: none, one search, or several steps.
Shared principle: put a checkpoint where a silent failure could happen, and give the system an alternative action when the check fails.

2. Intermediate: Corrective RAG (CRAG)

Corrective RAG adds a retrieval evaluator between retrieval and generation. It scores how relevant the retrieved passages are to the question and sorts the result into one of three cases, each with its own action.
VerdictMeaningAction
CorrectThe passages clearly answer the questionUse them, optionally trimming irrelevant parts
AmbiguousPartly relevant, not convincingUse with caution, or combine with another source
IncorrectThe passages do not helpDiscard them and fall back, for example to another index or a web search
The evaluator can be a small trained model, a reranker score with thresholds, or a language model prompt. Its decisions are only as good as its calibration, so thresholds must be tuned on labeled examples.

3. Intermediate: Self-RAG

Self-RAG is a research approach in which the language model itself learns to make retrieval decisions and to critique them. Instead of always retrieving, it decides whether retrieval is needed. After retrieval it judges whether each passage is relevant, and after writing it judges whether its answer is supported by the passages and useful.
In practice you can imitate this behavior with prompts and checks around a standard model: ask whether retrieval is needed, grade passages, draft, then verify. The trained version builds these judgments into the model, which can make them cheaper and more consistent.
Self-reflection loop: draft the answer, check that each claim is supported by the context, and if not, retry with a different query or different passages, up to a fixed number of attempts.

4. Intermediate: Adaptive RAG

Adaptive RAG classifies the incoming question and routes it to the cheapest strategy that is likely to work.
Question typeExampleStrategy
Needs no knowledge baseHello, or a general language questionAnswer directly without retrieval
Simple factualWhat is the refund policy?One retrieval step
Complex or multi-hopWho manages the team that owns billing?Several retrieval steps
The router can be rules, a small classifier or a language model prompt. Routing saves money and time on easy questions and spends effort only where it helps. A wrong route is costly, though: sending a hard question down the cheap path produces a confident but incomplete answer.

5. Advanced: Loops, Limits and the Cost of Judgment

Checks and retries make a system more careful, but they have costs and failure modes of their own.
Latency and price
Every grader call, retry or fallback search adds time and money. Set a hard limit on attempts.
Grader errors
A grader that is too strict makes the system abstain on answerable questions. One that is too lenient lets weak evidence through. Measure both.
Fallback risk
A web fallback brings text you do not control. Treat it as untrusted and label its source.
Define what counts as correct, ambiguous and incorrect, with thresholds tuned on labeled data.
Cap the number of retries and fallbacks per question.
Log every verdict and action so you can audit decisions.
Evaluate end-to-end quality and cost with and without the checks.

6. Visual Explanation: Grade, Route and Retry

Panel A shows the corrective decision paths. Panel B shows how the code below handled five questions. Panel C is a failure explorer for the grader.
Panel A · Three Verdicts, Three ActionsThe retrieval evaluator decides
Correct
Score high
Passages answer the question
↓
Generate from them
Ambiguous
Score medium
Partly relevant
↓
Use with a warning or add a source
Incorrect
Score low
Passages do not help
↓
Fall back or abstain
Panel B · Five QuestionsFrom the code in section 7
QuestionRouteGradeOutcome
hellono retrievalnonedirect reply
what is the refund policysinglecorrectanswered from the index
how fast is express shippingsingleambiguousanswer marked low confidence
when did the Atlas laptop launchsingleincorrectweb fallback found it
who won the matchsingleincorrecthonest abstention
Panel C · Grader FailuresClick a failure to open it
Click to see why abstaining is a valid outcome▼
A system that says it could not find a reliable source is more trustworthy than one that always answers. Corrective designs treat honest refusal as a correct action.
How to read it: Panel A is the rule, Panel B shows it in action, and Panel C reminds you that every judge can be wrong.

7. Python Implementation: Routing, Grading, Fallback and a Retry Check

The program has an internal knowledge base and a separate set of fallback documents standing in for a web search. A router sends greetings straight to a reply, and everything else to retrieval. A grader turns the best match score into correct, ambiguous or incorrect, and each verdict triggers a different action: use the passage, warn about low confidence, fall back to the second source, or abstain. The last lines show a self-check: a draft answer is verified against the evidence, and a failing draft triggers a retry.
corrective_rag.pyPython 3.10+ · Standard library only
internal = [    "Refunds are issued within 5 business days of an approved return.",    "Standard shipping takes 5 to 7 business days.",]web = [    "The Atlas laptop launched in October 2026 with 32 GB of memory.",]stop_words = set(["the", "is", "are", "a", "what", "when", "did", "of", "for", "an", "about", "me", "tell"]) def stem(word):    return word[:-1] if word.endswith("s") and len(word) > 3 else word def words(text):    cleaned = "".join(ch if ch.isalnum() or ch == " " else " " for ch in text.lower()).split()    return set(stem(w) for w in cleaned if w not in stop_words) def best(query, docs):    q = words(query)    scored = [(len(q & words(d)) / len(q), d) for d in docs]    return max(scored) def route(query):    greetings = ["hello", "hi", "thanks"]    if query.lower().strip(" ?!") in greetings:        return "no retrieval"    if " and " in query.lower():        return "multi-step"    return "single retrieval" def grade(score):    if score >= 0.5:        return "correct"    if score >= 0.25:        return "ambiguous"    return "incorrect" def answer(query):    path = route(query)    if path == "no retrieval":        return path, "-", "Hello! How can I help?"    score, doc = best(query, internal)    verdict = grade(score)    if verdict == "correct":        return path, verdict, doc    if verdict == "ambiguous":        return path, verdict, doc + " (low confidence, please verify)"    web_score, web_doc = best(query, web)    if web_score >= 0.25:        return path, verdict + " -> web fallback", web_doc    return path, verdict + " -> abstain", "I could not find a reliable source." for q in ["hello", "what is the refund policy", "how fast is express shipping", "when did the Atlas laptop launch", "who won the match"]:    path, verdict, text = answer(q)    print("Q:", q)    print("   route:", path, "| grade:", verdict)    print("   answer:", text) def supported(claim, evidence):    c = words(claim)    return len(c & words(evidence)) / len(c) >= 0.6 print("")evidence = internal[0]drafts = ["Refunds are instant.", "Refunds are issued within 5 business days."]for attempt, claim in enumerate(drafts, start=1):    ok = supported(claim, evidence)    print("Draft", attempt, "supported:", ok, "->", claim)    if ok:        break
Expected output
Q: hello   route: no retrieval | grade: -   answer: Hello! How can I help?Q: what is the refund policy   route: single retrieval | grade: correct   answer: Refunds are issued within 5 business days of an approved return.Q: how fast is express shipping   route: single retrieval | grade: ambiguous   answer: Standard shipping takes 5 to 7 business days. (low confidence, please verify)Q: when did the Atlas laptop launch   route: single retrieval | grade: incorrect -> web fallback   answer: The Atlas laptop launched in October 2026 with 32 GB of memory.Q: who won the match   route: single retrieval | grade: incorrect -> abstain   answer: I could not find a reliable source. Draft 1 supported: False -> Refunds are instant.Draft 2 supported: True -> Refunds are issued within 5 business days.
The refund question is graded correct. The express shipping question only partly matches, so the answer is returned with a low-confidence warning. The question about the Atlas laptop has no internal match, so the system falls back to the second source. The sports question has no match anywhere and gets an honest refusal. In the self-check, the first draft says refunds are instant, which the evidence does not support, so the loop rejects it and accepts the second draft. A real system uses models for grading and drafting, but the control flow is the same.

Knowledge Check

Q1. What does the retrieval evaluator in corrective RAG decide?
Q2. What is the purpose of adaptive RAG routing?
Q3. Why should a retry loop have a hard limit?

Key Points

✓
Plain pipelines never check retrieval quality; corrective, self-reflective and adaptive designs add checkpoints.
✓
Corrective RAG grades retrieval as correct, ambiguous or incorrect and takes a different action for each.
✓
Self-RAG makes the model decide when to retrieve and critique passages and its own output.
✓
Adaptive RAG routes questions to no retrieval, one step or several steps by complexity.
✓
Self-reflection loops draft, verify support, and retry within a fixed attempt limit.
✓
Graders and routers can be wrong in both directions, so tune thresholds on labeled data and log every decision.
✓
Honest abstention is a valid and valuable outcome.

The Big Picture

These designs move RAG from a pipeline that hopes to a system that checks. The checks are not free, and they are only as good as their judges, but they are what lets a system recover from bad retrieval or refuse politely instead of bluffing.
Route the question → Retrieve → Grade the evidence → Repair or fall back → Verify the answer
Up Next: Specialized RAG Architectures. You will explore hierarchical RAG, graph RAG, knowledge graphs, multi-document and multi-source designs, and RAG over structured data with SQL.