Background Emerging evidence highlights the gut-brain axis as a key pathway linking diet and anxiety, yet the key determinants remain unclear. Most studies have focused on single components of diet and rarely integrate long- and short-term intake. Furthermore, prior gut-brain work has focused on microbiome composition, while functional features remain underexplored. In this study, we investigated associations between long- and short-term dietary intake, gut microbiome composition and functions, and anxiety in a subclinical cohort of 46 females (18-24 years) from the United Kingdom. Results Long-term diet quality was assessed using the Healthy Eating Index (HEI-2020) derived from a food frequency questionnaire, stratifying participants into lower and higher diet quality clusters. Short-term dietary intake was assessed via 24-hour recalls. Shotgun metagenomics of stool samples was used to assess differences in alpha and beta diversity indices, species abundances, and bacterial pathways putatively metabolizing gut-brain-axis-relevant molecules. Anxiety was measured using the State-Trait Anxiety Inventory (state subscale STAI-s). Regression models identified diet quality (HEI cluster) as the primary dietary feature of anxiety variation. The presence of Ruminococcus gnavus and Flavonifractor plautii and the abundances of Bilophila wadsworthia and Bacteroides thetaiotaomicron were positively associated with anxiety. The presence of Feacalibacterium prausnitzii and greater abundances of butyrate, propionate, and GABA synthesis pathways were inversely associated with anxiety. Non-linear models revealed a U-shaped relationship between inositol synthesis and STAI-s. Finally, we found that habitual diet quality may modulate anxiety-related responses to short-term dietary variation. Conclusions These findings reveal widespread links between long-term diet quality, microbiota composition and function, and anxiety symptoms. These results point towards several promising targets for prebiotic, probiotic, postbiotic, and dietary interventions aimed at reducing anxiety.