WASHINGTON — During a surprise trip to Afghanistan on Sunday, President Obama thanked American troops for their service during a decade of war, saying that their sacrifices had ensured the country would never again be used as a base for terrorist attacks against the United States.  “I thank you as your commander in chief because you inspire me,” Mr. Obama told about 3,000 troops in a hangar at Bagram Airfield after landing in the country early Sunday morning. “I’m here to say thank you. And I’m here to say how proud I am of you.”  The trip was unannounced, and Mr. Obama slipped out of the White House secretly on Saturday evening ahead of Memorial Day. The country music singer Brad Paisley traveled on Air Force One with Mr. Obama to Afghanistan and performed for about an hour before the president spoke.  Mr. Obama chose not to meet with Hamid Karzai, the departing president of Afghanistan, and did not travel to Kabul, the country’s capital. But he vowed that he would work with whoever wins the presidential runoff election scheduled for June 14: either Abdullah Abdullah or Ashraf Ghani.  White House officials said the trip was intended as a visit with troops, not as an opportunity for the two presidents to meet. They said American officials offered Mr. Karzai the opportunity to meet Mr. Obama at the Bagram Airfield, but were not surprised that it did not happen.  “The president will likely be speaking by phone with President Karzai in the days to come, and also looks forward to working with Afghanistan’s next president after the election is complete,” a White House official told reporters in Afghanistan.  In his remarks to the troops, Mr. Obama made clear that he still intends to keep a small military force in Afghanistan beyond 2014 despite longstanding resistance to an enduring American presence from Mr. Karzai. Mr. Obama’s comments indicated that he may have ruled out the idea of a complete withdrawal of all American forces when the N.A.T.O.-led combat mission ends later this year.  “Once Afghanistan has sworn in its new president, I’m hopeful we will sign a bilateral security agreement that lets us move forward,” Mr. Obama said. “And with that bilateral security agreement, assuming it is signed, we can plan for a limited military presence in Afghanistan beyond 2014.”  Mr. Obama vowed that “we are going to make sure that Afghanistan can never again — ever — be used to launch a terrorist attack against our country.”  Mr. Obama got his biggest applause from the troops when he praised them for making sure that “America’s war in Afghanistan will come to a responsible end.” The crowd also roared with approval when the president said he frequently tells corporate leaders, “If you want somebody who can get the job done, hire a vet.”  Mr. Obama did not make any major policy announcements in his remarks to the troops. He is expected to wait until he returns to the United States to offer his latest foreign policy and national security vision, during a speech that he is scheduled to give at the West Point graduation on Wednesday.  In 2009, Mr. Obama used a similar setting at West Point to announce a decision to add more troops to the effort in Afghanistan, pushing the total United States presence there past 100,000, while at the same time saying he intended to draw down the effort there over several years.  Mr. Obama has said that all United States combat troops will have left Afghanistan by the end of this year. American officials hope to leave a small contingent of forces beyond the 2014 withdrawal deadline for the international security force in Afghanistan, to allow for more training of Afghan forces and continuing counterterrorism operations.  Mr. Karzai had negotiated an agreement to allow for such a force to remain, but later refused to sign it. Both of the remaining presidential candidates have indicated that they would sign the agreement upon taking office.  The visit to Afghanistan comes as Mr. Obama prepares to draw the second of two wars to an official close, making good on a promise that he made as a candidate to pull the United States out of such conflicts.  But Mr. Obama has also struggled to define the parameters of his foreign policy as he has been confronted with other conflicts in Iran, Syria, Libya and the ongoing tension with Russia in Ukraine. Ben Rhodes, the president’s deputy national security adviser, told reporters that Mr. Obama is likely to speak about that issue when he returns from Afghanistan.  “We are at a bit of a turning point in our foreign policy generally,” Mr. Rhodes told reporters traveling aboard Air Force One. “Our foreign policy is going to be a lot different than it has been over the past decade, and the president will speak to what that transition will mean.”  Officials said that Mr. Obama is expected to be on the ground in Afghanistan for only a few hours, after a 13-hour flight through the night on Saturday.  “We have been looking for some period of time to come to Afghanistan. After the first round of the election, we thought it would be a good time to come for a troop-focused visit,” Mr. Rhodes said, adding that the decision not to meet with the candidates or Mr. Karzai was made because “we didn’t want to get in the middle of election season.”  Sunday’s visit marks the fourth time that Mr. Obama has traveled to Afghanistan as president and comes as he and his national security team weigh how to proceed with a postwar presence in the country.  Mr. Rhodes said that officials are still weighing what that commitment might look like, and said that no decisions had been made. He said that Mr. Obama believed that the surprise visit to Afghanistan was an important part of hearing directly from his commanders about the future of the mission.  “We also of course are making some decisions about the future of our commitment to Afghanistan,” Mr. Rhodes said. “It is important for him to come before he articulates a decision.”
The president did not meet with the departing Afghan leader, Hamid Karzai, instead focusing on talking to American forces about the upcoming withdrawal.