Perhaps the Obama administration has learned its lesson from the collapse of Iraq. After more than a year of negotiation and the delayed conclusion of a presidential election, the Afghan government has signed a bilateral security agreement that will keep some US bases open and leave 9,800 American troops in the country for counter-terrorism operations and logistical support:

The United States and Afghanistan on Tuesday signed a vital security deal that allows some American troops to remain in Afghanistan beyond this year, ensuring a continuing U.S. presence in the region.

The Bilateral Security Agreement allows for 9,800 U.S. soldiers to stay in the country past 2014 to help train, equip and advise Afghan military and police forces. It arrives as the Taliban Islamist movement is increasingly attacking areas around the country in an effort to regain control as most foreign troops are scheduled to leave by the end of the year.

The signing was undertaken a day after Ashraf Ghani was sworn in as Afghanistan’s new president in a power-sharing government in the first democratic handover of power in the nation’s history. Ghani’s predecessor, Hamid Karzai, who had presided over the country since the U.S.-led invasion in 2001, had refused to sign the agreement, souring relations with Washington.

A separate agreement will keep other NATO forces in the country as well. The outline of this deal has been widely known for a few months, as the Hill notes:

President Obama announced he was looking for a “responsible end” to the war in Afghanistan during his visit to the country in May. At the time, he outlined the administration’s plan to draw down all but 9,800 of the 32,000 troops that remained in the country by the end of the year. That number will fall even lower by 2016.

A number of Republicans have blamed the current unrest in Iraq and the rise of Islamic militants there on the failure of the United States and the Iraqi government to come to a status of forces agreement after U.S. combat missions ended there.

Of course, the war isn’t coming to an end in Afghanistan any more than it came to an end in Iraq. The Taliban have picked up their efforts as the US prepared to leave, and will no doubt continue to pressure Kabul politically as well as militarily for years to come. The best that the US can do in Afghanistan is attempt to keep the Afghan security forces from collapsing while all sides tire of the fight and find a way to settle the tribal wars that have been ongoing since the Soviet withdrawal.

That is a tougher task than the US faced in 2011 in Iraq. American intervention had largely settled the tribal/sectarian conflict in Iraq by acting as guarantor for real power-sharing in Baghdad. By 2008, the surge had marginalized AQI and had the tribal leaders in the west looking for ways to partner with the Shi’ites in government and in the military. The three years of malign neglect that followed allowed Nouri al-Maliki to poison the atmosphere, and our withdrawal betrayed the Sunni tribal leaders that had allied with us and gave Maliki carte blanche to purge the Sunnis and Kurds from all aspects of governance and security. Had the Obama administration followed the advice of his military advisers and stayed in Iraq the way he’s staying in Afghanistan, ISIS’ genocidal sweep would likely have never occurred.

The residual-force arrangement may not prove successful in keeping Afghanistan from collapse, but at least they show that someone’s learned a lesson from the American withdrawal from Iraq.