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, the president announced Wednesday in a blunt acknowledgment that America will remain entangled there despite his aspirations to end the war.

In a statement 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, flanked by top military leaders.

President Barack Obama pauses while making a statement on Afghanistan from the Roosevelt Room of the White House in Washington, Wednesday, July 6, 2016. (AP Photo/Susan Walsh)

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 Washington to rethink its exit strategy.

The numbers reflect a compromise between Obama’s original plan and what many military commanders had urged.

The military has argued to keep closer to the 9,800 troops now there to help assist the Afghans and guard against a Taliban resurgence. Last month, a group of more than a dozen former U.S. ambassadors and commanders in Afghanistan urged him to “freeze” the current level for the rest of his presidency and let the next president make adjustments.

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

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.

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.

Republican leaders in Congress who favor a larger troop force said Obama’s new plan was preferable to the old one, but they criticized him for not keeping the full 9,800. Arizona Sen. John McCain, the Senate Armed Services Committee chairman, said it was difficult to see any rationale for withdrawing anyone given Obama’s concession about how dangerous Afghanistan still is.

“President Obama’s announcement today is a belated recognition that the war in Afghanistan has not yet been won,” added House Speaker Paul Ryan, R-Wis.

As the military has prepared for the planned drawdown, it has become clear that keeping just 5,500 troops might not be logistically feasible.

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.

The Obama administration declared in December 2014 that the U.S. combat mission in Afghanistan had ended, but for troops serving there the dangers have not ended. Obama pointed out that 38 Americans had died in just the past 18 months.

Though Obama touted progress in Afghanistan, including better-trained security forces and some advances in fighting the Taliban, the situation remains shaky, with Afghan battlefield deaths rising and civilian casualties hitting a record high. Just last month the Pentagon said in a report to Congress that Afghans were feeling less secure than at any other recent time.

Obama has been under pressure from U.S. allies to make a decision following a NATO announcement last month that the alliance would maintain troops in regional locations around Afghanistan. NATO’s future involvement in the fight is to be a major topic when Obama attends a NATO summit later this week in Warsaw, Poland.

Obama said boosting the planned troop levels would help other countries prepare their own contribution to the fight. He said his decision should help the next president make good decisions about the future of U.S. involvement.

“l firmly believe the decision I’m announcing is the right thing to do,” Obama said.

Copyright 2016 The Associated Press. All rights reserved. This material may not be published, broadcast, rewritten or redistributed.