Epilepsy (EP) is characterized by recurring spontaneous seizures, often leading to long-standing cognitive impairment, significantly impacting the quality of life. Due to the unknown pathogenic pathways, developing effective treatments for epilepsy-associated cognitive impairment has been challenging. Metformin, a first-line oral antidiabetic drug, has proven effective in various EP models. Recent studies have shown that metformin can modulate gut microbiota composition and improve cognitive function. However, the protective effect of metformin on cognitive function in the chronic phase of epilepsy in rats is still unclear. Furthermore, whether its effect has to do with the gut microbiota remains to be determined. This study used the pilocarpine-induced chronic epilepsy model to mimic the pathological process of epilepsy-related cognitive impairment. Combined with 16 s rDNA sequencing and 4D-Label free proteomics analysis, multi-omics analysis was performed. We discovered that metformin treatment improved spatial learning and memory, reduced hippocampal neuronal damage, and enhanced synaptic plasticity in epileptic rats. Additionally, 16S rDNA sequencing revealed that metformin treatment reversed microbiota alterations associated with epilepsy, particularly by increasing the abundances of Alloprevotella, Blautia, Romboutsia, and Phascolarctobacterium, while reducing Prevotellaceae NK3B31 group and Ruminococcus 1. This modulation helped reduce the release of microbial toxins and promoted intestinal homeostasis maintenance. Further mechanistic studies involving proteomics and ELISA testing showed that metformin treatment inhibited the TLR4/MyD88/NF-κB pathway and alleviated systemic inflammation in epileptic rats. As can be seen, metformin therapy's amelioration of cognitive impairment in epilepsy may be facilitated by the gut microbiota, with the underlying mechanism potentially involving the suppression of TLR4/MyD88/NF-κB signaling and the mitigation of inflammation.