Summary 1. Nearly all mutualisms entail the production of resources by one species that attract and reward the species with which it interacts. As such resource production could otherwise be allocated to growth or reproduction, mutualists are predicted to minimize these investment costs. Here, we employ optimal defence theory for plant secondary compounds to evaluate plant production of extrafloral nectar (EFN) resources to attract and reward ants for resistance against herbivores. 2. Through ant exclusion and artificial herbivory experiments, we examined investment in EFN by both buds and fruits of Pachycereus schottii (senita cacti) in the Sonoran Desert of North America. We tested predictions of optimal defence theory that plants invest more in high value parts (fruits) through constitutive EFN, less in low value parts (buds) through induced EFN, and that for a given plant part (buds or fruits), constitutive and induced EFN are negatively correlated. 3. Constitutive levels of EFN were greater in fruits than in buds. Only buds showed induced EFN production following artificial herbivory. Constitutive EFN scaled positively with fruit size but not bud size. Induced EFN was negatively correlated with constitutive EFN for fruits (but not buds), suggesting a potential trade-off in these two herbivore resistance traits. 4. These results indicate that P. schottii minimizes its costs by investing in induced EFN for buds and constitutive EFN for fruits. Moreover, by shifting from induced to constitutive EFN with the morphogenesis of buds to fruits, our results show that investment in EFN can change as a particular plant tissue increases from a lower to a higher value. 5. Synthesis . Indirect defence through EFN production is in accord with that of direct defence through plant secondary compounds, thereby supporting optimal defence theory as a general framework with which to evaluate the costs of producing resources (i.e. the defence) by plants, lycaenids and homopterans to attract and reward ants for their resistance against natural enemies in protection mutualisms.