The District government agreed yesterday to close the troubled Cedar Knoll juvenile facility and allow an outside lawyer to monitor two other juvenile institutions as part of a sweeping settlement of a lawsuit that charged that young offenders are beaten and subjected to unsafe conditions.

Heading off a potentially embarrassing trial in D.C. Superior Court, city lawyers concluded a week of intense negotiations with the plaintiffs by agreeing to a wide range of reforms, including increased staffing, improved educational and therapeutic programs and curbing disciplinary actions. The settlement does not affect a wide-ranging criminal investigation of the city's youth agency by federal authorities.

The settlement also includes the appointment of a three-member panel that will operate for two years. It will determine how the more than 300 youths who are currently detained by the city will be supervised and find ways of reducing that number through alternative community placements.

"We got precisely what we wanted," said Public Defender Cheryl Long, whose independent city agency brought the class-action suit against the city along with the American Civil Liberties Union's National Prison Project.

"This is not merely a matter of addressing an overcrowding problem," she said. "It really touches upon everything in the way of treating children from the quality of education to medical care to the disciplinary system."

A trial of the suit had been scheduled to begin Monday but was delayed by Judge Ricardo M. Urbina while lawyers for both sides engaged in nearly round-the-clock talks. The city's Youth Services Administration, which operates the three youth facilities, has been subjected to nearly a year of scrutiny by federal investigators who are examining allegations of fraud and mismanagement by officials.

The settlement of the suit, which still must be approved by Urbina, follows earlier lawsuits that have forced the city to relinquish some of its control over the District's mental health and corrections system.

Audrey Rowe, the D.C. social services commissioner, said yesterday she was satisfied with the settlement, while acknowledging that the appointment of an outside monitor is another encroachment on home rule.

Although "you never want to have an outsider coming in looking over your shoulder," Rowe said, the attorney selected by the two sides has a "commitment to D.C."

The two sides announced yesterday that Michael Lewis, 43, the deputy director of the National Institute for Dispute Resolution, has agreed to accept the three-year position. Lewis, a District native, previously was a special master at a state penitentiary in Washington state and a consultant to the California juvenile system.

Under the terms of the settlement, the city has agreed to:Review the status of all children in its care each month and set up a priority listing of youths who should be released from secured facilities. No longer accept youths at Cedar Knoll in Laurel beginning June 1, 1987. The dilapidated facility, which the city attempted to phase out last year, will finally be closed Dec. 1, 1987. Add no new beds to facilities until the panel makes its recommendation in three months. Last January, the city announced plans to expand its maximum security Oak Hill facility, also in Laurel, by 80 beds at a cost of $2 million. Implement a one-child-per-bedroom policy at the the Receiving Home in Northeast Washington beginning Oct. 1. Some youngsters are sleeping in the hallways at the 38-bed facility, which has recorded at least 10 suicide attempts this year. The same requirement will take effect immediately at Oak Hill. Investigate and take action on all complaints made by youths against staff members. In court papers filed with the lawsuit, employes acknowledged that allegations of abuses by staff members frequently were not resolved. Improve diagnostic, educational and medical services. These improvements will include hiring new teachers, expanding vocational and special education programs and providing 24-hour medical care. Students also will be grouped according to academic ability, rather than randomly. Not exceed a student-teacher ratio of 15 to 1 in regular academic classes and 10 to 1 in special education classes. The school at the Receiving Home will be turned over to the D.C. Board of Education. Stop using restraints as punishment or a substitute for proper staffing. Court documents detailed instances in which youths were handcuffed to beds and locked in rooms because of insufficient staff. Now leg irons can be used only when youths are being transported, in cases where there is a risk of escape or to detain youths in maximum security at Cedar Knoll. Comply with national health and housing standards and the D.C. fire codes. The suit complained of vermin-infested housing with erratic heat and ventilation and hazardous fire conditions.

Mayor Marion Barry said yesterday that the agreement "will provide a stable foundation for our planning and operation of the Youth Services Administration over the coming years."

In a statement, Barry emphasized that the city had been working to improve the beleaguered youth agency in recent months.

Barry forced the agency's director, Patricia Quann, to resign May 30 after reports of more than $6 million of overtime payments in the last three years to agency employes and extensive contract fraud.

A federal grand jury here is investigating the overtime payments. The FBI and General Accounting Office also have been auditing the agency for 10 months.

Barry said that the city has hired additional employes and established new policies to reduce overtime and to improve drug surveillance. As he has done before, Barry blamed many of the agency's problems on an unexpected increase last year in youths placed in District custody.

The appointment of a monitor and a panel is expected to help the city better manage its responsibilities for juveniles. Lewis, who said he was first approached about the monitor's position over the July 4 weekend, will begin his duties Aug. 1. Lewis will be responsible for ensuring compliance with the settlement and will report to the parties and to the court.

The creation of a monitor's post was the major obstacle to reaching an agreement on the lawsuit because city officials viewed the initial proposal as usurping District authority, according to arguments filed by city attorneys.

The D.C. Public Defenders Service and the ACLU lawyers insisted that an outsider who could report to the court was essential to enforcement. As a compromise, the two sides agreed to allow the monitor to consult with former Superior Court judge John D. Fauntleroy, in order to give the city a greater voice. Fauntleroy was recently appointed by the mayor to monitor court orders affecting the operation of Lorton Reformatory and the D.C. Jail.

Lewis called his new part-time assignment a "tough job," but added that the fact that both sides were able to reach an agreement without going to trial "should make the job much easier."

The agreement also calls for several outside consultants to be hired, including Martin Gerry, a special education specialist. It was the city's failure to provide federally required special education at the juvenile facilities that prompted a congressional hearing last fall that led to the federal investigations.

Rowe complained yesterday about the costs of hiring consultants, which she estimated at $175,000. She valued the other expenses in hiring additional staff at $2 million.

The effect of the settlement, according to ACLU lawyer Stephen Ney, resulted in the "same impact as if the judge had ordered the changes after a trial.