Diverse forms of cultivation have evolved across the tree of life. Efficient farming requires that the farmer deciphers and actively promotes conditions that increase crop yield. For plant cultivation, this can include evaluating tradeoffs among light, nutrients, and protection against herbivores. It is not understood if, or how, nonhuman farmers evaluate local conditions to increase payoffs. Here, we address this question using an obligate farming mutualism between the ant Philidris nagasau and epiphytic plants in the genus Squamellaria that are cultivated for their nesting sites and floral rewards. We focused on the ants' active fertilization of their crops and their protection against herbivory. We found that ants benefited from cultivating plants in full sun, receiving 7.5-fold more floral food rewards compared to shade-cultivated plants. The higher reward levels correlated with higher levels of crop protection provided by the ants. However, while high-light planting yielded the greatest immediate food rewards, sun-grown crops contained less nitrogen compared to shade-grown crops. This was due to lower nitrogen input from ants feeding on floral rewards instead of insect protein gained from predation. Despite this tradeoff, farming ants optimize crop yield by selectively planting their crops in full sun. Ancestral state reconstructions across this ant-plant clade show that a full-sun farming strategy has existed for millions of years, suggesting that nonhuman farmers have evolved the means to evaluate and balance conflicting crop needs to their own benefit.