For years, Mr. Brownlee and his small team had been building a case that the maker of the painkiller OxyContin had misled the public when it claimed the drug was less prone to abuse than competing narcotics. The drug was believed to be a factor in hundreds of deaths involving its abuse.

Mr. Giuliani, celebrated for his stewardship of New York City after 9/11, soon told the prosecutors they were wrong.

In 2002, the drug maker, Purdue Pharma of Stamford, Conn., hired Mr. Giuliani and his consulting firm, Giuliani Partners, to help stem the controversy about OxyContin. Among Mr. Giuliani’s missions was the job of convincing public officials that they could trust Purdue because they could trust him.

So it was no small success when, after the call, Mr. Brownlee did what many people might have done when confronted with such celebrity: He went out and bought a copy of Mr. Giuliani’s book, “Leadership.”

“I wanted to be prepared for my meetings with him,” Mr. Brownlee said in a recent interview.

Increased scrutinyOver the past few weeks, Mr. Giuliani’s consulting business has received increasing scrutiny, at times forcing him to defend his business as he campaigns for the Republican presidential nomination.

But his work for Purdue, the company’s first and longest-running client, provides a window into how he used his standing as an eminent lawyer, a Republican insider and a national celebrity to aid a controversial client and build a business fortune.

A former top federal prosecutor, Mr. Giuliani participated in two meetings between Purdue officials and the head of the Drug Enforcement Administration, the agency investigating the company. Giuliani Partners took on the job of monitoring security improvements at company facilities making OxyContin, an issue of concern to the D.E.A.

As a celebrity, Mr. Giuliani helped the company win several public relations battles, playing a role in an effort by Purdue to persuade an influential Pennsylvania congressman, Curt Weldon, not to blame it for OxyContin abuse.

Despite these efforts, Purdue suffered a crushing defeat in May at the hands of Mr. Brownlee when the company and three top executives pleaded guilty to criminal charges.

Mr. Giuliani, who declined to discuss his work for Purdue for this article, has refused to talk in detail about his firm’s clients. He has said that he is no longer involved in the day-to-day management of the firm, which still represents Purdue.

Giuliani Partners would not say how much Purdue had paid it, but one consultant to the drug maker estimated that Mr. Giuliani’s firm had, in some years, earned several million dollars from the account.

In the OxyContin case, Mr. Giuliani’s supporters suggest that as a cancer survivor himself, he was driven by a noble goal: to keep the company’s proven pain reliever available to the widest circle of sufferers.

“I understand the pain and distress that accompanies illness,” Mr. Giuliani said at the time. “I know that proper medications are necessary for people to treat their sickness and improve their quality of life.”

To drive OxyContin’s sales, Purdue, beginning in 1996, set in motion what D.E.A. officials described as perhaps the most aggressive promotional campaign for a high-powered narcotic ever undertaken. It promoted the drug not only to pain specialists, but to family doctors with little experience in treating serious pain or recognizing drug abuse.

As a result of the expanded access, critics charged, OxyContin wound up in the high schools and street corners of rural America where curious teenagers crushed the pill, defeating the time-release formula, and ended up addicts, or in some cases, dead.

Dennis Lee, the Virginia state prosecutor for Tazewell County, an area hard hit by OxyContin abuse, said he was stunned several years ago to learn that Mr. Giuliani was working for Purdue. He had a favorable impression of Mr. Giuliani, he said, and a poor opinion of the company, which he said had played down and dissembled about its drug’s problem.

“I was shocked,” Mr. Lee said, “that he would basically become a mouthpiece for Purdue.”

Denials and lobbyingGiuliani Partners served clients with a range of needs. The firm helped large accounting firms fight computer hackers and promoted Nextel’s efforts to expand its access to public airwaves. But some of the 55-person firm’s clients, like Purdue Pharma, were facing more difficult legal and public relations problems.

There were, for instance, the backers of a planned natural gas terminal in Long Island Sound who were facing stiff environmental opposition. Another client was a former cocaine smuggler hoping to win federal contracts for a computer system to track down terrorists.

On the business of these clients andothers, Giuliani Partners carved out a lucrative niche in corporate consulting, crisis management and security.

In the process, Mr. Giuliani, a Brooklyn native whose legal career had largely been spent in government, became a corporate trouble-shooter with homes in the Hamptons and on the Upper East Side. According to financial disclosure forms filed in May, his net worth was more than $30 million.

The crisis that brought Purdue to Mr. Giuliani in 2002 involved OxyContin, a time-released form of the narcotic oxycodone, which had turned into a blockbuster product with annual sales of more than $1 billion.

