President Biden’s blunders in what is — suddenly — a Taliban-dominated Afghanistan may be measured in many ways. One is by searching the sorriest episodes of U.S. foreign policy history for an analogy. Former defense secretary and former CIA director Leon E. Panetta drew a comparison with the Bay of Pigs, the 1961 U.S. attempt to overthrow Cuba’s Fidel Castro, which ended with hundreds of CIA-backed invaders killed or captured after President John F. Kennedy denied them air cover.

Another parallel: the desperate plight of U.S. allies at the fall of South Vietnam in April 1975. Images of Afghans, terrified of Taliban rule, clinging to a departing U.S. military aircraft — some fell to their deaths — are indeed reminiscent of the last days in Saigon. Imagine how desperate a person must be to risk piggybacking on a moving airplane; such is the betrayal of the trust so many Afghans placed in the United States.

Worse, this was avoidable. Conventional military triumph was not in the cards in Afghanistan, as Mr. Biden forcefully insisted in a speech to the nation Monday, in which he blamed his predecessors and Afghanistan’s political leaders for failures that set the stage for today’s disaster. Contrary to his and others’ cliches about “endless war,” though, U.S. troops had not been in major ground operations, and had endured very modest casualties, since 2014. Mr. Biden statically measures the dollar costs of staying in Afghanistan. Yet there will be costs, potentially high ones, attached to a botched withdrawal, too. A small U.S. and allied military presence — capable of working with Afghan forces to deny power to the Taliban and its al-Qaeda terrorist allies, while diplomats and nongovernmental organizations nurtured a fledgling civil society — not only would have been affordable but also could have paid for itself in U.S. security and global credibility.

Mr. Biden might have renegotiated the withdrawal deal his predecessor, Donald Trump, cut with the Taliban. Certainly the Taliban’s repeated violations of that pact gave Mr. Biden a legitimate reason for doing so. A regional diplomatic push for a more sustainable political deal was outlined in February by the congressionally authorized Afghanistan Study Group.

But even if you reject all of these arguments — as Mr. Biden did, claiming any presence would have led to more combat for U.S. troops — the pullout need not have degenerated into catastrophic spectacle. He could have planned to leave maintenance contractors, who kept the Afghan military’s medevac helicopters and other crucial aircraft in flying shape, knowing that air support was critical to that army’s ability and willingness to fight. He could have foreseen the need to maintain some presence until Americans and allies had left the country.

In short, the president could have listened to the many seasoned hands — inside and outside his own administration — who advised him that there were alternatives to precipitous, unconditional withdrawal. Mr. Biden instead set an arbitrary deadline — Aug. 31 — for a full U.S. pullout. Yes, the Afghan military’s demoralization and failure to fight came as a rude disappointment, as the president emphasized, but it’s fair to ask why, if he was sure the cause was lost, their quick surrender came as such a surprise to him. The blame-shifting is especially unseemly given that some 66,000 Afghan fighters have given their lives in this war during the past 20 years, alongside 2,448 U.S. service members.

Mr. Biden said he stands by his decision, and takes responsibility. Well and good: He must now turn his attention to limiting the damage — starting with the obligation the United States owes to those Afghans who trusted America’s commitment to their country and staked their personal futures on it. The analogy here is from British history: It is a “Dunkirk moment,” as a 40-member group of House members warned in a letter to the president Aug. 13. There are not only thousands of former translators and others who worked directly for the U.S. military and civilian agencies, and their families; there are also the many others — “Priority 2” personnel, in State Department parlance — who supported the broader U.S. and NATO-led project for the country: development workers, journalists, women’s activists and their families. Mr. Biden demanded that the Taliban allow U.S. troops to take Americans and Afghans out, and he must make maximum use of the United States’ modest remaining leverage to back that up. The sad fact, though, is that the administration’s shocking failure to plan for this contingency has left all of these people — along with an unknown number of Americans now “sheltering in place” at State Department instruction — potentially at the mercy of the Taliban.

Mr. Biden pledged as well a robust effort on what he described as the United States’ only vital interest in Afghanistan: preventing renewed terrorist strikes from bases in that country. This can be done, he said, “over the horizon,” from bases in the greater Middle East — but, if so, it will be without the benefit of the intelligence and other capabilities based in Afghanistan that the United States has just abandoned.

The point of leaving Kabul is to save resources that may now be devoted to geopolitical struggles with Russia and China, Mr. Biden argued. Supposedly these rivals would have been delighted to see U.S. forces tied down indefinitely in Afghanistan. Maybe so; but then it is hard to imagine that they are not delighted today, as U.S. allies in Europe and Asia are dismayed, at the incompetent handling of the withdrawal.