Details of the C.I.A.’s previously unreported contribution to the ransom demanded by Al Qaeda were drawn from the letters and from interviews with Afghan and Western officials speaking on the condition of anonymity because of the sensitivity of the issue. The C.I.A. declined to comment.

The diplomat freed in exchange for the cash, Abdul Khaliq Farahi, was serving as the Afghan consul general in Peshawar, Pakistan, when he was kidnapped in September 2008 as he drove to work. He had been weeks away from taking up his new job as Afghanistan’s ambassador to Pakistan.

Afghan and Pakistani insurgents had grabbed Mr. Farahi, but within days they turned him over to Qaeda members. He was held for more than two years.

The Afghan government had no direct contact with Al Qaeda, stymieing negotiations until the Haqqani network, an Afghan insurgent faction with close ties to Al Qaeda, stepped in to mediate.

Qaeda leaders wanted some captive militants released, and from the letters it appeared that they calibrated their offer, asking only for men held by Afghan authorities, not those imprisoned by the Americans, who would refuse the demand as a matter of policy. But the Afghans refused to release any prisoners, “so we decided to proceed with a financial exchange,” Mr. Rahman wrote in the June 2010 letter. “The amount we agreed on in the deal was $5 million.”

The first $2 million was delivered shortly before that letter was written. In it, Mr. Rahman asked Bin Laden if he needed money, and said “we have also designated a fair amount to strengthen the organization militarily by stockpiling good weapons.” (The Qaeda leaders named in the letters were identified by aliases. Bin Laden, for instance, signed his letters Zamray; Mr. Rahman, who was killed in a drone strike in Pakistan in August 2011, went by the alias Mahmud.)