Loss of control over harmful drug seeking is one of the most intractable aspects of addiction, as human substance abusers continue to pursue drugs despite incurring significant negative consequences. Human studies have suggested that deficits in prefrontal cortical function and consequential loss of inhibitory control could be crucial in promoting compulsive drug use. However, it remains unknown whether chronic drug use compromises cortical activity and, equally important, whether this deficit promotes compulsive cocaine seeking. Here we use a rat model of compulsive drug seeking in which cocaine seeking persists in a subgroup of rats despite delivery of noxious foot shocks. We show that prolonged cocaine self-administration decreases ex vivo intrinsic excitability of deep-layer pyramidal neurons in the prelimbic cortex, which was significantly more pronounced in compulsive drug-seeking animals. Furthermore, compensating for hypoactive prelimbic cortex neurons with in vivo optogenetic prelimbic cortex stimulation significantly prevented compulsive cocaine seeking, whereas optogenetic prelimbic cortex inhibition significantly increased compulsive cocaine seeking. Our results show a marked reduction in prelimbic cortex excitability in compulsive cocaine-seeking rats, and that in vivo optogenetic prelimbic cortex stimulation decreased compulsive drug-seeking behaviours. Thus, targeted stimulation of the prefrontal cortex could serve as a promising therapy for treating compulsive drug use.