After a year of contentious negotiations, the United States and Greece agreed yesterday on a new defense pact that will allow U.S. air and naval bases to remain in Greece for the next eight years.

The agreement, initialed in Athens, marks what is likely to be the last time that U.S.-Greek base-rights negotiations create a prolonged controversy. The clash has been between Greece's commitments to North Atlantic Treaty Organization defenses and its internal nationalistic pressures to oust the four U.S. bases and 20 smaller units from Greek soil.

With the Cold War rapidly drawing to an end and the Soviet-led Warsaw Pact posing a greatly diminished military threat to the West, the U.S. need for a strong air and naval presence along NATO's southern flank in Greece is considered likely to disappear by the time the new pact expires in eight years.

In fact, U.S. officials said yesterday, the latest agreement will mean a substantial reduction in the American presence in any case, because it incorporates plans, announced by Defense Secretary Richard B. Cheney in January, to close two of the bases as part of a worldwide cost-cutting drive.

The installations to be closed are the Hellenikon Air Force Base outside Athens and a naval communications station at Nea Makri, northeast of the capital. Both have aroused considerable anti-American sentiment over the years, including occasional terrorist attacks on their personnel, because of their high visibility in the densely populated Athens area.

The principal remaining installations are a large naval base at Souda Bay and an Air Force electronic surveillance station at Gournes, both on the southern island of Crete. State Department spokesman Margaret Tutwiler said some of the operations at Hellenikon will be transferred to Souda, but she added, "The overall U.S. presence in Greece will be reduced substantially with the closure of Hellenikon."

Prime Minister Constantine Mitsotakis, who will visit here next week, had pledged to reach a bases agreement following the election victory in April of his conservative New Democracy Party. The government of former Socialist prime minister Andreas Papandreou, ousted in national elections a year ago, was hampered in previous negotiations by its desire for nationalist support, and the talks had languished until their revival under Mitsotakis.

Tutwiler said the agreement will be signed next month after the English and Greek texts have been reconciled. In the meantime, the text remains secret, and it is not known what provision, if any, is made for U.S. aid to Greece. The United States is now providing Greece almost $350 million in military assistance, and President Bush's request to Congress for the 1991 fiscal year, beginning Oct. 1, calls for $345.5 million in military aid.