Deciphering the neural code underlying goal-directed behavior is a long-term mission in neuroscience1,2. Neurons exhibiting preparation and movement-related activity are intermingled in the premotor and motor cortices3,4, thus concealing the neural code of planned movements. We employed a combination of electrophysiology, pathway-specific optogenetics, phototagging, and inverse reinforcement learning (RL) to elucidate the role of defined neuronal subpopulations in the rat rostral and caudal forelimb areas (RFA and CFA), which correspond to the premotor and motor cortical areas. The inverse RL enabled the functional dissection of spatially intermingled neuronal subpopulations, complementing our pathway-specific optogenetic manipulations and unveiling differential functions of the preparation and movement subpopulations projecting from RFA to CFA. Our results show that the projecting preparation subpopulation suppresses movements, whereas the projecting movement subpopulation promotes actions. We found the influence of RFA on CFA to be adaptable, with the projection either inhibiting or exciting neurons in the superficial and deep CFA layers, depending on context and task phase. These complex interactions between RFA and CFA likely involve the differential recruitment of inhibitory interneurons in the CFA, which is supported by our electron microscopy analysis of the connectivity between these regions. We provide here unprecedented mechanistic insights into how the premotor and primary motor cortices are functionally and structurally interlinked with the potential to advance neuroprosthetics.