The United States and Somalia agreed yesterday on a military facilities arrangement that will place U.S. forces in an African country that is warring with its Soviet and Cuban-supported neighbor, Ethiopia.

The agrreement for U.S. use of Somali ports and airfields, initialed at the State Department yesterday afternoon by negotiators for the two countries, is part of the Carter administration's drive to build up American military forces near the oil-rich Persian Gulf.

The United States previously reached agreement to use military facilities in Oman on the southeast tip of the Arabian Peninsula, and Kenya in east Africa.

The deal with Somalia is more complicated and controversial because of that country's struggle with its traditional enemy, Ethiopia, over a disputed region.

Somalia formerly was a Soviet ally complete with a Soviet military base, and Ethiopia was an American ally with U.S. bases. The countries changed partners after the 1974 Ethiopian revolution, which deposed Emperor Haile Selassie and especially after Soviet and Cuban forces came to Ethiopia's defense in late 1977 against invading Somali regulars.

President Carter had instructed his top aides early in his administration to "move in every possible way to get Somalia to be our friend." The United States briefly considered a military supply relatinship in 1977, but backed off after Somalia invaded Ethiopia to win control of the Somali-speaking Ogaden region. Because the Somalis crossed recognized international boundaries, nearly all other African countries have backed Ethiopia.

The agreement initialed yesterday provides for the United States to supply $20 million in military sales credits this year, and another $20 million credit plus $5 million in budgetary support next year. U.S. negotiators said Somalia has agreed in writing that it will not use American-supplied equipment to fight in Ethiopia.

The U.S. officials also said that Somalia has given "firm assurances" verbally that it will not use its regular military forces across the Ethiopian border.

American officials conceded that it is far from certain how reliable such assurances will be, in view of Somalia's record of fighting for territory the Somalis consider rightfully theirs.

Somalia began a renewed buildup of its forces in the disputed Ogaden region late last year. Early this summer, Somali regular army battalions armed with tanks and other heavy weapons fought on Ethiopian soil U.S. sources confirmed.

Ten days ago, Ethiopia publicly threatened to invade Somalia if the battle continued, raising the specter of Soviet and Cuban forces supporting an attack on a country increasingly allied with the United States. When the threats were made, however, the Somali troops reportedly were withdrawing from Ethiopia after suffering major losses.

African specialists with the government and a few members of Congress have expressed concern about the U.S. military alliance with Somali, on grounds that it is a high-risk venture in a country that no outside power has been able to control fully. The White House and the Defense Department" by the United States to Somaof the port facilities at Mogadishu and the Soviet-built air and naval installations at Berbera.

The agreement initialed yesterday, which is expected to be signed today, includes "no formal security commitment" by the United States to Somalia, according to U.S. negotiators. However, Washington officials as well as the Somalis recognized that the presence of U.S. forces in Somalia is likely to be a strong implied commitment.

This assurance is believed to be the central reason why Somalia wanted the U.S. connection, and why in recent weeks it suddenly and drastically reduced its demands (which at one point were $2 billion in U.S. aid and an explicit security commitment) after hints by Secretary of State Edmund S. Muskie that Washington was becoming cool to the stalemated negotiatiions. s

The facilities use arrangement is in the form of an executive agreement that must be reported to Congress but that does not require a two-thirds Senate vote, as is the case with a formal treaty.

Rep. Stephen J. Solarz (D-N.Y.), chairman of the Arfica subcommittee of the House Foreign Affairs Committee, has served notice of hearings and made plan his objections to the agreements. Several othher committees of Congress are expected to review the deal, because U.S. funds must be "reprogrammed" to support it.

State Department officials said that despite expected questioning and some opposition, the odds are that Congress will not block the arrangement in view of the increasing concern for U.S. military strength in the Persian Gulf.