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 that 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, N.Y.

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.

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.