The summer of 2001 looked like a promising one for Afghanistan. Broad opposition to the Taliban was growing, and to the Bush administration’s point man on Afghanistan, Zalmay Khalilzad, it seemed that under the leadership of the charismatic officer Ahmad Shah Massoud, the Northern Alliance could be paving the way for a return to the relative stability the country had enjoyed for 50 years before the Soviet invasion of 1979.

Then Sept. 9 came. Khalilzad, then the senior director at the National Security Council for the Persian Gulf, Iraq, Iran and Afghanistan, learned that Massoud had been assassinated by al Qaeda.

It looked like a new playbook was going to be needed to fight the Taliban. Early on the morning of Sept. 11, Khalilzad walked into the White House Situation Room to work on finalizing a new directive for President Bush’s signature. There was some talk about a freak accident at the World Trade Center when National Security Council Advisor Condoleezza Rice convened the meeting. An assistant handed Rice a message, and she snapped shut the portfolio of papers in front of her. The second tower had been hit. “We need to go,” she said.

No one knew what to do. Khalilzad paints a picture of White House staffers wandering aimlessly in the street. A dizzying new era had begun. And from that day, to the fight against the Taliban and the war in Iraq, Khalilzad was a key figure for many of the signal decisions that would define that era, as he explains in his penetrating memoir “The Envoy: From Kabul to the White House, My Journey Through a Turbulent World” (St. Martin’s Press).

Khalilzad, a Sunni who is the highest-ranking Muslim ever to serve in the federal government, became ambassador to both Afghanistan and Iraq in the Bush years, but long before that he had a knack for being in the room while history was being written. In 1979, for instance, he met with the Ayatollah Khomeini, then the leader of the Iranian opposition while living in exile in Paris. The sinister nature of the cleric’s intentions immediately became clear. The ayatollah didn’t know that Khalilzad, a young academic at Columbia University whose writings were just beginning to attract notice in Washington, understood Persian when an aide told him in that language, “Tell the American professor that we want democracy and rights for women — this is what Americans like to hear.”

Khalilzad recalls, “I came away perturbed. I realized that Khomeini had a clear set of totalitarian ideas and an intricate plan to implement them.” Khomeini left the young academic with a stack of sketchy “research materials” that included ridiculous propaganda about the shah. One book “proved” that the shah was an agent of zionism by including a photo of the monarch meeting with Israeli statesman Shimon Peres. Except it wasn’t a picture of Shimon Peres, Khalilzad realized: It was a picture of President Carlos Andres Perez of Venezuela. A month later, Khomeini returned to Iran and swept to power as the shah fled.

Khalilzad was a State Department advisor in 1987 when the then-leader of the Afghan rebels who were fighting the Soviet invaders went to the White House, at his urging, to meet President Reagan. Khalilzad wasn’t supposed to be present, but at the last minute he was pressed into service as an interpreter because the original interpreter lacked a sufficient security clearance.

Younis Khalis, the mujahideen leader, secured permission from his fellow fundamentalists to meet Reagan on condition that he bring the truth of Islam to the American leader. Nervous, Khalilzad whispered the translation of Khalis’ words into the president’s ear, but Reagan told him he should say everything out loud so that everyone in the Oval Office (including Vice President George H.W. Bush and three top cabinet officials) could hear. Startled, Khalilzad related the words the guerilla soldier had spoken: “Islam is a religion of righteousness and peace. And given its universal truth, Mr. Khalis would like to invite you, Mr. President, to accept the religion of Islam.”

Replied Reagan, “Well, please tell Mr. Khalis that we have our own religion. Furthermore, today, the struggle is between believers and non-believers. As fellow believers, we are on the same side. We are with you in your struggle against the Soviet Union.” The Afghan rebel was so elated by Reagan’s words that he asked for a copy of the speech Reagan later gave to the press. It turned out Reagan had been riffing, working with only a stack of note cards, which he happily handed over to his dazzled new friend.

Years later, with media dubbing Khalilzad “Bush’s favorite Afghan,” the diplomat worked closely with Bush 43. Invited to Camp David in late 2001, Khalilzad was ushered into a room in which President Bush was studying a huge map of Afghanistan. The envoy was sitting down reading The Washington Post when he was jolted by a sudden slap on the back. “Hey, Zal, what are you doing?” Bush asked. When Khalilzad said he was trying to learn about what was going on in the world, the president replied, “You won’t find that out by reading The Washington Post!”

Khalilzad sought to persuade the president that the conventional wisdom about Afghanistan being a hissing snake pit of disputatious tribes was incorrect: Before the Soviet invasion, the country had mostly enjoyed internal peace and strengthening national institutions.

The Muslim world was suffering a crisis of civilization, Khalilzad believed, and it fell to the United States to work with moderates to nudge Islam forward. “The president was quiet,” Khalilzad recalls. “Whether he was lost in deep thought or shocked by the magnitude of the challenge ahead, I could not tell.”