But along the way, the pain medication had also become a popular drug for abuse. Among the company’s critics were officials at the Drug Enforcement Administration who said OxyContin had been a factor in hundreds of overdose deaths. Some D.E.A. officials and others also charged that Purdue had hyped the drug’s resistance to abuse and then failed to act swiftly when its misuse became apparent.

Purdue Pharma, which is owned by the Sacklers, a New York-area family who are known as museum benefactors, denied it had done anything wrong. But facing a growing number of investigations and lawsuits, it spent millions on public relations experts, lobbyists and top-tier law firms.

One piece, however, was missing: a highly credible and well-connected political figure to serve as its point man. Purdue Pharma executives saw Mr. Giuliani as that person, said a former company spokesman.

“He was just on the cover of Time Magazine, Man of the Year,” that former official, Robin Hogen, said. “Everyone was talking about his extraordinary leadership in 9/11.”

Giuliani Partners became involved in every aspect of the company’s problems, from the ballooning investigation by Mr. Brownlee to repairing its battered image. Mr. Giuliani personally took on some tasks, but a half-dozen members of his firm, including Bernard B. Kerik, the former New York City police commissioner, were also involved.

Mr. Giuliani’s most important liaison to the company was Daniel S. Connolly, who had been a top lawyer in his administration. He spent so much time at Purdue that he was issued a security pass.

“His judgment was always sought on almost any topic,” said Mr. Hogen, who now works for a public relations agency in San Francisco.

Mr. Connolly regularly attended Monday morning crisis management sessions to develop programs that would shift the public spotlight away from OxyContin. The issue, the company said, was not its conduct but the larger question of prescription drug abuse.

To help draw attention to that issue, Mr. Giuliani became the public face of a program called Rx Action Alliance, a consortium of drug makers, physicians and law enforcement authorities working to curtail such abuse.

“He was America’s mayor,” Mr. Hogen said of Mr. Giuliani’s role as a catalyst for the company’s efforts. “People were drawn to him.”

One person attracted by Mr. Giuliani’s star power was Mr. Weldon, who was upset because young people in his Pennsylvania district were abusing OxyContin. Mr. Weldon, who lost his seat in 2006, said in a recent interview that he had told the company he planned to publicly speak out against it.

“This is really kind of outrageous,” Mr. Weldon recalled telling a Purdue representative. “You have got to do something more than say you are concerned about it.”

At Mr. Weldon’s urging, the company agreed to finance a program aimed at curbing prescription drug abuse. It also sent Mr. Giuliani to an inaugural press conference for the program, held at a high school in Mr. Weldon’s district. With Mr. Giuliani at his side, Mr. Weldon opted not to criticize the company.

“I am proud to be in Pennsylvania today standing with Curt Weldon — a true leader,” Mr. Giuliani said at the event. “I applaud the efforts of Congressman Weldon and of Purdue Pharma in taking this battle in the right direction.”

Credit for damage controlAsa Hutchinson, the director of the Drug Enforcement Administration in 2002, hardly needed an introduction to Mr. Giuliani. So it was perhaps not surprising that Purdue chose Mr. Giuliani as the person to meet with Mr. Hutchinson at a time when the drug maker was under intense scrutiny by the D.E.A.

“You need to have somebody who has clout to get in the door to legitimately make your presentation,” said Jay P. McCloskey, a former United States attorney in Maine who until recently worked for Purdue as a consultant.

By 2002, Mr. Giuliani was already helping to raise money for a D.E.A. museum, and his firm was part of a $1 million Justice Department consulting contract to advise it on reorganizing its major drug investigations.

The D.E.A. was not only critical of how OxyContin had been marketed, its inspectors had found widespread security and record-keeping problems at the company’s manufacturing plants.

Several top D.E.A. staffers were recommending that the agency impose severe sanctions against the drug maker, including possible restrictions on how much OxyContin it could make.

At two meetings, the first at Giuliani Partners in early 2002, Mr. Giuliani and Purdue’s executives argued that they were already taking steps to eliminate any problems.

Mr. Kerik had been sent to Purdue’s manufacturing plants to revamp internal security, they assured Mr. Hutchinson. The federal investigators, they argued, should back down and give them a chance to prove they could handle the problem on their own.

After the meetings, Mr. Hutchinson, who generally did not get involved in individual investigations, asked D.E.A. officials several times to brief him on the inquiry, Laura Nagel, the official in charge of it, has said in previous interviews. She declined to comment for this article.

