The Obama administration and key U.S. senators are close to a deal on legislation that aims to end the National Security Agency’s collection of millions of Americans’ phone call logs for counterterrorism purposes.

The USA Freedom Act also would ban the “bulk collection” of Americans’ personal data by other agencies under several statutes as well as secure other surveillance reforms. A compromise bill could be introduced as soon as Thursday, Senate aides said.

“We are very close to finalizing an agreement that incorporates the input of the administration, the privacy community, and the technology industry,” Senate Judiciary Committee Chairman Patrick J. Leahy (D-Vt.) said in an e-mail to The Washington Post. “These stakeholders are coming together behind my legislation, which will give our intelligence officials clear-cut guidelines, and will let the American people know that their privacy is going to be protected.”

Leahy has been working for months to produce a bill that can pass the Senate and, ultimately, the House.

“We feel it’s a good deal,” said a Senate aide who is not authorized to discuss the matter and spoke anonymously. “We feel we’re going to get a lot of support.”

The House in May passed its own version of the USA Freedom Act, which also would ban bulk collection under various statutes. But critics said the bill’s language governing data-gathering was ambiguous, raising concerns that it still would allow the large-scale collection of data from phone companies and other entities.

The key compromise in the Senate was to clarify the definition of a “specific selection term,” to make explicit that it could not, for instance, mean a “term based on a broad geographic region, including a city, state, zip code or area code,” according to a draft copy of the bill obtained by The Post.

It also states that the purpose of the selection term is “to narrowly limit the scope” of items sought by the government.

“We strongly support that language,” said Harley Geiger, senior counsel at the Center for Democracy & Technology, who suggested such changes in congressional testimony. “This is one of the key ways that the bill aims to prevent bulk collection.”

President Obama in January called on Congress to work with the administration to craft legislation that would end NSA bulk collection and achieve other reforms. For instance, he called for a panel of advocates from outside the government to provide an independent voice before the Foreign Intelligence Surveillance Court, which oversees domestic surveillance law in a classified setting.

The Senate bill creates such a panel, specifying that the advocates must act in support of “individual privacy and civil liberties.”

It also requires a declassification review of any surveillance court opinion that includes a significant interpretation of law, including any new meaning of “specific selection term.”

National Security Council spokesman Edward Price said that “we are encouraged by the recent progress in the Senate.”

Leahy would like the bill to go to the floor before the August recess, but Senate aides said it is uncertain whether floor time can be scheduled by then.