To optimize the targeted production efficiency of high-value nitrogen-containing compounds (NCCs: pyrrole, pyridine, nitriles, amides), this study extensively analyzed the interactions among cellulose, hemicellulose, and lignin under K and Ca presence during nitrogen-rich pyrolysis. By establishing a theoretical model, it confirmed how the three components interact during the pyrolysis process. The highest yield of biochar was observed with Ca. The proportion of NCCs in bio-oil increased by 4.45% with the addition of Ca and decreased by 1.81% with K. GC–MS characterization compared the evolution of intermediate chemical structures during the reaction with the formation of final products, specifically tracing NCCs generation pathways in bio-oil. First-time definition of bio-oil component conversion rates during nitrogen-rich pyrolysis. Aldehydes, ketones (with minimum conversion rates of 92.62% and 71.37%, respectively), and furans were identified as the most easily converted components into NCCs. Correlation analysis of bio-oil components supported this finding. Possible reaction pathways for NCCs generation during nitrogen-rich pyrolysis of ternary mixtures under mixed Ca or K addition conditions were identified. Additionally, the introduced machine learning model exhibited a high R2 value of 0.95 in predicting the product yields of ternary mixtures nitrogen-rich pyrolysis. This validates its effectiveness in handling interacting component systems. These results reveal interaction mechanisms among biomass three components in nitrogen-rich pyrolysis under K and Ca presence, offering new insights for optimizing NCCs production.