Skip to content
HS®
All projects

(005) / 012

Self-HealingRAG

A RAG pipeline that audits its own answers claim by claim, retries with a reformulated query, and falls back honestly instead of inventing facts.

Category
Retrieval-augmented generation
Year
2026
Last pushed
Aug 2026
Primary language
Python
RETRIEVEGENERATECRITICRETRY ≤ 2ACCEPT / FALLBACK
Conceptual diagram — retrieve, generate, critique, and loop back when a claim is unsupported.

(01) Overview

A retrieval-augmented generation system that does more than retrieve documents and generate an answer. Every answer is audited by a critic for grounding in the retrieved chunks. Rejected answers trigger a bounded query-reformulation loop, and when the retry limit is reached the graph returns an evidence-limited fallback instead of inventing facts.

(02) Problem statement

A plain RAG pipeline is a straight line: retrieve, generate, return. If retrieval misses, the model often fills the gap with something plausible. Nothing in the pipeline notices, and the user cannot tell a grounded answer from an invented one.

(03) Technical approach

Turn the line into a cycle with a judge in it. After generation, a critic checks each claim against the retrieved evidence and routes the run one of three ways:

  • all claims grounded → accept
  • a claim unsupported and retries remain → reformulate and retrieve again
  • a claim unsupported and retries exhausted → fallback

The retry cap makes the loop bounded, so the system always terminates.

(04) System architecture

The workflow is a LangGraph state graph over a single RAGState object.

The cyclic graphConceptual diagram · select a stage

Loop: Unsupported claim and retries remain → Reformulate → Retrieve

Stage 01 — Question

RAGState carries the question, current query, chunks, answer, verdict, retry counters and trace.

(05) Implementation details

Documents are ingested into a local, persisted Chroma vector store. Generation uses llama-3.1-8b-instant through Groq's OpenAI-compatible API. The state carries the original question, the current query, retrieved chunks, the generated answer, the critic's verdict and reasoning, the list of unsupported claims, retry controls, the final answer and a trace of every node visited.

Runs can be streamed as a colour-coded, node-by-node trace. For a question the corpus covers, the path is retrieve → generate → critic → accept with zero retries. For one it does not cover, the critic forces a reformulation before the graph accepts an answer that says the context does not contain the information.

(06) Main features

  1. 01Retrieves the top four chunks from a persisted Chroma vector store.
  2. 02A critic audits each generated claim against the retrieved evidence.
  3. 03Rejected answers trigger critic-suggested query reformulation, up to a retry cap (default two).
  4. 04When the cap is reached the graph returns an evidence-limited fallback.
  5. 05Colour-coded node-by-node trace of every run.
  6. 06Scripts comparing plain RAG with the self-healing graph and reporting retry and fallback metrics.

(07) Technology stack

PythonLangGraphLangChainChromaDBSentence-TransformersGroq (llama-3.1-8b-instant)pypdf

(08) Engineering challenges

  • Writing a critic that is strict but fair. The critic experiments (04_critic.py) are among the largest files in the project; a critic that rejects everything is as useless as one that accepts everything.
  • Knowing when to stop. Evaluation scripts compare retry caps and reformulation strategies rather than assuming more retries are better.
  • An honest failure mode. The fallback states what the evidence does not cover instead of producing a best guess.

(09) Evaluation and results

The repository includes scripts that compare plain RAG with the self-healing graph and report retry, fallback and answer-change metrics. Their output files are generated locally and are not committed, so no figures are quoted here.

Links