This paper develops a mathematical/economic framework to address the following question: Given a particular population, a specific HIV prevention program, and a fixed amount of funds that could be invested in the program, how much money should be invested? We consider the impact of investment in a prevention program on the HIV sufficient contact rate (defined via production functions that describe the change in the sufficient contact rate as a function of expenditure on a prevention program), and the impact of changes in the sufficient contact rate on the spread of HIV (via an epidemic model). In general, the cost per HIV infection averted is not constant as the level of investment changes, so the fact that some investment in a program is cost effective does not mean that more investment in the program is cost effective. Our framework provides a formal means for determining how the cost per infection averted changes with the level of expenditure. We can use this information as follows: When the program has decreasing marginal cost per infection averted (which occurs, for example, with a growing epidemic and a prevention program with increasing returns to scale), it is optimal either to spend nothing on the program or to spend the entire budget. When the program has increasing marginal cost per infection averted (which occurs, for example, with a shrinking epidemic and a prevention program with decreasing returns to scale), it may be optimal to spend some but not all of the budget. The amount that should be spent depends on both the rate of disease spread and the production function for the prevention program. We illustrate our ideas with two examples: that of a needle exchange program, and that of a methadone maintenance program.