For years, Iraq has managed to pull off an unlikely balancing act, allowing armed forces tied to both the United States and Iran, an American nemesis, to operate on its soil.

Now things are getting shaky.

When Washington, Tehran and Baghdad all wanted the same thing — the defeat of the Islamic State terrorist group — the relationships were fairly tenable, but in recent months, as the war in the Gaza Strip sends ripples across the region, American and Iranian-backed forces have clashed repeatedly in Iraq and Syria. A U.S. strike on one of those militias last week killed 16 Iraqis, and Iraq is saying it has had enough.

“Our land and sovereign authority is not the right place for rival forces to send messages and show their strength.” the office of Prime Minister Mohammed Shia al-Sudani said in a statement on Sunday.

For many years, both Iran and the United States had their proponents within the Iraqi government, and the Iranian-backed armed groups and the American troops lived in a tolerable if uneasy balance.

That started to change in 2020 after the United States killed one of Iran’s top security and intelligence commanders, Gen. Qassim Suleimani, a widely revered figure at home, in a drone attack as he was visiting Iraq. The Iranians began pushing hard for the U.S. military to be ejected.

Iraqi leaders resisted, in part because of divisions over which country Iraq should lean toward. Even after 2022, when parties close to Iran were able to form a government, there was a notable distinction between what Iraq officials said about the United States publicly and what they said in private.