Treatment of bacterial infections currently focuses on choosing an antibiotic that matches a pathogen's susceptibility, with less attention paid to the risk that even susceptibility-matched treatments can fail as a result of resistance emerging in response to treatment. Combining whole-genome sequencing of 1113 pre- and posttreatment bacterial isolates with machine-learning analysis of 140,349 urinary tract infections and 7365 wound infections, we found that treatment-induced emergence of resistance could be predicted and minimized at the individual-patient level. Emergence of resistance was common and driven not by de novo resistance evolution but by rapid reinfection with a different strain resistant to the prescribed antibiotic. As most infections are seeded from a patient's own microbiota, these resistance-gaining recurrences can be predicted using the patient's past infection history and minimized by machine learning-personalized antibiotic recommendations, offering a means to reduce the emergence and spread of resistant pathogens.