The United States had a record trade deficit last year with countries ruled by communist governments, the U.S. International Trade Commission reports.

Overall trade was also a record, with total exchanges in both directions increasing to $13.8 billion from $11.7 billion in 1986. But the United States bought $2.5 billion worth of goods more than it sold, compared with 1.6 billion the previous year.The report, made available this week, details only the second time since 1965 that the United States has had a deficit in trade with these countries.

The deficit would have been greater except for the U.S. government subsidies on the sale of wheat, a major export to communist nations. The Soviet Union, China, Poland and Bulgaria along with U.S. agricultural interests all benefited from these subsidies.

The Soviets agreed to buy 4 million metric tons, or about 144 million bushels, for delivery before Sept. 30, their first purchases since October 1985. By the end of the year, they had agreed to buy another 4.8 million tons, or about 174 million bushels, for the following crop year. U.S. exports of wheat to the Soviets were worth $389 million in 1987.

U.S. subsidies on these sales varied from $19 to $40 a ton. They were given to meet competition from other countries, largely in western Europe, which also subsidized exports.

The commission, an independent U.S. government agency, said that although the Soviets had a good overall grain crop last year, wheat output declined by nearly 15 percent and much of what they produced was not quality wheat.

China bought 4 million tons. Sales of U.S. wheat to China were worth $139 million in 1987.

The growth of U.S. imports from communist governments was almost entirely the result of increased buying from China. More than a third of the U.S. puchases from China were clothes and other textiles for a total value of $2.5 billion, up from $2.1 billion the year before.