Data from previous studies have suggested a possible association between antibiotic use in infancy and risk of childhood obesity, with implications for health-care delivery and obesity prevention strategies. However, whether the observed association was due to antibiotic use or underlying infection, or both, is unclear. We aimed to disentangle the effect of antibiotic use in infancy from that of underlying infection on the risk of childhood obesity. In this longitudinal birth cohort study, we included infants in the Kaiser Permanente Northern California population born between Jan 1, 1997, and March 31, 2013. We used electronic medical records to ascertain data for antibiotic use, infection diagnosis, and anthropometric measurements (and thus BMI and obesity status) from birth up to age 18 years. We used standard mixed-effects logistic regression for repeated measurements to analyse multiple BMI measurements per child (median five measurements) and to obtain odds ratios (ORs) and 95% CIs for obesity risk. We also did a substudy in 547 same-sex twin pairs with discordant exposure status to substantiate our findings. 260 556 individuals were included in our analysis. After controlling for maternal age, race or ethnic origin, pre-pregnancy BMI, preterm delivery, low birthweight, maternal antibiotic use, and infection during pregnancy, infection without antibiotic use in infancy was associated with an increased risk of childhood obesity compared with controls without infection (OR 1·25, 95% CI 1·20-1·29). A clear dose-response relation was seen between infection episodes and risk of childhood obesity (ptrend <0·0001). By contrast, compared with infants with untreated infection, antibiotic use during infancy was not associated with risk of childhood obesity (1·01, 0·98-1·04). Neither broad-spectrum nor narrow-spectrum antibiotics were associated with risk of childhood obesity. These findings were supported by the results of the twin set analysis. Infection, but not antibiotic use, during infancy is associated with risk of childhood obesity. This finding will need to be replicated in future studies. Although our results do not rule out a potential effect of antibiotics on microbiome composition and the use of antibiotics should always be judicious, they suggest that treatment of common infections with antibiotics in infancy is unlikely to be a main contributor to childhood obesity. Kaiser Permanente Center for Effectiveness & Safety Research.