Bacteria can swim upstream in a narrow tube and pose a clinical threat of urinary tract infection to patients implanted with catheters. Coatings and structured surfaces have been proposed to repel bacteria, but no such approach thoroughly addresses the contamination problem in catheters. Here, on the basis of the physical mechanism of upstream swimming, we propose a novel geometric design, optimized by an artificial intelligence model. Using Escherichia coli, we demonstrate the anti-infection mechanism in microfluidic experiments and evaluate the effectiveness of the design in three-dimensionally printed prototype catheters under clinical flow rates. Our catheter design shows that one to two orders of magnitude improved suppression of bacterial contamination at the upstream end, potentially prolonging the in-dwelling time for catheter use and reducing the overall risk of catheter-associated urinary tract infection.