PRESIDENT TRUMP offered in his speech on Afghanistan Monday a rare but welcome story of self-correction. His “original instinct,” he said, was “to pull out” of the country, but after studying the issue with his advisers over several months he realized that “the consequences of a rapid exit are both predictable and unacceptable.” That was the right conclusion, and Mr. Trump deserves credit for changing his position in a way that is likely to displease some of his political supporters. The U.S. mission in Afghanistan will continue — not because a quick victory is on the horizon; it’s not, as Mr. Trump seems to understand. It will continue, because as Mr. Trump also came to understand, the alternative — a quick defeat — would be so much worse.

The Afghanistan strategy the president announced flows from two important insights about the long and frustrating U.S. military engagements there and in the Middle East since Sept. 11, 2001. First, precipitous U.S. withdrawal produces a vacuum that is likely to be filled by terrorists. That, as Mr. Trump correctly observed, was the result of the Obama administration’s retreat from Iraq in 2011.

The second lesson is that the extent and length of U.S. military operations must be dictated by progress on the ground, not timetables motivated by political considerations in Washington. The surge of U.S. forces launched by President Barack Obama in 2009 was undermined from the beginning by a public schedule for their withdrawal, which gave the Taliban and its allies in Pakistan cause to wait out the United States.

The absence of a clear exit strategy can be a danger in its own right, and Mr. Trump stressed that “our commitment is not unlimited, and our support is not a blank check.” But the reality is that preventing Afghanistan’s return to its state before 2001 is worth even a prolonged continuation of U.S. troop deployments and the inevitable fatalities that come with them. American deaths in Afghan operations since 2001 now number 2,403, including 11 this year, which is fewer than the 3,000 who died in the 9/11 attacks. Though some describe Afghanistan as America’s longest war, it can also be compared with U.S. military deployments in Germany, Japan and South Korea, which have lasted far longer and which, as the Pacific naval accidents this summer underlined, also have their cost in lives.

Certainly, Afghan leaders and their American and NATO allies must continue to seek avenues for ending the conflict and stabilizing the country. There, Mr. Trump’s plans sound a lot like those of his predecessors: demand better performance from the Afghan government; pressure Pakistan to end its support for the Taliban and other terrorists; support eventual negotiations on a political settlement. Yet this administration appears poorly prepared to pursue those aims. The State Department still lacks senior personnel who could broker such deals or exercise leverage in Islamabad. The special office on Afghanistan and Pakistan once headed by the formidable Richard Holbrooke has been shut down.

Mr. Trump made a point of declaring an end to the era of U.S. “nation-building,” apparently unaware that the two presidents before him said the same thing. That hackneyed mantra ignores the reality that unless the Afghan government becomes more accountable and less riven by factional infighting and corruption, U.S. forces will never be able to safely leave the country. Promoting institution-building, including fair democratic elections, is not at odds with “principled realism,” as Mr. Trump suggests; it is a vital part of any realistic exit strategy.