WASHINGTON (AP) — The U.S. will slow its troop drawdown in Afghanistan, leaving a force of 8,400 when President Barack Obama completes his term.

Flanked by top military leaders at the White House, Obama said the security situation in Afghanistan is "precarious" and the Taliban remain a threat roughly 15 years after the U.S. invaded in the aftermath of 9/11. He said he was committed not to allow any group to use Afghanistan "as a safe haven for terrorists to attack our nation again."

"It is in our national security interest — especially after all the blood and treasure we've invested in Afghanistan over the years — that we give our Afghan partners the very best opportunity to succeed," Obama said.

There are currently about 9,800 troops in Afghanistan, and Obama had planned to pull that back to 5,500 by year-end. But a Taliban resurgence and the Afghan military's continuing struggles have led the White House to rethink its exit strategy.

Top military leaders had wanted the White House to stay closer to the current 9,800 there when he leaves office. Last month, a group of more than a dozen former U.S. ambassadors and commanders in Afghanistan publicly urged him to "freeze" the current level for the rest of his term and let the next president make any adjustments.

Though U.S. officials said Obama was acting on a formal Pentagon recommendation of 8,400 troops, in recent weeks there were ongoing talks between the White House and the Pentagon, suggesting the final figure was the result of those discussions.

Obama's announcement comes with major implications for his legacy. He came into office promising to end the wars in Afghanistan and Iraq, but he'll leave with the U.S. still enmeshed in conflicts in both of those countries while wrestling with new ones in Syria and Libya.

Afghan President Ashraf Ghani welcomed Obama's decision. In Kabul, spokesman Haroon Chankhansuri called the decision "a sign of continued partnership between our nations to fight our common enemy and strengthen regional stability."

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. "Our resolve is high and our determination is firm."

The president said the U.S. mission would remain narrowly focused on "training and advising" Afghan forces and supporting counterterrorism operations against the remnants of al-Qaida, the group that attacked the U.S. on Sept. 11.

"We are no longer engaged in a major ground war in Afghanistan," he said. At the peak, in 2010, U.S. troop levels surged to 100,000.

Republican leaders in Congress 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 the remaining troops, calling it "more a political decision by President Obama than a military one."

Gen. John F. Campbell, the top U.S. commander in Afghanistan until March, warned Congress earlier this year that reducing the number too sharply would make it tougher to train Afghan forces and perform counterterror operations at the same time. The military was also concerned that it would need more than 5,500 to provide security and logistics support for allies fighting alongside the U.S.