The disposal of Chinese medicinal herbal residues (CMHRs) derived from Chinese medicine extraction poses a significant environmental challenge. Aerobic composting presents a sustainable treatment method, yet optimizing nutrient conversion remains a critical concern. This study investigated the effect and mechanism of biochar addition on nitrogen and phosphorus transformation to enhance the efficacy and quality of compost products. The findings reveal that incorporating biochar considerably enhanced the process of nutrient conversion. Specifically, biochar addition promoted the retention of bioavailable organic nitrogen and reduced nitrogen loss by 28.1 %. Meanwhile, adding biochar inhibited the conversion of available phosphorus to non-available phosphorus while enhancing its conversion to moderately available phosphorus, thereby preserving phosphorus availability post-composting. Furthermore, the inclusion of biochar altered microbial community structure and fostered organic matter retention and humus formation, ultimately affecting the modification of nitrogen and phosphorus forms. Structural equation modeling revealed that microbial community had a more pronounced impact on bioavailable organic nitrogen, while humic acid exerted a more significant effect on phosphorus availability. This research provides a viable approach and foundation for regulating the levels of nitrogen and phosphorus nutrients during composting, serving as a valuable reference for the development of sustainable utilization technologies pertaining to CMHRs.