WASHINGTON — President Obama halted the withdrawal of American military forces from Afghanistan on Thursday, announcing that the United States will keep thousands of troops in the country through the end of his term in 2017 and indefinitely prolonging the American role in a war that has already lasted 14 years.

In a brief statement from the Roosevelt Room in the White House, Mr. Obama said he continued to oppose the idea of “endless war.” But the president, who once traveled to Afghanistan to declare “the light of a new day on the horizon,” said Thursday that a longer-term American presence there was vital to the security of the United States and a country that is beset by the Taliban, their allies from Al Qaeda, and militants from the Islamic State.

“While America’s combat mission in Afghanistan may be over, our commitment to Afghanistan and its people endures,” said Mr. Obama, flanked by Vice President Joseph R. Biden Jr. and top military leaders. “I will not allow Afghanistan to be used as safe haven for terrorists to attack our nation again.”

The decision, reached after what White House officials called an “extensive, lengthy review,” ensures that Mr. Obama will leave office in 15 months without making good on a seminal promise of his presidency: to responsibly end the military involvements started by his predecessor, George W. Bush, and withdraw all American troops from the battlefields of Iraq and Afghanistan.

While the president has succeeded in vastly reducing the American military presence in both countries, the United States has returned a modest force to Iraq to help Baghdad in its fight against the Islamic State. And with Thursday’s announcement, Mr. Obama leaves a commitment of thousands of troops — and the decision about how and when to end the war in Afghanistan — to his successor.

The current American force in Afghanistan of 9,800 troops will remain in place through most of 2016 under the administration’s revised plans, before dropping to about 5,500 at the end of next year or in early 2017, Mr. Obama said. He called it a “modest but meaningful expansion of our presence” in that country.

The president said he was not disappointed by a decision that clouds what he had said in 2012 was “a clear path to fulfill our mission in Afghanistan.” Mr. Obama said Thursday that the administration had always understood the potential for adjustments in troop levels even as the military sought to withdraw troops from battle.

But by making his announcement, Mr. Obama conceded that despite more than a decade of fighting, and years building the Afghan Army and the police — a project that has cost the United States more than $65 billion — the Afghan forces are still not fully up to the task of protecting their country.

The Taliban are now spread through more parts of the country than at any point since 2001, according to the United Nations. Last month they scored their biggest victory of the war, seizing the northern city of Kunduz and holding it for more than two weeks before pulling back.

Mr. Obama noted the dangers, saying, “In key areas of the country, the security situation is still very fragile, and in some areas, there is risk of deterioration.” After 2017, he said, American forces will remain on several bases in Afghanistan to “give us the presence and the reach our forces require to achieve their mission.”

He did not specifically mention Iraq, where a full troop withdrawal has been followed by a surge in violence from the Islamic State. But he said the mission in Afghanistan had the benefit of a clear objective, a supportive government and legal agreements that protect American forces — three factors not present in Iraq.

“Every single day, Afghan forces are out there fighting and dying to protect their country,” Mr. Obama said. “They’re not looking for us to do it for them.”

Image

American Army soldiers at a base in the Khogyani district of Afghanistan in August.CreditWakil Kohsar/Agence France-Presse — Getty Images

Officials said some of the American troops will continue to train and advise Afghan forces, while others will carry on the search for Qaeda fighters, militants from the Islamic State and other groups that have found a haven in Afghanistan.

Josh Earnest, the White House press secretary, said politics played “absolutely no role” in the president’s decision. But he said the next president — Democrat or Republican — will inherit a situation that is a “dramatically improved one when compared to the situation that President Obama inherited.”

Critics said Mr. Obama’s actions did not go far enough to confront Al Qaeda and other threats in Afghanistan.

“While this new plan avoids a disaster, it is certainly not a plan for success,” Representative Mac Thornberry, Republican of Texas and chairman of the House Armed Services Committee, said in a statement.

Speaker John A. Boehner said in a separate statement that he was “glad the administration finally admits President Obama’s arbitrary political deadlines are ‘self-defeating.’ ”

Even before Kunduz fell to the Taliban, the administration had been under growing pressure to abandon plans that would have cut by about half the number of troops in Afghanistan next year, and then drop the American force to about 1,000 troops based only at the embassy in Kabul by the start of 2017.

Video

Important speeches illustrate President Obama’s shifting stance on keeping troops in Afghanistan, beginning with his days as a senator.CreditCreditDoug Mills/The New York Times

Now, instead of falling back to the American Embassy, Mr. Obama said, the military will be able to maintain operations at Bagram Air Field to the north of Kabul, the main American hub in Afghanistan, and at bases outside Kandahar in the country’s south and Jalalabad in the east. The bases are crucial for counterterrorism operations and for flying drones used by the military and the C.I.A., which had also argued for keeping troops in Afghanistan to help protect its own assets.

President Ashraf Ghani of Afghanistan also pressed for Mr. Obama to keep more troops in his country. Many in Washington feared an American pullback just when Afghanistan had a leader who, unlike his predecessor, Hamid Karzai, appears willing to partner with the United States and is acutely aware of his country’s need for help from the United States and its NATO allies.

The American military has repeatedly stepped in this year to aid Afghan forces battling the Taliban, launching airstrikes and at times sending Special Operations troops to join the fight. But the recent fighting in Kunduz also exposed the limits of foreign forces in Afghanistan, which now total 17,000, including American and NATO troops.

Thursday was not the first time the administration has revised its withdrawal plans. During Mr. Ghani’s visit to Washington in March, Mr. Obama announced that the United States would keep 9,800 troops in Afghanistan through 2015, instead of cutting the force in half, as had been originally planned. At the time, the White House maintained that almost all troops would be pulled out by 2017.

His announcement will allow the military to continue carrying out secret operations focused primarily in eastern Afghanistan against people it suspects of being militant leaders. Those operations are intended to follow a “lighter footprint” model of targeted strikes.

New details about the Pentagon’s drone war from 2011 to early 2013 were disclosed on Thursday in classified documents published by The Intercept, a national security news website. The documents included a set of briefing slides assessing Operation Haymaker, an effort to hunt down Taliban and Qaeda militants in Afghanistan from January 2012 to February 2013.

During that period, there were 56 airstrikes that killed 35 suspects. Those strikes also killed 219 people who do not appear to have been specifically targeted but were labeled “enemy killed in action,” the documents showed.

A version of this article appears in print on , Section A, Page 1 of the New York edition with the headline: In Reversal, Obama Says U.S. Soldiers Will Stay in Afghanistan to 2017. Order Reprints | Today’s Paper | Subscribe