Clinical AI
RAG
Healthcare AI
Retrieve-or-Refuse
Cardiology
Langfuse
Open Source

HeartSafe RAG: Building a Retrieve-or-Refuse Cardiology Agent

January 25, 2026
12 min read
By Mohamed Fakhry, M.B.B.Ch.
HeartSafe RAG: Building a Retrieve-or-Refuse Cardiology Agent

In cardiology, a missed diagnosis can be fatal. In AI, a hallucinated recommendation can be just as dangerous. I built HeartSafe RAG around one design rule: if the guidelines don't support it, the system should not say it.

It is designed not to guess or improvise. It retrieves, verifies, or refuses to answer.

The Problem: When LLMs Play Cardiologist

Large language models are confidently wrong about medicine all the time. They'll cite non-existent studies, invent drug interactions, and deliver fabricated guidelines with the same authoritative tone as verified facts.

In heart failure management, where the difference between a Class I and Class III recommendation can determine whether a patient lives or dies, "confidently wrong" is unacceptable.

I needed a system that would rather say "I don't have guideline support for that question" than hallucinate an answer that sounds plausible but could harm a patient.

The Architecture: Hybrid Retrieval + Strict Enforcement

1. Ingestion Pipeline

The 2022 AHA/ACC Heart Failure Guidelines get parsed, chunked, and embedded into a dual-index system. Every piece of context carries its source lineage, page numbers, section headers, recommendation classes.

2. Hybrid Retrieval: FAISS + BM25

Semantic search alone isn't enough for medical queries. A patient asking about "water pills" needs to match "loop diuretics." A clinician asking about "GDMT" needs exact terminology matching.

HeartSafe combines FAISS vector search for semantic understanding with BM25 keyword matching for precision. The fusion ensures both conceptual relevance and terminological accuracy.

3. The Refusal Mechanism

Here's where most RAG systems fail: they retrieve something and generate something, even when the context doesn't actually support the query. HeartSafe implements strict retrieval enforcement. If the retrieved chunks don't meet relevance thresholds, the model refuses to answer rather than fabricate.

This isn't a limitation. It's the entire point. A doctor who says "I need to look that up" is more trustworthy than one who invents an answer.

4. LLM-as-a-Judge Evaluation

Answers to a 40-question golden dataset run through an automated evaluation pipeline, tracked in Langfuse. The judge scores three dimensions: correctness (does the answer match guideline content?), safety (could this response cause harm?), and adherence (is every claim grounded in retrieved context?).

The Stack

LLM:Llama 3.1 8B (llama-3.1-8b-instant) via Groq at temperature 0, for fast interactive queries
Embeddings:PubMedBERT via sentence-transformers for domain-aware semantic search
Vector Store:FAISS for similarity search
Keyword Index:BM25 for exact terminology matching
Backend:FastAPI with health checks and structured logging
Observability:Langfuse for full trace logging and evaluation tracking

What Retrieve-or-Refuse Does Not Guarantee

Retrieval enforcement and rule-based validation reduce hallucination risk. They do not eliminate it. Retrieved passages can be relevant but incomplete, the model can still misread a passage it was given, and the validation rules only catch the errors they were written to catch.

The 40-question golden dataset and LLM-as-a-Judge scoring measure behavior on those questions, not on every question a clinician might ask. HeartSafe RAG is an open-source research project. It is not a medical device and has not been clinically validated.

Updated November 2026: the stack below reflects the current repository, and this post was retitled to describe the design accurately. The original title promised "zero hallucinations", which no retrieval system can guarantee.

Why This Matters

HeartSafe RAG isn't just a technical exercise. It's a template for how clinical AI should work: evidence-first, transparent about limitations, and auditable at every step.

The repo is open source. The architecture is documented. The evaluation pipeline is reproducible. Because if we're going to build AI systems that touch patient care, we need to show our work.

The Full Repository is Live

Clone it. Break it. Improve it. HeartSafe RAG is open source because clinical AI needs more eyes, not fewer.

Building clinical AI that has to be right?

I'm Mohamed Fakhry, a physician-engineer (M.B.B.Ch., Cairo University) working on clinical AI architecture, engineering and reliability. I help HealthTech teams find and fix reliability problems in RAG, evaluation, and safety before they reach clinicians.

Mohamed Fakhry

Physician-Engineer working on clinical AI architecture, engineering and reliability, and on healthcare software that has to hold up in real-world use.

Physician-Engineer (M.B.B.Ch.) • Clinical AI Engineering • Former CEO

Connect

Building reliable, production-grade healthcare AI systems.

© 2026 Mohamed Fakhry. All rights reserved.

Built with care using modern web technologies