JOHANNESBURG, NOV. 2 -- The South African government and the African National Congress, after three months of wrangling, have reached agreement on a set of guidelines providing for the phased release of hundreds of political prisoners and the return of 40,000 exiles.

The agreement, announced late last night, meets the last major condition posed by the ANC for starting negotiations on a new, non-racial constitution. But the format for those talks and the question of who will take part remain in dispute and are not expected to be resolved before early or mid-1991.

The accord was made known by Justice Minister Kobie Coetsee, who released a document spelling out the guidelines that both sides agreed to follow in dealing with the release of political prisoners and the return of exiles.

The guidelines deal with procedures for defining "political crimes," who may qualify for indemnity, and how those persons should apply for their release. It also makes provisions for the quick return of thousands of exiles who left the country illegally.

The agreement does not provide for a general amnesty but does give the opportunity for anyone, and not just ANC members, to apply for classification as a "political prisoner" and thus be freed or return home without fear of being arrested for past political offenses.

A committee of high-ranking government and ANC officials had been meeting since early spring to discuss ANC demands that those imprisoned for politically related offensives be freed and that 40,000 exiles living in 35 countries be allowed to return home without fear of prosecution. The committee, which repeatedly ran into difficulties, missed an Oct. 1 target date for starting a prisoner release.

Indications that more haggling lies ahead surfaced today when the ANC announced at a press conference that it planned to submit the names of 3,622 persons now serving sentences who it believes should qualify as "political prisoners."

Coetsee last night estimated the number of persons likely to be released at only 250 to 300 but then added that under the widest, most liberal definition of a "political prisoner," the figure might go as high as 600.

The much higher ANC estimate is believed to include 2,500 to 3,000 people convicted of various minor offenses during massive political protests that swept this country starting in 1985 and led to the imposition of a three-year state of emergency.

Among those who may apply for indemnity are 55 persons on death row who the independent Human Rights Commission believes fall in the category of "political prisoners." They include 14 blacks involved in the murder of a policeman in Upington in 1985, and a white ANC member, Bruce McBride, who was sentenced to death in 1987 for masterminding a car bomb explosion in Durban that killed three women.

The most controversial case among death row inmates, however, is likely to be that of Barend Hendrik Strydom, a former policeman, who was sentenced to death for the random killing of seven blacks on a street in Pretoria in 1985. He is regarded by some right-wingers as a hero, and a "political prisoner" like the jailed ANC militants.

Among others who may benefit from the agreement is Sathyndranth Maharaj, an ANC National Executive Committee member, and seven others arrested last July on charges stemming from alleged terrorist activities and now on trial in a Durban court. Coetsee indicated that they would be eligible to apply for release since their alleged crimes had occurred prior to the Oct. 8 deadline for amnesty consideration.

One major proviso of the accord, Coetsee said, is that the speed with which all political prisoners will be freed will be tied to the progress made by the government and ANC in a separate set of talks over the ANC's stated commitment to suspend its armed struggle.

However, ANC spokeswoman Gill Marcus said the agreement made no such linkage and that the government was giving its own interpretation on this point. "They have linked the two {sets of talks}, but we never agreed to that," she said.