Iraqi federal police forces are seen at the front line against Islamic State militants in the eastern suburbs of Ramadi in the Anbar province of Iraq. (AP)

The expanding U.S. military campaign against the Islamic State group in Iraq relies in part on an uneasy, arms-length partnership with Shiite militias backed by Iran — organizations that were once relentlessly effective killers of U.S. troops.

Now, as the campaign enters its second year, there are signs that this awkward alliance may be fraying: militia threats of renewed attacks on U.S. personnel, a greater U.S. effort to bolster Sunni forces that are traditional adversaries of Iran and accusations that the U.S. air campaign has at times targeted Shiite forces.

The shared desire to defeat the Islamic State appears to be enough so far to keep the militias and the Americans working in common cause. But officials and experts said both sides know that their broader regional objectives are in conflict.

“Let’s be frank,” a senior U.S. military official said. “They are watching us, and we are watching them.”

The Pentagon says it does not coordinate with Iranian-backed units. But since June 2014, when a lightning Islamic State advance pulled the United States back into military operations in Iraq, both sides have developed informal arrangements for avoiding conflict. For the United States, that has mainly meant carefully mapping locations of Iran-backed militias before launching airstrikes and making sure U.S. advisers keep their distance.

In at least one case, that has put U.S. personnel and Iranian-backed operatives on opposite corners of the same base.

In the winter, U.S. officials began to explore setting up an outpost at Taqaddum, an air base in an eastern area of Anbar province, where they hoped U.S. forces could help Iraqi troops organize tribal fighters in a way that had paid off elsewhere in Anbar.

But Iraqi military officials cautioned their American counterparts that 30 to 50 operatives from Kataib Hezbollah, an Iranian-backed militia that the United States considers a terrorist group, were already positioned at the base.

In the spring, American officials turned to Prime Minister Haider al-Abadi with an appeal: Before U.S. troops could arrive, the officials told the prime minister, they needed distance between Americans and the Iran-backed fighters.

Eventually, the militiamen picked up and moved to a different part of the sprawling base. “That provided the geographic space,” the official said, required to establish the American mission there, which is helping Iraqis plan an offensive for the nearby city of Ramadi.

In the chaotic weeks following the fall of the Iraqi city of Mosul, American officials were unsure how those militias would respond to the White House plan to renew U.S. military operations in Iraq. Unlike the last war, when a force of more than 160,000 fought house-to-house across Iraq, the U.S. military planned to establish a small presence, limited to advising local forces and retraining the Iraqi army.

“In the early days, we were working towards similar goals, so we didn’t have major problems with them,” said a senior U.S. defense official, who like others spoke on the condition of anonymity to discuss planning. At the same time, Iranian-backed brigades, some of which had already been fighting in Syria, had vowed to halt the rise of the Islamic State. “We were all responding to this emergency situation.”

But American officials had reason to be nervous. The Pentagon blamed Iranian-backed groups such as Kataib Hezbollah and Asaib Ahl al-Haq for the death of at least 500 U.S. service members in the 2003-2011 war.

Orchestrating the unexpected return to Iraq was Gen. Lloyd J. Austin III, the head of U.S. Central Command, whose feelings about the militias were personal. Austin had served as operational commander in Iraq in 2008-2009, when militias were pounding American bases with powerful weapons whose production U.S. officials said was supported by Iran. He later became the top commander in Iraq. Planning renewed operations in 2014, Austin carried those earlier losses with him, officials said.

When U.S. military officials arrived in Baghdad, they saw that Iranian influence had grown in Baghdad in the years following the U.S. departure in 2011. It had expanded even further when, after the fall of Mosul, Iran was quicker than the United States to offer expansive military aid.

Lukman Faily, Baghdad’s ambassador in Washington, signaled that Iraq values assistance from the United States and Iran. “We will try to accommodate all sides to the nth degree, because we all face a common threat with Daesh,” he said, using another name for the Islamic State.

The militias’ ranks had swollen after Grand Ayatollah Ali Sistani, the top Shiite cleric in Iraq, issued a general call to arms in response to the Islamic State. That resulted in the mobilization of tens of thousands of Shiite volunteers. While many of those fighters formed new units known as “population mobilization” fighters under government auspices, others joined existing militia groups such as the Badr Organization, which had been one of the most powerful Shiite forces since early in the Iraq war.

