US President Barack Obama and Afghan President Hamid Karzai have signed a strategic partnership accord that charts the future of US-Afghan relations beyond the end of the NATO combat mission in the country in 2014.
Obama, on an unannounced visit to Kabul late on Tuesday, acknowledged that there will be difficult days ahead for Afghanistan, but said the Afghan people were taking control of their own future.
"The wages of war have been great for both our nations," Obama said, adding that he looked forward to a future of peace.
The two leaders shook hands after the signing, which took place in Karzai's palace in the Afghan capital.
The partnership spells out the US relationship with Afghanistan beyond 2014, covering security, economics and governance.
The deal is limited in scope and essentially gives both sides political cover: Afghanistan is guaranteed its sovereignty and promised it won't be abandoned, while the US gets to end its combat mission in the long and unpopular war but keep a foothold in the country.
The deal does not commit the United States to any specific troop presence or spending. But it does allow the US to potentially keep troops in Afghanistan after the war ends for two specific purposes: continued training of Afghan forces and targeted operations against al-Qaeda.
The terror group is present in neighboring Pakistan, but has only a nominal presence inside Afghanistan.
Obama's arrival in Afghanistan came a year after American Navy SEALs killed al-Qaeda chief Osama bin Laden in a raid deep inside Pakistan.
The US president, making only his third trip to Afghanistan since taking over as commander-in-chief in 2009, arrived at 17:50 GMT and was also to address the American people from Bagram airbase at 23:30 GMT, an official said.
Obama's last trip in December 2010 lasted only a few hours when he flew into Bagram air base, outside Kabul, to meet US troops but he did not meet with Karzai.
Ties between Kabul and Washington have strained since last May amid a series of bloody massacres and incidents by US troops against Afghan civilians as a 130,000-strong US-led NATO force fights a fierce Taliban insurgency.
The last of the remaining 87,000 American troops in the country are due to pull out by the end of 2014, some 13 years after a US-led campaign in late 2001 to oust the Taliban regime accused of harbouring bin Laden.
Relations between Pakistan and the United States also plunged over the May 2, 2011, raid that killed bin Laden, tracked down to a compound in the Pakistani military town of Abbottabad after a decade-long global manhunt.
Obama's top counter-terrorism aide, John Brennan, on Monday argued that al-Qaeda was losing "badly" amid a US drone campaign in the Pakistan-Afghanistan region, and that its core leadership would soon be "no longer relevant".
The campaign had left the terror groups seriously weakened, and unable to replace wiped-out leaders, he said.
Bin Laden had also been frustrated by the demise of his group, which was behind the September 11, 2001 attacks against the United States, and vented his anger in documents seized from his compound by the SEAL commandos.
"He confessed to 'disaster after disaster'" for al-Qaeda, Brennan said, noting some of the captured material would be published online this week by the Combating Terrorism Center at the US Military Academy at West Point.
Brennan also said subsequent US operations to wipe out senior al-Qaeda leaders in Pakistan had left the group reeling.
"Under intense pressure in the tribal regions of Pakistan, they have fewer places to train and groom the next generation of operatives, they're struggling to attract new recruits.
"Morale is low," Brennan said in a speech at the Woodrow Wilson Center for International Scholars.
He has also brushed aside criticism by Obama's political opponents that he has exploited the anniversary of bin Laden's killing for political gain.
"All that I know is that the president made the decision when he was given the opportunity to take a gutsy decision, to carry out that raid with our special forces in Abbottabad, Pakistan," Brennan told ABC television on Sunday.
"The president made that decision. I think the American people are, you know, clearly very appreciative and supportive of that decision. We're safer today as a result."
Brennan noted Obama took the decision to go ahead with the raid against the advice of some of his most senior advisers who had reservations about the operation, which was fraught with peril for the Navy SEALS.
Senior Obama aides are clearly using the president's decision to launch the high-risk raid as an implicit comparison to the character of his presumptive Republican rival Mitt Romney.
Obama, a Democrat, is battling to be elected to a second four-year term in the White House in the November 6 elections.
Obama's campaign last week released a video to mark the anniversary and suggested bin Laden might be alive today had Romney been in the White House.
US Senator John McCain, who lost to Obama in the 2008 elections and who remains one of his most dogged critics, has said the advertisement politicised an issue that should not be fodder for November's presidential campaign.
"Shame on Barack Obama for diminishing the memory of September 11th and the killing of Osama bin Laden by turning it into a cheap political attack ad," he said.