Corrective RAG: grade the retrieval, search again if it is weak
Plain RAG trusts whatever comes back. Corrective RAG checks it first, and if the evidence is weak it searches again before the model writes anything.
Interactive simulation. It needs JavaScript; the text below explains what it shows.
What you are looking at
Step 1 retrieves, step 2 grades each chunk with an evaluator score, step 3 decides whether to search again with rewritten queries, step 4 keeps only the useful sentences. All RAG simulations share one knowledge base of 18 chunks from 11 documents of a fictional outdoor-gear shop, and three questions: one phrased differently from the documents, one that needs two documents, and one about the whole corpus.
Things to try
- Run all three questions and see when the second search triggers.
- Compare the grades with the 'relevant' marks in the knowledge base.
- Consider the cost: one grading call, and sometimes one more search.
How it works in real systems
CRAG (Yan et al., 2024) adds a lightweight retrieval evaluator and a web-search fallback. In LangGraph it is a grading node with a conditional edge back to retrieval, as in the agent guide on this site.
Code lab
The same idea in real code, step by step. Copy each block into a notebook or a file and run it in order.
Step 1 · Index the knowledge base
Install the libraries and index a small knowledge base with a sentence-embedding model. Normalised embeddings make the dot product equal to cosine similarity.
pip install sentence-transformers rank-bm25 numpy openai
import numpy as np
from sentence_transformers import SentenceTransformer
chunks = [
"Most items can be returned within 30 days of delivery for a full refund.",
"Damaged or defective items qualify for a full refund or a free replacement; send a photo of the damage to support first.",
"Refunds are issued within five business days after the returned item reaches our warehouse.",
"Parcels damaged in transit must be reported to the carrier within 48 hours of delivery.",
"The Ridge 2 tent weighs 1.9 kg and uses Aurora ripstop fabric for its fly sheet.",
"Aurora ripstop fabric is woven by Lumen Textiles at its mill in Porto, Portugal.",
"Customer complaints in Q3 were mostly about late deliveries during the September peak.",
]
emb = SentenceTransformer("all-MiniLM-L6-v2") # 384 dimensions, small and fast
E = emb.encode(chunks, normalize_embeddings=True)
def dense_search(q, k=3):
s = E @ emb.encode([q], normalize_embeddings=True)[0]
top = np.argsort(-s)[:k]
return [(int(i), float(s[i])) for i in top]
Step 2 · A retrieval grader
A small helper to call a chat model (any OpenAI-compatible endpoint works, including a local server):
from openai import OpenAI
client = OpenAI() # reads OPENAI_API_KEY
def ask(prompt, model="gpt-4o-mini"):
r = client.chat.completions.create(model=model, messages=[{"role": "user", "content": prompt}], temperature=0)
return r.choices[0].message.content
def grade(q, passage):
r = ask(f"Does this passage help answer the question? Reply with exactly one word: correct, ambiguous or incorrect.\nQuestion: {q}\nPassage: {passage}")
return r.strip().lower().split()[0]
Step 3 · The corrective loop in LangGraph
Retrieve, grade, and if too little is relevant, rewrite the query and search again (or fall back to web search) before generating.
# pip install langgraph
from typing import TypedDict
from langgraph.graph import StateGraph, START, END
class S(TypedDict):
question: str; query: str; docs: list; tries: int; answer: str
def retrieve(s): return {"docs": [i for i, _ in dense_search(s["query"], 3)]}
def grade_docs(s): return {"docs": [i for i in s["docs"] if grade(s["question"], chunks[i]) == "correct"]}
def rewrite(s): return {"query": ask(f"Rewrite as a search query using policy wording: {s['question']}"), "tries": s["tries"] + 1}
def generate(s): return {"answer": answer(s["question"], s["docs"])}
def decide(s): return "generate" if len(s["docs"]) >= 1 or s["tries"] >= 2 else "rewrite"
g = StateGraph(S)
for n, f in [("retrieve", retrieve), ("grade", grade_docs), ("rewrite", rewrite), ("generate", generate)]: g.add_node(n, f)
g.add_edge(START, "retrieve"); g.add_edge("retrieve", "grade")
g.add_conditional_edges("grade", decide, {"generate": "generate", "rewrite": "rewrite"})
g.add_edge("rewrite", "retrieve"); g.add_edge("generate", END)
app = g.compile()
Step 4 · Run it
Put the retrieved chunks in the prompt, numbered, and ask for an answer that cites them and admits when the context is not enough.
def answer(question, ids):
ctx = "\n".join(f"[{n+1}] {chunks[i]}" for n, i in enumerate(ids))
return ask(f"Answer using only the context. Cite sources like [1]. If the context is not enough, say you don't know.\n\nContext:\n{ctx}\n\nQuestion: {question}")
q = "Can I get my money back if my tent arrived torn?"
print(app.invoke({"question": q, "query": q, "docs": [], "tries": 0, "answer": ""})["answer"])