ImportanceGiven the personal and social burdens of opioid use disorder (OUD), understanding time trends in OUD prevalence in large patient populations is key to planning prevention and treatment services.ObjectiveTo examine trends in the prevalence of OUD from 2005 to 2022 overall and by age, sex, and race and ethnicity.Design, Setting, and ParticipantsThis serial cross-sectional study included national Veterans Health Administration (VHA) electronic medical record data from the VHA Corporate Data Warehouse. Adult patients (age ≥18 years) with a current OUD diagnosis (using International Classification of Diseases, Ninth Revision, Clinical Modification [ICD-9-CM] and International Statistical Classification of Diseases, Tenth Revision, Clinical Modification [ICD-10-CM] codes) who received outpatient care at VHA facilities from January 1, 2005, to December 31, 2022, were eligible for inclusion in the analysis.Main Outcomes and MeasuresThe main outcome was OUD diagnoses. To test for changes in prevalence of OUD over time, multivariable logistic regression models were run that included categorical study year and were adjusted for sex, race and ethnicity, and categorical age.ResultsThe final sample size ranged from 4 332 165 to 5 962 564 per year; most were men (89.3%-95.0%). Overall, the annual percentage of VHA patients diagnosed with OUD almost doubled from 2005 to 2017 (0.60% [95% CI, 0.60%-0.61%] to 1.16% [95% CI, 1.15%-1.17%]; adjusted difference, 0.55 [95% CI, 0.54-0.57] percentage points) and declined thereafter (2022: 0.97% [95% CI, 0.97%-0.98%]; adjusted difference from 2017 to 2022, −0.18 [95% CI, −0.19 to −0.17] percentage points). This trend was similar among men (0.64% [95% CI, 0.63%-0.64%] in 2005 vs 1.22% [95% CI, 1.21%-1.23%] in 2017 vs 1.03% [95% CI, 1.02%-1.04%] in 2022), women (0.34% [95% CI, 0.32%-0.36%] in 2005 vs 0.68% [95% CI, 0.66%-0.69%] in 2017 vs 0.53% [95% CI, 0.52%-0.55%] in 2022), those younger than 35 years (0.62% [95% CI, 0.59%-0.66%] in 2005 vs 2.22% [95% CI, 2.18%-2.26%] in 2017 vs 1.00% [95% CI, 0.97%-1.03%] in 2022), those aged 35 to 64 years (1.21% [95% CI, 1.19%-1.22%] in 2005 vs 1.80% [95% CI, 1.78%-1.82%] in 2017 vs 1.41% [95% CI, 1.39%-1.42%] in 2022), and non-Hispanic White patients (0.44% [95% CI, 0.43%-0.45%] in 2005 vs 1.28% [95% CI, 1.27%-1.29%] in 2017 vs 1.13% [95% CI, 1.11%-1.14%] in 2022). Among VHA patients aged 65 years or older, OUD diagnoses increased from 2005 to 2022 (0.06% [95% CI, 0.06%-0.06%] to 0.61% [95% CI, 0.60%-0.62%]), whereas among Hispanic or Latino and non-Hispanic Black patients, OUD diagnoses decreased from 2005 (0.93% [95% CI, 0.88%-0.97%] and 1.26% [95% CI, 1.23%-1.28%], respectively) to 2022 (0.61% [95% CI, 0.59%-0.63%] and 0.82% [95% CI, 0.80%-0.83%], respectively).Conclusions and RelevanceThis serial cross-sectional study of national VHA electronic health record data found that the prevalence of OUD diagnoses increased from 2005 to 2017, peaked in 2017, and declined thereafter, a trend primarily attributable to changes among non-Hispanic White patients and those younger than 65 years. Continued public health efforts aimed at recognizing, treating, and preventing OUD are warranted.