BACKGROUND: Health inequalities persist into old age. We aimed to investigate risk factors for socioeconomic differences in frailty that could potentially be modified through policy measures. METHODS: In this multi-wave longitudinal cohort study (Whitehall II study), we assessed participants' socioeconomic status, behavioural and biomedical risk factors, and disease status at age 45-55 years, and frailty (defined according to the Fried phenotype) at baseline and at one or more of three clinic visits about 18 years later (mean age 69 years [SD 5·9]). We used logistic mixed models to examine the associations between socioeconomic status and risk factors at age 50 years and subsequent prevalence of frailty (adjusted for sex, ethnic origin, and age), with sensitivity analyses and multiple imputation for missing data. FINDINGS: Between Sept 9, 2007, and Dec 8, 2016, 6233 middle-aged adults were measured for frailty. Frailty was present in 562 (3%) of 16 164 person-observations, and varied by socioeconomic status: 145 (2%) person-observations had high socioeconomic status, 241 (4%) had intermediate status, and 176 (7%) had low socioeconomic status, adjusting for sex and age. Risk factors for frailty included cardiovascular disease, depression, smoking, high or abstinent alcohol consumption, low fruit and vegetable consumption, physical inactivity, poor lung function, hypertension, and overweight or obesity. Cardiometabolic markers for future frailty were high ratio of total to high-density lipoprotein cholesterol, and raised interleukin-6 and C-reactive protein concentrations. The five most important factors contributing to the frailty gradient, assessed by percent attenuation of the association between socioeconomic status and frailty, were physical activity (13%), interleukin-6 (13%), body-mass index category (11%), C-reactive protein (11%), and poor lung function (10%). Overall, socioeconomic differences in frailty were reduced by 40% in the maximally-adjusted model compared with the minimally-adjusted model. INTERPRETATION: Behavioural and cardiometabolic risk factors in midlife account for more than a third of socioeconomic differences in frailty. Our findings suggest that interventions targeting physical activity, obesity, smoking, and low-grade inflammation in middle age might reduce socioeconomic differences in later-life frailty. FUNDING: British Heart Foundation and British Medical Research Council