(Bloomberg Opinion) -- Almost 18 years ago in Afghanistan, a lifetime for some of the soldiers fighting there, the U.S. entered its longest war. It did so honorably, after terrorists sheltered by the then-ruling Taliban killed nearly 3,000 people on American soil.

It must now strive for an exit that is no less honorable.

In Doha, American and Taliban negotiators are reportedly nearing a deal to end hostilities. This would trade a full withdrawal of U.S. troops for a Taliban commitment to deny terrorists a base from which to launch another major attack. The Taliban would also agree to discuss sharing power with other Afghan factions and to accept a cease-fire, although the details of those promises remain vague.

Broadly speaking, this would be good news. It recognizes the hard reality that the Afghan war cannot be won. After battling the world’s most powerful military for almost two decades, the Taliban control more territory today than at any point since their fall from power in 2001. The U.S. has spent more than $840 billion, and at its peak deployed more than 100,000 troops, to achieve a mere stalemate. And the Taliban won’t stop fighting unless the U.S. promises that its remaining 14,000 troops in the country will leave.

The exit that’s envisaged is right to focus on the chief U.S. interest: preventing the country’s use as a haven by terrorists. It’s good that the U.S. has helped expand the liberties enjoyed by many Afghans — particularly women — but remember that those freedoms are still far more limited in the countryside and in areas not controlled by the government. A stable power-sharing agreement might leave progress, such as it is, relatively intact. Under conditions of peace, and removed from the taint of Western influence, ordinary Afghans might have a better chance of preserving their gains.

The question then is how best to achieve such stability.

Reports suggest President Trump wants to pull out American forces before the 2020 election. This would be wrong. A schedule tied to the U.S. political calendar would encourage the Taliban to drag out talks and then seize power outright. This could start a civil war like the one that followed the Soviet withdrawal in 1989. In the subsequent chaos, there would be space for groups such as al-Qaeda to take root.

The U.S. should instead try to preserve as much leverage as possible, for as long as possible. The most important factor is troop strength. Some U.S. forces (such as those engaged in training, as opposed to counterterrorism) can be brought home soon, but others should remain until the Taliban have shown they can keep a cease-fire, have agreed to a power-sharing framework and, preferably, have started to implement it. A smaller number of U.S. commandos, combined with airpower and local allies, should resist the spread of al-Qaeda and Islamic State while intra-Afghan talks proceed.

Money is another source of leverage. Any Afghan government will need generous financial assistance, not least to buy loyalty. Taliban commanders recognize this; some have suggested that if the U.S. withdraws its tanks, it would be welcome to return with bulldozers and cranes. U.S. negotiators should be clear that any attempt to impose the Taliban’s will by force, or to suppress women and minorities, would be met with a swift cutoff of all such assistance.

The U.S. and its allies should continue to give financial support to any legitimate government and its military, and to Afghan civil society, news media and education. Civilian aid has always been the least of the U.S.’s burdens in Afghanistan. Trump’s State Department has disbursed less than $3 billion in total. Slashing that assistance now would seriously erode the progress ordinary Afghans have made since 2001.

Finally, the U.S. must bring its diplomatic leverage to bear. Pakistan, Saudi Arabia and the Gulf states should be urged not to promote local proxies and disrupt the intra-Afghan peace process. China and Iran, despite frictions on other fronts, need to be engaged. Both are interested in getting the U.S. out of Afghanistan, preventing the spread of extremism and restoring stability in the region. All these parties should be brought into the process and given a stake in the outcome. 

The U.S. can’t afford to be naive. It should work with Pakistan and Central Asian states to develop “over-the-horizon” capabilities — the ability to strike at terrorist targets from just beyond Afghanistan’s borders using airpower and special forces. Ideally, the U.S. would continue to share intelligence with Afghan commando forces and perhaps even eventually reestablish a counterterrorism presence within the country. In any event, it will need the ability to disrupt al-Qaeda and Islamic State from afar.

At the same time, the threat to the U.S. needs to be kept in perspective. Al-Qaeda is a diminished force in Afghanistan, and the Taliban have almost as much interest in uprooting Islamic State as Washington does. Spending billions more on battling the Taliban is not the smartest way to disrupt such terrorist groups.

It’s time to end this war. For its own sake, as well as for Afghanistan’s, the U.S. needs to do it carefully.

--Editors: Nisid Hajari, Clive Crook.

To contact the senior editor responsible for Bloomberg Opinion’s editorials: David Shipley at davidshipley@bloomberg.net, .

Editorials are written by the Bloomberg Opinion editorial board.

©2019 Bloomberg L.P.