Obama appeared optimistic that the Afghan government soon would sign a bilateral security agreement allowing the US 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 plans shortly.
That announcement could come as early as Wednesday, when Obama delivers the commencement address at the US Military Academy at West Point, New York.
Obama secretly arrived at Bagram Air Field, the main US base in Afghanistan, under the cover of darkness for his first trip to the war zone since 2012. He 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.
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 US and international forces in Afghanistan are withdrawing ahead of year-end deadline.