The agreement enables Russia to expand the market for its gas, which now goes mostly to Europe.
It "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 provides "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."
For China, the world's second-largest economy, the deal will help ease gas shortages and curb its reliance on coal.
U.S. Secretary of State John Kerry noted that Russia and China have been trying to work out an energy agreement for 10 years and said the deal "isn't a sudden response to what's been going on" in Ukraine.
"And if the world benefits as a result of that, that's fine," he said.
Similarly, State Department spokeswoman Jen Psaki said of the prospect of closer Russia-China relations: "It is not a surprise to us that countries that are neighbors communicating about how to work together, whether that's through an economic partnership or otherwise."
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.
Gas is due to begin flowing to China as early as 2018.
The contract is worth $400 billion, Gazprom CEO Alexei Miller told Russian news agencies. That is greater than the gross domestic product of South Africa.
U.S. Treasury Secretary Jacob Lew had appealed to China during a visit last week to avoid taking steps that might offset the West's sanctions against Russia over its lightning annexation of Crimea in March. However, American officials have acknowledged China's pressing need for energy.
The contract is "particularly important" at a time when Europe has threatened to cut gas imports and reduce its dependence on Russia because of the Ukraine crisis, Alexander Lukin, a deputy head of the Russian Diplomatic Academy under the country's Foreign Ministry, was quoted as saying 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."
Putin met on 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.
China and Russia have been negotiating the deal for more than a decade but had been hung up over the price.
Russia will invest $55 billion in fulfilling the contract, while China will invest at least $20 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 center on the Pacific will allow Russia to export to markets in Japan and South Korea.
"Without any overstatement, it will be the world's biggest construction project for the next four years," Putin said.
Isachenkov reported from Moscow.