BEIRUT — As President Trump has been ramping up sanctions and threats against Iran over the past eight months, Tehran has been operating on the assumption that he would not dare risk an outright war, especially with an election approaching.

The drone strike that ended the life of Maj. Gen. Qasem Soleimani, the strategy’s architect and chief enforcer, blew up that calculation, creating a stunning new dilemma for Iran’s leaders as they look for ways to retaliate — without the benefit of their master strategist.

That Iran will retaliate is not in question, analysts say. Not to do so would be a sign of weakness that could jeopardize the enormous influence Iran has gained in the region over the past four decades.

The targeted killing of Soleimani eroded the deterrent that Iran believed it had established with a campaign of attacks against shipping in the Persian Gulf and rocket strikes on bases in Iraq, said Kamel Wazne, a Beirut-based political analyst. From Iran’s point of view it is now imperative to restore that, he said.

“Iran has to retaliate, and it will be a retaliation to restore the deterrence lost by this assassination,” he said.

But he and other analysts also believe Iran also has no appetite for a full-scale war with the United States that would deplete its already precarious finances and leave it heavily outgunned. Iran, analysts say, has to calibrate its response — inflicting enough damage on the United States that it is seen to be avenging Soleimani’s death without precipitating an all-out war.

The question is how?

As the Middle East braced for the answer, fearful of any misstep by either side, hints began to emerge that Tehran is in no hurry to inflict the “harsh revenge” promised by Iran’s supreme leader, Ayatollah Ali Khamenei.

Eulogies for Soleimani from Iranian leaders have emphasized “patience” as much as vengeance. Iranian military officials have suggested that Iran’s regional proxies, rather than Iran itself, will be charged with responding.

“The Islamic Republic of Iran refuses to take any emotional and hasty action,” Brig. Gen. Abolfazl Shekarchi, a spokesman for the armed forces, told Iran’s Mehr news agency on Saturday.

“We will set up a plan, patiently, to respond to this terrorist act in a crushing and powerful manner,” he said. “We are the ones who set the time and place of our reciprocal response.”

Already a narrative is emerging among Iranian commentators and officials that the United States is seeking to drag Iran into a war and that Iran should not fall into the trap.

Brig. Gen. Ali Fadavi told Iranian State TV that although Soleimani was the senior commander of Iran’s most powerful branch of the prestigious Revolutionary Guard, it might not be Iran that strikes back.

“The great front of the resistance is geographically ready to carry out this harsh revenge,” he said, a reference to the wider network of Iranian proxy forces, such as Lebanon’s Hezbollah, known in Iranian circles as the “Axis of Resistance.”

That Iran has plenty of options is not in doubt. In his role as the commander of the Quds Force — the wing of the powerful Islamic Revolutionary Guard Corps’ charged with exporting Iran’s revolutionary ideology — Soleimani built a network of loyalist militias in Iraq, Syria, Lebanon and Yemen that can be called upon to shield Tehran from direct implication in attacks on U.S. or allied targets.

Houthi rebels in Yemen claimed responsibility for a major attack on Saudi oil facilities in September, even though the U.S. government asserted the missile strikes were launched from Iran. In Iraq, U.S. military officials charge that the Iran-backed Kataib Hezbollah militia has been carrying out a string of rocket attacks against Iraqi military bases where U.S. troops are located, one of which killed a U.S. contractor late last month, triggering the unexpected escalation that culminated in Soleimani’s killing.

On Saturday, Trump warned Iran that any strikes against “Americans or American assets” will bring U.S. retaliation against 52 unspecified Iranian targets — “representing the 52 American hostages” held at the former U.S. Embassy in Tehran after Iran’s 1979 Islamic revolution.

“Iran itself, WILL BE HIT VERY FAST AND VERY HARD. The USA wants no more threats!” Trump tweeted.

In years gone by, Iranian allies have blown up American embassies and kidnapped American citizens with devastating effect, driving American troops and diplomats out of Lebanon in the 1980s and propelling the ascent of the Iranian-allied Hezbollah movement there.

Riad Kahwaji, who heads the Inegma defense consultancy in Dubai, listed a range of options Iran or its allies could deploy. They include renewed attacks on Persian Gulf shipping, strikes against U.S. or allied military bases around the region, bombs at American embassies and attempts to assassinate or abduct American officials and citizens.

But it remains unclear what Iran can do that surpasses the attacks it has already carried out without provoking a bigger escalation.

Since May, Iran has been harassing ships and firing rockets at American troops in Iraq as part of a strategy overseen by Soleimani to punish the United States for pulling out of the Iran nuclear deal and imposing the harsh economic sanctions that have devastated Iran’s economy.

Multiple tweets by Trump deriding “endless wars” in the Middle East, his abrupt withdrawal of troops from Syria, and his last-minute retreat from a threat to retaliate for the downing of an American drone had all fueled confidence in Tehran that Iran could play on Trump’s aversion to war by needling him with relatively small-scale attacks and perhaps weaken his resolve.

But what more can Iran do that “it has not already done?” Rasha al-Aqeedi, who edits the Iraqi newsletter Irfaa Sawtak, said in a tweet. “Kidnap foreigners? Strike military bases? Sack embassies? Assassination attempts? Target GCC oil interests? Send out militias to crackdown on adversaries?”

Iran is facing constraints on its room to maneuver in many of those places, said Hanin Ghaddar, of the Washington Institute for Near East Policy. So extensive is the reach of its influence that its allies have become integral components of the governments in Iraq and Lebanon, making Iran a target of widespread anti-government protests in recent months.

Militias backed by Soleimani have played a big part in crackdowns against the protesters in both countries, and there were videos showing celebrations by some Iraqi activists at the news of his death.

“Iran cannot go to war in the region. In Iraq their options are becoming very limited for them because any escalation in Iraq exposes them to more attacks by the U.S.,” Ghaddar said. “In Lebanon it’s going to be difficult because of the financial crisis. They cannot fund a war in Lebanon or anywhere else.”

The most likely arena for an escalation is in Iraq, where the spiral of confrontation began and where Soleimani was killed, analysts say. Rockets fired Saturday into the Green Zone in Baghdad and at Balad Air Base, where U.S. troops are present, suggested Iran intends to keep up the pressure on Iraq.

The killing of Soleimani on Iraqi soil and the airstrikes last Sunday that killed 25 members of the Kataib Hezbollah militia have fueled calls for the expulsion of U.S. troops from Iraq. On Saturday, the Iraqi army said it would impose new restrictions on the 5,000 U.S. troops in Iraq to help with the fight against the Islamic State.

"My sense is that we will see an escalation in Iraq," said Maha Yahya, director of the Carnegie Middle East Center. "But I don't think the Iranians really want a war with the U.S. I don't think they are interested in an all-out regional conflict."

"The problem is that all it takes is one small error and the whole region would be engulfed," she said.