KABUL, Afghanistan (CNN) -- President Barack Obama, speaking early Wednesday in Afghanistan at the tail end of a surprise visit there, discussed how the war will end and promised a steady drawdown of U.S. troops.
Obama committed to pulling 23,000 troops out of the country by the end of summer and sticking to the 2014 deadline to turn security fully over to the Afghan government. He also said that NATO will set a goal this month for Afghan forces to be in the lead for combat operations next year.
"We will not build permanent bases in this country, nor will we be patrolling its cities and mountains. That will be the job of the Afghan people," the president said during a speech at Bagram Air Base.
His trip was Obama's third visit to Afghanistan since taking office. It coincided with the first anniversary of the U.S. raid that killed Osama bin Laden in neighboring Pakistan.
Obama said his administration has been in direct talks with the Taliban. "We've made it clear that they can be a part of this future if they break with al Qaeda, renounce violence, and abide by Afghan laws ... The path to peace is now set before them," he said.
And the president promised not to keep troops in harm's way "a single day longer than is absolutely required for our national security. But we must finish the job we started in Afghanistan, and end this war responsibly."
"This time of war began in Afghanistan, and this is where it will end," Obama said.
Earlier in the trip, Obama and Afghan President Hamid Karzai signed an agreement outlining cooperation between their countries once the U.S.-led international force withdraws in 2014.
Some U.S. forces will remain in a post-war Afghanistan as military advisers, but both U.S. and Afghan officials have yet to decide on how many troops will continue supporting the Afghan military, and for how long.
At a signing ceremony for the Strategic Partnership Agreement, Obama said that neither country asked for the war that began more than a decade earlier, but now they would work in partnership for a peaceful future.
"There will be difficult days ahead, but as we move forward in our transition, I'm confident that Afghan forces will grow stronger; the Afghan people will take control of their future," Obama said.
Addressing a concern in Afghanistan that the United States will abandon the country once its troops leave, Obama said, "With this agreement, I am confident that the Afghan people will understand that the United States will stand by them."
He later added that the United States "did not come here to claim resources or to claim territory. We came here with a very clear mission to destroy al Qaeda," referring to the terrorist organization responsible for the September 11, 2001, attacks.
Obama's address came nine years to the day after then-President George W. Bush delivered his "Mission Accomplished" speech aboard the aircraft carrier USS Abraham Lincoln, announcing the end of major combat operations in Iraq.
Karzai offered his thanks to the American people for helping Afghanistan, and the presidents shook hands after signing the document in the atrium of the King's Residence, part of the Presidential Palace in Kabul.
"This agreement will close the season of the past 10 years and is going to open an equal relationship season. With the signing of this agreement, we are starting a phase between two sovereign and independent countries that will be based on mutual respect, mutual commitments and mutual friendship," the Afghan president said.
Obama warned the Afghan people and, later, U.S. troops he met with, of difficult days ahead. In remarks to troops at Bagram, Obama sounded emotional as he said that soldiers could see friends get hurt or killed as the mission winds down.
"There's going to heartbreak and pain and difficulty ahead, but there's a light on the horizon because of the sacrifices you've made," he said.
The security risks in Afghanistan were evident from the secretive nature and timing of the trip. Obama landed in Afghanistan in the cover of darkness, and the signing ceremony occurred in the late evening.
The Strategic Partnership Agreement provides a framework for the U.S.-Afghanistan partnership for the decade after the U.S. and allied troop withdrawal, according to senior administration officials who briefed reporters on the flight.
Specific levels of U.S. forces and funding are not set in the agreement and will be determined by the United States in consultation with allies, the officials said on condition of not being identified.
Noting the anniversary of the bin Laden mission, the officials called it a resonant day for the Afghan and American people.
More than 130,000 troops from 50 countries serve in Afghanistan, according to the NATO-led International Security and Assistance Force. The United States is the biggest contributor, providing about 90,000 troops, followed by the United Kingdom (9,500), Germany (4,800) and France (3,600).
The war that began in 2001 is increasingly unpopular in the United States, with the latest CNN/ORC International poll in late March showing 25% of respondents supporting it and 72% opposing it.
More than 2,700 troops from the United States and its partners have died in the war, the majority of them American.
In 2011, the United States outlined its plan to withdraw its troops from Afghanistan by the end of 2014. The move was followed by withdrawal announcements by most of the NATO nations.
Last week, Afghan National Security Adviser Rangin Daftar Spanta and U.S. Ambassador Ryan Crocker initialed a text that outlined the kind of relationship the two countries want in the decade following the NATO withdrawal.
The deal had been long expected after Washington and Kabul found compromises over the thorny issues of "night raids" by U.S. forces on Afghan homes and the transfer of U.S. detainees to Afghan custody.
It seeks to create an enduring partnership that prevents the Taliban from waiting until the U.S. withdrawal to try to regain power, the senior administration officials said.
This trip was the President's third visit to Afghanistan.