The US has signed a peace agreement with Taliban militants aimed at bringing an end to 18 years of bloodshed in Afghanistan and America’s longest war.

Under the agreement, Washington would draw its forces down to 8600 from 13,000 in the next three to four months, with the remainder withdrawing in 14 months.

The complete pull-out would depend on the Taliban meeting their commitments to prevent terrorism.

President George W Bush ordered the US-led invasion of Afghanistan in response to the September 11 attacks in 2001.

Some US troops currently serving had not been born when the World Trade Centre was attacked.

It only took a few months to topple the Taliban, but the war dragged on for years as the US tried to establish a stable state after years of war.

The Taliban regrouped and currently hold sway over half the country.

The US spent more than 750 billion dollars, and on all sides the war cost tens of thousands of lives.

More than 100,000 Afghans have been killed or wounded since 2009, which is when the UN Assistance Mission in Afghanistan began documenting casualties.

US secretary of state Mike Pompeo attended the ceremony in Qatar, where the Taliban have a political office, but did not sign the agreement. It was signed by US peace envoy Zalmay Khalilzad and Taliban leader Mullah Abdul Ghani Baradar.

READ MORE: Afghans caught between rock and a hard place as ‘new Taliban’ rises

Pompeo had privately told a conference of US ambassadors at the State Department this week that he was going only because President Donald Trump had insisted on his participation, according to two people present.

Dozens of Taliban members had earlier held a small victory march in Qatar in which they waved the militant group’s white flags, according to a video shared on Taliban websites.

“Today is the day of victory, which has come with the help of Allah,” said Abbas Stanikzai, one of the Taliban’s lead negotiators, who joined the march.

Trump has repeatedly promised to get the US out of its “endless wars” in the Middle East, and the withdrawal of troops could provide a boost as he seeks re-election.

READ MORE: MoD denies new torture claims from Iraq and Afghanistan wars

Under the agreement, the Taliban have promised not to let extremists use the country as a staging ground for attacking the US or its allies, but officials in Washington are reluctant to trust the Taliban to fulfil these obligations.

The Taliban have committed to sending a “clear message that those who pose a threat to the security of the United States and its allies have no place in Afghanistan”.

It will instruct its members not to cooperate with groups or individuals that threaten the United States, according to the deal.

READ MORE: Daesh claims responsibility for deadly Kabul wedding attack

The prospects for Afghanistan’s future are uncertain. The agreement sets the stage for peace talks involving Afghan factions, which are likely to be complicated.

Under the agreement, 5000 Taliban are to be released from Afghan-run jails, but it is not known if the Afghan government will do that.

It is also not clear what will become of gains made in women’s rights since the toppling of the Taliban, which had repressed women and girls under a strict brand of Sharia law. In areas not held by the Taliban, women had won back the right to go to school, work and attend public places.