Self-medication is a specific therapeutic behavioral change in response to disease or parasitism. The empirical literature on self-medication has so far focused entirely on identifying cases of self-medication in which particular behaviors are linked to therapeutic outcomes. In this study, we frame self-medication in the broader realm of adaptive plasticity, which provides several testable predictions for verifying self-medication and advancing its conceptual significance. First, self-medication behavior should improve the fitness of animals infected by parasites or pathogens. Second, self-medication behavior in the absence of infection should decrease fitness. Third, infection should induce self-medication behavior. The few rigorous studies of self-medication in non-human animals have not used this theoretical framework and thus have not tested fitness costs of self-medication in the absence of disease or parasitism. Here we use manipulative experiments to test these predictions with the foraging behavior of woolly bear caterpillars (Grammia incorrupta; Lepidoptera: Arctiidae) in response to their lethal endoparasites (tachinid flies). Our experiments show that the ingestion of plant toxins called pyrrolizidine alkaloids improves the survival of parasitized caterpillars by conferring resistance against tachinid flies. Consistent with theoretical prediction, excessive ingestion of these toxins reduces the survival of unparasitized caterpillars. Parasitized caterpillars are more likely than unparasitized caterpillars to specifically ingest large amounts of pyrrolizidine alkaloids. This case challenges the conventional view that self-medication behavior is restricted to animals with advanced cognitive abilities, such as primates, and empowers the science of self-medication by placing it in the domain of adaptive plasticity theory.