Parasites may alter their behaviour to cope with changes in the within-host environment. In particular, investment in transmission may alter in response to the availability of parasite resources or host immune responses. However, experimental and theoretical studies have drawn conflicting conclusions regarding parasites' optimal (adaptive) responses to deterioration in habitat quality. We analyse data from acute infections with six genotypes of the rodent malaria species Plasmodium chabaudi to quantify how investment in transmission (gametocytes) is influenced by the within-host environment. Using a minimum of modelling assumptions, we find that proportional investment in gametocytogenesis increases sharply with host anaemia and also increases at low parasite densities. Further, stronger dependence of investment on parasite density is associated with greater virulence of the parasite genotype. Our study provides a robust quantitative framework for studying parasites' responses to the host environment and whether these responses are adaptive, which is crucial for predicting the short-term and evolutionary impact of transmission-blocking treatments for parasitic diseases.