WASHINGTON — Top U.S. military commanders, who only a few months ago were planning to pull the last American troops out of Afghanistan by year's end, are now quietly talking about an American commitment that could keep thousands of troops in the country for decades.

The shift in mindset, made possible by President Barack Obama's decision last fall to cancel withdrawal plans, reflects the Afghan government's vulnerability to continued militant assault and concern that terror groups like al-Qaida continue to build training camps whose effect could be felt far beyond the region, said senior military officials.

The military outlook mirrors arguments made by many Republican and Democrat foreign policy advisers, looking beyond the Obama presidency, for a significant long-term American presence.

"This is not a region you want to abandon," said Michèle Flournoy, a former Pentagon official who would likely be considered a top candidate for Secretary of Defense in a Hillary Clinton administration. "So the question is what do we need going forward given our interests?"

Senior American commanders have been surprised by al-Qaida's resilience and ability to find a haven in the Afghan countryside as well as the Taliban's repeated seizure of large tracts of contested territory.

In November, the U.S. military sent a company of elite U.S. Rangers to southeastern Afghanistan to help Afghan counter-terrorism forces destroy an al-Qaida training camp in a "fierce fight" that lasted for several days.

The training camp was "absolutely massive," said Brig. Gen. Wilson Shoffner, a military spokesman in Afghanistan.

"No matter what happens in the next couple of years Afghanistan is going to have wide ungoverned spaces that violent extremist organizations can take advantage of," Shoffner said. "The camp that developed in southeastern Kandahar is an example of what can happen."

In Afghan President Ashraf Ghani, U.S. officials said they have a willing and reliable partner who can provide bases to attack terror groups not just in Afghanistan, but also throughout South Asia for as long as the threat in the chronically unstable region persists.

The new American mindset also marks a striking change for Obama, who campaigned on a promise to bring American troops home and has said repeatedly that he doesn't support the "idea of endless war." And it highlights a major shift for the American military, which has spent much of the last decade racing to hit milestones as part of its broader "exit strategy" from Afghanistan and Iraq. These days, that phrase has largely disappeared from the military's lexicon.

In its place, there's a broad recognition in the Pentagon that building an effective Afghan Army and police force will take a generation's commitment, including billions of dollars a year in outside funding and constant support from thousands of foreign advisers on the ground.

"What we've learned is that you can't really leave," said a senior Pentagon official with extensive experience in Afghanistan and Iraq. "The local forces need air support, intelligence and help with logistics.They are not going to be ready in three years or five years. You have to be there for a very long time."

There are now 9,800 U.S. troops in Afghanistan, some of them advising local forces and some focused on hunting down al-Qaida and other hardline militants. Current plans call for Obama to halve that force by the time he leaves office, but he could defer the decision to the next president.

The U.S. military's current thinking reflects its painful experience in Iraq, where Iraqi Army forces collapsed less than three years after American forces left in 2011. Senior American commanders who spent nearly a decade and hundreds of billions of dollars of taxpayer money building the Iraqi Army have been shocked at how little of that force remains today.

"The speed and extent of the withdrawal in Iraq is a cautionary tale," said Flournoy, who now heads the Center for a New American Security.

In Helmand Province, for example, where American troops suffered the heaviest losses of the war, Afghan units have struggled to hold onto territory taken by American forces from the Taliban in 2011 and 2012. "There's a real will-to-fight issue there," said a senior military official in Kabul.

Senior American commanders said that the Afghan troops in the province have lacked effective leaders as well as the necessary weapons and ammunition to hold off persistent Taliban attacks. Some Afghan soldiers in Helmand have been fighting in tough conditions for years without a break to see their family, leading to poor morale and high desertion rates.

Gen. John Campbell, the top American commander, has sent special operations forces to the province to help direct American airstrikes and provide help with planning. An American soldier was killed and two others were wounded earlier this month fighting alongside the Afghans.

In addition, about 300 U.S. troops in Helmand are advising Afghan commanders at the Corps level, well removed from the front lines.

The American support is designed to arrest the immediate losses, but building an effective and sustainable fighting force that can manage contested areas such as Helmand Province, will take many years, said U.S. military officials.

The Afghan units lack effective mid-level officers and sergeants, foreign officials say, who can lead troops in combat and aren't captive of patronage networks that dominate the country and sap soldier morale. Seeding the force with mid-level officers often requires bringing in young leaders from outside of the current system and training them from scratch.

"I think a generational approach has value," Shoffner said.

Senior American officials point to improvements in areas such as evacuating wounded troops from the battlefield. As recently as 2013 it took the Afghan Army 24 hours on average to get medical assistance to wounded troops. Now help usually arrives in four hours, still longer than desired. But other critical areas, such as building an effective resupply system for the country or a capable air force, can't be accomplished in a few years. Many of the American pilots flying close air support missions for the Afghan military have 10-15 years of experience.

"How long does it take to grow a 15-year pilot? It takes about 15 years," Shoffner said. "We're starting a little late with the Air Force."

Senior U.S. military officials and some former Obama administration officials increasingly compare U.S. plans for Afghanistan with its approach to South Korea, where the United States has maintained tens of thousands of troops for decades. Other top officials cite the example of Colombia where the United States has long provided training, money and contractors.

"Our presence right now helps serve as a significant bulwark against instability, and at a cost that I think is reasonable to bear," said Daniel Feldman, who until recently served as the Obama administration's special representative for Afghanistan and Pakistan "Particularly if we're not proposing a significant combat role, I think the American people would be open to the argument of sticking with Afghanistan."​

The difference between Afghanistan and other long-term American commitments in Korea and Colombia is that Afghanistan remains a far more dangerous and unstable place for American personnel. Even though Afghan troops have assumed the lead combat role throughout the country with U.S. troops in an advisory role, Americans still face real dangers and have taken recent casualties there.

In some cases, senior U.S. officials have been surprised by the Taliban comeback in the last year. Emboldened by the departure of most foreign forces, Taliban fighters have seized district centers, inflicted heavy losses on government forces, and temporarily overran a provincial capital last fall. Now, Afghan forces must also grapple with an aggressive local branch of the Islamic State.

Some officials hold out hope that a long-term military presence might be unnecessary, if hoped-for peace talks with the Taliban make progress. The Afghan government has asked Pakistan, home to many Taliban leaders, to push the militants into talks.

A generational U.S. footprint "doesn't need to be the case," said Jeff Eggers, a former senior White House official with long experience working on Afghanistan and Pakistan. "The Korea model is not necessary if the peace process moves forward -- that's the preferred path for all parties."

The obstacles to peace talks, though, are huge. Senior officials in Kabul and Islamabad are riven by suspicion and the Taliban remains deeply fractured following the revelation that its long time leader Mullah Omar has been dead for several years.