“Security vacuum,” Ambassador Stevens wrote in his personal diary on Sept. 6 in Tripoli, in one of the few pages recovered from the Benghazi compound.

“Militias are power on the ground,” he wrote. “Dicey conditions, including car bombs, attacks on consulate,” he continued. “Islamist ‘hit list’ in Benghazi. Me targeted on a prominent website (no more off compound jogging).” A map of his Tripoli jogging route had appeared on the Internet, seemingly inviting attacks, diplomats said.

But when he arrived from Tripoli for a visit, he was glad to be back in Benghazi. “Much stronger emotional connection to this place,” he wrote in his diary on Sept. 10, “the people but also the smaller town feel and the moist air and green and spacious compound.”

By 7 a.m. on Sept. 11, guards at the American Mission had spotted a man taking photographs with a cellphone on the second floor of an unfinished building next to the Venezia Restaurant across the street, according to interviews with the compound’s Libyan guards as well as the State Department report.

When the guards approached, the photographer fled in a police car with two others, all in the uniforms of a quasi-official militia known as the Supreme Security Committee. Fawzi Wanis, a former commander of the group, said he suspected that the men were doing reconnaissance for someone else.

“We had all kinds in the Supreme Security Committee, from Islamist extremists to drunks,” Mr. Wanis said.

In his diary, Mr. Stevens wrote, “Never ending security threats…”

Around dusk, the Pan-Arab satellite networks began broadcasting footage of protesters breaching the walls of the American Embassy in Cairo, pulling down the American flag and running up the black banner of militant Islam. Young men around Benghazi began calling one another with the news, several said, and many learned of the video for the first time.

Mr. Stevens, who spent the day in the compound for security reasons because of the Sept. 11 anniversary, learned about the breach in a phone call from the American Embassy in Tripoli. Then a diplomatic security officer at the Benghazi mission called to tell the C.I.A. team. But as late as 6:40 p.m., Mr. Stevens appeared cheerful when he welcomed the Turkish consul, Ali Akin, for a visit.

There was even less security at the compound than usual, Mr. Akin said. No armed American guards met him at the gate, only a few unarmed Libyans. “No security men, no diplomats, nobody,” he said. “There was no deterrence.”

Sept. 11, 2012 9:02 P.M. A police car stationed in front of the compound belatedly arrives. It stays parked outside for 40 minutes and abruptly leaves. The attack begins moments later. 9:42 P.M. Attackers storm through the main gate and set barracks and cars on fire. Mr. Stevens, Mr. Smith and a security officer in the main villa lock themselves in a safe room. Attackers set the main villa’s living room on fire. They bang at the safe room’s door, but do not enter. Three American armed officers outside the main villa rush to retrieve rifles, radios and bulletproof vests in the security quarters. Two of them try to return to the villa, but find the alley between the two buildings overtaken by attackers. Outnumbered and outgunned, they barricade themselves in the security quarters. As the main villa is engulfed in smoke, the security officer tries to guide Mr. Stevens and Mr. Smith outside through a window. The officer leaves, but the two men do not follow. The officer re-enters the building multiple times, but is unable to find the two men.

At 8:30 p.m., British diplomats dropped off their vehicles and weapons before flying back to Tripoli. At 9:42 p.m., according to American officials who have viewed the security camera footage, a police vehicle stationed outside turned on its ignition and drove slowly away.

A moment later a solitary figure strolled by the main gate, kicking pebbles and looking around — a final once-over, according to the officials.

The attack began with just a few dozen fighters, according to those officials. The invaders fired their Kalashnikovs at the lights around the gate and broke through with ease.

The compound had a total of eight armed guards that night: five Americans and three Libyans affiliated with the February 17 militia. All of them fell back. The Americans raced to grab their weapons in the compound’s other buildings but then found a swarm of attackers blocking their way to the main villa.

Mr. Stevens and an information officer took refuge in the villa’s safe room while an armed security officer positioned himself to defend it.

Reports from the scene ricocheted around the city in frantic phone calls telling competing stories. Abu Baker Habib, a Libyan-American friend of Mr. Stevens, began calling for help from a handful of the most important militia leaders, like Mr. Bin Hamid and Mr. Gharabi. But a false report spread much wider and faster: that guards in the compound had shot and wounded Libyans who had come only to protest.

“They told each other that the Americans had killed a Libyan,” Mr. Gharabi said. “For that reason, everybody would go.”

