The defensive alkaloid gramine not only protects barley and other grasses from insects but also negatively affects their palatability to ruminants. The key gene for gramine formation has remained elusive, hampering breeding initiatives. In this work, we report that a gene encoding cytochrome P450 monooxygenase CYP76M57, which we name AMI synthase (AMIS), enables the production of gramine in Nicotiana benthamiana, Arabidopsis thaliana, and Saccharomyces cerevisiae. We reconstituted gramine production in the gramine-free barley (Hordeum vulgare) variety Golden Promise and eliminated it from cultivar Tafeno by Cas-mediated gene editing. In vitro experiments unraveled that an unexpected cryptic oxidative rearrangement underlies this noncanonical conversion of an amino acid to a chain-shortened biogenic amine. The discovery of the genetic basis of gramine formation now permits tailor-made optimization of gramine-linked traits in barley by plant breeding.