Module 4 · Next-generation RAG architectures
Self-correcting RAG: CRAG and Self-RAG
5 min read
A naive RAG trusts its search: whatever it returns, it generates an answer. When the passages are off-topic, the model fills the gap with plausible generalities; when they are relevant, it sometimes adds details that are not there. Self-corrective techniques add checkpoints: grade the passages before generating, verify the answer afterwards. This lesson presents CRAG and Self-RAG, and their simplified version in Prism.
Two families of checks
| Before generation | After generation | |
|---|---|---|
| Question | Do the retrieved passages answer the question? | Is the answer supported by the passages? |
| Technique | CRAG (Corrective RAG) | Self-RAG-style verification |
| Action on failure | Search differently, or admit not knowing | Regenerate more strictly, or remove unsupported claims |
| Failures addressed | 1 (missing content), 2 (not retrieved) | 4 (badly used), hallucinations |
CRAG: grading the passages
In the original paper, a lightweight evaluator classifies the retrieved documents; depending on the verdict, they are used, supplemented by a web search, or replaced. Prism keeps the essence with an LLM as the evaluator, with structured output:
class Grade(BaseModel):
verdict: Literal["pertinent", "partiel", "hors_sujet"] = Field(
description="pertinent : répond à la question ; partiel : utile mais incomplet ; "
"hors_sujet : inutile")
The Literal forces one of three values (relevant, partial, off-topic); the descriptions specify the meaning of each. The logic:
hits = retriever.search(question, k)
kept = [h for h in hits if grade(question, h) != "hors_sujet"]
actions.append(f"évaluation : {len(kept)}/{len(hits)} passages gardés")
if not kept:
rewritten = query.rewrite(question)
actions.append(f"reformulation : {rewritten}")
hits = retriever.search(rewritten, k)
kept = [h for h in hits if grade(question, h) != "hors_sujet"]
actions.append(f"nouvelle évaluation : {len(kept)}/{len(hits)} passages gardés")
if not kept:
return CorrectiveResult("Je ne trouve pas cette information dans la base.", [], actions)
Three benefits: off-topic passages no longer pollute the context (failure 4); a missed search gets a second chance with a rewritten query; and when nothing is relevant, the system admits it instead of inventing (failure 1). On the out-of-scope question « Vendez-vous des imprimantes 3D ? » (do you sell 3D printers?), the offline trace shows exactly this path — grading keeps 0 of 5 passages, rewriting, grading again keeps 0 of 5, and the answer is "I cannot find this information in the database":
actions : ['évaluation : 0/5 passages gardés', 'reformulation : vendez imprim 3d',
'nouvelle évaluation : 0/5 passages gardés']
réponse : Je ne trouve pas cette information dans la base.
The actions list is not decorative: it is the trace that explains, after the fact, why the system answered the way it did (lesson 5.4).
Self-RAG: verifying the answer
Self-RAG, in its original form, trains a model to emit reflection tokens (should I search? is this passage relevant? is my sentence supported?). Without training a model, you can keep its most useful idea: a check after generation.
answer, sources = answer_with_citations(question, kept)
context = "\n".join(f"[{h.chunk.article_id}] {h.chunk.text}" for h in kept)
check = llm.structured("support_check", SupportCheck,
f"Extraits :\n{context}\n\nRéponse à vérifier :\n{answer}")
if not check.supported:
actions.append(f"affirmations non soutenues : {check.unsupported} → régénération stricte")
answer, sources = answer_with_citations(question, kept, strict=True)
The check lists the unsupported claims; on failure, the answer is regenerated with a stricter instruction. A cheaper alternative is to remove the unsupported sentences rather than regenerate everything.
Citations, a free check
Generation asks for a citation after each piece of information, and Prism only keeps the citations actually present in the context:
provided = {h.chunk.article_id for h in hits}
cited = [c for c in dict.fromkeys(CITATION.findall(answer)) if c in provided]
A citation to an article that was not provided is a hallucination signal; an answer with no citation at all, a signal of an answer "from memory". These two checks cost nothing and can be monitored in production.
The cost of checks
| Check | Additional calls per question |
|---|---|
| Grading the passages (one call per passage) | k (5 here) |
| Rewrite + second grading, if needed | 1 + k |
| Verifying the answer | 1 |
| Strict regeneration, if needed | 1 |
Grading each passage separately is simple but expensive. Two common optimizations: grade all passages in a single call (a list of verdicts as structured output), and assign grading to a small fast model. A third builds on lesson 3.2: the re-ranker's score, once calibrated, often replaces LLM grading to decide whether the search failed.
As with query transformations, these checks are justified by a measured error rate. On customer support where a wrong answer has consequences (a refund wrongly promised), verification is well worth its cost; on an internal document search engine, a citation displayed and checkable by the user is often enough.
Key takeaways
- CRAG grades passages before generating: it drops off-topic ones, searches differently, or admits not knowing.
- Self-RAG-style verification checks that every claim in the answer is supported by the passages.
- Citations, checked against the context provided, are a free check worth monitoring.
- These checks multiply calls: batched grading, a small model, or the re-ranker's score.
- Trace every corrective action: it is what explains an answer after the fact.
Practice
- Run
examples/05_agentic_and_corrective.pyoffline and read CRAG's actions for each question. - Rewrite
gradeto grade the five passages in a single call, with a list of verdicts as structured output. - Replace LLM grading with a threshold on the re-ranker's score, and compare, over the 45 questions, the number of justified and unjustified "I don't know" answers.