Ray Takeyh is a senior fellow at the Council on Foreign Relations.

On the surface, there is not much that commends Iran’s Supreme Leader Ayatollah Ali Khamenei. An anti-Semite, he has frequently questioned the Holocaust and defamed Israel in despicable terms. As a conspiracy theorist, he endlessly weaves strange tales about the United States and its intentions. As a national leader, he has ruthlessly repressed Iran’s once-vibrant civil society while impoverishing its economy. And yet Khamenei is also a first-rate strategic genius who is patiently negotiating his way to a bomb.

After years of defiance, Khamenei seems to appreciate that his most advantageous path to nuclear arms is through an agreement. To continue to build up his atomic infrastructure without the protective umbrella of an agreement exposes Iran to economic sanctions and the possibility of military retribution. While in the past Khamenei may have been willing to cross successive U.S. “red lines,” the price of such truculence was financial stress that he feared could provoke unrest. Unlike many of his Western interlocutors, Khamenei appreciates that his regime rests on shaky foundations and that the legitimacy of the Islamic revolution has long been forfeited. The task at hand was to find a way to forge ahead with a nuclear program while safeguarding the regime and its ideological verities.

In many ways, a nuclear agreement is the answer to Khamenei’s multiplicity of dilemmas. A good agreement for the supreme leader, however, has to be technologically permissive and of a limited duration. Since the exposure of Iran’s illicit nuclear program in 2002, its disciplined diplomats have insisted that any accord must be predicated on the Nuclear Non-Proliferation Treaty (NPT), which, in their telling, grants Iran the right to construct a vast nuclear infrastructure. In exchange for such a “right,” they would be willing to concede to an inspection regime within the leaky confines of the NPT. And for much of that time, the great powers rebuffed such presumptions from a state that has been censured by numerous U.N. Security Council resolutions and denies the International Atomic Energy Agency reliable access to its facilities and scientists.

As Khamenei held firm, however, the great powers grew wobbly. With the advent of the Joint Plan of Action in November 2013, Iran’s fortunes began to change. Washington conceded to Iran’s enrichment at home and agreed that eventually that enrichment capacity could be industrialized. The marathon negotiations since have seen Iran attempt to whittle down the remaining restrictions, while the United States tries to reclaim its battered red lines. For Khamenei, the most important concession that his negotiators have won is the idea of a sunset clause. Upon the expiration of that clause, there would be no legal limits on Iran’s nuclear ambitions. If the Islamic Republic wants to construct hundreds of thousands of sophisticated centrifuges, build numerous heavy-water reactors and sprinkle its mountains with enrichment installations, the Western powers will have no recourse. And once Iran achieves that threshold nuclear status, there is no verification regime that is guaranteed to detect a sprint to a bomb. An industrial-size nuclear state has too many atomic resources, too many plants and too many scientists to be reliably restrained.

As Khamenei presses toward an accord that will place him in an enviable nuclear position, he can also be assured that technical violations of his commitments would not be firmly opposed. Once a deal is transacted, the most essential sanctions against Iran will evaporate. It is unlikely that Europeans, much less China or Russia, would agree to their reconstitution should Iran be caught cheating. And as far as the use of force is concerned, the United States has negotiated arms-control compacts for at least five decades and has never used force to punish a state that has incrementally violated its treaty obligations. As the reaction to North Korea’s atomic provocations shows, the international community typically deals with such infractions through endless mediation. Once an agreement is signed, too many nations become invested in its perpetuation to risk a rupture.

Iran’s achievements today are a tribute to the genius of an unassuming midlevel cleric. In a region where many dictatorial regimes have collapsed, the Islamic Republic goes on. Khamenei is in command of the most consequential state from the Persian Gulf to the banks of the Mediterranean. He has routinely entered negotiations with the weakest hand and emerged in the strongest position. God is indeed great.