Objective To examine the prevalence of HIV in a cohort of people who have used secondary mental health services in the UK. Design Retrospective cohort study. Setting Routinely collected clinical data from secondary mental health services in South London, UK available for research through the Clinical Record Interactive Search tool at the National Institute for Health and Care Research Maudsley Biomedical Research Centre were matched with pseudonymised national HIV surveillance data held by the UK Health Security Agency using a deterministic matching algorithm. Participants All adults aged 16+ who presented for the first time to mental health services in the South London and Maudsley (SLaM) National Health Service Trust between 1 January 2007 and 31 December 2018 were included. Primary outcome Point prevalence of HIV. Results There were 181 177 people who had contact with mental health services for the first time between 2007 and 2018 in SLaM. Overall, 2.47% (n=4481) of those had a recorded HIV diagnosis in national HIV surveillance data at any time (before, during or after contact with mental health services), 24.73 people per 1000. HIV point prevalence was highest in people with a diagnosed substance use disorder at 3.77% (n=784). A substantial percentage of the sample did not have a formal mental health diagnosis (27%), but even with those excluded, the point prevalence remained high at 2.31%. Around two-thirds of people had their diagnosis of HIV before contact with mental health services (67%; n=1495). Conclusions The prevalence of HIV in people who have had contact with mental health services was approximately 2.5 times higher than the general population in the same geographical area. Future work should investigate risk factors and disparities in HIV outcomes between those with and without mental health service contact.