When Khalilzad returned to his native city in 2002, it was as Bush’s Special Envoy to Afghanistan. Kabul, once a lively city, was now a wasteland where functioning districts had been replaced by piles of rubble. On the drive in, Khalilzad spotted his own childhood house, damaged in the wars but still standing. Security personnel talked him out of knocking on the door to see who lived there. In the palace where a king had gently ruled during Khalilzad’s youth, the Taliban had smashed in or blackened every image of every living thing, whether human, plant or animal, in accordance with fundamentalist Islamic prohibitions.

As Khalilzad struggled to help form a new provisional government in the wake of the Taliban’s ouster, he says, he worked a quiet back channel with Iran. US relations with the country had “actually improved since 9/11,” Khalilzad notes, because both countries were backing the Northern Alliance against the Taliban. (Iran is mostly Shiite, whereas the Taliban are Sunnis.) Working with moderate Iranian diplomats, Khalilzad coaxed Iran into supporting the US choice of Hamid Karzai as the new leader of Afghanistan.

Even more delicate diplomatic work involved Joe Biden, then part of a delegation of US senators. Sen. Biden got in an argument with an Afghan political hand named Younus Qanooni that got a little out of hand. The then-senator called up Khalilzad late at night desperately in need of help patching things up. Sheepishly, Biden “confessed with considerable remorse that he had threatened to drop B-52s on Qanooni,” Khalilzad recalls.

The diplomat called up Qanooni to talk about the incident. “Don’t even mention that man to me!” said Qanooni.

But Khalilzad coaxed the other man into making a pot of tea and brought Biden over to chat. In a little while the senator and Qanooni were trading war stories like old buddies, and when Khalilzad left them they were still talking merrily.

They continued chatting for hours, Khalilzad learned later, and Biden became a trouper around the compound despite the frazzled wartime conditions, bunking in a sleeping bag spread out on the floor of a conference room and waiting his turn for the shower along with everyone else, clad only in a towel. When a Marine standing behind him took his picture, Biden turned to ask what he was doing. The Marine said the photo was for his mom. Biden said she would hardly be able to recognize him from behind, so he proudly posed for a picture from the front.

Khalilzad left Afghanistan better than he found it, but the Bush administration clearly didn’t fully understand the breadth of difficulties it would face there. “Zal, you have to take your hand off the bicycle seat!” then-Secretary of Defense Donald Rumsfeld once told Khalilzad, meaning he had to let the country proceed on its own.

“Mr. Secretary, there is no bicycle,” was Khalilzad’s regretful response. But soon it was on to working on a plan for postwar Iraq, where Khalilzad would shack up in the ornate house of Saddam Hussein’s mother-in-law while serving as ambassador. Quoting President Dwight Eisenhower, he notes, “The plan is useless but planning is essential.”

After meeting Khalilzad on a trip to Baghdad during which he didn’t say much but asked thoughtful questions, then-Sen. Barack Obama told President Bush he disagreed with the administration’s approach to Iraq but had confidence in the ambassador, whom he mentioned in his book “The Audacity of Hope.”

As the situation deteriorated in Iraq, Khalilzad was struck by the carelessness of some leaders. As civil unrest grew, then-Iraqi Prime Minister Ibrahim al-Jaafari, a Shia, said the insurgents merely had to “let off some steam.” Shocked, Khalilzad said, “Do you understand that scores of innocent Iraqis are being killed right now in the streets and in their homes?” The prime minister “brushed off the question.”

The envoy left Iraq “profoundly disappointed,” though he believes “One of the most astonishing lessons to me of the Afghanistan and Iraq campaigns was how a small number of democratically minded leaders, educated in the West, could play positive, outsize roles under the right circumstances.” He thinks President Bush must be faulted for “the inadequacy of his deliberations before the invasion.”

However, he adds, “the same cannot be said of his engagement with Iraqi policy during my ambassadorship. He asked probing questions about the military situation . . . Careful presidential command proved the decisive factor in the implementation of the surge strategy.”

Yet he says a central problem with Iraq was the evolution of the Shia Prime Minister Nouri Maliki from what he calls Maliki I to Maliki II — a cross-sectarian figure turned bitter partisan.

President Obama’s failure to engage was critical here: “The tragedy of Iraq, and Maliki as a leader, stemmed from President Obama’s decision to withdraw from Iraq, rejecting military recommendations to leave a substantial residue force in the country,” Khalilzad writes. “Maliki sought a comprehensive strategic partnership with the United States. The Obama administration responded with an offer of a token US force.”

Khalilzad adds that “Once Maliki concluded that he could not count on the United States, he became what I call Maliki II. Letting his sectarian instincts guide him, he purged opponents.”

Khalilzad carefully repeats his words: Obama’s decision to squander the hard-won successes of the surge in Iraq was “a tragedy.”