BEIJING — China signed a huge, long-awaited deal on Wednesday to buy Russian natural gas, giving Beijing a new source of clean energy and Moscow a diplomatic boost as it faces international sanctions for its aggressive actions in Ukraine.

With the stroke of a pen, Russia significantly shifted its economic relations with its neighbors, creating a major new export market to the east and reducing its reliance on European customers at a time when its relations with the West are at their lowest point since the Cold War.

Russian President Vladimir Putin called the deal a “watershed event” and said implementation would start “tomorrow.”

The 30-year deal was announced after meetings in Shanghai between Putin and Chinese President Xi Jinping. It is worth an estimated $400 billion, Alexei Miller, chief executive of the Russian energy giant Gazprom, told Russian reporters.

The deal marked a new partnership between two countries that have at times mistrusted each other but have also sought to counter U.S. influence in global affairs.

China’s booming economy has created a growing need for energy, especially cleaner sources of power, given its reliance on coal, which has produced major pollution problems.

The agreement allows Russia to diversify its gas exports at a time when the crisis in Ukraine has accelerated calls in Europe to rely less on energy supplies from Russia. Europe gets about 30 percent of its gas from Russia.

U.S. Treasury Secretary Jack Lew appealed to China in a visit last week to avoid actions that might limit the impact of recent Western sanctions against Russia. But a U.S. official, who was not authorized to speak by name, said the United States would distinguish between deals that have long been in the works — such as this one — and new agreements that seek to fill space left by U.S. and European Union sanctions.

The deal will involve developing natural gas fields in Russia and building pipelines from Russia to China. The cost of building the infrastructure alone is expected to top $70 billion, said Mikhail Krutikhin, an energy and oil analyst at RusEnergy, a Moscow think tank.

The agreement was 10 years in the making, and price had long been the stumbling block. On Wednesday, the final per-unit price of the gas remained a mystery.

The $400 billion figure quoted by Gazprom’s Miller is probably the result of a formula that could include other costs — such as construction, transportation of the gas and other fees — making it difficult to work backward to the price per unit.

Miller called the price a “commercial secret.”

Analysts at IHS Energy — who have tracked the deal’s progress — said in a written analysis that they believe the final price was “closer to what Russia wanted than what China was initially prepared to pay.”

“This is Gazprom’s biggest contract. We don’t have a contract like this with any other company,” said Miller in Shanghai, according to Russia’s Interfax news agency.

The agreement met with approval from many people in Russia, which has been swept by rising nationalism and anti-Western rhetoric related to the crisis in Ukraine.

One caller to the Ekho Moskvy radio station declared the gas deal “another victory for Putin because he managed to sell gas for European prices,” while another listener suggested the new level of ­Russian-Chinese cooperation must be a “nightmare for America.”

Suspicions about price

Putin said that the gas prices in the deal were pegged to the price of oil and petroleum products. That represents a win for Russia, analysts said, since oil prices are expected to remain high. European customers have been fighting for years to have natural gas prices float, based on market demand.

“This is the largest contract in the history of the gas industry of the former USSR and the Russian Federation,” Putin told reporters in Shanghai. The infrastructure costs to develop the natural gas fields needed to supply China will top those of the Sochi Games — which are believed to have been in the tens of billions of dollars, officials said.

But the missing price details raised the suspicions of some Russians, who suspect Putin dropped the price of gas significantly for China in a desperate maneuver to ensure a steady cash flow for Gazprom in the face of sinking revenue and Western sanctions.

“There’s something fishy in the contract,” said Krutikhin, the think-tank analyst, suggesting that Russia got a bad bargain.

Erica S. Downs, a China energy expert at the Washington-based Brookings Institution, cited other possible reasons for the secrecy. “Too high a price could anger China’s current suppliers in Central Asia,” she said. “Too low a price could affect Russia’s European buyers. And there’s the optics of the deal. If nobody knows the price, then no one can say who came out better or worse.”

Gazprom charged European customers on average about $380 per 1,000 cubic meters in 2013. Unnamed individuals quoted in Russian media estimated the price in the China deal at $350 per 1,000 cubic meters of gas, based on earlier projections of a long-term price tag of $400 billion.

But if the actual figure was much less than that, the deal is not as profitable as the Kremlin is making it out to be, Krutikhin said. Gazprom is already losing out to American and European competition; gas demand in Europe has been stagnant; and the threat of mounting Western sanctions over Ukraine is “making Mr. Putin jittery,” he added.

Russian officials on Wednesday also hinted at a possible “prepayment” totaling $25 billion.

An online notice posted on China National Petroleum’s Web site said that under the deal, Russia would begin supplying China with 38 billion cubic meters of natural gas a year beginning in 2018.

Russia will be responsible for building processing plants, doing field development and constructing pipelines on its side, and China will be responsible for the pipeline construction within its own borders.

Hauslohner reported from Moscow. Xu Yangjingjing and Gu Jinglu in Beijing and Michael Birnbaum in Kiev also contributed to this report.