Homeostasis of internal carbon dioxide (CO2) and oxygen (O2) levels is fundamental to all animals. Here we examine the CO2 response of the nematode Caenorhabditis elegans. This species inhabits rotting material, which typically has a broad CO2 concentration range. We show that well fed C. elegans avoid CO2 levels above 0.5%. Animals can respond to both absolute CO2 concentrations and changes in CO2 levels within seconds. Responses to CO2 do not reflect avoidance of acid pH but appear to define a new sensory response. Sensation of CO2 is promoted by the cGMP-gated ion channel subunits TAX-2 and TAX-4, but other pathways are also important. Robust CO2 avoidance in well fed animals requires inhibition of the DAF-16 forkhead transcription factor by the insulin-like receptor DAF-2. Starvation, which activates DAF-16, strongly suppresses CO2 avoidance. Exposure to hypoxia (<1% O2) also suppresses CO2 avoidance via activation of the hypoxia-inducible transcription factor HIF-1. The npr-1 215V allele of the naturally polymorphic neuropeptide receptor npr-1, besides inhibiting avoidance of high ambient O2 in feeding C. elegans, also promotes avoidance of high CO2. C. elegans integrates competing O2 and CO2 sensory inputs so that one response dominates. Food and allelic variation at NPR-1 regulate which response prevails. Our results suggest that multiple sensory inputs are coordinated by C. elegans to generate different coherent foraging strategies.