The anterior cingulate cortex plays a pivotal role in the cognitive and affective aspects of pain perception. Both endogenous and exogenous opioid signaling within the cingulate mitigate cortical nociception, reducing pain unpleasantness. However, the specific functional and molecular identities of cells mediating opioid analgesia in the cingulate remain elusive. Given the complexity of pain as a sensory and emotional experience, and the richness of ethological pain-related behaviors, we developed a standardized, deep-learning platform for deconstructing the behavior dynamics associated with the affective component of pain in mice, LUPE (Light aUtomated Pain Evaluator). LUPE removes human bias in behavior quantification and accelerated analysis from weeks to hours, which we leveraged to discover that morphine altered attentional and motivational pain behaviors akin to affective analgesia in humans. Through activity-dependent genetics and single-nuclei RNA sequencing, we identified specific ensembles of nociceptive cingulate neuron-types ex-pressing mu-opioid receptors. Tuning receptor expression in these cells bidirectionally modulated morphine analgesia. Moreover, we employed a synthetic opioid receptor promoter-driven approach for cell-type specific optical and chemical genetic viral therapies to mimic morphine's pain-relieving effects in the cingulate, without reinforcement. This approach offers a novel strategy for precision pain management by targeting a key nociceptive cortical circuit with on-demand, non-addictive, and effective analgesia.