Bacteria have evolved intricate innate immune systems against phage infection1-7. Gabija is a highly abundant prokaryotic defense system in bacteria and archaea consisting of two components, GajA and GajB8. We previously demonstrated that GajA functions as a DNA endonuclease and is inhibited by ATP9. However, the mechanism of the anti-viral defense by Gabija is elusive. Here, we utilize cryo-EM to capture the Gabija system in five states, GajA in apo or complex with DNA, GajA inhibited by ATP, and GajAB in apo or with ATP/Mg2+. Our structures show that GajA is a rhombus-shaped tetramer that binds DNA in a bipartite manner. GajA recognizes and bends the substrate DNA to align the DNA cleavage site with the catalytic residues in the Toprim domain. DNA binding requires the opening of the hinged clamps, which are locked in a closed conformation upon ATP-binding, therefore inhibited by ATP. GajB does not bind DNA alone but docks on the GajA tetramer and is activated by the nicked DNA generated by GajA. Thus, GajA is activated by local depletion of cellular ATP, generating nicked DNA that activates GajB and ultimately leads to cell death. Our study unveiled a ~500 kDa supramolecular assembly of GajAB and provided mechanistic insights into the Gabija system.