In the months leading up to Xi Jinping making common cause with Russian President Vladimir Putin, the Chinese leader was focused on one country, and it wasn’t Ukraine.

His ambitions for alignment with Mr. Putin had one main purpose: presenting a united front against the U.S. The result, according to Chinese officials, foreign-policy advisers to Beijing and an analysis of public statements, was the Feb. 4 China-Russia declaration that the countries’ friendship had “no limits.”

Russia’s subsequent invasion of its neighbor is forcing Beijing into adjusting its foreign policy in a way that risks damaging relations with the U.S.-led West and undoing years of efforts to paint itself as a responsible world leader.

In Beijing, the ripple effects of a move that may cost China dearly are now sinking in, say the officials and advisers. Some officials say they are fearful of the consequences of getting so close to Russia at the expense of other relationships—especially when Russian aggression against Ukraine is isolating Moscow in much of the world.

Since Russia invaded Ukraine at the end of February, the U.S. and allied countries have imposed heavy sanctions on Russia. WSJ’s Shelby Holliday dives into how these sanctions are affecting everyone from President Vladimir Putin to everyday Russian citizens. Photo: Pavel Golovkin/Associated Press

Already, many politicians from Washington to Brussels have grouped Beijing together with Moscow as a new “axis”—a term giving Western alliances more reason to disengage from China and form closer ties among themselves.

“Elevating the partnership with Russia on the eve of its invasion of Ukraine was a massive foreign-policy blunder by Xi,” said Jude Blanchette, a China specialist at the Center for Strategic and International Studies, a Washington think tank focused on international relations. “The cost is very real for China and is exposing the limits of Xi’s policy.”

Just a year ago, Mr. Xi was making remarks such as “The East is rising, and the West is in decline,” as China’s economy roared back while much of the world was mired in Covid-19 lockdowns. Beijing was trying to shape and inhabit a role for itself as a leader on combating climate change and a benevolent sponsor of developing nations, even as it was ramping up nationalistic rhetoric, including around its claims to Taiwan.

This year, during which Mr. Xi is expected to break with precedent and seek a third term in power, he is facing an economic downturn at home that is largely a result of his own policies—and a geopolitical shift in which China has placed itself on one side of a gulf that has almost all of the rest of the world on the other side.

What Beijing will do next depends on how hard the U.S. will push sanctions on Russia, say the foreign-policy advisers. Mr. Xi also likely will continue to maintain his partnership with Mr. Putin, as Beijing sees little chance of improvement in its U.S. ties and needs to keep Russia around as its most important strategic collaborator even if it isn’t an outright ally. But that will require China to continue to straddle an increasingly difficult diplomatic position.’

In internal meetings over the past year, the officials and foreign-policy advisers say, Mr. Xi has emphasized the U.S. as the biggest threat to China’s interests, centering China’s foreign policy on aligning with Russia to confront Washington.

Beijing started planning for the Feb. 4 Xi-Putin summit in late November, as soon as Mr. Putin accepted the invitation to attend the opening of the Beijing Winter Olympics.

At the time, Western nations including the U.S., the U.K., and Canada were laying the groundwork for a diplomatic boycott of the Games over China’s human-rights record. The Biden administration was about to kick off a Summit for Democracy in early December that sought to establish a clear alternative to Beijing’s autocratic rule. Those moves infuriated Beijing and drove its decision-making, say the officials and advisers, who are familiar with the process leading to the Feb. 4 declaration.

One of Mr. Xi’s objectives was to lay out an ideological foundation for the partnership between China and Russia, those people said. To that end, the Chinese ambassador to Washington teamed up with his Russian counterpart in publishing an unusual joint opinion piece in late November in the magazine of the Center for the National Interest, a Washington-based conservative think tank established by former President Richard Nixon.

The two argued that democracy “can be realized in multiple ways” and isn’t the prerogative of any one country or group of countries. It called China “a whole-process, socialist democracy” and said democracy was the fundamental principle of Russia’s “democratic federative law-governed state.”

The Feb. 4 joint statement said both countries “have profound democratic traditions rooted in a thousand years of development,” a phrase reflecting Mr. Xi’s ideological views. A thousand years ago, China was in the emperor-ruled and feudal Song Dynasty, known for a highly developed bureaucracy.

It was Beijing that suggested including that the two countries’ friendship has “no limits”—wording read with apprehension in the West—according to the officials and advisers. The intention was less a declaration China would stand by Russia in case of war than a strong message to the U.S. about the resolve the two have in confronting what they see as increased American threats, the people said.

What didn’t factor into Beijing’s planning was Moscow’s increasingly heated rhetoric on Ukraine, those people said. Even as the Russian troop buildup along the border of the Eastern European country intensified and Chinese officials were presented with American intelligence on a likely Russian invasion, Beijing still dismissed that scenario as unlikely.

And, these people said, China saw Mr. Putin’s brinkmanship as already getting him desired results, including a divided North Atlantic Treaty Organization.

“China’s eagerness to present a strong alignment with Russia to counter the U.S. caused it to miss all the signs and to go in a dangerous direction,” said Yun Sun, director of the China program at the Stimson Center, a Washington think tank focused on promoting peace and security.