In natural ecosystems, non-pathogenic bacteria are consumed by bacterivorous nematodes, but microorganisms have evolved defence strategies against their predators. Here, Wang et al. show that bacteria isolated from cow dung induce the formation of predatory structures in the fungus Arthrobotrys oligospora to promote the capture and elimination of nematodes. Bacteria increase the production and secretion of urea by upregulating the expression of arginase, which converts arginine into urea and ornithine; secreted urea is taken up by the fungus via the urea transporter and catabolized to ammonia. Importantly, ammonia functions as a signalling molecule that promotes a lifestyle switch in A. oligospora from the saprophytic to the predacious stage, inducing the fungus to form trap structures and kill nematodes. These findings further our understanding of the complex predator–prey interactions in microbial communities.