With 33 million people living with human immunodeficiency virus (HIV) worldwide and 2.7 million new infections occurring annually, additional HIV prevention and treatment efforts are urgently needed. However, available resources for HIV control are limited and must be used efficiently to minimize the future spread of the epidemic. We develop a model to determine the appropriate resource allocation between expanded HIV prevention and treatment services. We create an epidemic model that incorporates multiple key populations with different transmission modes, as well as production functions that relate investment in prevention and treatment programs to changes in transmission and treatment rates. The goal is to allocate resources to minimize R 0, the reproductive rate of infection. We first develop a single-population model and determine the optimal resource allocation between HIV prevention and treatment. We extend the analysis to multiple independent populations, with resource allocation among interventions and populations. We then include the effects of HIV transmission between key populations. We apply our model to examine HIV epidemic control in two different settings, Uganda and Russia. As part of these applications, we develop a novel approach for estimating empirical HIV program production functions. Our study provides insights into the important question of resource allocation for a country's optimal response to its HIV epidemic and provides a practical approach for decision makers. Better decisions about allocating limited HIV resources can improve response to the epidemic and increase access to HIV prevention and treatment services for millions of people worldwide.