BAGRAM AIR FIELD, Afghanistan — President Barack Obama slipped into Afghanistan for a surprise visit Sunday and made clear that the U.S. will likely maintain a limited role here even after its combat mission ends this year and America’s longest war comes to a close.
‘‘America’s commitment to the people of Afghanistan will endure,’’ he pledged.
Speaking to troops gathered in an airplane hangar on this sprawling military base, Obama said the war had reached a pivotal point, with Afghan forces assuming primary responsibility for their country’s security. But while many of the 32,800 U.S. forces now in Afghanistan will leave in the coming months, Obama said a continued military presence could help protect gains made during nearly 13 years of fighting.
‘‘After all the sacrifices we've made, we want to preserve the gains that you have helped to win and we’re going to make sure that Afghanistan can never again, ever, be used again to launch an attack against our country,’’ Obama declared.
At least 2,181 members of the U.S. military have died during the nearly 13-year Afghan war and thousands more have been wounded.
Obama told the troops, ‘‘For many of you, this will be your last tour in Afghanistan,’’ a comment was met with an eruption of applause. ‘‘America’s war in Afghanistan will come to a responsible end.’’
The president appeared optimistic that the Afghan government soon would sign a bilateral security agreement allowing the U.S. to keep some forces in the country to train Afghans and launch counterterrorism operations. He has been considering keeping up to 10,000 troops in Afghanistan and said he would announce his decision shortly.
That announcement could come as early as Wednesday, when Obama delivers the commencement address at the U.S. Military Academy at West Point, New York.
Obama arrived at Bagram Air Field, the main U.S. base in Afghanistan, under the cover of darkness for his first trip to the war zone since 2012. He spent about four hours at the base and did not go to Kabul, the capital, to meet with Hamid Karzai, the mercurial president who has had a tumultuous relationship with the White House.
Instead, officials said Obama wanted to keep the focus during his Memorial Day weekend visit on the troops serving in the war’s closing months. Karzai’s office said it had declined a U.S. Embassy invitation for him to go to Bagram to see Obama. The White House said Obama was not meeting with the outgoing Afghan president in order to avoid getting involved in Afghan politics.
Instead, Obama called Karzai from Air Force One on his way back to the U.S. A senior administration official traveling with the president said the two leaders discussed the progress that has been made by Afghan security forces and its successful first round of elections.
Obama told Karzai he would be in touch with him before announcing any decision on the U.S. troop presence in Afghanistan after 2014.
Obama’s visit, his fourth to Afghanistan as president, came at a time of transition for a country long mired in conflict. Most of the U.S. and international forces in Afghanistan are withdrawing ahead of the year-end deadline. Elections are underway to replace Karzai, the only president Afghanistan has known since the U.S.-led invasion in 2001.
Karzai stunned the White House by refusing to sign a bilateral security agreement needed to keep U.S. troops in Afghanistan after this year. His decision has delayed U.S. decision making on a post-2014 presence, leading Obama to ask the Pentagon to work up plans for a possible full withdrawal of American forces.
But with both candidates on the ballot in next month’s Afghan presidential election runoff vowing to quickly sign the security agreement, Obama appeared more confident Sunday that there would be a continued U.S. troop presence after 2014.
After an overnight flight from Washington, Obama attended a briefing with U.S. officials. He said that as he entered the briefing room, he saw a poster of the Twin Towers destroyed in the 2001 terrorist attacks.
‘‘It’s a reminder of why we’re here,’’ he said.
Obama was accompanied by a few advisers, including senior counselor John Podesta, whose son is serving in Afghanistan. Country singer Brad Paisley joined Obama on Air Force One and entertained the troops as they waited for the president.
As is typical of recent presidential trips to war zones, the White House did not announce Obama’s visit in advance. Media traveling with Obama for the 13-hour flight had to agree to keep the trip secret until the president had arrived.
After his remarks, Obama visited with injured service members being treated at a base hospital.
The president’s visit took place against the backdrop of growing outrage in the United States over the treatment of America’s war veterans. More than two dozen veterans’ hospitals across America are under investigation over allegations of treatment delays and deaths, putting greater scrutiny on the Veterans Affairs Department. The agency already was struggling to keep up with the influx of forces returning home from Afghanistan and Iraq.
‘‘We’re going to stay strong by taking care of our wounded warriors and our veterans,’’ Obama said to applause. ‘‘Because helping our wounded warriors and veterans heal isn’t just a promise. It’s a sacred obligation.’’
Obama has staked much of his foreign policy philosophy on ending the two wars he inherited from his predecessor, George W. Bush.
The final American troops withdrew from Iraq in the closing days of 2011 after the U.S. and Iraq failed to reach a security agreement to keep a small American residual force in the country. In the years that have followed the American withdrawal, Iraq has been battered by resurgent waves of violence.
U.S. officials say they’re trying to avoid a similar scenario in Afghanistan. While combat forces are due to depart at the end of this year, Obama administration officials have pressed to keep some troops in Afghanistan after 2014 to continue training the Afghan security forces and undertake counterterrorism missions.
Pentagon officials have pushed for as many as 10,000 troops; others in the administration favor as few as 5,000 troops. Obama has insisted he will not keep any Americans in Afghanistan without a signed security agreement that would grant those forces immunity from Afghan law.