Using Bayesian neural networks and evolutionary modelling, we show that trophallaxis evolved twice in ants: 130 million years ago when flowering plants became more abundant and ants began harvesting sugary liquids from plants and their insect partners, and again in others ~90 million years ago.