Zero Hallucinations RAG: A Guide to Fact-Checking LLMs

Aug 7, 2026

Introduction

The promise of Retrieval-Augmented Generation (RAG) is to ground Large Language Models (LLMs) in factual data, yet the persistence of AI hallucinations remains a critical barrier to their enterprise adoption. Models that confidently invent facts are not just unhelpful; they are a liability. To address this, a new wave of research focuses on making RAG systems verifiable and trustworthy. The open-source project “Zero Hallucinations RAG” by Fareed Khan provides a clear, practical Python implementation of one such powerful technique, demonstrating a three-step pipeline to ensure every claim an LLM makes is backed by evidence.

What Is Zero Hallucinations RAG?

Zero Hallucinations RAG is an open-source project that implements the methodology from the academic paper “Zero Hallucinations in RAG” to create a fact-checking layer for LLM-generated answers. Written as a Python-based Jupyter Notebook, this repository provides a hands-on guide to building a RAG system that actively verifies its own output against the provided source documents. The core of the project is a distinct three-step process: Generation, Extraction, and Verification. This pipeline forces the LLM to first generate an answer, then deconstruct that answer into individual factual claims, and finally, check each of those claims against the original context, effectively eliminating unsupported statements.

Why This Project Matters

While RAG was designed to solve the hallucination problem, studies have shown that it can still produce significant factual errors, sometimes with even greater confidence than a non-RAG model. This project matters because it moves beyond simple retrieval and generation, tackling the crucial “last mile” problem of AI trust: verification. Before techniques like this, developers had to choose between blindly trusting the LLM’s output or implementing complex, ad-hoc validation logic.

This repository provides a clear, repeatable, and research-backed pattern for enforcing truthfulness. By implementing the “Generate, Extract, Verify” loop, it provides a blueprint for building RAG systems that are not just powerful, but also auditable and safe for high-stakes applications in fields like medicine, finance, and law, where factual accuracy is non-negotiable. It demonstrates that achieving near-zero hallucination is an engineering challenge that can be solved with a systematic, multi-layered defense.

Key Features: The Three-Step Pipeline

The entire project revolves around a simple but powerful three-stage process to ensure responses are grounded in facts. Every step is clearly implemented in the accompanying Jupyter Notebook.

  • Step 1: Generation: This is the standard RAG process. The system takes a user query, retrieves relevant document chunks from a knowledge base, and then feeds this context along with the query to an LLM (in this case, Llama3 via the Groq API) to generate an initial, conversational answer.
  • Step 2: Extraction: This is the first critical innovation. The generated answer from Step 1 is fed back into the LLM with a new, specific prompt that instructs it to act as a “fact-checker.” Its only job is to break down the conversational answer into a list of discrete, verifiable factual claims. For example, a sentence like “The capital of France is Paris, which is known for the Eiffel Tower” would be extracted into two separate claims: “The capital of France is Paris” and “Paris is known for the Eiffel Tower.”
  • Step 3: Verification: In the final and most important step, each individual claim extracted in Step 2 is checked against the original source documents retrieved in Step 1. The LLM is prompted one last time for each claim, asking it to assign a verdict: “Supported,” “Not Supported,” or “No Information.” Only the claims that are explicitly supported by the source documents are kept, forming the final, verified answer.

How This Technique Compares

The “Generate, Extract, Verify” method is a specific technique for run-time hallucination prevention. It fits into a broader ecosystem of tools and methods aimed at improving RAG reliability.

Approach Zero Hallucinations RAG Advanced Retrieval (Reranking) RAG Evaluation Frameworks (Ragas, OpenLIT)
Purpose Run-time answer verification Improve context quality *before* generation Post-hoc evaluation and testing of a RAG pipeline
Stage of Application After initial generation During retrieval In CI/CD, testing, and monitoring
Primary Output A filtered list of factually supported claims A highly relevant, ordered list of context documents Metrics (e.g., faithfulness, answer relevancy)
Core Idea Don’t trust the LLM; make it show its work and verify each step. Better context in leads to better answers out. Systematically measure and score the quality of the RAG system.

vs. Advanced Retrieval: Techniques like using a reranker (as this project does with Cohere’s model) or query transformations aim to improve the quality of the context given to the LLM. The philosophy is that better input will lead to better output. The Zero Hallucinations technique is complementary; it acts as a final safety net, assuming that even with perfect context, the LLM might still make mistakes or misinterpret the information.

vs. RAG Evaluation Frameworks: Tools like Ragas or OpenLIT are designed to test a RAG pipeline by running it against a dataset and generating scores for metrics like “faithfulness” (the opposite of hallucination). These are crucial for CI/CD and system-level monitoring. The Zero Hallucinations technique, however, is a run-time component designed to prevent the hallucination from reaching the user in the first place, rather than just detecting it during testing.

Getting Started: Installation

This project is designed as a self-contained demonstration. To run it, you’ll need to clone the repository and install the necessary Python dependencies.

Prerequisites

  • Python 3.x
  • An API key from Groq for the LLM.
  • An API key from Cohere for the reranker.
  • An API key from an embedding provider (the notebook uses Cohere).

Installation Steps

First, clone the repository from GitHub and navigate into the project directory.

git clone https://github.com/FareedKhan-dev/rag-zero-hallucinations.git
cd rag-zero-hallucinations

Next, install the required Python libraries using the `requirements.txt` file.

pip install -r requirements.txt

Finally, create a `.env` file in the root of the project directory and add your API keys. This file is used to securely load your credentials into the notebook environment.

GROQ_API_KEY="your_groq_api_key_here"
COHERE_API_KEY="your_cohere_api_key_here"

How to Use the Project

The entire implementation is contained within the `Zero_Hallucinations.ipynb` Jupyter Notebook. The intended workflow is to open this notebook and execute the cells sequentially to understand how each part of the pipeline works.

