OREANDA-NEWS  Turkey’s mercurial president, Recep Tayyip Erdogan, is in political difficulty before elections next year, with his economy imploding, a central bank nearly out of foreign exchange and volcanic inflation running at about 80 percent annually.

President Vladimir V. Putin of Russia has his own troubles, with the war in Ukraine bogging down and tough economic sanctions hitting Russia’s industry and broader economy.

The mutual challenges have pushed the two men closer together than ever. They have met twice in the last three weeks, most recently last weekend in Sochi, Russia, hoping to lessen their vulnerabilities by expanding their partnership and agreeing, Mr. Erdogan said, on economic cooperation that he hoped would total $100 billion.

It is a relationship that raises the hackles of Mr. Erdogan’s NATO allies, as he provides Mr. Putin a sizable hole in the dam of sanctions the West has tried hard to build in its effort to stymie Mr. Putin’s war in Ukraine. Some wonder where Mr. Erdogan’s real loyalties lie, beyond his own self-interest.

There is little doubt that, for now, the bond is proving to be mutually beneficial, as details of their negotiations emerge in the aftermath. For Mr. Putin, the benefits include energy and arms sales, investment and a close connection to a member of NATO, which is trying to isolate him and help Ukraine defeat his invading army.

Turkey, which is not a member of the European Union, has refused to apply Western sanctions against Russia. It is exploring ways to work with otherwise sanctioned Russian banks and accept payments through Russian credit cards. Russian gas flows unimpeded through the TurkStream pipeline. There are also reports that Russia is seeking Turkish help in providing “subsystems” for its weapons, which can no longer source Western components directly.

For Mr. Erdogan, the benefits involve cash infusions into the central bank, cheap energy, global importance, a large export market, renewed Russian tourism and, crucially, apparent Russian acquiescence to his politically popular efforts to crush Kurdish separatism in Syria, where Russia supports the Syrian government of Bashar al-Assad.

But the two leaders remain first-order frenemies, each a prickly strongman who has gathered up extraordinary powers to himself and keeps his own counsel. When they met in Tehran last month, Mr. Erdogan left Mr. Putin standing alone for nearly a minute, as the Russian leader, notorious himself for the waiting-game trick, shifted uncomfortably in front of whirring cameras.

The move was interpreted as a subtle reminder of the altering power balance between the two men — Mr. Putin had kept Mr. Erdogan waiting before — as they work together, even while trying to maintain the upper hand. Increasingly, relations between the two countries come down to relations between them. The discussions between the two autocrats are also closely held, with the Turkish Foreign Ministry, let alone the public, largely kept in the dark.

“Turkish foreign policy has entered a very dangerous period,” said Ilhan Uzgel, a political scientist who taught international relations at Ankara University before being fired by presidential decree. “The two leaders come together and make a negotiation. But only the two leaders sitting in the palace alongside a few other people, a very small group, know the content of these negotiations.”

Mr. Erdogan has bought sophisticated Russian antiaircraft missiles that undermine NATO security and single-handedly moved to block NATO membership for Sweden and Finland, lifting his objections for now, but with the expectation that there will be more drama to come before the Turkish Parliament votes on whether to ratify their accession sometime this autumn.

The obstructionism could only delight Mr. Putin, who has long warned against the Nordic states joining the alliance.

Washington is watching carefully, stating officially that “we have urged Turkey to not become a safe haven for illicit Russian assets or transactions,” and urging Turkey to reduce its energy dependence on Russia. The statement also noted that Turkey supports Ukrainian sovereignty and territorial integrity, and that Mr. Erdogan has called the Russian invasion “unacceptable.”

Indeed, Turkey has opposed the Russian invasion of Ukraine, blocked Russian warships from entering the Black Sea and sold weapons to Kyiv, including sophisticated drones that have helped kill Russian soldiers.

For the West, Mr. Erdogan’s ability to deal with Mr. Putin has not been all bad. Turkey has kept close diplomatic ties to Moscow and is acting as the main mediator between Russia and Ukraine for grain deliveries and possible peace talks. Mr. Erdogan or his top aides speak to Mr. Putin and President Volodymyr Zelensky of Ukraine several times a week.

“Erdogan is keeping all his options open, which is what countries tend to do when they think only of their own self-interest, which is not what allies do,” said Ivo Daalder, a former U.S. ambassador to NATO. “He’s figured out a way to play his game, but he’s doing it at the expense of an alliance which is key to his own security.”

Having a NATO ally with good lines of communication to Mr. Putin is a good thing, Mr. Daalder added, “so long as he’s saying the right things, trying to resolve issues coherent with the goals of the alliance and not undermining it.”

Mr. Erdogan’s main goal, Turkish analysts agree, is his own re-election, and he is looking for help with both the economy and his effort to fight what he considers to be Kurdish terrorism in Syria and at home.

“The objective of the Erdogan government isn’t to relieve Putin, it’s to create the right conditions for itself on the way to the elections,” Professor Uzgel said.

“Erdogan has three worries,” he said. “One, to tell the West that he can do business with Putin. Second, he’s expecting the cash coming in from Russia to temporarily relieve the currency rates. Third, he wants to be on the same page with Russia for a possible incursion he wants to carry out inside Syria.”

Mr. Erdogan is doing badly in opinion polls with elections due by June next year. His major vulnerabilities stem from the ruinous economy and from popular exhaustion and resentment with the millions of refugees it hosts.

“On both issues Putin holds enormous leverage over Erdogan,” said Asli Aydintasbas of the European Council on Foreign Relations. Russia is a source of hard currency, cheap energy and jobs, she said, while it would take only a few Russian bombing runs over northern Syria to flood another two million refugees across the border into Turkey.

Regional security threats, which include a tentative peace settlement in the fight over Nagorno-Karabakh — Turkey supports Azerbaijan, while Russia intervened to save Armenia — mean that any Turkish government would want a balanced working relationship with Russia, said Sinan Ulgen, director of EDAM, a Turkish research institution.

“Turkey needs a diplomatic partnership with Russia in our neighborhood, given crisis areas like Syria or Nagorno-Karabakh, so it doesn’t have the luxury of isolating Russia,” Mr. Ulgen said.

Mr. Erdogan’s ability to bring Russian and Ukrainian foreign ministers together and to mediate the deal to get Ukrainian (and Russian) grain out of the blockaded Black Sea “validates Turkey’s balanced approach to Russia,” Mr. Ulgen said. “Turkey has been pro-Ukraine without being anti-Russia.”

Turkish officials, he said, “are also aware of the thin line between not implementing sanctions and giving the perception or acting as the country that helps Russia evade sanctions.”

The Putin-Erdogan relationship is a strange one, with both countries “openly cooperating but also fighting proxy wars” in Syria and Libya, while Turkey needs Russian acquiescence to go after Syrian Kurds and preserve the tenuous cease-fire between Armenia and Azerbaijan, said Ms. Aydintasbas.

“No one in Ankara is happy that Russia is controlling parts of Turkey’s northern flank on the Black Sea and parts of its southern flank with Syria, but they understand they have to negotiate a relationship with Russia and establish a modus vivendi,” she said. “The only alternative is fighting.”

Returning Friday from his meeting with Mr. Putin in Sochi, Mr. Erdogan told reporters: “Mr. Putin holds a fair attitude toward Turkey.”

He added: “The mutual understanding we have built with Mr. Putin on trust and respect assures our relations.”

Reporting was contributed by Carlotta Gall in Kyiv, Ukraine, and Nimet Kirac in Istanbul.