A House subcommittee took a dim view yesterday of the Carter administration's decision to sell arms to Somalia in exchange for the right to use the ports of Berbera and Mogadishu on the Horn of Africa.

Members of the House Foreign Affairs Africa subcommittee at the first public hearing on the Somali base agreements, expressed concern that the United States would be dragged into African conflicts, either indirectly or directly.

Subcommittee Chairman Stephen J. Solarz (D-N.Y.) focused on what the Carter administration would do if the Somalis resumed their military campaign in neighboring Ogaden in Ethiopia. He warned that American weapons could end up being used there.

The Ogaden is a region in southeast Ethiopia that insurgents are trying to annex to Somalia. Communist forces, including Cubans, are helping Ethiopia combat the annexation attempt.

Carter administration officials "have been assured orally and in writing that the Somalis will not introduce regular forces into the Ogaden," Richard M. Moose, assistant secretary of state for African affairs, told the subcommittee.

He said the written pledge goes back to 1978 when the United States was discussing an arms deal with Somalia that never materialized, while the "oral assurance" came "over the course of the last few weeks."

In a series of exchanges with Moose, Solarz stressed that the subcommittee would feel more comfortable about the military agreements with Somalia if that country had put its pledge in writing about not using force or American weapons in the Ogaden.

"Are there any contingency plans if the Somalis send regular forces into the Ogaden?" asked Solarz. "What do we do then?"

"If they're in something more than a de minimus presence," replied Moose, the military agreements just forged would be "cast in very serious doubt." He said in a later exchange that any Somali violations "would jeopardize further agreements."

Moose, under repeated questioning by subcommittee members, said there was no significant presence of Somali regular troops in the Ogaden at the moment.

The agreements call for the United States to sell weaponry to Somalia, starting with $20 million in fiscal 1980 throught the reprogramming of funds and $20 million in fiscal 1981. Robert H. Pelletreau, representing the Pentagon, said the deal could lead to an extended military sales relationship with Somalia.

In addition to selling the Somalis antiaircraft weaponry, communications equipment and trucks under this first sales deal, Pelletreau, said, the United States will spend another $11 million to improve the Somali ports of Berbera and Mogadishu. The port improvements would include putting in navigational buoys, and building fuel storage tanks and pumping stations for the use of American ships and other forces.

Moose stressed that the Somalis are subject to the standard prohibition in U.S. arm sales agreements against using American weaponry offensively in another country. He said the United States could cut off the supply line if the Somalis took American weapons into the Ogaden.

"Yes," said Rep. Millicent Fenwick (R-N.J.), "but are they aware we do it?"

Leslie H. Gelb, a former director of the State Department's politico-military affairs office invited to attend the hearing to critique the arms agreements, said "the big problem" is whether the Somalis will live up to their pledges on the Ogaden.

The administration, Gelb said, should "make clear in advance" to the Somalis that arms would be cut off if they violate the agreement. He said after the hearing that the United States has seldom enforced that provision of arms sales agreements with other countries.

The Somalis could retaliate for any arms cutoff by forbidding U.S. forces to use Berbera and Mogadishu, Solarz said. Moose conceded this was "a very real possibility."

However, in response to such doubts, Moose repeatedly told the subcommittee that the Somalis had concluded that they should pursue a political, not a military, solution to the long simmering conflict over the Ogaden.

"If all this is true," said Solarz in a tone of incredulity, "you may be a candidate for the Nobel Peace Prize.

"Have we thought through how we would respond to an attack on Berbera?" Solarz asked Moose.

"Obviously, we would be concerned," Moose replied.

"Supposing Ethiopia started to drop some bombs on Berbera?" Solarz pressed.

"We would have to defend ourselves" as an immediate response, Moose said, and then assess the situation.

"I have some very deep concerns" about selling American weaponry to Somalia, said Rep. William H. Gray (D-Pa.). Rep. Howard E. Wolpe (D-Mich.) said he, too, was worried about the same thing. Ethiopia might regard the weapons as increasing Somalia's threat to that country, Wolpe said.

The first $20 million in arms sales to Somalia "does not give Somalia very much increase in capability," said the Pentagon's Pelletreau.

Since the United States already has agreements for using bases in Kenya, Oman and Diego Garcia, Rep. Floyd J. Fithian (D-Ind.) asked, "What more would you get by taking on the African problem?" Pelletreau replied that the Somali ports would give the United States better control over the sea lanes and provide American forces with more flexibility of action.

"Once you get your foot in there," cautioned Fithian, "you can't extricate without some political fallout."