The notebook is structured logically. It begins by loading the necessary libraries and environment variables. It then defines the data and performs the initial retrieval and reranking steps to gather context. After that, it provides separate, clearly-defined Python functions for each of the three main stages: `generation`, `extraction`, and `verification`. By running these cells one by one, you can inspect the output of each stage—seeing the initial conversational answer, the extracted list of facts, and the final list of verified claims—to gain a practical understanding of the entire process.

Code Examples

The core logic is encapsulated in three functions within the notebook. Below are conceptual representations of their roles.

The `generation` Function

This function takes the user’s question and the retrieved documents as input. It formats them into a prompt for a powerful generator model like Llama 3 and returns the initial, possibly unverified, answer.

def generation(question: str, context: str) -> str:
    # ... prompt formatting ...
    llm = ChatGroq(model_name="llama3-8b-8192")
    # ... invoke LLM and return response ...
    return initial_answer

The `extraction` Function

This function receives the initial answer from the `generation` step. It uses a specific prompt that instructs the LLM to act as a fact-checker and output a JSON list of all factual statements made in the answer.

def extraction(answer: str) -> list:
    # ... fact-extraction prompt formatting ...
    llm = ChatGroq(model_name="llama3-8b-8192")
    # ... invoke LLM and parse JSON output ...
    return list_of_claims

The `verification` Function

This function iterates through each claim from the `extraction` step. For each claim, it makes a final call to the LLM, providing the claim and the original source documents, and asks for a simple verdict: “Supported,” “Not Supported,” or “No Information.”

def verification(claims: list, context: str) -> list:
    verified_claims = []
    for claim in claims:
        # ... verification prompt formatting ...
        llm = ChatGroq(model_name="llama3-8b-8192")
        verdict = llm.invoke(prompt).content
        if verdict == "Supported":
            verified_claims.append(claim)
    return verified_claims

Real-World Use Cases

  • Enterprise Knowledge Bases: An employee asking a question about an internal policy document needs a factually correct answer, not a plausible-sounding guess. This technique ensures the RAG system only provides information that is explicitly stated in the documentation.
  • Legal and Compliance: A paralegal using a RAG system to summarize case law cannot afford to rely on hallucinated details. The verification step ensures that every claim about a legal precedent is directly traceable to the source text.
  • Medical Information Systems: For chatbots providing information to patients or clinicians based on medical literature, preventing hallucinations is a matter of safety. This pipeline provides a critical safety layer to filter out any unverified statements.
  • Financial Analysis: An analyst using a RAG system to query financial reports needs precise, verifiable data. The verification process can confirm that figures and statements in the generated summary are directly supported by the quarterly reports.

Contributing and Community

As this project is a research implementation, it doesn’t have a formal contribution guide or a dedicated community channel. It serves primarily as a public demonstration of a specific technique. Those interested in contributing or asking questions should use the standard GitHub channels, such as opening an Issue or a Pull Request on the repository. The project’s value lies in its clear implementation, providing a foundation that other developers can adopt and build upon in their own applications.

Conclusion

The “Zero Hallucinations RAG” project is an invaluable resource for any developer serious about building trustworthy AI systems. It moves the conversation about hallucinations from a theoretical problem to a practical engineering challenge with a clear, implementable solution. By providing the complete code for the “Generate, Extract, Verify” pipeline, Fareed Khan has offered a powerful pattern that can be adapted and integrated into production-grade RAG applications.

While no system can guarantee absolute zero hallucinations in every scenario, this multi-layered approach makes them significantly harder to produce and easier to detect. If you are building a RAG system where factual accuracy is paramount, exploring the notebook in this repository is an essential step towards creating a system that users can truly trust.

Resources

What is RAG hallucination?

RAG hallucination occurs when a Retrieval-Augmented Generation system produces an answer that contains factual inaccuracies or information not supported by the retrieved source documents. Even though RAG is designed to ground the LLM in provided context, the model can still misinterpret the text, blend facts from different sources incorrectly, or invent details altogether.

What is the core idea behind the Zero Hallucinations RAG technique?

The core idea is to not blindly trust the initial output of the LLM. It enforces a self-verification loop where the system generates an answer, extracts the factual claims from its own answer, and then rigorously checks each claim against the original source documents to filter out any unsupported information before presenting the final result.

Is this a production-ready library?

No, this project is not a library intended for direct import. It is an educational implementation in a Jupyter Notebook designed to demonstrate the “Generate, Extract, Verify” technique. Developers are meant to learn from this pattern and adapt the logic into their own production applications, potentially using frameworks like LangChain or LlamaIndex.

How does this approach impact latency?

This technique will inherently increase the latency of a response. It requires multiple sequential calls to an LLM (one for generation, one for extraction, and then one for each extracted claim during verification). This trade-off of speed for accuracy makes it most suitable for applications where factual correctness is more critical than instantaneous response times.

What LLMs are used in this project?

The Jupyter Notebook implementation uses Llama3-8b-8192 hosted on the Groq API for the core generation, extraction, and verification tasks. It also utilizes models from Cohere for embedding and reranking the retrieved documents to improve context quality before the generation step.

Can I use this technique with my own RAG pipeline?

Yes. The principles and code patterns in this repository are designed to be adaptable. You can integrate the extract and verify steps into any existing RAG pipeline, regardless of the specific vector database, retriever, or generator model you are using.

How does this differ from just prompting the LLM to cite its sources?

Simply prompting an LLM to add citations is a good first step, but it is not foolproof, as models can sometimes fabricate citations or cite the wrong source. The Zero Hallucinations RAG technique is more robust because it programmatically extracts each claim and forces a separate, explicit verification check for each one against the actual text, providing a much higher degree of confidence in the final output.