All higher plants produce polyphenols, for defence against above-ground herbivory. These polyphenols also influence the soil micro- and macro-fauna that break down plant leaf litter. Polyphenols therefore indirectly affect the fluxes of soil nutrients and, ultimately, carbon turnover and ecosystem functioning in soils. It is unknown how earthworms, the major component of animal biomass in many soils, cope with high-polyphenol diets. Here, we show that earthworms possess a class of unique surface-active metabolites in their gut, which we term `drilodefensins'. These compounds counteract the inhibitory effects of polyphenols on earthworm gut enzymes, and high-polyphenol diets increase drilodefensin concentrations in both laboratory and field populations. This shows that drilodefensins protect earthworms from the harmful effects of ingested polyphenols. We have identified the key mechanism for adaptation to a dietary challenge in an animal group that has a major role in organic matter recycling in soils worldwide.