Animals use their sensory system to detect cues in their external environment, then communicate, process, and integrate these cues through the nervous system in order to elicit a specific response. Taste is an important cue used by animals to explore their external environment and can modulate various aspects of animal behavior and physiology. A major ongoing challenge for animals is to detect and respond to the presence of a variety of pathogens in their environment. However, to date, the links between the sensory system and the response to pathogenic threats remain poorly understood. Here we show that Drosophila larvae use their taste system to detect bacterial peptidoglycans in their environment and respond by modulating the activity of their cellular immune system. We show that specific PeptidoGlycan Receptor Proteins (PGRPs) act in bitter taste neurons, via the IMmune Deficiency (IMD) pathway. These PGRPs mediate signaling in taste neurons and control immune cells production in the larval hematopoietic organ, the lymph gland. The taste-mediated sensing of bacteria in larvae primes the immune system, and improves survival after infection in adult flies. These results demonstrate that sensory inputs such as taste play an important role in protecting animals from bacterial infection by providing a powerful adaptive response to potential pathogens. Overall, our findings add to the growing list of examples of crosstalk between the nervous and immune systems and provide novel and important mechanisms for linking them.