Abstract: Neural representations of sensory stimuli are modulated by a variety of contextual factors, such as information on other stimuli present in the environment, the novelty or familiarity of the sensory inputs, and behavioral goals. Despite decades of attention in systems neuroscience, many questions persist regarding how sensory codes adapt to these different variables. Here, we study this problem in the olfactory system, which combines the advantages of being highly compact, allowing us to develop solvable theoretical models, and highly conserved, allowing us to derive general principles that are testable in relatively simple animal models (such as the locust or the fly) but are widely applicable across animal species, from insects to vertebrates. Our approach is based on the information-theoretic premise that optimal codes strive to maximize the overall entropy (decodability) of neural representations while minimizing neural costs. A novel feature of our theory is that it incorporates contextual feedback: this allows us to predict how optimal odor representations are modulated by top-down signals that represent the overall multi-sensory environment. We also show that the emerging optimal solutions can be implemented at the level of neural circuits through neuroplasticity. This result reconnects our theoretical findings to biologically plausible processes, thus bridging normative and mechanistic levels of analysis. Our theory is generalizable to other sensory systems and establishes a conceptual foundation for studying sensory coding associated with behavior.