WASHINGTON — President Barack Obama scrapped plans Wednesday to cut American forces in Afghanistan by half before leaving office, a dispiriting blow to his hopes of extricating the U.S. after 15 years of fighting. He said he'll leave 8,400 troops to address the country's "precarious" security situation.

Obama's new drawdown plan, announced alongside top military leaders, reinforced the likelihood that the U.S. will remain entangled in Afghanistan for years to come as America works to suppress a resurgent Taliban and train a still-struggling Afghan military. Indeed, Obama said his goal was to ensure the next president has the foundation and flexibility to fight terrorism there "as it evolves."

Obama acknowledged that few Americans might have expected U.S. troops would still be in Afghanistan this long after the 2001 invasion following the 9/11 attacks. But he said perseverance was needed to prevent al-Qaida from regrouping and the Islamic State group from spreading. He said if terrorists regain control of territory, they'll try to attack the U.S. again.

"We cannot allow that to happen. I will not allow that to happen," he declared.

Obama, who had revised the exit plan several times before, had most recently expected to leave 5,500 troops when his term ends in January, down from roughly 9,800 there currently. His move to slow that withdrawal reflected the Afghan military's continuing inability to secure the nation independently, demonstrated by escalating Taliban attacks that have killed scores in recent weeks.

The new plan, announced the day before Obama attends a NATO summit in Poland, marked the culmination of a delicate debate within his administration about how many troops to pull out — if any.

Though U.S. officials said Obama had accepted the Pentagon's formal recommendation of 8,400 troops, top military leaders had urged the White House to stay closer to the current 9,800. In an unusually public lobbying campaign, last month more than a dozen former ambassadors and commanders urged him to "freeze" the current level for the rest of his term

In the end, Obama appeared to settle on a number that would show continued progress toward drawing down without jeopardizing the mission.

Elected after vowing to end the wars in Afghanistan and Iraq, Obama has struggled to deliver a legacy of leaving the U.S. less encumbered by foreign conflicts than he found it. Although he's declared U.S. combat operations over in both countries, the U.S. is still deep in conflict in both, plus major new fighting that has emerged in Syria and Libya since he took office.

In Congress, Republican leaders who favor a larger force said Obama's new plan was preferable to the old one, but they criticized him for not keeping the full 9,800. Sen. Lindsey Graham, R-S.C., said the partial drawdown would increase the dangers for remaining troops, calling it "more a political decision by President Obama than a military one."

Yet some Democrats, frustrated by the inability to fully end the war, said they were disappointed — for the opposite reason.

"Today, the longest war in American history just got longer," said Rep. Jim McGovern, D-Mass.

Ultimately, it will be up to the next president to decide the level of U.S. involvement. Democrat Hillary Clinton has aligned herself with Obama's handling of Afghanistan, while Republican Donald Trump has remained vague and has criticized Obama for revealing too much publicly about deployment decisions.

In Kabul, Afghan President Ashraf Ghani applauded Obama's decision. A brief statement from his spokesman called it "a sign of continued partnership between our nations to fight our common enemy and strengthen regional stability."

But the Taliban said the U.S. action would only prolong the war.

"What Obama could not do with 149,000 troops, he will not be able to do with 8,400 troops," Taliban spokesman Zabiullah Mujahid said on Twitter.

At the peak, in 2010, U.S. troop levels surged to 100,000, fighting alongside forces from U.S.-allied countries.

The president said the U.S. is "no longer engaged in a major ground war," and insisted the mission remains narrowly focused on "training and advising" Afghan forces and counterterrorism operations against the remnants of al-Qaida. Yet just last month, the White House gave the military expanded authority to conduct airstrikes against the Taliban in support of Afghan troops.

Though Obama touted progress in Afghanistan, including better-trained security forces, the situation remains perilous, with Afghan battlefield deaths rising and civilian casualties hitting a record high. Last month the Pentagon told Congress that Afghans were feeling less secure than at any other recent time. Obama also pointed out that 38 Americans had died in the past 18 months.