WASHINGTON — After armed men stormed the American diplomatic mission and assaulted a C.I.A. base in Benghazi, Libya, in 2012, the United States turned to a Libyan to help gather information about Ahmed Abu Khattala, a prime suspect in the deadly attacks.

The soft-spoken informant befriended Mr. Khattala and began visiting his house in Benghazi. He gave Mr. Khattala money, a vehicle and computers, pleasing Mr. Khattala, who eventually began confiding in the man.

The 40-year-old informant testified matter-of-factly for hours in federal court in Washington on Monday and Tuesday under the pseudonym of Ali Majrisi. He implicated Mr. Khattala in the attacks that killed four Americans, including Ambassador J. Christopher Stevens, and spoke of his initial reservations about working with the United States, which eventually paid him a $7 million reward for his work.

“There was lot of dangers in that thing. It was really a hard decision to make,” he said, explaining that he wanted to help his home city of Benghazi.