The retired NYPD detective who headed a multimillion-dollar prostitution and gambling ring with a cohort of other cops was sentenced Tuesday to four to 12 years in prison.

Ludwig Paz’s punishment comes as part of a plea deal with prosecutors in which he copped to two counts of attempted enterprise corruption and one count of promoting prostitution in the third degree.

He also agreed to forfeit $20,840 in profits.

“I want to apologize to the court, the DA’s Office and the NYPD for my wrongful actions. I deeply regret them,” Paz, who appeared in Queens Supreme Court wearing an orange jumpsuit, told Judge Ronald Hollie. “I want to apologize and say sorry and ask for forgiveness to my family for my actions and all the pain and suffering that I’ve caused them.”

Defense lawyer Frank Kelly said outside court that Paz’s plea deal was hammered out to “save his family” and to shave off time his wife and co-defendant, Arelis Peralta, received.

He originally faced up to 25 years in prison for enterprise corruption, the top charge.

“He took responsibility. He got more time than we originally had worked out in order to save his wife and his two daughters,” said Kelly. “He did it for his family. He did the right thing by his family.”

Paz, 52, was assigned to the NYPD vice squad when he ran brothels in Brooklyn, Queens and Nassau County, with the help of seven active-duty police officers. His two daughters, Jarelis Guzman and Arisbel Guzman, were also indicted in the case.

Prosecutors said Paz paid Brooklyn South Vice Detective Rene Samaniego as much as $500 a week to serve as his “primary informant” tipping him off about impending raids, including detailed descriptions of undercover officers.

In order to try to avoid getting caught, the mastermind set up new protocols for new brothel customers to weed out the undercovers — knowing full well that cops are barred from exposing themselves in front of prostitutes.

Under the updated rules, new clients were required to undress and allow themselves to be fondled before getting past security, prosecutors said.

Paz’s brothels charged $40 for 15 minutes of sex and up to $160 for a full hour — and raked in more than $2 million between August 2016 and September 2017.

Peralta, the disgraced detective’s wife, also pleaded guilty to two counts of enterprise corruption and sentenced to 364 days in jail, which she has already served.

She was in court Tuesday with the couple’s 12-year-old son and sobbed during the proceeding.

“I’m just devastated,” she said as she left court.

Paz has been locked up since his arrest in September 2018.

His crew also ran illegal lotteries at delis and beauty salons in Queens and Brooklyn.

The Guzman sisters previously pleaded guilty to first-degree attempted promoting prostitution in exchange for conditional discharges, while Samaniego is awaiting sentencing.

The charges were the result of a three-year investigation that stemmed from a 2015 tip that some police officers, including those active and retired, were operating the brothels.