WASHINGTON — With pressure building on the White House to slow or completely halt the withdrawal of American troops from Afghanistan, senior officials said that President Obama appears increasingly willing to keep a force there large enough to carry on the hunt for Al Qaeda and Islamic State militants.

For President Obama, leaving more than a small force to protect the embassy in Kabul beyond next year would mean abandoning his goal to bring home almost all American troops before leaving office. But even though Mr. Obama has declared the American war in Afghanistan to be over, the Taliban’s recent advances have convinced the Pentagon, many in Congress and much of the national security establishment in Washington that it is critical for American troops to remain there.

The insurgents are now spread through more of Afghanistan than at any point since 2001, according to the United Nations, and last month they seized the city of Kunduz with only a few hundred fighters. At the same time, Al Qaeda operatives are still finding havens in the mountains of Afghanistan, and the Islamic State has gained a toehold by recruiting disaffected Taliban, opening a treacherous new chapter in the war.

Those developments have led the White House to become increasingly convinced of the need to keep some kind of counterterrorism force in Afghanistan. Just how large a force — and whether American troops should also continue training Afghan forces — remains a subject of debate inside and outside the administration, said the officials, who asked to remain anonymous to discuss decision making within the administration.