A man who plotted to blow up New York City's subway system may walk out of prison within days after spending almost a decade helping federal investigators with terror cases.

On Thursday afternoon, a federal judge in Brooklyn sentenced Najibullah Zazi, 33, to 10 years in prison, which is close to the amount of time Zazi has been in federal custody.

"I am sorry for the harm that I caused," Zazi told the court. "I am asking for forgiveness."

"I tried my best to correct a horrible mistake. I am not the same person."

Click here to read the NBC New York version of this story

Judge Raymond Dearie told Zazi that the "government has no reservation" as to his mindset, but said, "You'll understand my healthy skepticism."

Before pronouncing the sentence, Dearie said Zazi was being given an "unthinkable" second chance and that he had earned it.

"If you wish to appeal this ruling, you should have your head examined," the judge said.

Zazi pleaded guilty in 2010 to an Al Qaeda-inspired plot to attack the subway. Federal sentencing guidelines indicated he should serve life in prison, but his sentencing was delayed again and again as he cooperated with authorities.

According to a sentencing memo from prosecutors filed this February, he provided "extraordinary" assistance, including evidence that led to the conviction of his two closest friends and testified at trials in the United States. Prosecutors said he had met with the government more than 100 times while in federal custody, and had "directly contributed to the prosecution of numerous individuals."

Najibullah Zazi is escorted by U.S. Marshals after a helicopter landing at a New York Police Department facility in Brooklyn, New York, on Sept. 25, 2009. NYPD via Reuters file

Many pages of the memo that detail his cooperation are redacted.

"Once Zazi decided to cooperate in February 2010, he gave the government his full, complete cooperation," the memo said.

The memo does not make a recommendation to the judge for a specific sentence, "under the terms of the cooperation agreement in this matter."

Zazi was born in Afghanistan and raised in Pakistan until he was a teenager. In 1999, he and his family moved to the borough of Queens in New York City.

In 2008, according to the sentencing memorandum, he started watching Al Qaeda videos on the internet "and came to view the American and NATO forces in Afghanistan as occupying forces."

That August, he and two co-conspirators traveled to Pakistan and were recruited by Al Qaeda. He was trained at a camp in the Waziristan region of Pakistan in how to make explosive devices, and agreed to carry out a suicide bombing attack. Among the targets suggested by Al Qaeda leaders was the New York City subway system.

Zazi came back to the U.S. in early 2009 and began living with relatives in the Denver area, where he drove an airport shuttle.

FBI agents began eavesdropping on Zazi's phone conversation. By the summer of 2009, he was buying the needed ingredients for TATP, an explosive used in several high-profile jihadi attacks in the West.

On Sept. 9, 2009, Zazi began driving from Colorado to Queens. He was tailed by FBI agents. After being stopped and searched by police at the George Washington Bridge, he arrived in New York City on the afternoon of Sept. 10 with plans to finish building his bombs.

Zazi and his two co-conspirators, Adis Medunjanin and Zarein Ahmedzay, rode the subway and developed plans to blow up backpack bombs near the busy Grand Central and Times Square stations in Manhattan. On Sept. 11, his rental car was towed for a parking violation, and was searched. A laptop in the car held notes on making bombs in his handwriting.

Wary of discovery, Zazi threw away his bombmaking materials and flew back to Denver. He was arrested in Colorado on Sept. 19, and ultimately charged in the Eastern District of New York with conspiracy to use weapons of mass destruction.

Zazi's two co-conspirators are both in federal prison. Ahmedzay pleaded guilty to terrorism charges and received a sentence of 10 years. Medunjanin was found guilty on terrorism charges and sentenced to life imprisonment.

Zazi's father received a four and a half year sentence for destroying evidence and lying to investigators. Imam Ahmad Afzali was charged with tipping Zazi off to government surveillance but negotiated a plea deal in which he left the U.S. permanently instead of serving jail time.

In February 2010, Zazi pleaded guilty to three counts — material support for terrorism, conspiracy to use weapons of mass destruction, and conspiracy to commit murder in a foreign country — and agreed to cooperate with authorities. The offenses carried a possible sentence of life without parole, but his ongoing cooperation, which began even before his guilty plea, meant sentencing was delayed repeatedly starting in 2011.