Objective: Patients receiving medication for opioid use disorder (MOUD) may continue using nonprescribed drugs or have trouble with medication adherence, and it is difficult to predict which patients will continue to do so. In this study, we develop and validate an automated risk-modeling framework to predict opioid abstinence and medication adherence at a patient's next attended appointment and evaluate the predictive performance of machine-learning algorithms versus logistic regression. Methods: Urine drug screen and attendance records from 40,005 appointments drawn from 2742 patients at a multilocation office-based MOUD program were used to train logistic regression, logistic ridge regression, and XGBoost models to predict a composite indicator of treatment adherence (opioid-negative and norbuprenorphine-positive urine, no evidence of urine adulteration) at next attended appointment.Results: The XGBoost model had similar accuracy and discriminative ability (accuracy, 88%; area under the receiver operating curve, 0.87) to the two logistic regression models (accuracy, 88%; area under the receiver operating curve, 0.87). The XGBoost model had nearly perfect calibration in independent validation data; the logistic and ridge regression models slightly overestimated adherence likelihood. Historical treatment adherence, attendance rate, and fentanyl-positive urine at current appointment were the strongest contributors to treatment adherence at next attended appointment. Discussion: There is a need for risk prediction tools to improve delivery of MOUD. This study presents an automated and portable risk-modeling framework to predict treatment adherence at each patient's next attended appointment. The XGBoost algorithm appears to provide similar classification accuracy to logistic regression models; however, XGBoost may offer improved calibration of risk estimates compared with logistic regression.