SummaryBackgroundAntiretroviral therapy (ART) scale-up in sub-Saharan Africa combined with weak routine virological monitoring has driven increasing HIV drug resistance. We investigated ART failure, drug resistance, and early mortality among patients with HIV admitted to hospital in Malawi.MethodsThis observational cohort study was nested within the rapid urine-based screening for tuberculosis to reduce AIDS-related mortality in hospitalised patients in Africa (STAMP) trial, which recruited unselected (ie, irrespective of clinical presentation) adult (aged ≥18 years) patients with HIV-1 at admission to medical wards. Patients were included in our observational cohort study if they were enrolled at the Malawi site (Zomba Central Hospital) and were taking ART for at least 6 months at admission. Patients who met inclusion criteria had frozen plasma samples tested for HIV-1 viral load. Those with HIV-1 RNA of at least 1000 copies per mL had drug resistance testing by ultra-deep sequencing, with drug resistance defined as intermediate or high-level resistance using the Stanford HIVDR program. Mortality risk was calculated 56 days from enrolment. Patients were censored at death, at 56 days, or at last contact if lost to follow-up. The modelling strategy addressed the causal association between HIV multidrug resistance and mortality, excluding factors on the causal pathway (most notably, CD4 cell count, clinical signs of advanced HIV, and poor functional and nutritional status).FindingsOf 1316 patients with HIV enrolled in the STAMP trial at the Malawi site between Oct 26, 2015, and Sept 19, 2017, 786 had taken ART for at least 6 months. 252 (32%) of 786 patients had virological failure (viral load ≥1000 copies per mL). Mean age was 41·5 years (SD 11·4) and 528 (67%) of 786 were women. Of 237 patients with HIV drug resistance results available, 195 (82%) had resistance to lamivudine, 128 (54%) to tenofovir, and 219 (92%) to efavirenz. Resistance to at least two drugs was common (196, 83%), and this was associated with increased mortality (adjusted hazard ratio 1·7, 95% CI 1·2–2·4; p=0·0042).InterpretationInterventions are urgently needed and should target ART clinic, hospital, and post-hospital care, including differentiated care focusing on patients with advanced HIV, rapid viral load testing, and routine access to drug resistance testing. Prompt diagnosis and switching to alternative ART could reduce early mortality among inpatients with HIV.FundingJoint Global Health Trials Scheme of the Medical Research Council, UK Department for International Development, and Wellcome Trust.