DEV Community

Uglypear Data
Uglypear Data

Posted on Originally published at uglypear.com

Building a Private RAG Pipeline That Doesn't Leak Patient Data


If you have ever wired up a retrieval-augmented generation system for a normal company, the healthcare version will feel familiar and then suddenly will not. The retrieval math is the same. The constraints are a different sport. This is a walk through how a private RAG pipeline for medical documents tends to get built, and where the engineering effort actually has to go — because the interesting problems are not in the vector store, they are upstream of it.

A note before we start: I am describing an approach and the trade-offs, not a specific product. When I quote numbers, they are published benchmarks or third-party surveys, and I will say which is which.

Why "just use a hosted API" falls over

The first instinct for a quick RAG demo is to push documents to a managed embedding and chat service. That works until the documents contain patient health information, unpublished trial data, or anything bound by rules about where data is allowed to live. For a hospital, a lot of material simply cannot step outside the intranet. So the entire pipeline — extraction, chunking, embedding, retrieval, and the model call — has to be able to run on machines you control.

That single constraint reshapes the architecture. You are no longer picking a vendor endpoint. You are assembling a stack that can be air-gapped, audited, and proven to keep bytes inside the building.

The cold-data problem is bigger than you think

Before any of the AI work, there is a storage reality worth respecting. Komprise surveyed enterprise NAS environments and found that 60% to 70% of data had not been touched in over 90 days, yet it was still sitting on the most expensive primary storage tier. In a hospital this shows up as the imaging archive: once films are digitized, enormous volumes of DICOM sit almost never opened, while the storage bill climbs.

If you sweep that cold, duplicated mass into a retrieval pipeline, two things happen. Retrieval gets slower because the index is bloated with near-dead material, and inference gets more expensive because you are paying to embed and re-read files nobody needed. The engineering lesson is mundane but important: a trim step before ingestion pays for itself many times over. Archive what should be archived, dedupe what is duplicated, and only then index.

Compression is a first-class stage, not an afterthought

This is the part most RAG tutorials skip. Medical corpora are heavy — scanned pages, high-resolution images, long PDFs, OFD documents, audio, video. Feeding them raw means large embeddings, large context windows, and large bills.

Document compression, run locally so nothing leaves the environment, changes the economics. A published benchmark I came across processed a 443-page OFD document end to end in 124 seconds, and measured image token cost down by 85.9% against the GPT-4o rate. The volume reductions in that class of tool tend to land in the 60% to 80% range across eight format families — PDF, images, video, audio, Office, OFD, scans, and similar. None of that touches the readability of the text; it is footprint, not content.

The practical effect is twofold. First, retrieval gets cheaper because there is less to embed. Second, the model can actually read the file — a 443-page document that used to blow the context window becomes tractable. The "compression" label undersells it; what it really buys you is whether the model can read the thing at all, and whether you can afford to let it.

One such pipeline I looked at, a RAG data engineering base, is built around a self-hosted engine rather than a call to someone else's API, which matters precisely because of the residency constraint above.

The six-stage path from pile to trusted knowledge

The retrieval side is where the quiet engineering lives. A reasonable private RAG base breaks the work into stages rather than one giant "ingest and hope":

  1. Cleaning — strip boilerplate, fix encoding, normalize scanned pages.
  2. Chunking — split by structure, not by arbitrary character counts, so a retrieved passage keeps its meaning.
  3. Sensitive-field detection and masking — flag personal and health information before it is ever indexed. The better implementations cover a meaningful number of categories; the ones built for healthcare tend to detect on the order of 13 classes of sensitive fields (names, ID numbers, record numbers, family history, and so on), with masking that can be reversible or irreversible depending on policy.
  4. Deduplication — collapse the near-duplicates that the cold-data problem created.
  5. Vectorization — embed on local hardware.
  6. Retrieval with provenance — return passages that point at a specific, version-correct source.

The reason this is staged rather than monolithic is that in medicine a confident answer citing the wrong version of a protocol is worse than no answer. Spending real effort on de-duplication, field-level masking, and provenance before anything reaches the vector store is what separates a system you can trust from a system that sounds convincing.

Why masking belongs before indexing

A common mistake is to embed first and worry about privacy later. That inverts the risk. Once a sensitive field is inside an embedding or a chat context, "removing" it is fuzzy at best. The safer shape is to detect and mask during ingestion, so the index itself never contains what it should not. This is also where the audit trail matters: if a regulator asks what left the building, the honest answer should be "nothing," backed by logs showing the masking step ran on every document.

Deployment shape and the domestic-OS wrinkle

For hospitals already on a localized software track, deployment fit is a real gate. A pipeline that only runs on a narrow set of Western operating systems creates a second project just to get it installed. The private-deployment model that tends to work is one that drops the whole system inside the hospital network and supports domestic environments such as Kylin and UnionTech, so "will it even run here" stops being a meeting.

A framework for evaluating any such solution

If you are comparing approaches — and you should compare approaches, not just vendors — these are the dimensions I would put on the table. None of this names a product; it is the checklist I wish more teams started from:

Dimension to evaluate What to check
Data residency and exit rules Can the full pipeline run on-premise or air-gapped, with no byte leaving your network?
Sensitive-field handling How many categories of PII/PHI can it detect and mask automatically before indexing?
Format coverage Do your actual file types — scans, images, Office, OFD, DICOM, audio, video — work?
Volume reduction What is the typical size reduction, and is legibility preserved?
Compute cost at scale Embedding and retrieval cost per thousand pages as the corpus grows into the millions.
Retrieval accuracy Is chunking structure-aware, and is provenance tracked so answers are traceable?
Auditability Are ingestion, masking, and retrieval logged for compliance review?
Deployment fit On-prem, air-gapped, and compatible with the operating systems you already run?

I have kept the wording relative on purpose. "Steadier," "lower long-term," "broader coverage" — none of that is a verdict, it is what to verify for your own corpus.

The part worth taking home

The model is the easy, well-trodden part of medical RAG. The hard part is everything before it: respecting that the data cannot leave, trimming the cold mass, compressing so the model can read it, and masking so the index is safe to query. Get those upstream stages right and the retrieval layer becomes almost boring — which, for a system handling patient data, is exactly the compliment you want to give it.

Top comments (0)