Experts say Tehran miscalculated that Mr. Trump’s desire to avoid war with Iran would restrain the American military if its forces were attacked. Kirsten Fontenrose, a former National Security Council official now with the Atlantic Council, said the current upheaval and protests in Iraq had allowed Iranian proxies to “operate unfettered.” Iran’s stepped-up activity convinced the United States that it needed to act.

“We knew Iran would pursue a policy of continued low-level escalation with the U.S. and allies at least through the U.S. elections,” she said. “Iran knew that President Trump does not want an all-out war. Iran’s mistake was in assuming that U.S. strategic thinking would not adapt to conditions on the ground.”

While Mr. Trump has sought to wind down the American war against the Islamic State in Iraq and Syria, he has built up forces meant to deter against Iranian attacks. Trump administration officials have also said American forces were in Iraq to help counter Iranian influence in Iraq and the region.

As rocket attacks by Iranian proxies have increased in recent weeks, some Defense Department officials have been worried that the situation could escalate beyond the kind of shadow conflict that the United States and Iran have been engaged in.

The death of the American contractor and the response by the Pentagon could potentially lead to further escalation. Iran could respond with a renewed roadside bomb campaign or more powerful rocket and missile attacks, a move that would most likely result in a more aggressive response by the United States.

American military commanders have warned for months about a growing risk of attacks by Iranian proxy forces on American interests and forces in the region, as Tehran chafes against the Trump administration’s renewed economic sanctions and campaign to force it to renegotiate the 2015 nuclear deal.

So far Iran and its proxies have mostly focused on American allies and partners. The United States has accused Iran of striking at oil tankers in the Arabian Sea and launching drone and cruise missile attacks on Saudi oil facilities. Iran also shot down an American drone.