Mr. Gharabi, who was at a friend’s wedding a hundred miles away, knew that some of his fighters would join the attack, so he sent a delegation of “wise men” to deter them, he said. Mr. Bukatef of the February 17 Brigade was in Tripoli that night but said in an interview that he also believed some of his men had participated.

Soon scores, if not hundreds, of others were racing to the scene. Some arrived with guns, some with cameras. The attackers had posted sentries at Venezia Road, adjacent to the compound, to guard their rear flank, but they let pass anyone trying to join the mayhem. Witnesses said young men rushing inside had left empty pickup trucks from Ansar al-Shariah, but also all the other big militias ostensibly allied with the government.

Sept. 11, 2012 10:05 P.M. A C.I.A. team leaves the Annex in two vehicles, trying and failing to get help from militia members it finds along the way. It takes enemy fire as it approaches the mission. The Annex team rescues the officer in the monitoring office and joins the search for the two missing Americans. They find Mr. Smith in the main villa, dead from smoke inhalation, but not the ambassador. Cellphone video posted online shows Mr. Stevens's body being recovered later by a crowd from a window near the villa's entrance.

There is no doubt that anger over the video motivated many attackers. A Libyan journalist working for The New York Times was blocked from entering by the sentries outside, and he learned of the film from the fighters who stopped him. Other Libyan witnesses, too, said they received lectures from the attackers about the evil of the film and the virtue of defending the prophet.

Mr. Abu Khattala’s whereabouts on the day before the attack could not be determined, nor could his precise role in its planning. People who know him say he was at work as usual in the days leading up to it.

“His neighborhood is full of people like him,” said the leader of a major Islamist militia, speaking on the condition of anonymity for fear of reprisals. “So it is easy for him to pick up a phone and rally people around him.”

Witnesses at the scene of the attack identified many participants associated with Ansar al-Shariah. Mr. Abu Khattala’s presence and leadership were evident. He initially hung back, standing near the crowd at Venezia Road, several witnesses said. But a procession of fighters hurried to him out of the smoke and gunfire, addressed him as “sheikh” and then gave him reports or took his orders before plunging back into the compound.

A local Benghazi official named Anwar el-Dos arrived on the scene and identified Mr. Abu Khattala as directing the fighters, people present said. Then Mr. Dos approached Mr. Abu Khattala for help entering the compound.

The two drove into the mission in Mr. Abu Khattala’s pickup truck, the witnesses said. As he moved forward, the fighters parted to let them pass.

Mr. Abu Khattala, in an interview, recounted meeting Mr. Dos that night. Mr. Dos declined to comment. When the truck doors opened inside the compound, witnesses said, Mr. Dos dived to the ground to avoid gunfire that was ringing all around. But Mr. Abu Khattala strolled coolly through the chaos.

“He was just calm as could be,” a young Islamist who had joined the pillaging said, speaking on the condition of anonymity for fear of retribution. Around 11:30 p.m., Mr. Abu Khattala showed up on internal security cameras, according to officials who have viewed the footage.

Sept. 11, 2012 11:30 P.M. The five security agents originally in the mission leave the compound in an armored vehicle and come under attack. They manage to escape, but are followed to the Annex. Back at the main villa, Americans are attacked again. The team is unable to find the ambassador and retreats to the Annex carrying Mr. Smith’s body. Shortly after they reach the Annex, the compound is attacked with gunfire and rocket-propelled grenades, intermittently, for an hour.

Witnesses described utter bedlam inside. Men looted suits of clothes and carried them out on their hangers. They lugged out televisions. Some emerged from buildings clutching food they had found, and one poured what appeared to be Hershey’s chocolate syrup into his mouth. Others squabbled over trophies as small as a coil of rope left on the ground.

A newly acquired and uninstalled generator sat near the main gate, with large cans of fuel beside it. Attackers stumbled upon it within 15 minutes of entering the compound, according to officials who have seen the video footage, and soon begun using the fuel to set fire to vehicles and buildings.

Libyan militia leaders who might have intervened to help the Americans washed their hands of the attack. At the militias’ so-called joint operations room inside the February 17 Brigade headquarters, the commander in charge was Mr. Bargathi of the militia called the Preventive Security Brigade. He had also been a friend and neighbor of Mr. Abu Khattala since childhood.

He said he immediately radioed the Libyan guards in the compound and told them not to resist the assault. “I told them: ‘Don’t shoot. Just run away from the place,' ” he said. “Because I knew that it was not wise to provoke. These are not like normal attackers, and it might enrage them more. They might kill everyone inside.”

