BAGRAM AIR FIELD, Afghanistan (AP) — On a surprise visit to Afghanistan, President Barack Obama pledged Sunday to bring America’s longest war to a “responsible end” by the close of the year. He promised a decision soon on keeping a small contingent of troops to help protect gains made over nearly 13 years of combat.

He told troops at this sprawling military base that the war had reached a pivotal point, with Afghan forces assuming primary responsibility for their country’s security. By 2015, many of the 32,800 American forces now in Afghanistan will depart.

“For many of you, this will be your last tour in Afghanistan,” Obama said as hundreds of U.S. troops inside an airplane hangar erupted in applause. “America’s war in Afghanistan will come to a responsible end.”

Obama secretly slipped in Afghanistan under the cover of darkness for his first trip to the war zone since 2012.

Obama was spending just a few hours on the base and had no plans to go to Kabul, the capital, to meet with Hamid Karzai, the mercurial president who has had a tumultuous relationship with the White House.

This story has been updated.