Mounting effective resistance against pathogens is costly in terms of energy and nutrients. However, it remains unexplored whether hosts can offset such costs by adjusting their dietary intake so as to recoup the specific resources involved. We test this possibility by experimentally challenging caterpillars (Spodoptera littoralis) with a highly virulent entomopathogen (nucleopolyhedrovirus), under dietary regimes varying in the content of protein and digestible carbohydrate. We found that dietary protein influenced both resistance to pathogen attack and constitutive immune function to a greater extent than did dietary carbohydrate, indicating higher protein costs of resistance than energy costs. Moreover, when allowed to self-compose their diet, insects surviving viral challenge increased their relative intake of protein compared with controls and those larvae dying of infection, thus demonstrating compensation for protein costs associated with resistance. These results suggest that the change in the host's nutritional demands to fight infection induces a compensatory shift in feeding behaviour.