How unicellular organisms optimize the production of compounds is a fundamental biological question. While it is typically thought that production is optimized at the individual-cell level, secreted compounds could also allow for optimization at the group level, leading to a division of labor where a subset of cells produces and shares the compound with everyone. Using mathematical modelling, we show that the evolution of such division of labor depends on the cost function of compound production. Specifically, for any trait with saturating benefits, linear costs promote the evolution of uniform production levels across cells. Conversely, production costs that diminish with higher output levels favor the evolution of specialization - especially when compound shareability is high. When experimentally testing these predictions with pyoverdine, a secreted iron-scavenging compound produced by Pseudomonas aeruginosa, we found linear costs and, consistent with our model, detected uniform pyoverdine production levels across cells. We conclude that for shared compounds with saturating benefits, the evolution of division of labor is facilitated by a diminishing cost function. More generally, we note that shifts in the level of selection from individuals to groups do not solely require cooperation, but critically depend on mechanistic factors, including the distribution of compound synthesis costs.