The development of a low-cost, accurate sensing strategy for trace levels of chloramphenicol (CAP) residues in food is crucial but remains challenging. In this study, a high-performance electrochemical platform was developed to detect CAP in animal-derived foods using Ni2P embedded N, P-co-doped mesoporous carbon nanospheres (Ni2P@N,P-MCS). The Ni2P@N,P-MCS material, with a unique mesoporous structure and large specific surface area, exhibited excellent adsorption properties. The N, P-co-doped heteroatoms enhanced the electric conductivity and active centers of MCS, facilitating fast mass transport and electron transfer. The introduction of Ni2P increased the active sites and enhanced the electrocatalytic activity. Leveraging these advantages, the electrochemical sensor based on Ni2P@N,P-MCS demonstrated outstanding detection performance with a wide linear range from 0.04 to 250 μM and a low limit of detection of 12 nM. The sensor also showed good reproducibility, stability, and anti-interference properties. Furthermore, the sensor was successfully tested in milk and pork samples, showing satisfactory recovery rates and indicating its feasibility for real sample analysis.