Historically, antibiotic treatment guidelines have aimed to maximize treatment efficacy and minimize toxicity, but have not considered the evolution of antibiotic resistance. Optimizing the duration and dosing of treatment to minimize the duration of symptomatic infection and selection pressure for resistance simultaneously has the potential to extend the useful therapeutic life of these valuable life-saving drugs without compromising the interests of individual patients.Here, using mathematical models, we explore the theoretical basis for shorter durations of treatment courses, including a range of ecological dynamics of bacteria that cause infections or colonize hosts as commensals. We find that immunity is an important mediating factor in determining the need for long duration of treatment. When immunity to infection is expected, shorter durations that reduce the selection for resistance without interfering with successful clinical outcome are likely to be supported. Adjusting drug treatment strategies to account for the impact of the differences in the ecological niche occupied by commensal flora relative to invasive bacteria could be effective in delaying the spread of bacterial resistance.