By that new standard, things had improved in Afghanistan. By August 2010, 100,000 American troops were on the ground in Afghanistan and were pushing back the Taliban in some critical areas. Despite uneven progress in the military campaign, Ryan Crocker, a diplomat who had reopened the American Embassy in Kabul in 2002 and served there again as ambassador in 2011, recalled thinking, “Wow, this place looks great!”

The Navy SEAL raid that killed Osama bin Laden in Pakistan in May 2011 added to Mr. Obama’s conviction that he was on the way to closing the books on the war.

At midnight on May 1, 2012, Air Force One rolled out from behind a hangar at Joint Base Andrews to pick up the president for a secret trip to Afghanistan. He was going to sign a strategic partnership agreement with President Hamid Karzai that set the terms for relations after 2014, when the United States was scheduled to withdraw its combat troops and turn over Afghanistan’s security to the Afghans.

Aides to Mr. Obama had advised him not to go for security reasons, but he saw it as an important milestone.

The agreement promised an “enduring partnership” between the United States and Afghanistan, with pledges of American help in developing the Afghan economy and public institutions. Yet the promises obscured a starker reality: Mr. Obama had accelerated the timetable for drawing down American troops, and he was looking beyond the war.

Speaking to a national TV audience from Bagram Air Base, he suggested that America’s experience in Afghanistan had come full circle. “One year ago, from a base here in Afghanistan, our troops launched the operation that killed Osama bin Laden,” he said. “The goal I set — to defeat Al Qaeda, and deny it a chance to rebuild — is now within our reach.”

Earlier, Mr. Obama had met for an hour with Mr. Karzai. The two had long had a rocky relationship — on an earlier trip, Mr. Obama excoriated Mr. Karzai for the rampant corruption in the Afghan government — and this session did little to improve their rapport. Mr. Crocker recalled that the president was “very aloof, almost cold, which bothered me a bit because I’d worked a solid damn year to get Karzai in a better place with us.”