Natural Language Processing Identification of Nonprescribed Fentanyl Use in Electronic Health Records: Algorithm Development and Validation Study
Open Access
- 9 September 2026
- journal article
- Published by JMIR Publications Inc. in Journal of Medical Internet Research
- Vol. 28, e97485
Abstract
Background: Overdose and suicide due to nonprescribed fentanyl use have increased significantly, yet health care systems lack reliable methods to identify patients who use nonprescribed fentanyl. International Classification of Diseases codes are inconsistent and do not specify nonprescribed fentanyl use. Objective: This study aimed to develop natural language processing approaches to identifying nonprescribed fentanyl use in electronic health record (EHR) documentation. Methods: This retrospective study included Veterans Health Administration patients seen between April 5, 2023, and December 23, 2024. A term list was developed to identify fentanyl-related mentions in clinical text, and 250-character snippets surrounding identified mentions were extracted. Veterans (n=3878) were randomly sampled from 5 predefined groups based on the presence of 1 of 4 terms (“fent,” “blues,” “M30s,” and “tranq”) in their EHR documentation. Physician annotators classified snippets into “nonprescribed fentanyl use,” “prescribed fentanyl use,” or “other,” with interannotator agreement evaluated using the mean pairwise Cohen κ. Cross-validation folds were constructed at the patient level between training and test sets. Penalized logistic regression, Bio-ClinicalBERT, Llama 3-8B, and Mistral-7B were trained on labeled data and compared. Model performance was evaluated using precision, recall, and F 1 -scores for each class, with a focus on the nonprescribed fentanyl use class as the primary label of clinical interest using bootstrapped 95% CIs. A fairness analysis and Shapley additive explanations analysis were performed using Bio-ClinicalBERT. External validation was performed using Bio-ClinicalBERT on an independent sample of 200 snippets, each representing a unique patient from January 2025 to June 2026, with precision reported as the primary validation metric. Results: Of 7389 snippets, 9.6% (n=709) were classified as “nonprescribed fentanyl use,” 40.3% (n=2981) were classified as “prescribed fentanyl use,” and 50% (n=3699) were classified as “other.” Interannotator agreement was high (κ=0.822). Llama 3-8B achieved the highest F 1 -score for nonprescribed fentanyl use (0.87, 95% CI 0.83-0.92), followed by Mistral-7B (0.80, 95% CI 0.75-0.84), Bio-ClinicalBERT (0.80, 95% CI 0.74-0.85), and penalized logistic regression (0.74, 95% CI 0.73-0.75). Performance was consistent across demographic subgroups, with lower performance for the nonprescribed fentanyl use class observed in female and Hispanic subgroups. Shapley additive explanations analysis revealed clinically meaningful discriminating terms for each class, although subword tokens required contextual interpretation. External validation of Bio-ClinicalBERT demonstrated a precision of 0.79 for nonprescribed fentanyl use. Conclusions: Natural language processing can identify nonprescribed fentanyl use in EHR documentation, although model performance for this class was lower than overall model performance, reflecting the clinical complexity of identifying nonprescribed use and the variable ways in which clinicians document this problem. This approach may support risk prediction and targeting of interventions to patients exposed to nonprescribed fentanyl.Keywords
This publication has 28 references indexed in Scilit:
- Decline in US Drug Overdose Deaths by Region, Substance, and DemographicsJAMA Network Open, 2025
- Recent Drug Overdose Mortality Decline Compared With Pre–COVID-19 TrendJAMA Network Open, 2025
- Caring for Hospitalized Adults With Opioid Use Disorder in the Era of FentanylJAMA Internal Medicine, 2024
- Development and validation of an electronic health records‐based opioid use disorder algorithm by expert clinical adjudication among patients with prescribed opioidsPharmacoepidemiology and Drug Safety, 2023
- Randomized Policy Evaluation of the Veterans Health Administration Stratification Tool for Opioid Risk Mitigation (STORM)Journal of General Internal Medicine, 2022
- Evaluation of the Recovery Engagement and Coordination for Health–Veterans Enhanced Treatment Suicide Risk Modeling Clinical Program in the Veterans Health AdministrationJAMA Network Open, 2021
- VA Efforts to Reduce Opioid Overdose Deaths in At-Risk VeteransJAMA, 2019
- Validity of International Classification of Diseases codes in identifying illicit drug use target conditions using medical record data as a reference standard: A systematic reviewDrug and Alcohol Dependence, 2019
- Using natural language processing of clinical text to enhance identification of opioid‐related overdoses in electronic health records dataPharmacoepidemiology and Drug Safety, 2019
- Characterizing fentanyl-related overdoses and implications for overdose response: Findings from a rapid ethnographic study in Vancouver, CanadaDrug and Alcohol Dependence, 2018