The Supreme Court ruled unanimously yesterday that public employe unions must set up strict safeguards to ensure that no money collected from nonmembers is used to support political or ideological activities.

The decision, written by Justice John Paul Stevens, struck down a dues-collection procedure used by a Chicago teachers union under an agency shop agreement. Under such agreements, workers who do not belong to a union are required to pay a fee to compensate the union for collective bargaining and other activities that benefit all employes.

Stevens said unions, to prevent "free riders" from receiving benefits without joining, may demand fees from nonmembers. But the Chicago procedure was unconstitutional because it "failed to minimize the risk that nonunion employes' contributions might be used for impermissible purposes," such as ideological causes or political lobbying.

The Chicago union, Stevens said, did not include "an adequate explanation of the basis for the fee, a reasonably prompt opportunity to challenge the amount of the fee before an impartial decisionmaker and an escrow for the amounts reasonably in dispute while such challenges are pending."

As a result, Stevens said, the procedures violated the First Amendment rights of nonunion members to freedom "to associate for the advancement of ideas, or to refrain from doing so, as he sees fit."

The case, Chicago Teachers Union v. Hudson, began after several teachers challenged the union's determination that 95 percent of all dues were spent on collective bargaining activities and 5 percent on political or ideological activities.

A District Court judge upheld the arrangement, but the 7th U.S. Circuit Court of Appeals struck it down unanimously in 1984.

Edwin Vieira Jr., an attorney for the teachers challenging the fees, said yesterday that the ruling would affect millions of public school teachers and other public employes who belong to unions with agency shop provisions.

Several states have taken steps to handle complaints such as those brought by the Chicago teachers, Vieira said. An attorney for the union could not be reached for comment.

In another ruling yesterday, the justices split, 5 to 4, in deciding that the Constitution does not restrict prison officials' power to use lethal force to quell disturbances.

Justice Sandra Day O'Connor, writing for the majority in Whitley v. Albers, said Oregon State Penitentiary guards did not violate the constitutional rights of an inmate shot during a disturbance in which a guard had been taken hostage.

The wounded inmate, Gerald Albers, argued that he was shot in cold blood after the disturbance had subsided. He argued that the shooting violated his Eighth Amendment rights to be free from cruel and unusual punishment.

O'Connor said the facts of the case showed no constitutional violation. "The infliction of pain in the course of a prison security measure," O'Connor said, "does not amount to cruel and unusual punishment simply because it may appear in retrospect that the degree of force authorized or applied for security purposes was unreasonable."

O'Connor, following recent decisions giving prison officials broad powers, said prison administrators "should be accorded wide-ranging deference in the adoption and execution of policies and practices that in their judgment are needed to preserve internal order and discipline and to maintain institutional security."

That deference, she said, "does not insulate . . . actions taken in bad faith or for no legitimate purpose, but it requires that neither judge nor jury freely substitute their judgment for that of officials who have made a considered choice." The decision reversed an appeals court ruling ordering that Albers' claims be presented to a jury.

Justice Thurgood Marshall, joined in dissent by Justices William J. Brennan Jr., Harry A. Blackmun and Stevens, said that the majority "just does not believe his Albers' story" and that it would be appropriate for a jury to decide whether the shooting violated Albers' rights.