Metabolic alterations are increasingly implicated in neurological disorders, including Alzheimer's disease (AD), highlighting the relevance of the peripheral metabolome, shaped by genetic and environmental exposures, for brain health. We examined the relation of 991 blood metabolites with cognition and magnetic resonance imaging (MRI) measures cross-sectionally in 1,082 dementia-free middle-aged participants of the population-based Rotterdam Study and quantified contributions of genetic variation, lifestyle, comorbidities, medication and gut microbiota to metabolite variance. Cognition-associated metabolites were replicated in two independent cohorts of older adults and tested for associations with incident AD longitudinally in one cohort. Twenty-two metabolites were associated with MRI measures. Fourteen metabolites showed replicated associations with cognition, with ergothioneine exhibiting the largest effect. The metabolite signature of cognition mirrored that of incident AD. Lifestyle, clinical variables and medication were the strongest determinants of cognition-associated and MRI-associated metabolites, explaining up to 28.6% of their variance. Antacid use was associated with worse cognition and lower ergothioneine levels, which mediated 31.5% of the negative medication effect, suggesting implications for AD prevention.