When historians look back at the moment, they may well conclude that Washington ended up much like other great powers that entered Afghanistan’s rugged mountains and punishing deserts: frustrated, immobilized, no longer willing to bear the huge costs. The British retreated in 1842 after suffering 4,500 killed, amid massacres that preceded the invention of the roadside bomb. They gave up their sovereignty over the country in 1919, in another retreat that heralded the beginning of the unwinding of an empire.

The Soviet Union abandoned its decade-long effort to control the country in 1989, months before the fall of the Berlin Wall marked the collapse of the Communist superpower. That led to the chaos and power vacuums that Bin Laden exploited, and that the United States vowed it would never again allow to fester.

The American-led attack began on Oct. 7, 2001, with the name “Operation Enduring Freedom.” Mr. Bush, in an address to the nation from the Treaty Room of the White House, promised to “win this conflict by the patient accumulation of successes, by meeting a series of challenges with determination and will and purpose.”

After the destruction of the twin towers of the World Trade Center, a plane that hit the Pentagon and another that crashed in a field in Pennsylvania, the country was behind him. While a few warned of the dangers of entering the “graveyard of empires,” it seemed more a war of retribution and justice seeking than an effort at nation building.

Yet inevitably, mission creep set in.

After Bin Laden was hunted down in Pakistan in May 2011, and with Al Qaeda a much-diminished threat, politicians struggled to explain what American troops were fighting to accomplish. More than 2,400 service members have died in combat since the invasion, according to the website icasualties.org. Neither Mr. Obama nor Mr. Trump could make a plausible case that after nearly two decades the United States had much of a role to play other than prop up a weak democracy.