An implantable system terminates atrial fibrillation by applying optimized anti-tachycardia pacing (ATP). In one implementation, the system senses and paces at multiple sites on the left atrium. At each site, the system senses reentrant circuits causing the atrial fibrillation. In one implementation, the system applies ATP tuned to the frequency of the reentrant circuit at the electrode that senses the most regular reentrant circuit. In another implementation, the system applies ATP at multiple electrodes, delivering each pulse at each site when the excitable gap is near the site. In other variations, the ATP is optimized for different patterns of sequential, simultaneous, or syncopated delivery to terminate the atrial fibrillation. The system can also monitor multiple heart chambers for cardiac events that favor terminating atrial fibrillation via ATP. The system then times delivery of the ATP according to these cardiac events.