When environmental conditions vary stochastically, individuals accrue fitness benefits by exhibiting phenotypic plasticity. Here we analyze a general dynamic‐programming model describing an individual’s optimal energy allocation in a stochastic environment. After maturation, individuals repeatedly decide how to allocate incoming energy between reproduction and maintenance. We analyze the optimal fraction of energy invested in reproduction and the resultant degree of plasticity in dependence on environmental variability and predictability. Our analyses reveal unexpected patterns of optimal energy allocation. When energy availability is low, all energy is allocated to reproduction, although this implies that individuals will not survive after reproduction. Above a certain threshold of energy availability, the optimal reproductive investment decreases to a minimum and even vanishes entirely in highly variable environments. With further improving energy availability, optimal reproductive investment gradually increases again. Costs of plasticity affect this allocation pattern only quantitatively. Our results show that optimal reproductive investment does not increase monotonically with growing energy availability and that small changes in energy availability can lead to major variations in optimal energy allocation. Our results help to unify two apparently opposing predictions from life‐history theory, that organisms should increase reproductive investment both with improved environmental conditions and when conditions deteriorate (“terminal investment”).