President Barack Obama has told US troops in Afghanistan that America's longest war will come to "a responsible end" at the end of 2014.
Mr Obama was cheered by soldiers during the unannounced visit to Bagram Airfield outside Kabul.
It comes on the eve of Memorial Day, when Americans commemorate troops who have died in service.
Foreign combat troops are due to withdraw from the country by the end of the year.
The US is seeking to keep a small number of troops there to train Afghan security forces. But that plan depends on the next Afghan president - due to be elected next month - signing a bilateral security agreement that incumbent Afghan President Hamid Karzai has refused to authorise.
President Obama had invited his Afghan counterpart to the base but Mr Karzai refused, saying he would only meet Mr Obama at his palace in Kabul, officials said.
Instead, Mr Obama called the Afghan leader from Air Force One on his way back to the US, officials said, saying he would be in touch before announcing any decision on the planned US troop presence after 2014.
Mr Obama praised US troops for reversing the momentum of the Taliban
"By the end of this year... Afghans will take full responsibility for their security and our combat mission will be over. America's war in Afghanistan will come to a responsible end."
Mr Obama said the US would remain committed to Afghanistan provided the incoming president signed the security agreement.
Both of the candidates who will face each other in the second round of the Afghan election next month are committed to signing the security deal with the US that would allow a small force of some 10-15,000 US troops to remain next year.
Afghanistan was at a pivotal moment, he said, with US forces having decimated al-Qaeda, reversing the momentum of the Taliban, and leaving a legacy of better health and education and strong Afghan forces.
"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," he said.