The trade deficit in the U.S. unexpectedly shrank in August to the lowest level in seven months as exports edged up to a record.

The gap decreased 0.5 percent to $40.1 billion, the smallest since January, from a revised $40.3 billion in July, the Commerce Department reported today in Washington. The median forecast in a Bloomberg survey of 70 economists called for a deficit of $40.8 billion. The gain in exports was driven by capital goods such as telecommunications equipment, and the petroleum shortfall was the smallest since 2004.

Demand from the nation’s trading partners is holding up even as the global economic expansion cools, giving American manufacturers a lift. Imports have been restrained as the U.S. comes closer to energy independence than it has in almost three decades, limiting the need for foreign oil.

A shrinking deficit could be “a net positive for the GDP figures,” Scott Anderson, chief economist at Bank of the West in San Francisco, said before the report.

Bloomberg survey estimates ranged from trade deficits of $39 billion to $44.7 billion. The Commerce Department initially reported a $40.5 billion shortfall for July.

Exports climbed 0.2 percent to $198.5 billion. Demand for American-made capital goods advanced by $1 billion.

Imports increased 0.1 percent to $238.6 billion in August.

The petroleum deficit shrank to $13.1 billion, the smallest since July 2004. Imports of the fuel fell to $27.2 billion, the least since November 2010.

After eliminating the effects of price fluctuations, which generates the numbers used to calculate gross domestic product, the trade deficit was little changed at $47.9 billion from $47.8 billion in July. That’s down from an average $51.6 billion in the second quarter, putting trade on track to boost growth.

The U.S. economy expanded at the fastest pace since 2011 in the second quarter, with GDP rising at a 4.6 percent annualized rate after a first quarter contraction, Commerce Department figures showed last month. Growth is expanded to slow to a 3 percent clip in the third quarter, according to a median estimate of economists surveyed by Bloomberg.

By Victoria Stilwell