The US has no intention of keeping its troops in Afghanistan and does not want any permanent military bases there, President Barack Obama has said.
In a keynote speech to the Muslim world in Cairo, the president said that it would be costly and politically difficult to continue the conflict.
But he said there were still violent extremists in Afghanistan and Pakistan.
The US would use a combination of diplomacy and a commitment to root out "terrorists", the president said.
"Make no mistake: we do not want to keep our troops in Afghanistan," the president said.
"We seek no military bases there. It is agonizing for America to lose our young men and women. It is costly and politically difficult to continue this conflict.
"We would gladly bring every single one of our troops home if we could be confident that there were not violent extremists in Afghanistan and Pakistan determined to kill as many Americans as they possibly can. But that is not yet the case."
President Obama said that the US was part of a coalition of 46 countries in Afghanistan and America's commitment would not weaken, despite the costs involved.
The president said that the US troops would not remain indefinitely
"Indeed, none of us should tolerate these extremists. They have killed in many countries. They have killed people of different faiths - more than any other, they have killed Muslims," he said.
The president said the action of the militants were irreconcilable with the rights of human beings, the progress of nations and with Islam.
"The enduring faith of over a billion people is so much bigger than the narrow hatred of a few. Islam is not part of the problem in combating violent extremism - it is an important part of promoting peace."
The president said that military power alone is not going to solve the problems in Afghanistan and Pakistan, which was why the US planned to invest $1.5bn each year over the next five years to help Islamabad build schools, hospitals, roads and businesses in addition to aiding people who have been displaced by the recent conflict in the north-west.