The former district attorney who declined a decade ago to bring sex-crime charges against Bill Cosby testified Tuesday that he believes his decision shields the comedian from ever being prosecuted in the case.

Former Montgomery County District Attorney Bruce Castor took the stand at a pretrial hearing in a bid by Cosby's lawyers to get the case against the TV star thrown out because of what they say is a non-prosecution agreement with Castor.

The current district attorney has said there is no record of any such agreement.

Castor admitted the only place the matter was put in writing was in the 2005 press release announcing his decision not to prosecute.

Under questioning, he acknowledged that he didn't draw up a formal immunity agreement filed with a judge because, he said, Cosby was worried that would make him look bad. Also, Castor said, "It was unnecessary because I concluded there was no way the case would get any better."

Bill Cosby

Bill Cosby, seen here arriving at court on Dec. 30, 2015, in Elkins Park, Pa., is charged with felony sexual assault. His lawyers are trying to get the charge thrown out. (Matt Rourke/Associated Press)

Judge Steven T. O'Neill said he hopes to rule on Wednesday whether to throw out the case.

Deal 'for all time'

Cosby, 78, was arrested and charged in December with drugging and violating former Temple University athletic department employee Andrea Constand at his suburban Philadelphia mansion in 2004. He could get up to 10 years in prison if convicted.

Castor said Tuesday that he believed Constand's story but that proving it would have been problematic because of serious flaws in the case, and so he declined to bring charges.

He said that he made the decision as a representative of the state and that it was intended to last in perpetuity.

"For all time, yes," Castor said when pressed.

Bill Cosby

Members of Bill Cosby's law team, from left, Monique Pressley, Christopher Tayback and Brian McMonagle, arrive at the Montgomery County Courthouse on Tuesday before a pre-trail hearing in Norristown, Pa. (Clem Murray/Philadelphia Inquirer/Associated Press)

And he suggested that Cosby and his lawyer at the time had the same understanding, because Cosby later agreed to testify without invoking his Fifth Amendment right against self-incrimination in a lawsuit brought against him by Constand.

"Cosby would've had to have been nuts to say those things if there was any chance he could've been prosecuted," Castor said, referring to the damaging testimony unsealed last summer.

Castor said he hoped — correctly, it turned out — that the decision not to prosecute would prod Cosby to testify in the lawsuit and help Constand win damages. She eventually settled for an undisclosed amount.

"I thought making Mr. Cosby pay money was the best I was going to be able to set the stage for," the former DA said. He added: "I was hopeful that I had made Ms. Constand a millionaire."

He said he and Cosby's then-attorney, Walter Phillips, did not have an actual agreement that Cosby would testify in exchange for not being prosecuted. Phillips has since died.

Need it in writing

Kevin Steele, the newly elected DA who is pursuing the case, has said Cosby would need an immunity agreement in writing to get the case thrown out. He has said he has no evidence one exists.

Prosecutors on Tuesday pressed Castor on numerous, seemingly inconsistent statements he made over the years on whether Cosby could still face charges. Castor sparred with prosecutors, parsing the language in his press release and in various emails sent to his successor.

He said he was referring in some passages to bringing charges against Cosby in connection with other women and other crimes but not in connection with the Constand encounter.

While Castor was called as a witness by Cosby's side, the former DA said he is rooting for the prosecution.

"I'm not on your team here," Castor told Cosby lawyer Brian McMonagle. "I want them to win."

Cosby watched the back and forth between Castor and the lawyers from the defence table, swiveling his head slightly as if at a tennis match. He stifled a cough with a fist and showed no reaction even as the courtroom erupted periodically in laughter over a quip from the talkative Castor or one of the lawyers.

Cosby admitted affairs

In a barrage of allegations that have destroyed Cosby's image as America's Dad, dozens of women have accused the former TV star of drugging and sexually assaulting them since the 1960s. But this is the only case in which he has been charged.

The unsealing of the testimony from Constand's lawsuit prompted Castor's successors to reopen the case and ultimately charge Cosby.

Cosby admitted in the deposition that he had affairs with young models and actresses, that he obtained quaaludes to give to women he wanted to have sex with and that he gave Constand three pills at his home. He said he reached into her pants but insisted it was consensual.

Castor defended his decision not to bring charges, testifying that he saw Constand's year-long delay in reporting the allegations, inconsistencies in her statements and her contact with a lawyer before going to police as problematic.

Castor said Constand's delay thwarted his ability to test her hair or fingernails for evidence she was drugged.

Separate lawsuit dropped

Anne Poulin, a law professor at Villanova University, said the defence has a high bar to meet to get the case thrown out early on. But "if they can win without this ever going to trial, then they've done their client a big service."

In related news, a Los Angeles judge ordered Cosby to attend another deposition in a lawsuit filed by a woman who says the comic forced her to perform a sex act on him at the Playboy Mansion around 1974, when she was 15.

Also on Tuesday, model Chloe Goins dropped a lawsuit accusing Cosby of drugging and sexually assaulting her at the Playboy Mansion in 2008. Goins gave no explanation.