As the two-decade-long U.S. war in Afghanistan comes to its technical end with a peace deal signed between the U.S. and the Taliban, both poles of the current political spectrum have concluded, along with most of the country, that the massive investment of resources and lives in this conflict was materially, not to mention morally, fruitless. Western confidence has been undermined, and the belief that prosperous democracies can project their power for good now seems anachronistic — naive to some, arrogant to others. There is a consensus: It is time to "end our endless wars."

However, it is dangerous to draw such an unqualified conclusion. Bill Clinton and Tony Blair learned from Kosovo — where intervention stopped a war — and Rwanda — where calls for intervention went unheeded and up to a million people were butchered — that inaction can in fact be shameful, and this fostered a categorical conviction in righteous humanitarian intervention that propelled them into the follies of this millennium's first decade in Iraq and Afghanistan. The belief that liberalism can be forced on societies at gunpoint was then handed a crusading energy by the shock of 9/11. But now, the received wisdom is intervention does not work, but what if this new conviction propels us into the next decade's disaster? Why must we swing between such unnuanced extremes?

It is important we understand that sometimes failing to intervene is the more costly decision, both morally and in terms of national self-interest.

When it comes to the Afghan war, the mistake was not in the intervention itself, rather in how it was managed. Soon after the initial invasion crippled the Taliban and sent it into hiding, its leadership consistently asked to meet at the negotiating table, but were rebuffed. Understandably, Washington's blood was up after September 2001, and the Bush administration was categorical: "We do not negotiate with terrorists."

This was a mistake. Talks could have brought a reduction in NATO involvement under better terms than are being agreed now. The intervention could have been conducted with a pragmatism that acknowledged the impossibility of all-out military victory and brought the Taliban to the table early. The NATO presence could still have brought the country what it did: huge improvements in agriculture, infrastructure projects like the successful Dahla Dam rebuild, the social empowerment of women, and health initiatives like the one that vaccinated children nationwide.

Writing in The Atlantic, Sen. Elizabeth Warren (D-Mass.) is right to decry the "approach to foreign policy that relies on the U.S. military to achieve the impossible, instead of doing the hard work of statecraft." But this isn't, as she claims, an argument for ending the endless wars: We can, and should, ask the military to do the possible, while running intensive statecraft at the same time.

Many think the cost of military intervention is simply too high — but there are times when the cost of doing nothing is far greater. This is the case, for example, in Syria, where more than 1 million internal refugees face what the U.N. calls the "biggest humanitarian horror story of the 21st century."

Learning the wrong lessons from Western intervention in Iraq and Afghanistan, the Obama administration chose not to provide the rebels who rose up in 2011's Arab Spring with the support necessary to topple Syrian President Bashar al-Assad. A stronger U.S. commitment early on could have prevented ISIS from rising in Syria — and prevented the huge, expensive war against ISIS that followed. It would also have helped stem the tide of refugees that continues to fuel de-democratizing forces across Europe. It could also have meant the end of the rapacious, vindictive Assad, who now terrorizes his people for daring to resist him. Intervening in Syria now would be a mistake, and this is another lesson: To be effective, intervention must be timely.

The U.S. did maintain a limited troop presence in Northeastern Syria, but, to President Trump, these troops were simply more Americans involved in an endless war. As far as he saw it, the generals and national security advisers who urged him to let them stay were just repeating the conventions that got the U.S. too deeply involved in the Middle East to begin with. So, Trump got the troops out — and all hell broke loose. Turkey and Assad moved to fill the vacuum, America's Kurdish allies were betrayed, ISIS fighters escaped captivity, and a million homeless Syrians now face devastation.

In Libya, another country whose civil war seems to be reaching a nightmarish crescendo after a botched Western intervention, the problem was not in the intervention per se, but in, as former President Obama himself diagnosed, "failing to plan for the day after." That failure, he contends, was his worst mistake as president. Obama shared Trump's instinct for retrenchment — most people agree with them both today — but here he directly admits that instinct was wrong: It was more investment, not less, that would have prevented today's Libyan hellscape. But why did Obama fail to commit fully after Moammar Gaddafi was toppled? Surely it was the mistakes made in Iraq and Afghanistan that drained, and continue to drain, Western governments of the confidence in their ability to do good through action.

There are many examples of effective intervention and long-term presence, and they're not obscure: Japan, Taiwan, and Eastern Europe have their own, complex, imperfect stories, but each is a relatively stable, prosperous beneficiary of many decades of political and military investment. The United Nations Command in South Korea maintains a presence amid a war that has technically never ended since its outbreak in 1950.

Another, lesser known example is the British-led United Nations involvement in Sierra Leone, in which military intervention ended a violent decade-long civil war. The Revolutionary United Front, whose leaders were convicted of war crimes in 2009, was bearing down on Freetown, the country's capital, but the British intervention stopped them short and brought peace. What's more, long-term engagement in the conflict's wake bolstered institutions and restored stability. Sierra Leone is still a country with problems, but it is no longer a country at war. It is no coincidence that this successful campaign began in 2000, at the height of Western confidence, before the tragedy of 9/11 turned intervention from something undertaken with discernment, strategy, and planning, into a solipsistic exercise in emotional therapy, craving glorious, impossible, all out victory.

"The first, the supreme, the most far-reaching act of judgment that the statesman and commander have to make is to establish ... the kind of war on which they are embarking," wrote the Prussian General Carl Von Clausewitz, in Vom Kriege (On War), 200 years ago, "neither mistaking it for, nor trying to turn it into, something that is alien to its nature." Failing to heed this guidance led to mistakes in Iraq and Afghanistan, but heeding it, as Western governments could have done in Syria soon after the Arab Spring by giving decisive support to legitimate democratic forces, can lead to intervention that works.

After the Afghanistan chapter closes, the world will continue to present Western governments with stark and urgent decisions on whether they should intervene. If the horror currently developing in Idlib province were unfolding, instead, in 1999, then Washington, intoxicated with recent successes and moral certitude, would be working much harder to protect the refugees — and that would be the right thing to do. Caution is always appropriate, but the window of opportunity is invariably narrow, and closing fast.

As a broader geopolitical strategy, well-judged intervention plus its sometimes-necessary accompaniment — indefinite military effort — can help keep the forces of totalitarianism at bay; not everywhere, and never without diplomatic engagement. Rather than vacillating between opposite doctrines that say intervening is good or not, or realistic or not, democracies should be engaging their collective thought to get better at it. We must remember that the destructive force behind the Afghanistan conflict was the collapse of the Twin Towers, not the principle of intervention itself, or even that of endless war.

Want more essential commentary and analysis like this delivered straight to your inbox? Sign up for The Week's "Today's best articles" newsletter here.