Nelson L. Castro, a former Democratic state assemblyman from the Bronx who secretly recorded conversations as a government informant, avoided prison on Thursday, largely because of his cooperation that a judge characterized as by all accounts “extraordinarily valuable, and indeed historic.”

Mr. Castro’s double life began in 2009 and continued over four years as he helped the authorities gather evidence while serving in the Assembly and twice winning re-election. Prosecutors have said his assistance directly or indirectly helped them to prosecute a half-dozen people, including another Democrat from the Bronx, Assemblyman Eric A. Stevenson. After being convicted in a bribery scheme, Mr. Stevenson lost his seat and was sentenced to three years in prison.

Judge Paul A. Engelmayer of Federal District Court sentenced Mr. Castro to two years of probation and 500 hours of community service for lying to the authorities. He said that though Mr. Castro had acted in self-interest, “the good that you did by cooperating over a period of years exposed and put an end to far more official corruption and misconduct than you engaged in.”

The judge said the city and state had “a long and sorry history of official corruption.” Too often, he said, elected officials “talk a good game about fighting corruption and then they do nothing about it.”

“For all your warts, for all your misdeeds, you did something about it,” Judge Engelmayer added.

Mr. Castro, 42, was elected in 2008 and re-elected in 2010 and 2012 from the 86th Assembly District. His unusual journey from assemblyman to informer began after the Bronx district attorney’s office charged him in July 2009 with three counts of perjury related to his testimony in a civil suit related to election law. The indictment was kept secret.

Mr. Castro agreed to cooperate with the district attorney’s office, and in February 2011, the United States attorney’s office in Manhattan began working with Bronx prosecutors in handling his cooperation, according to the government’s sentencing letter.

Mr. Castro originally entered into an agreement by which he would not be prosecuted, but he was charged last year with lying to investigators when he was asked about public interviews he had given after being told not to discuss his role in the case. In August 2013, he pleaded guilty to a charge of making false statements — the offense for which he was sentenced on Thursday. He has also pleaded guilty to the Bronx perjury charges, for which he is to be sentenced in October.

Mr. Castro’s role as an informer was revealed in April 2013 when Preet Bharara, the United States attorney in Manhattan, announced corruption charges against Mr. Stevenson and said Mr. Castro had resigned his seat and was cooperating with the government.

In court on Thursday, Mr. Castro told the judge that he took full responsibility for his actions. “I broke rules and violated laws, talking myself into believing that it was all right,” he said. He added that he had come to “feel very good” about both his cooperation and his Assembly service, and had approached both “with the same level of enthusiasm.”

“I am hopeful that my efforts have helped to improve the political culture in Albany,” Mr. Castro said.

His lawyer, Michael C. Farkas, had said in a sentencing memo that his client made what must have amounted to hundreds of hours of surreptitious recordings. Mr. Farkas also said that during his years of cooperation, Mr. Castro often expressed the belief “that he felt he was making up for the wrongs he had committed.”

“He also confided in me that it drove him to be the best representative of his district that he could be,” Mr. Farkas wrote.

Mr. Bharara’s office described Mr. Castro’s cooperation in a memo to the judge, calling it “significant and useful.” The government said Mr. Castro deserved credit for cooperating while he was a sitting assemblyman, which “provided law enforcement with access in real time to information and developments coming out of Albany.”

In addition, prosecutors said, Mr. Castro’s decision, “which is now widely known, hopefully serves as a deterrent to current and future legislators who may be offered or consider soliciting bribes.”

A version of this article appears in print on , on Page A28 of the New York edition with the headline: Former Assemblyman Turned Informer Avoids Prison. Order Reprints | Today’s Paper | Subscribe