We studied the chemically-mediated interactions of the encrusting sponge Crambe crambe, one of the most toxic and widespread species in rocky sublittoral habitats in the Northwestern Mediterranean. Guanidine alkaloids accounted for C. crambe's toxicity, which seems to have multiple functions in nature, as evidence has been found for antifouling, antipredation, and space competition roles. We investigated the factors underlying the chemical defence strategy of this species by assessing variation in the production of toxic substances as a function of different biological and environmental variables. The working hypothesis was that the production of these metabolites should be optimized according to the biological features (morphogenesis, reproduction, growth, life history) and ecological conditions (biotic pressures and abiotic factors) of the particular specimens. One cell type, the spherulous cell, which was concentrated near the sponge's surface, accumulated the toxic substances. Within-specimen analyses showed that toxicity was higher in the ectosome than in the choanosome of the sponges. There was a seasonal pattern of change in the toxicity of the species. Life-history stage also proved significant in the production of toxic substances: larvae were non-toxic, and feeding-deterrence experiments showed that larvae and newly metamorphosed individuals were not protected from predation, while two-week-old recruits already showed strong feeding deterrence. Overall, toxicity increased from small to medium-sized adult sponges, and decreased again in larger individuals. Variation in toxicity was also found at an ecological level: the values at a highly competitive site dominated by slow-growing animal species were higher than those at an adjacent, well-lit site with algal dominance. The relative investment in structural material (collagen, fibres, spicules...) was also higher in the shaded habitat, thus a positive relationship was found between investment in chemical and physical defences. In the two habitats compared, allocation to defence correlated negatively with reproduction and growth, and positively with survival. The results showed that C. crambe can adjust, at organismal and population levels, the production of bioactive substances to different environmental and physiological situations. Space competition emerged as a key factor explaining the variation found in the production of bioactive substances.