Bacterial heterodimeric tryptophan-containing diketopiperazines (HTDKPs) are a growing family of bioactive natural products. They are challenging to prepare by chemical routes due to the polycyclic and densely functionalized backbone. Through functional characterization and investigation, we herein identify a family of three related HTDKP-forming cytochrome P450s (NasbB, NasS1868 and NasF5053) and reveal four critical residues (Qln65, Ala86, Ser284 and Val288) that control their regio- and stereo-selectivity to generate diverse dimeric DKP frameworks. Engineering these residues can alter the specificities of the enzymes to produce diverse frameworks. Determining the crystal structures (1.70-1.47 Å) of NasF5053 (ligand-free and substrate-bound NasF5053 and its Q65I-A86G and S284A-V288A mutants) and molecular dynamics simulation finally elucidate the specificity-conferring mechanism of these residues. Our results provide a clear molecular and mechanistic basis into this family of HTDKP-forming P450s, laying a solid foundation for rapid access to the molecular diversity of HTDKP frameworks through rational engineering of the P450s.