Mycotoxins threaten global food safety, public health and cause huge socioeconomic losses. Early detection is an effective preventive strategy, yet efficient biomarkers for early detection of aflatoxigenic Aspergillus species are lacking. Here, we proposed to use untargeted metabolomics and machine learning to mine biomarkers of aflatoxigenic Aspergillus species. We systematically delineated metabolic differences across 568 extensive field sampling A. flavus and performed biomarker analysis. Versicolorin B, 11-hydroxy-O-methylsterigmatocystin et.al metabolites shown a high correlation (from 0.71 to 0.95) with strains aflatoxin-producing capacity. Molecular networking analysis deciphered the connection of aflatoxins and biomarkers as well as potential emerging mycotoxins. We then developed a model using the biomarkers as variables to discern aflatoxigenic Aspergillus species with 97.8% accuracy. A validation dataset and metabolome from other 16 fungal isolates confirmed the robustness and specificity of these biomarkers. We further demonstrated the solution feasibility in agricultural products by early detection of biomarkers, which predicted aflatoxin contamination risk 35–47 days in advance. A developed operable decision rule by the XGBoost algorithm help regulators to intuitively assess the risk prioritization with 87.2% accuracy. Our research provides novel insights into global food safety risk assessment which will be crucial for early prevention and control of mycotoxins.