The Carter administration and the government of Somalia are moving toward an agreement which would allow U.S. military access to Somali ports and airfields as a way to help defend the nearby oil-rich Persian Gulf in an emergency, according to American officials.

Negotiations over access to the strategically located Somali facilities had been deadlocked for many months, mostly over Somali demands for economic and military assistance which at one point would have cost some $2 billion over th e next 10 years, an amount the United States said was out of the question.

A Somali delegatton will arrive in Washington next week to meet with State and Defense Department aides to work out remaining details in a deal which officials here say is likely to involve about $40 million in U.S. military assistance over the next two years.

Though U.S. officials remain cautious about these forthcoming talks with the sometimes mercurial government of President Mohammed Siad Barre, one senior defense official, in an assessment repeated elsewhere in government, said "all the signs look positive."

In the aftermath of the hostage-taking in Iran last November and the Soviet invasion of Afghanistan in December, the administration began seeking access to facilities in three countries bordering on the Indian Ocean -- Oman, Kenya and Somalia. Such facilities could serve as potential staging bases if the United States had to send in large military forces to protect the Gulf and its oilfields.

Agreements have been worked out with Oman and Kenya. But the Somali deal stalled, in part over how much the United States would pay.

There was also, however, considerable concern in some quarters here that an agreement with Somalia could drag the United States into the continuing battle, in the disputed Ogaden desert region, against neighboring Ethiopia, which is backed and armed by the Soviet Union and Cuba.

These concerns have been underlined in recent days by Ethiopian charges that Somalia recently has undertaken a new military offensive with regular army units across the internationally recognized boundary in the Ogaden. The Ethiopian Foreign Ministry, in a statement to diplomats in Addis Ababa and in a press conference here yesterday by embassy charge d'affaires Tesfaye Demeke, said that there is a limit to Ethiopia's restraint, and suggested that its military forces may cross into Somalia if the Ogaden struggle continues.

Ethiopia has also dispatched "urgent" statements to the United Nations, Organization of African Unity and the Cuban government, in its capacity as head of the nonaligned movement, charging that Somalia "aggression" with support of "international imperialism" is a threat to peace.

U.S. officials said there have been reports of multi-battalion action by Somali regulars in the Orgaden in recent weeks, but that now it is reported to be on the wane.

In an effort to minimize Washington's potential involvement in the continuing somali-Ethiopian strugle, the U.S. military assistance to Somalia will be in the form of credit in purchase equipment and " defensive" arms such as antiaircraft missiles, according to official sources.

Though Somalia is some 1,300 miles from the Gulf (closer than Kenya but farther than Oman), the facilities there are the most impressive and important of the three. This is because of the 15,000-foot concrete runway plus aircraft hangars and port facilities at Berbera that the Soviet Union built in Somalia before it was thrown out in 1977 by Barre.

Furthermore, Somalia is close to Saudi Arabia, the most vital oil resource in the region, and so a U.S. presence is viewed as especially important.

Faced with heavy Somali demands, the administration previously adopted a take-it-or-leave-it stance with Somalia with respect to the terms of the U.S. proposal. While some give may be required for a formal agreement, officials claim the Somalis have dropped their demands for huge amounts of aid and are not insisting on a defense pact with the United States or that this county back Somali claims in the Ogaden. The Somalis, U.S. officials say, now report there are only a few problems remaining and they can be solved quickly.

Aside from the forthcoming military aid package, the United States is providing $11 million in development aid and $50 million in refugee assistance to Somalia this year and has another $13 million $37 million, respectively, budgeted for next year.