Updated

After lengthy delays, U.S. and Afghan officials signed a security pact Tuesday to keep American troops in Afghanistan beyond year's end, aiming to prevent the country from descending into the kind of chaos that has plagued Iraq following the Pentagon's withdrawal.

President Barack Obama has pressed hard for the agreement, but his comments about the situations in the two countries have brought criticism from some quarters. While he has touted the Afghan accord as crucial to protecting progress in the fight against al-Qaida, he's also insisted that had he reached a similar pact with Iraq, it would have done little to stop the rise of the Islamic State militants now wreaking havoc there and in neighboring Syria.

"The only difference would be we'd have a bunch of troops on the ground that would be vulnerable," Obama said in August, shortly after authorizing airstrikes in Iraq. "And however many troops we had, we would have to now be reinforcing, I'd have to be protecting them, and we'd have a much bigger job."

The president and his advisers have repeatedly said they were left with no choice but to withdraw from Iraq. Under an agreement signed by former President George W. Bush, U.S. troops had to leave by the end of 2011 unless an extension was signed.

Negotiations over the terms of a new deal collapsed when it became clear that Iraq's parliament would not give American forces immunity from prosecution, as is typical of such agreements. Obama administration officials also rejected former Iraqi Prime Minister Nouri al-Maliki's offer to sign an executive order granting Americans immunity.

But White House critics, as well as some former administration officials, have suggested that Obama put his desire to end the Iraq war ahead of concerns about the security vacuum the U.S. might leave behind. The president has repeatedly heralded the withdrawal of American forces as the fulfillment of his campaign pledge to bring the unpopular war to a close.

Vali Nasr, who served as a State Department adviser during Obama's first term, said, "The administration's leaning was to say we're going to leave, we really want to find all of the reason why we're able to leave Iraq." What's happened to Iraq since then, he said, appears to have affected the way the administration views the necessity of staying in Afghanistan.

"There's some motivation to avoid Afghanistan turning into a crisis of ISIS magnitude," said Nasr, who is now dean of the Johns Hopkins School of Advanced International Studies. He referred to the Islamic State group by one of its many names.

Even before the rise of the Islamic State group, the White House showed enormous flexibility in trying to get the Afghan deal done. U.S. officials first warned their Afghan counterparts that if the security accord was not signed by the end of 2013, the Pentagon would have to start planning for a full withdrawal. But when the year ended, the White House moved back the deadline, saying Afghan President Hamid Karzai needed to sign off within weeks.

Karzai surprised U.S. officials by ultimately saying he would not sign the accord and would instead leave that task for his successor. But the results of the race to replace Karzai took months resolve, finally coming to a conclusion Monday with the swearing in of Ashraf Ghani Ahmadzai as Afghanistan's second elected president.

Ahmadzai signed the security agreement Tuesday, nearly one year after the White House's initial deadline.

House Speaker John Boehner, R-Ohio, cast the agreement as a key step in completing the U.S. mission in Afghanistan.

"This is what should have happened in Iraq, and it's essential that the Obama administration does not repeat the same mistakes that it made there," he said.

The agreement provides a legal framework for the United States to keep about 9,800 troops in Afghanistan to train, advise and assist that country's national security forces after the current international combat mission ends Dec. 31. Obama announced earlier this year that he would cut the number of troops in half by the end of 2015 and leave only about 1,000 in a security office after the end of 2016, as his presidency comes to a close.

The Afghan government also is expected to sign an agreement this week with NATO that would outline the parameters for 4,000 to 5,000 additional international troops — mostly from Britain, Germany, Italy and Turkey — to stay in Afghanistan in noncombat roles after the end of this year.

U.S. military officials say al-Qaida is in "survival mode" in Afghanistan after 13 years of war, but that if all international forces left, the terrorist network would see it as a victory, regroup and again use the region to plan and conduct operations against the West.

Some White House critics have questioned whether Obama's decision to publicly choreograph plans to withdraw troops in 2016 allows al-Qaida to simply wait the U.S. out.

"What good really is a (bilateral security agreement) that has an endpoint?" Sen. John McCain, R-Ariz., said in an interview. "It's almost pointless."

Indeed, there are serious questions about the ability of the Afghan security forces to take on the militants, even with a residual U.S. force. While Obama has insisted the Afghan war will be over by the time he leaves the White House, the security agreement with Afghanistan does allow for U.S. troops to stay in the country for 10 more years.

"He would pay a tremendous political price," Nora Bensahel, a senior fellow at the Center for a New American Security, said of the prospect Obama could keep troops in Afghanistan beyond 2016. "But he has the option to pay that political price if he wants."

___

Associated Press writer Rahim Faiez in Kabul, Afghanistan, contributed to this report.

___

Follow Julie Pace at http://twitter.com/jpaceDC