SHANGHAI, China -- China signed a landmark deal Wednesday to buy Russian natural gas worth about $400 billion, giving a boost to diplomatically isolated President Vladimir Putin and expanding Moscow's ties with Asia.

Price negotiations on the 30-year deal continued into the final hours of a two-day visit by Putin to China, during which both sides had said they hoped to sign an agreement.

Putin was in Shanghai for an Asian security conference where China's president called for a new model of Asian security co-operation based on a regional group that includes Russia and Iran and excludes the United States.

The gas deal gives Moscow an economic boost at a time when Washington and the European Union have imposed visa bans and asset freezes on dozens of Russian officials and several companies over Ukraine. It allows Russia to diversify its markets for gas, which now goes mostly to Europe.

The agreement "opened the door for Russia to enter into Asia's gas market," said Keun-Wook Paik, senior research fellow at the Oxford Institute for Energy Studies.

Politically, the deal has provided "a breathing space for Russia," Paik said. "Russia, and Putin, can demonstrate it's not completely isolated because of the Ukraine crisis. Russia has demonstrated that they have a very reliable strategic partnership with China."

Russia's economy has been bruised by its dispute with the West over Ukraine's tilt toward the European Union, a shift that inflamed Moscow's insecurities about declining influence and sparked its annexation of Crimea in March.

The supplies will help to ease gas shortages in China, the world's second-largest economy, and curb reliance on coal.

The agreement calls for Russian government-controlled Gazprom to supply state-owned China National Petroleum Corp. with 38 billion cubic meters of gas annually, Gazprom spokesman Sergey Kupriyanov told The Associated Press. That would represent about a quarter of China's current annual gas consumption of nearly 150 billion cubic meters.

The contract is worth a total of $400 billion, Gazprom CEO Alexei Miller told Russian news agencies. That figure is greater than the GDP of South Africa. Gas is due to begin flowing to China as early as 2018.

The U.S. treasury secretary, Jacob Lew, appealed to China during a visit last week to avoid taking steps that might offset sanctions. However, American officials have acknowledged China's pressing need for energy.

The contract "particularly important" at a time when Europe has threatened to cut gas imports and reduce its dependence on Russia because of the Ukraine crisis, said Alexander Lukin, a deputy head of the Russian Diplomatic Academy under the country's Foreign Ministry, quoted by the RIA Novosti news agency.

"We will be able to show to Europe that we have other customers," Lukin said.

Alexei Pushkov, a Kremlin ally who is head of the international affairs committee of the Russian parliament's lower house, said on Twitter: "The 30-year gas contract with China is of strategic significance. Obama should give up the policy of isolating Russia: It will not work."

CNPC announced it had signed a deal but it gave no details.

Putin met Tuesday with Chinese President Xi Jinping while gas negotiations continued in private.

The Russian leader's visit is a "major step toward a strategic partnership of the two nations," said Mikhail Margelov, head of the foreign affairs committee in the upper house of the Russian parliament, according to RIA Novosti.

The Ukraine crisis and Western sanctions on Russia had raised expectations Moscow would compromise to secure the gas deal.

The price appears to be closer to the level Russia wanted, according to analyst Xizhou Zhou of IHS Energy. In exchange, the two sides dropped a requirement for prepayment that was a feature of Chinese purchases of Russian oil, he said.

"This higher price level reflects China's willingness to pay more for cleaner fuel," said Zhou in a report.

China and Russia have been negotiating the deal for more than a decade but had been hung up over the gas price.

"Gazprom is under increasing geopolitical and competitive pressure to diversify its market toward the East, while China's gas market remains supply constrained as demand continues to surge," said Zhou.

Russia will invest $55 billion in fulfilling the contract while China will invest $22 billion, Putin told Russian reporters in Shanghai. He said the gas price would be based on a formula linked to that of oil and oil products.

Plans call for building a pipeline to link China's northeast to a line that carries gas from western Siberia to the Pacific port of Vladivostok. The development of a gas centre on the Pacific will allow Russia to export to prosperous markets in Japan and South Korea.