D.E.A. officials say Mr. Giuliani ultimately did not affect the inquiry’s course. But Purdue Pharma did succeed in favorably resolving the matter. In 2004, it paid a $2 million fine to settle the D.E.A. record-keeping charges without admitting any wrongdoing. The sum was far smaller than the amount first recommended by Ms. Nagel, which one former D.E.A. official said was $20 million.

By the time of the 2004 settlement, it appeared that Purdue, with Mr. Giuliani’s help, had averted any significant damage. As the tide was turning, the drug maker’s top lawyer, Howard R. Udell, gave credit to Mr. Giuliani.

“We believe that government officials are more comfortable knowing that Giuliani is advising Purdue Pharma,” Mr. Udell said in a promotional brochure put out by Giuliani Partners. “It is clear to us, and we hope it is clear to the government, that Giuliani would not take an assignment with a company that he felt was acting in an improper way.”

Parents not persuadedThe limits of stature, though, were evident in Mr. Giuliani’s dealings with Mr. Brownlee, the federal prosecutor from Virginia, whose case against Purdue had been viewed by the company more as a nuisance than a threat.

It is easy to see how lawyers for Purdue might have underestimated the prosecutor. He ran a small office with 24 lawyers to cover 52 far-flung counties. But two of those lawyers, working out of a satellite office in the tiny town of Abingdon, Va., near the Tennessee border, had been investigating Purdue since 2002.

They had issued some 600 separate subpoenas and collected millions of company documents. The case stretched the office’s resources so thin that state prosecutors had to be deputized to handle other federal cases.

By comparison, Purdue’s defense team comprised all-stars, including Mr. Giuliani, Mr. Connolly and Mary Jo White, a former United States attorney in New York.

Mr. Giuliani had been advising Purdue about how to respond to Mr. Brownlee’s inquiry since its start in 2002, including reviewing documents the company had released in response to his subpoenas. And he shared the defense team’s view that Mr. Brownlee did not have any evidence to link the company to crimes, several of those lawyers said.

Early last year, however, Mr. Brownlee told Purdue that he was prepared to indict it and three top executives, including Mr. Udell, the lawyer. The company then handed Mr. Giuliani his most crucial assignment, to talk Mr. Brownlee down.

His selection was not by chance, company representatives said. They figured Mr. Brownlee, a younger federal prosecutor, would look up to Mr. Giuliani, who became a legend as a United States attorney in New York.

Between June and October 2006, Mr. Giuliani met or spoke with the prosecutor on six occasions. During those conversations, Mr. Giuliani was cordial but pointed in arguing against what he felt were flaws in the case.

Mr. Brownlee would not change course, though, even when the Purdue legal team appealed, unsuccessfully, at the 11th hour to his superiors at the Justice Department in Washington.

In October 2006, Mr. Brownlee told Mr. Giuliani and Purdue that he expected to ask for a grand jury indictment by the end of the month. Plea discussions ensued and Mr. Brownlee ultimately agreed that the three executives would not have to do jail time.

By this time, Mr. Giuliani was actively planning his presidential bid, as well as tending to other clients. On the day the legal team completed the plea deals with Mr. Brownlee, Mr. Giuliani was in Germany, giving a talk to business leaders.

He had a conference call with prosecutors for about a minute, but there really was not much left to discuss, except the weather.

“He said that it was raining,” Mr. Brownlee recalled.

In May, Purdue and its executives, after spending tens of millions of dollars to repair the company’s image, agreed to plea deals to avoid a trial. Together, they paid $634.5 million in fines and payments.

After years of denial and a high-profile public relations campaign, the company was forced to admit that it had misled doctors and patients. But to the parents of young people who had died getting high on OxyContin, the absence of jail time was evidence of Mr. Giuliani’s influence.

They voiced that view inside and outside the packed courtroom in Abingdon where the men were sentenced in July.

Mr. Giuliani was 360 miles away at the time, campaigning in Myrtle Beach, S.C., where he met with local firefighters and talked about 9/11. But his role in the case had been so substantial and sustained, the presiding judge felt compelled to address the parents’ concerns.

“It has been implied that because Mr. Giuliani is a prominent national politician, Purdue may have received a favorable deal from the government solely because of politics,” said the judge, James P. Jones of United States District Court. “I completely reject this claim.”

Even today, some of those parents are not persuaded. Ed Bisch, whose son died of an OxyContin overdose, said that he believed that Purdue got a free pass for years thanks to Mr. Giuliani.

“It was all because of Giuliani,” said Mr. Bisch. “And he got to take the money.”