Background Opiate substitution treatment (OST) is the main treatment for people addicted to heroin and other opioid drugs. However, there is limited information on how the delivery of this treatment affects mortality risk. Objectives To investigate the associations of mortality risk with periods during treatment and following cessation of treatment, medication type, co-prescription of other medication and dosing regimens during titration and detoxification. The trends with time of prescribed medication, dose and treatment duration were also explored. Design Prospective longitudinal observational study. Setting UK primary care between 1998 and 2014. Participants A total of 12,780 patients receiving methadone, buprenorphine or dihydrocodeine. Main outcome measures All-cause mortality relating to 657 deaths and drug-related poisoning relating to 113 deaths. Data sources Clinical Practice Research Datalink with linked information on cause of death from the Office for National Statistics. Results For both outcomes, the lowest mortality risk was observed after 4 weeks of treatment and the highest risk was observed in the first 4 weeks following cessation of treatment [e.g. for drug-related poisoning, incidence rate ratio (IRR) 8.15, 95% confidence interval (CI) 5.45 to 12.19]. There was evidence that the treatment period risks varied with OST medication. The largest difference in risk was for the first 4 weeks of treatment for both outcomes, with patients on buprenorphine being at lower risk than those on methadone (e.g. for drug-related poisoning, IRR 0.08, 95% CI 0.01 to 0.48). The co-prescription of benzodiazepines was associated with linearly increasing the risk of drug-related deaths by dose (IRR 2.02, 95% CI 1.66 to 2.47), whereas z-drugs (zolpidem, zopiclone and zaleplon) were associated with increased risk of both all-cause (IRR 1.83, 95% CI 1.59 to 2.12) and drug-related (IRR 3.31, 95% CI 2.45 to 4.47) mortality. There was weak evidence that higher initial and final doses were associated with increased all-cause mortality risk. In the first 4 weeks of treatment, the risk increased by 4% for each 5-mg increment in methadone dose (1-mg increase in buprenorphine) (hazard ratio 1.04, 95% CI 1.00 to 1.09). In the first 4 weeks after treatment ceased, a similar increment in final dose increased the risk by 3% (hazard ratio 1.03, 95% CI 0.99 to 1.07). There were too few deaths to evaluate the effects on drug-related poisoning. The proportion of OST patients receiving buprenorphine increased between 1998 and 2006. Median treatment duration was consistently shorter for buprenorphine than for methadone for each year studied (overall median duration of 48 and 106 days, respectively). Limitations As this was an observational study, the possibility remains of bias from unmeasured factors, which covariate adjustment and inverse probability weighting can eliminate only partially. Conclusions Using buprenorphine as an alternative to methadone may not reduce mortality overall despite resulting in lower IRRs from shorter treatment duration. Clinical guidance needs to consider strengthening warnings about the co-prescription of a range of drugs for OST patients. Future work Our analyses need to be replicated using other clinical data sets in the UK and in other countries. New interventions and trials are required to investigate improving the retention of OST patients in primary care. Funding The National Institute for Health Research Health Services and Delivery Research programme.