Sensory behaviors are often flexible, allowing animals to generate context-appropriate responses to changing environmental conditions. To investigate the neural basis of behavioral flexibility, we examined the regulation of carbon dioxide (CO2) response in the nematode Caenorhabditis elegans. CO2 is a critical sensory cue for many animals, mediating responses to food, conspecifics, predators, and hosts (Scott, 2011; Buehlmann et al., 2012; Chaisson and Hallem, 2012). In C. elegans, CO2 response is regulated by the polymorphic neuropeptide receptor NPR-1: animals with the N2 allele of npr-1 avoid CO2, whereas animals with the Hawaiian (HW) allele or an npr-1 loss-of-function (lf) mutation appear virtually insensitive to CO2 (Hallem and Sternberg, 2008; McGrath et al., 2009). Here we show that ablating the oxygen (O2)-sensing URX neurons in npr-1(lf) mutants restores CO2 avoidance, suggesting that NPR-1 enables CO2 avoidance by inhibiting URX neurons. URX was previously shown to be activated by increases in ambient O2 (Persson et al., 2009; Zimmer et al., 2009; Busch et al., 2012). We find that, in npr-1(lf) mutants, O2-induced activation of URX inhibits CO2 avoidance. Moreover, both HW and npr-1(lf) animals avoid CO2 under low O2 conditions, when URX is inactive. Our results demonstrate that CO2 response is determined by the activity of O2-sensing neurons and suggest that O2-dependent regulation of CO2 avoidance is likely to be an ecologically relevant mechanism by which nematodes navigate gas gradients.