Sub topic 17
Self-RAG and Corrective RAG in context
This chapter assumes you have read the earlier chapters on the agent loop, retrieval as a tool, and grading results. You should be comfortable with a loop that calls a retriever and a model. Self-RAG and Corrective RAG are names from research papers. Both describe ideas you have already built, packaged as specific recipes.
What the two names mean
Self-RAG (Asai et al., 2023) trains a model to emit special reflection tokens as it writes. These tokens mark whether retrieval is needed, whether a passage is relevant, whether the answer is supported by the passage, and whether the answer is useful. The decisions come from the model's own weights, so the pattern needs a fine-tuned model.
Corrective RAG (CRAG) (Yan et al., 2024) keeps retrieval fixed but adds a lightweight evaluator after it. The evaluator scores the retrieved documents as correct, incorrect, or ambiguous. Each verdict triggers a different action: use the documents, discard them and search elsewhere (often the web), or combine both.
How they map to what you built
| Idea in the paper | Where it appears in this tutorial |
|---|---|
| Self-RAG "retrieve?" token | Letting the model skip retrieval |
| Self-RAG relevance and support tokens | Grading results and grounding answers |
| CRAG evaluator | Result grading step after a search |
| CRAG "incorrect" action | Re-query or route to another source |
| CRAG "ambiguous" action | Merge results from more than one source |
In a prompted agent loop you get the same behavior without training. The model decides whether to call the tool. A grading step, either a second model call or a rule, decides whether to try again.
A corrective step in code
This sketch adds the CRAG idea to the classic pipeline. The functions grade, search and fallback_search are supplied by the caller.
def corrective_answer(question, search, grade, fallback_search, generate, k=4):
chunks = search(question, k)
verdict = grade(question, chunks) # returns "correct", "incorrect" or "ambiguous"
if verdict == "incorrect":
chunks = fallback_search(question, k)
elif verdict == "ambiguous":
chunks = chunks + fallback_search(question, k)
context = "\n\n".join(f"[{c['id']}] {c['text']}" for c in chunks)
prompt = (
"Answer using only the context below. "
"If the context does not contain the answer, say so.\n\n"
f"Context:\n{context}\n\nQuestion: {question}"
)
return generate(prompt)The flow here is fixed by your code. The model does not choose it. That makes it predictable, but it cannot loop more than once.
Choosing between them
- Use the CRAG shape when you want a fixed pipeline with one quality gate and a bounded cost.
- Use an agent loop with a grading tool when questions vary and may need several searches.
- Consider Self-RAG only if you can fine-tune or host a model trained for reflection tokens.
Exercise
A team uses a hosted model they cannot fine-tune. They want retrieval to be checked before generation. Which pattern fits best, and why?
Show answer
Corrective RAG, or an agent loop with a grading step. Both work with a prompted model. Self-RAG needs a model trained to emit reflection tokens, which a hosted model does not provide.