The popularity of such militias soared as the paramilitaries raced to respond to atrocities by the Islamic State. Their zeal provided comfort to American officials, who worried about exposing a small military force to the risk of conflict with former adversaries.

“Analytically, there was a sense that they hated ISIS more than they hated us,” said Derek Chollet, who was a senior Pentagon official as the Obama administration made its plans to resume operations in Iraq. ISIS and ISIL also are alternate names for the Islamic State.

In the initial months, American operations — mostly airstrikes — were focused in northern and western Iraq, while militia commanders focused efforts in Baghdad and the east.

But that segregation did not last. “As the geography and the battle space changed, so has our level of concern,” said the defense official who spoke on the condition of anonymity.

The potential for overlap became clear in the town of Amerli, where Shiite Turkmen residents were under siege by the Islamic State. By late August, as food and water grew short, the Pentagon authorized airstrikes. On the ground, a mix of Iraqi troops, peshmerga fighters and Shiite militiamen poured into the town. Pictures circulated online showing a visit by Qasem Soleimani, the head of Iran’s formidable Quds Force.

While U.S. officials said militia fighters were positioned away from Iraqi forces when the strikes took place, Amerli created the uncomfortable perception for Pentagon officials that the United States was providing air support for Iran-backed groups.

The battle to reclaim the Sunni city of Tikrit in the spring offered the United States a chance to crystallize a new methodology for navigating the militia presence on the battlefield.

Tikrit was of particular importance for militias following the Islamic State’s massacre of as many as 1,700 young Shiite soldiers. But the militia-led assault soon lost steam, and Iraqi leaders began to call for U.S. air power.

In March, in one of their regular meetings with Abadi, U.S. officials unfurled a map of the Tikrit area on a table in the premier’s office and discussed with him the positions of militia units and those of government forces around the city.

On March 25, American planes began dropping munitions over Tikrit. While U.S. strikes allowed Iraqi forces and some government-affiliated Shiite volunteer fighters to fight their way into the city, the Iranian-backed militias remained on the outskirts. Only later did they enter the city.

“Tikrit for us was an important moment for making clear what we would and would not do for supporting forces that are not the ISF,” the senior defense official said, referring to Iraqi security forces. “We have a baseline now for how we act and interact with these forces.”

The battle for Ramadi will be the next test for the Obama administration’s attempt to draw lines among the paramilitaries, one they hope will keep the uneasy peace among those jointly fighting the Islamic State.

But the militia backlash has also intensified. Some groups posted propaganda videos of past attacks on U.S. forces and pictures of weapons used in those attacks, such as a crude rocket system called an IRAM, with reminders of their utility against “American occupiers,” according to Phillip Smyth, a scholar at the University of Maryland.

“It was them doing the same thing they did last time in Iraq,” Smyth said. “I would actually argue it amounts to them trying to craft a casus belli” for a potential future resumption in U.S.-militia violence.

In May, angered by a proposal in the Congress, influential cleric Moqtada al-Sadr threatened to remobilize his feared militia. The following month, the head of Harakat Nujaba vowed “revenge” against U.S. forces he said were involved in a blast that killed other militia fighters. And last month, Asaib Ahl al-Haq leader Qais al-Khazali accused Washington of exploiting the Islamic State to achieve its goals in the Middle East. The Obama administration denies hitting any paramilitary units.

Ahmed Ali, a researcher with the American University of Iraq-Sulaimani, said the militias may value American operations, especially air power, more than they let on.

“In their minds, in their media, in their ideology, they cannot be openly aligned with the United States,” Ali said. “That is not their brand. Their brand is anti-American.”

Neither can the Iranian-backed groups afford to cross Iraq’s Shiite religious authorities, who have urged Iraqis to avoid sectarian clashes and could hold rogue units accountable.

Among some U.S. officials, the militias are regarded with grudging respect.

“Let’s give them some credit. They did get on their feet and push back in a pretty significant way,” another senior U.S. official said. “They organized and they fought, and they took a lot of casualties. And in their mind, they did it on their own without any help from anyone . . . except for Iran.”

Mustafa Salim in Baghdad contributed to this report.