The United States went into Afghanistan to avenge the 9/11 attacks. We stayed promising to rebuild the country — with President George W. Bush invoking, in 2002, “the best traditions of George Marshall” and then-Sen. Barack Obama, in 2008, calling on Americans to “heed Marshall’s lesson” — even as both leaders took pains to avoid the term nation-building.

By the time President Biden had decided to end our military commitment in Afghanistan, he insisted to Americans that the United States “did not go to Afghanistan to nation-build.”

In truth, the American effort there was something in between — a half-in, half-out approach, matching high expectations with muddled approaches, dragging on for two decades. Although the Afghan government may have fallen apart on Biden’s watch, those of us who covered the war in the early days saw the seeds of failure sown a long time ago.

As a Washington Post correspondent, beginning in 2001 I went from Kunduz in the north to Kandahar and Lashkar Gah in the south; to Jalalabad in the east and Herat in the west. I was embedded once with U.S. troops and another time toured with a visiting congressional delegation, but mostly I traveled only with an interpreter and driver, often alongside Italian photojournalist Ortensia Visconti. Over the war’s first decade, I saw a focused military effort devolve into an unfocused undertaking to make over a country. Something akin to nation-building took place during those early years. But it was never on the scale of the Marshall Plan; it lacked a clear commitment and objective, and the United States was facing an enemy that retreated but was never defeated.

It started with hope: Not long after traversing the Hindu Kush on horseback, I was in Kabul the day the Taliban fled, alongside fighters of the anti-Taliban, nominally pro-American Northern Alliance. Afghans welcomed the American intervention after five years under Taliban rule. Crowds in Kabul celebrated in the streets, women removed their burqas, and music — mostly prohibited during Taliban rule — blasted from cassette decks. Afghans were walking up to me to shake hands and say, “Thank you, America!”

But I also saw a city and later a countryside battered by war. Reconstructing the country would take money, personnel and a sustained commitment: Rebuilding Europe after World War II took years and billions of dollars. Above everything else, rebuilding would require security and relative peace maintained by American troops. President Barack Obama’s troop surge was years away, however, and the Taliban never went away. It continued to fight the U.S. military and Afghanistan’s U.S.-backed government, falling back across the border into Pakistan when needed, with the unofficial support of that country’s military and intelligence services.

Successive American leaders were unwilling to directly confront Pakistan, a nuclear power and an erstwhile ally in the war on terror. NATO allies dutifully joined the U.S. effort in Afghanistan, but as NATO Secretary General Jaap de Hoop Scheffer explained in 2004, coalition forces could secure the capital, but could not lead the fight in remote regions. And they wouldn’t be responsible for shutting down Afghanistan’s lucrative drug trade.

On my return trips to Afghanistan in 2004, I was surprised by how much the security situation had deteriorated in three years. Kabul for a while had been largely peaceful, with an expanded corps of foreign diplomats, aid workers and journalists venturing out without security guards, taking public taxis and dining in local restaurants. But suicide bombings had become common, and foreign aid workers were being kidnapped in broad daylight.

Outside of Kabul, U.S. soldiers, Marines and Afghans alike described how fighting the Taliban was a hit-and-miss exercise. When large numbers of American troops were stationed in a town or village, Taliban fighters would disappear into nearby mountains or almond groves. They would launch ambush attacks at night, then disappear effortlessly among the populace when American troops patrolled in the daytime. U.S. forces could rely on air power, but it was limited against a foe that could blend in with local farmers.

“As soon as the choppers come, they’re dropping their weapons and picking up their goats,” one frustrated American soldier told me. And that frustration extended to what was, by then, the biggest distraction from the Afghanistan war: Iraq. More than one service member complained to me that they felt their mission was being overlooked because Washington had shifted its attention to the war in Iraq.

At that time, the U.S. troop presence in Afghanistan was only about 20,000, while in Iraq there were about six times that many. The Iraq War dominated the headlines. The soldiers I traveled with said they had never seen an American reporter — indeed, I, too, had forgotten Afghanistan for a time while I spent time covering the American invasion of Iraq.

During one 2004 trip to Afghanistan, I got a firsthand glimpse of the Taliban’s hit-and-run tactics: I was with an Army unit called Alpha Troop in a part of Kandahar province that was eerily quiet during our two-day stay. Most of the unit returned to the city of Kandahar, leaving behind a platoon of about 20, and that’s when Taliban fighters attacked with mortars, rocket-propelled grenades and AK-47s — but the main force was already too far away to return, leaving soldiers slamming fists on the dashboards of their Humvees, pained as they listened to their brothers in arms on the radio, trying to fend off the attack.

That same evening on the road to Kandahar, we came upon a grisly discovery. Scattered along the roadside were the bodies of seven Afghan men who looked like they had either been executed or shot while trying to flee. The Army doctor with us estimated that the men had just recently been killed, because the blood was still fresh. We surmised that they may have been working with the Afghan government or suspected by the Taliban of assisting American troops. The Alpha Troopers, many barely old enough to drink back home, dutifully wrapped the corpses in body bags or rain ponchos and secured them on the tops of their vehicles, and then we went on our way.

The pivot to Iraq left Afghanistan, in some ways, as an afterthought. But by 2010, Obama had surged troop levels in Afghanistan to about 100,000, and civilian-run “provincial reconstruction teams” were more visible around the country, given the task of building schools and clinics, and distributing aid. But when I visited one that year, in impoverished Helmand province, the team was working behind fortifications and had to be protected by coalition troops. I learned that the Helmand PRT shut down a few years later, with some of its projects left incomplete.

At one point in 2004, my interpreter, driver and I had traveled to Kandahar from Kabul on a brand-new highway, a showpiece of the coalition effort that had significantly cut travel time between the two cities. But we were warned to drive only during early morning hours, and at high speed, because the Taliban was active along the highway. As we drove, the highway was empty except for a few trucks we passed, and it occurred to me that it symbolized America’s predicament: We could provide Afghanistan with an upgraded highway — an attempt at modernizing transportation in a country lacking sufficient infrastructure — but it was often too dangerous to travel because it couldn’t be secured militarily.

On my last trip to Kabul in 2010, I visited a coalition-run military training school for Afghan security forces. American trainers were teaching Afghan recruits some basic drills. But the Americans privately complained to me about the problem of desertions and officers withholding the pay of the soldiers — a problem that clearly remained and contributed to the Afghan military’s surprisingly rapid defeat.

During that trip, I encountered Sen. Carl Levin, for many years the senior Democrat on the Senate Armed Services Committee (he voted against the 2002 Iraq War resolution), who was there on a fact-finding mission. I had known him since I covered his first senate campaign in Michigan, and when I caught up with him, he complained about the lack of military trainers: “It is quite unacceptable, and we need a lot more of our coalition partners to step forward,” he told me. His frustration, too, was emblematic of America’s bind.

The swiftness of the Afghan army’s collapse and the Taliban’s retaking of Afghanistan was a shock. But it shouldn’t have been. The seeds of failure were planted years ago.