An AI-assisted, agentic pipeline for defensible, explainable, and auditable SNOMED CT clinical code discovery β built for NICE (National Institute for Health and Care Excellence).
Defining patient cohorts for healthcare analysis still relies on highly manual clinical code-list construction across fragmented sources. Analysts at NICE must navigate QOF rules, NHS Digital reference sets, and SNOMED-based resources to justify code selections β a process that is slow, inconsistent, and difficult to audit, especially for complex multi-morbidity phenotypes like obesity with Type 2 diabetes.
3C transforms a plain English clinical research question into a validated, evidence-grounded, and human-reviewable shortlist of SNOMED CT codes using a hybrid AI + deterministic pipeline β combining the semantic flexibility of LLMs with the reliability of deterministic NHS API searches.
"Not just an LLM generating codes β a defensible, explainable, and fully auditable evidence trail for expert review."
3C is orchestrated as a stateful 4-node LangGraph pipeline, blending LLM reasoning with deterministic data retrieval:
[Free-text clinical question]
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββ
β Node 1 Β· Query Understanding β
β GPT-4o-mini β 14-field structured JSON β
β + NHS FHIR Terminology Server API β
β (SNOMED synonym enrichment) β
βββββββββββββββββ¬ββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββ
β Node 2 Β· Deterministic SNOMED Search β
β NHS FHIR API Β· Concept hierarchy β
β traversal across diagnosis / meds / β
β observations branches (max recall) β
βββββββββββββββββ¬ββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββ
β Node 3 Β· Deterministic Validator β
β Scoring: provenance + consensus + β
β cosine similarity + course-check β
β β Tier 1 / Tier 2 / Tier 3 assignment β
βββββββββββββββββ¬ββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββ
β Node 4 Β· Justification via RAG β
β LangChain: ChromaDB + MMR retrieval β
β text-embedding-3-small (1536 dims) β
β GPT-4o-mini β exact NICE/QOF evidence β
βββββββββββββββββ¬ββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββ
β Human Review Interface β
β Code cards Β· tier badges Β· provenance β
β flags Β· confidence scores β
β Accept / Reject / Skip + feedback loop β
βββββββββββββββββββββββββββββββββββββββββββββ
| Component | Approach | Why |
|---|---|---|
| Query understanding | LLM (GPT-4o-mini) | Rules-based parsers fail on implicit context and compound phenotypes |
| SNOMED search | Deterministic (NHS FHIR API) | Guarantees auditability; no hallucinated codes |
| Validation | Deterministic (scoring model) | Patient safety requires reproducible, traceable decisions |
| Justification | RAG (LangChain + Chroma) | Grounds every output in official NICE/QOF guideline text |
| Orchestration | LangGraph (stateful graph) | Full state traceability across nodes; deterministic routing |
| Layer | Technology |
|---|---|
| Agentic Orchestration | LangGraph (stateful 4-node pipeline) |
| RAG Framework | LangChain (ChromaDB, MMR retrieval, PyMuPDFLoader, RecursiveCharacterTextSplitter) |
| LLM | OpenAI GPT-4o-mini |
| Embeddings | OpenAI text-embedding-3-small (1536 dimensions) |
| Vector Store | ChromaDB |
| Clinical APIs | NHS FHIR Terminology Server, QOF rules, NHS Digital Reference Sets, OpenCodelists |
| Document Parsing | PyMuPDFLoader + RecursiveCharacterTextSplitter (chunk size: 800) |
| Retrieval Strategy | Maximal Marginal Relevance (MMR) β diverse, non-redundant evidence chunks |
| Deployment | Render (free tier, 512MB RAM CPU) |
| Language | Python 3.10+ |
Three RAG approaches were evaluated:
- Approach A β Pre-RAG: structure query with RAG, then justify codes
- Approach B β Post-RAG: vectorise structured query fields for chunk retrieval
- Approach C β (Selected) β Post-RAG: vectorise each individual SNOMED code's short description to target the most relevant guideline chunks per code, then LLM judges relevance for the research question
Approach C produced richer, more code-specific justification text across a wider range of clinically relevant codes.
Each candidate code is scored across four signals:
| Signal | Weight | Description |
|---|---|---|
| Provenance | High | Code appears in QOF rules, NHS Digital refsets, or OpenCodelists |
| Consensus | Bonus | Code appears across multiple independent sources |
| Semantic Similarity | Medium | Cosine similarity between code definition and target concept |
| Course Consistency | Adjustment | Acute/chronic alignment with clinical context |
Codes scoring below 0.25 are excluded. Remaining codes are tiered (Tier 1 / 2 / 3) for human review.
AI is a decision-support tool, not an automated decision-maker. The human review interface presents code cards with full provenance, tier badges, and evidence quotes β users accept, reject, or skip each code. Feedback is stored persistently to improve future runs.
Test Case: "Obesity with Type 2 Diabetes"
| Pipeline Stage | Result |
|---|---|
| Candidate codes retrieved | 70 |
| Validated codes (post-scoring) | 54 |
| Tier 1 β high confidence | 13 |
| Tier 2 / 3 β human review required | 41 |
Highlights:
Type 2 diabetes mellitus,Brittle type 2 diabetes mellitus, and 11 more correctly promoted to Tier 1 (confidence: 0.85), appearing across QOF, NHS Digital, and OpenCodelistsType 2 diabetes mellitus in obeseachieved highest semantic score (0.81) with correct NICE NG28 evidence quote (p.48)Dapagliflozincorrectly justified using NICE NG28 (p.98): "Licensed for adults with type 2 diabetesβ¦"- Cost per query: ~Β£0.01 | Runtime: ~2 minutes | Deployed on 512MB CPU (no GPU required)
| Metric | Value |
|---|---|
| Cost per query | ~Β£0.01 |
| Average runtime | ~2 minutes |
| Infrastructure | Render free tier (512MB RAM, CPU only) |
| LLM calls per query | Query understanding (1Γ) + Code justification (1Γ per validated code) |
| Embedding calls | ~50+ SNOMED codes vectorised per query |
- Exclusion shield needs hardening β paediatric and gestational phenotypes are occasionally included incorrectly
- Tier calibration needs refinement β codes with very high semantic similarity (e.g., 0.81) can be downgraded to Tier 2 if absent from QOF/NHS curated lists
- Human feedback loop is stored but not yet integrated back into the scoring pipeline
- Precision vs. recall trade-off β the system currently excels at high-recall discovery; future work focuses on improving shortlist precision without losing rare but relevant codes
- Benchmarking against expert-reviewed gold-standard code lists is in progress
βββ src/ # Core pipeline code
β βββ pipeline/ # LangGraph node definitions
β βββ rag/ # LangChain RAG components (Chroma, embeddings, MMR)
β βββ validator/ # Deterministic scoring & tier assignment
β βββ api/ # NHS FHIR API integration
βββ docs/
β βββ LAB-FAM-NICE-Final-Report.pdf # Full technical report
β βββ 3C_Clinical_Code_Collector_presentation.pdf # Presentation deck
βββ README.md
Cambridge Data Science Career Accelerator β LAB FAM Group 2 | April 2026
| Name | Role |
|---|---|
| Ali Aydin Yildiz | Technical Lead β pipeline architecture, LangGraph/LangChain engineering, RAG design, deployment |
| Angelo di Legge | Team member |
| Bihter Ekin Kaplanlioglu | Team member |
| Farhio Ali | Team member |
| Jegamohan Vicneswararajah | Team member |
| Lemmy Emasit | Team member |