While much is known about the biosynthesis of secondary metabolites by filamentous fungi their biological role is often less clear. The assumption is these pathways have adaptive value to the organism but often the evidence to support this role is lacking. We provide the first genetic evidence that the fungal produced secondary metabolite, peramine, protects a host plant from insect herbivory. Peramine is a potent insect feeding deterrent synthesized by Epichloë/Neotyphodium mutualistic endophytes in association with their grass hosts. The structure of peramine, a pyrrolopyrazine, suggests that it is the product of a reaction catalysed by a two-module non-ribosomal peptide synthetase (NRPS). Candidate sequences for a peramine synthetase were amplified by reverse transcription polymerase chain reaction. Four unique NRPS products were identified, two of which were preferentially expressed in planta. One of these hybridized to known peramine producing strains. This clone was used to isolate an Epichloë festucae cosmid that contained a two-module NRPS, designated perA. Nine additional genes, which show striking conservation of microsynteny with Fusarium graminearum and other fungal genomes, were identified on the perA-containing cosmid. Associations between perennial ryegrass and an E. festucae mutant deleted for perA lack detectable levels of peramine. A wild-type copy of perA complemented the deletion mutant, confirming that perA is a NRPS required for peramine biosynthesis. In a choice bioassay, plant material containing the perA mutant was as susceptible to Argentine stem weevil (ASW) (Listronotus bonariensis) feeding damage as endophyte-free plants confirming that peramine is the E. festucae metabolite responsible for ASW feeding deterrent activity.