Redfield noted the similarity between the average nitrogen-to-phosphorus ratio in plankton (N:P = 16 by atoms) and in deep oceanic waters (N:P = 15; refs 1, 2). He argued that this was neither a coincidence, nor the result of the plankton adapting to the oceanic stoichiometry, but rather that phytoplankton adjust the N:P stoichiometry of the ocean to meet their requirements through nitrogen fixation, an idea supported by recent modelling studies. But what determines the N:P requirements of phytoplankton? Here we use a stoichiometrically explicit model of phytoplankton physiology and resource competition to derive from first principles the optimal phytoplankton stoichiometry under diverse ecological scenarios. Competitive equilibrium favours greater allocation to P-poor resource-acquisition machinery and therefore a higher N:P ratio; exponential growth favours greater allocation to P-rich assembly machinery and therefore a lower N:P ratio. P-limited environments favour slightly less allocation to assembly than N-limited or light-limited environments. The model predicts that optimal N:P ratios will vary from 8.2 to 45.0, depending on the ecological conditions. Our results show that the canonical Redfield N:P ratio of 16 is not a universal biochemical optimum, but instead represents an average of species-specific N:P ratios.