Habituation is a non-associative form of learning characterized by a decremented response to repeated stimulation. It is typically framed as a process of selective attention, allowing animals to ignore irrelevant stimuli in order to free up limited cognitive resources. However, habituation can also occur to threatening and toxic stimuli, suggesting that habituation may serve other functions. Here we took advantage of a high-throughput Caenorhabditis elegans learning assay to investigate habituation to noxious stimuli. Using real-time computer vision software for automated behavioral tracking and optogenetics for controlled activation of a polymodal nociceptor, ASH, we found that neuropeptides mediated habituation and performed an RNAi screen to identify candidate receptors. Through subsequent mutant analysis and cell-type-specific gene expression, we found that pigment-dispersing factor (PDF) neuropeptides function redundantly to promote habituation via PDFR-1-mediated cAMP signaling in both neurons and muscles. Behavioral analysis during learning acquisition suggests that response habituation and sensitization of locomotion are parts of a shifting behavioral strategy orchestrated by pigment dispersing factor signaling to promote dispersal away from repeated aversive stimuli.