Corrective RAG (CRAG)
- Naive RAG retrieves docs and answers blindly β even when the retrieved docs are irrelevant, the LLM hallucinates a plausible-sounding answer.
- Corrective RAG (CRAG) adds a grading step after retrieval: an LLM judges whether each doc is relevant. If not β rewrite the query or fall back to web search.
- Build it in LangGraph with four nodes:
retrieve β grade_documents β (web_search | generate)connected by a conditional edge.
Naive RAG has a dirty secret: it answers confidently even when the retrieved context is garbage. The user asks about a topic that isn't in your vector store, the retriever returns the least-irrelevant chunks, and the LLM weaves them into a hallucinated answer that sounds perfect. Corrective RAG fixes this by adding a self-check loop β grade the docs before you generate. One submodule per idea, ending with a cheat sheet.
Why naive RAG failsβ
The retriever always returns something. Cosine similarity doesn't know "none of these are relevant" β it just ranks. So you get three failure modes:
- Irrelevant retrieval β the top-k docs don't actually answer the question, but the LLM generates from them anyway.
- Partial retrieval β some docs are relevant, some aren't, and the LLM mixes them together indiscriminately.
- Out-of-scope queries β the question is outside the knowledge base entirely, but the system still produces an answer instead of saying "I don't know."
CRAG addresses all three by inserting a relevance gate between retrieval and generation.
The CRAG paper's key ideaβ
The Corrective Retrieval Augmented Generation paper (Yan et al., 2024) proposes a simple but effective pipeline:
- Retrieve documents as usual.
- Grade each document for relevance to the query (using a lightweight evaluator).
- Decide:
- If docs are relevant β proceed to generate.
- If docs are ambiguous β refine the query and re-retrieve.
- If docs are irrelevant β fall back to web search for fresh context.
- Generate the answer from the surviving (or newly fetched) context.
The key insight: retrieval quality is not guaranteed, so you must check it before trusting it.
Building CRAG in LangGraphβ
We'll build this as a four-node graph with a conditional edge after grading.