He volunteered that the leaders of Ansar al-Shariah had joined him in the operations room shortly after the attack began — underscoring the permeability of the line between threat and protector among Benghazi militias.

Of all the major militias in the city, Libya Shield was the best positioned to intervene. It was arguably the most formidable in the country at the time, and its leader, Mr. Bin Hamid received an urgent call from the ambassador’s friend Mr. Habib asking for help. Mr. Bin Hamid arrived at the scene within 30 minutes after the attack began, he said in an interview.

“The situation wasn’t suitable for me to go inside the compound,” Mr. Bin Hamid said. “And when the shooting stopped, we thought the Americans had been evacuated.”

A group of about 20 young men who had been hanging around the headquarters of the February 17 Brigade did try to help the Americans. But they ran into the attackers’ sentries on Venezia Road.

“They pointed their guns at us and said, ‘This is none of your business, go back,' ” said Sherif Emrajee el-Sherif, 18, a petroleum engineering student who was among those who tried to help the Americans.

Sept. 12, 2012 1:00 A.M. A seven-person American team from Tripoli arrives in Benghazi on a Libyan cargo jet, but struggles to negotiate an escort out of the airport. 5:00 A.M. The response team arrives at the Annex, accompanied by a convoy of about a dozen Libyan militia trucks. Minutes later, the Annex is attacked again. Three mortar rounds hit one of the buildings, killing the security officers Tyrone Woods and Glen Doherty. Two other officers are also wounded.

The militia fighters all followed an unstated code, the rescuers and other militiamen said. Never enter a public gunfight with other Libyans, for fear of setting off a cycle of retaliatory violence and demands for blood money. “It is normal,” Mr. Sherif explained. “Whatever happened, they were other Libyans.” (He and at least one other rescuer ultimately entered the compound with Americans from the C.I.A. Annex, and Mr. Sherif was shot in the leg in gunfire inside.)

As the melee continued, Mr. Abu Khattala drove to the headquarters of Ansar al-Shariah and an affiliated militia, Othman Ibn Affan, witnesses said.

At one point, a fighter asked Mr. Abu Khattala what to do with the remains of the compound. “Flatten it,” he said.

Later, Mr. Abu Khattala appeared to prepare for another phase of the attack. One young fighter with him told another to “cleanse ourselves for another battle” — an apparent reference to a subsequent attack on the C.I.A. Annex.

That phase appears to have been improvised that night. After the Americans fled from the mission to the C.I.A. Annex, it, too, came under a sporadic, low-grade attack for the first time, suggesting that the assailants had just learned of it. Later, guards there observed people lingering in a nearby pasture, stirring fears that they were plotting coordinates for launching a mortar attack.

Back in Tripoli, American diplomats scrambled to make sense of the news out of Benghazi. Many learned of Ansar al-Shariah’s existence from social media during the attack. They sent seven security officers to Benghazi in a borrowed Libyan cargo jet.

Sept. 12, 2012 6:30 A.M. Americans leave for the airport with support from a Libyan militia. One hour later, part of the staff leaves Benghazi on a chartered jet. 8:25 A.M. Mr. Stevens’s body arrives at the Benghazi airport in an ambulance. Hours earlier, about 1 a.m., some of the compound’s invaders found him in the main villa and took him to a hospital, where he was pronounced dead, apparently from smoke inhalation. 11:30 A.M. A second flight lands in Tripoli, with the remaining staff and the bodies of the four Americans.

Embassy officials had arranged for the team to be met by Fathi al-Obeidi, a trusted lieutenant of Mr. Bin Hamid of Libya Shield. But when the jet landed around 1 a.m., seemingly every commander in Benghazi was competing for the honor of escorting the Americans, even those who did nothing to stop the attack, including Mr. Bin Hamid himself.

A group from the Preventive Security Brigade, led that night by Mr. Abu Khattala’s old friend Mr. Bargathi, insisted on coming, and held the team up for hours on the tarmac, Mr. Obeidi said. And instead of the low-profile escort the Americans had sought, a parade of nearly a dozen pickup trucks ultimately joined them.

Shortly after the convoy arrived around 5 a.m., the C.I.A. Annex came under a new attack: the mortar rounds the guards had feared. Within 90 seconds, five had landed, the last three hitting the roof of the main building.

Almost all of the Libyan fighters who had insisted on accompanying the Americans from the airport fled immediately.

Two American security guards, Tyrone S. Woods and Glen A. Doherty, were killed by the mortar shells. Mr. Stevens and Sean Smith, an information officer, suffocated in the burning of the main villa in the diplomatic compound.