Leucine · AI Engineer · 2024–26
AI Deviation Investigator
A co-investigator for GMP deviations: root cause, with evidence and confidence
An experiment at Leucine: when a batch deviates, an orchestration agent runs the GMP investigation through phases and pinpoints the root cause from the company's own documents, acting as a co-investigator to the QA, not a replacement.

The chronicle
In drug manufacturing, whenever a batch shows a deviation from its standard measurements, GMP compliance requires a formal investigation to find the root cause and decide whether the batch is accepted or rejected. Traditionally a QA investigator reads through the paper trail, the SOPs, Master Batch Records (MBRs), deviation reports, raw-material specs and logs, to find the root cause, which can take months of manual work.
The system was deliberately built as a co-investigator rather than a replacement for the QA. Its central piece is an investigation orchestration agent that takes the investigation through phases, advancing to the next only on the QA's confirmation or feedback. First it identifies the type of deviation and the product affected from the deviation description; then it surfaces similar deviations from the historical record and what their RCA was, for reference; finally, in the root-cause phase, the RCA agent retrieves from the company's indexed documents via RAG and pinpoints the cause using standard pharma procedures, the 5-Why and the fishbone, presenting the RCA with evidence and confidence.
It began as an experiment in n8n, and we eventually shifted the entire process into our own codebase.
Movement I
The investigation, in phases
An orchestration agent runs the case the way a QA would, one phase at a time, never moving on without sign-off.
- Phase one identifies the type of deviation and the affected product from the deviation description
- Phase two surfaces similar deviations from the historical record, and what their RCA was, as reference
- The agent advances only on the QA's confirmation or feedback, a co-investigator, not an autopilot
Movement II
Grounded root-cause analysis
The final phase pins the cause to evidence, using the methods a pharma investigator already trusts.
- The RCA agent retrieves from the company's relevant indexed documents via RAG, building on the earlier phases
- It applies standard procedures, the 5-Why and the fishbone (Ishikawa), to pinpoint the root cause
- The RCA is presented to the QA with its evidence and a confidence, never as an unsupported verdict
Movement III
The indexing pipeline
Retrieval is only as good as the ingestion, and pharma documents are messy: scanned, tabular, deeply sectioned.
- Many SOPs and manufacturing records are scanned, so IBM's Docling does layout-aware OCR, preserving section hierarchy and structures like tables
- Chunking follows Docling's section tags (titles and depth), capped by character limits so no chunk grows too large; each carries section, page-range and other metadata, keeping tables and sections intact
- OpenAI 1536-dim embeddings per chunk are persisted with their metadata in PostgreSQL via pgvector for semantic similarity search at query time (more dimensions = finer semantic resolution, at more storage and compute)
- We chose pgvector over a dedicated vector DB like Pinecone: pharma's regulatory and data-residency constraints, a modest vector volume, and latency not being a concern made Pinecone overkill