AbstractDiet quality and the presence and abundance of conspecifics feeding on the same food resource are critical factors that affect functional traits of many animal species and influence ecological processes.We hypothesized that in insects whose larvae live in groups of varying sizes in ephemeral and nutritionally variable environments, extreme macronutrient content in the feeding substrate is a barrier for optimal performance that can be overcome by the collective feeding behaviour of large numbers of larvae.We experimentally examined how multiple immature and adult traits of the polyphagous tephritid fruit fly,Anastrepha ludens, vary as a function of the quality of its larval diet (protein + carbohydrate [P + C] content of 8.0%–12.6% w/w, and protein‐to‐carbohydrate [P/C] ratio of 1.8–0.11) across extreme levels of conspecific larval densities (60–600 larvae/60 g of diet).Overall, we found positive effects on survival of immature and adult stages, the duration of the larval stage, pupal weight and adult emergence of flies, as dietary P + C content increased, followed by negative effects when the diet had extremely high carbohydrate‐biased macronutrient content. Larval density had positive, negative and curvilinear effects depending on the macronutrient composition of the food, resulting in a range of fly responses through a continuous scale of macronutrient content and larval density combinations. Optimal performance ofA.ludens, in terms of larval development time, body weight and survival to adulthood, was found when the diet consisted of low P + C content and high P/C ratios, and low larval densities.Our findings point to a diet‐mediated relationship between density and survival to the adult stage, suggesting that large numbers (up to a point) of larvae can balance negative effects of conspecific competition when food is characterised by high macronutrient content and low P/C ratios. We conclude that diet quality and conspecific density interact in complex ways to shape trait expression and performance in a frugivorous fly. Addressing this interaction is key to advance the understanding of the mechanisms behind changes in population dynamics in insect species living in groups confined in isolated and ephemeral resources.Read the freePlain Language Summaryfor this article on the Journal blog.