Traditional Chinese medicine (TCM) comprising herbal formulas has been used for millennia to treat various diseases, such as insomnia, based on distinct syndrome types. Although TCM has been proposed to be effective in insomnia through gut microbiota modulation in animal models, human studies remain limited. Therefore, this study employs machine learning and integrative network techniques to elucidate the role of the gut microbiome in the efficacies of two TCM formulas - center-supplementing and qi-boosting decoction (CSQBD) and spleen-tonifying and yin heat-clearing decoction (STYHCD) - in treating insomnia patients diagnosed with spleen qi deficiency and spleen qi deficiency with stomach heat. Sixty-three insomnia patients with these two specific TCM syndromes were enrolled and treated with CSQBD or STYHCD for 4 weeks. Sleep quality was assessed using the Pittsburgh Sleep Quality Index (PSQI) and Insomnia Severity Index (ISI) every 2 weeks. In addition, variations in gut microbiota were evaluated through 16S rRNA gene sequencing. Stress and inflammatory markers were measured pre- and post-treatment. At baseline, patients exhibiting only spleen qi deficiency showed slightly lesser severe insomnia, lower IFN-α levels, and higher cortisol levels than those with spleen qi deficiency with stomach heat. Both TCM syndromes displayed distinct gut microbiome profiles despite baseline adjustment of PSQI, ISI, and IFN-α scores. The nested stratified 10-fold cross-validated random forest classifier showed that patients with spleen qi deficiency had a higher abundance of Bifidobacterium longum than those with spleen qi deficiency with stomach heat, negatively associated with plasma IFN-α concentration. Both CSQBD and STYHCD treatments significantly improved sleep quality within 2 weeks, which lasted throughout the study. Moreover, the gut microbiome and inflammatory markers were significantly altered post-treatment. The longitudinal integrative network analysis revealed interconnections between sleep quality, gut microbes, such as Phascolarctobacterium and Ruminococcaceae, and inflammatory markers. This study reveals distinct microbiome profiles associated with different TCM syndrome types and underscores the link between the gut microbiome and efficacies of Chinese herbal formulas in improving insomnia. These findings deepen our understanding of the gut-brain axis in relation to insomnia and pave the way for precision treatment approaches leveraging TCM herbal remedies.