One of the key challenges of evolutionary theory is to determine how cooperation is promoted in different environmental contexts. This is because the costs and benefits of cooperation can be difficult to identify as they have many facets and are often highly context-dependent. To address this challenge, we need new study systems where the costs and benefits of cooperative behaviour can be quantified at an individual level under different environmental conditions. Here, we review the opportunities to study this with the herbivorous, haplodiploid gregarious pine sawflies (genera Neodiprion and Diprion from the family Diprionidae, Hymenoptera). These species are social only during the larval stage, when they feed in groups and perform collective chemical defence against predators by deploying their defensive secretions in synchrony. Defence behaviour is costly for individuals, and the extent to which individuals contribute to defence also depends on environmental conditions. Furthermore, there is both individual and sex-biased variation in the secretion of defence fluid. Our review highlights the possibilities of using the pine sawfly system to manipulate both ecological (e.g., diet quality) and social (e.g., kin structure, sex ratio, or group density) conditions during the larval stage and to experimentally quantify the impact of the environment on cooperative interactions. As a result, pine sawflies can tell us what leads to heterogeneity in the expression of cooperation in collective goods and elucidate the role of ecological interactions in promoting or constraining cooperation.