Iran’s leaders are leery of siding publicly with Hamas because of the potential consequences of an Israeli victory. A Hamas defeat by Israel would deprive Iran not only of a valuable proxy force on the border with Israel but of a trump card to play with Washington, and it would further alienate it from the leadership of Egypt, Jordan and Saudi Arabia.

“Iran wants to sit at the negotiating table with Obama with all the cards of the region in hand: Palestine, Lebanon, Iraq, the relationship with Syria,” said Mustafa el-Labbad, an Iranian expert based in Cairo. “They are also being smart. They’re trying not to antagonize the U.S. very much, but with the Arabs they are going at it very hard, very roughly.”

For months, even before Israel invaded Gaza to stop rocket fire, Iranian officials and their proxies had been viciously attacking Egyptian and Saudi leaders for not doing enough to end the Israeli-imposed blockade of Gaza. But Iran does not function as a purely ideological state, and it does not operate from one center of power. When events started growing too hot, Ayatollah Khamenei stepped in and cooled talk of thousands of martyrs streaming to Gaza.

Commentators in Iran said that Ayatollah Khamenei decided it was time to act as a pragmatic brake on the ideologically radical president, Mahmoud Ahmadinejad. The president and his allies had stoked rage across Iran over what they said was the failure of Arab leaders to do more to end the Israeli fighting. But there was a big difference between rhetorical support, even encouragement, and direct involvement.

“Hamas is a very practical and useful tool for Iran, not an ideological one at all,” said Saeed Leylaz, an economist and political commentator in Tehran. “It is a very good tool for Iran, especially in its dealings with the U.S. What is happening right now in Gaza has made it easier for Iran to promote anti-U.S. feelings among Muslims.”