It’s the kind of political whiplash that other world leaders have felt as well. South Korean President Moon Jae-in, who staked his political fortunes on close collaborations with Trump over nuclear negotiations with North Korea, is now facing the president’s demands that Seoul increase its payments fivefold to support U.S. troops stationed on the Korean Peninsula.

Japanese Prime Minister Shinzo Abe has courted Trump relentlessly, with nearly four dozen meetings and phone calls and an elaborate state visit to Tokyo in the spring. But Tokyo was not spared from steel tariffs early in Trump’s tenure, and Trump contradicted Abe over the summer by refusing to declare North Korea’s short-range missile tests a violation of U.N. resolutions.

For Bolsonaro, a far-right leader who had patterned his campaign after Trump’s and aggressively sought to ingratiate himself with the White House, the tariffs represented an embarrassing reality check on his strategy of gambling his administration's foreign policy largely on good personal chemistry with a president who craves validation — but who views virtually all relationships as transactional and, potentially, disposable.

“This is a president who will develop close relationships but who will not necessarily be fully loyal to those close relationships,” said Fernando Cutz, a Western Hemisphere expert at the Cohen Group who served on the National Security Council under both Trump and President Barack Obama. “I don’t think Brazil understood that, but maybe they will now. I think this was a very big surprise to Brazil’s political system and its people. They really see Bolsonaro as a close friend of the president. This will feel like a pretty strong betrayal.”

The president’s Twitter missives, which also accused the two South American nations of devaluing their currencies, sent Brazil’s Foreign Ministry into a scramble to reach White House officials and mitigate the damage. Embassy officials in Washington frantically contacted the State Department, while Bolsonaro suggested he would try to call Trump directly. “I have an open channel with him,” Bolsonaro told reporters in Brasilia.

Yet it was unclear whether they would get quick answers. Inside Trump’s administration, a number of high-level U.S. officials in various government agencies also were caught off guard by the president’s tweets, according to two people familiar with the matter who spoke on the condition of anonymity to discuss private deliberations.

Trump’s punitive actions on Brazil and Argentina came just hours before he departed Washington for a NATO gathering in London, where U.S. allies were bracing for a mercurial president who has routinely berated them for not spending enough on mutual defense and unsettled them by suggesting the alliance was outdated.

For Trump, “what takes precedence is what’s good for him personally and what increases his power,” said Michael Shifter, president of the Inter-American Dialogue. He pointed to Colombian President Iván Duque, who had a warm first meeting with Trump in February, only to be criticized a month later by the president in response to illegal drugs smuggled into the United States.

Duque has “done nothing for us,” Trump declared.

“All of a sudden Trump decides to do something, presumably for his own political benefit,” Shifter said. “One by one the Latin American presidents are learning that being a close ally of Trump doesn’t pay off and you can’t really rely that you’re going to get favorable treatment.”

Foreign-policy experts acknowledged that no U.S. president has based decisions solely on personal relationships over larger geopolitical concerns. But Trump has long placed an overriding emphasis on personal fealty to him, forcing fellow leaders into an uncomfortable choice over what tone to take in dealing with his administration.

Some leaders, including French President Emmanuel Macron, German Chancellor Angela Merkel and former Mexican president Enrique Peña Nieto have at times struck a confrontational tone over Trump’s demands, provoking angry responses. Others, including Abe and Israeli Prime Minister Benjamin Netanyahu, have lavished Trump with praise and tied their administrations closely to his.

But perhaps none has been as overtly fawning as Bolsonaro, who earned the nickname “Trump of the Tropics” during a campaign in which he emulated Trump’s brash rhetoric en route to an upset victory. Trump was so enamored that he was the first world leader to call with congratulations after Bolsonaro won the election.

In a warm bilateral meeting at the White House last spring, Trump pledged to support Brazil’s efforts to become a full member of the Organization for Economic Cooperation and Development. Then Trump shocked the Brazilians and his own aides by suggesting that Brazil should become a member of NATO, an organization reserved for North Atlantic nations.

In August, with Brazil facing international condemnation for its handling of massive fires in the Amazon rainforest, Bolsonaro called Trump and persuaded him to represent Brazil’s position and push back against the criticism during the Group of 7 summit in France.

On Monday, Brazilian officials struck a tempered tone on the new tariffs. In a statement, the Bolsonaro government said it will “work to defend Brazilian trade interests and to safeguard trade flows.”

In private, diplomats were dumbfounded, emphasizing that the country has sought to strengthen its currency, the real, against the dollar, contrary to Trump’s contentions.

Nestor Forster, the acting Brazilian ambassador to Washington, was seeking more information from the Trump administration and Republican allies in Congress.

“Perhaps Trump is saying: ‘You’re my friend, but if it benefits me to go in another direction to meet my agenda for my political base, I will,’ ” said Ana Gimena Sanchez-Garzoli, an expert on the Andes region at the Washington Office on Latin America.

White House aides did not respond to requests for comment.

Some analysts suggested that Trump's tariffs were aimed at exerting leverage over Brazil and Argentina to limit their exports of soybeans to China, which has sought to dampen the impact of its own trade war with the United States by increasing agricultural purchases from other countries.

In any case, the president’s actions also were a sharp blow to Buenos Aires, which had enjoyed good relations under conservative President Mauricio Macri, who has golfed with Trump. Cutz, the former NSC official, said Trump’s relations with Macri were so positive that the president did not complain about Argentina’s trade imbalance with the United States as he did in regards to many other countries.

But Macri lost his reelection bid last month to Alberto Fernández, a more liberal candidate whose political views are likely to raise Trump’s ire, Cutz said.

Reflecting on Bolsonaro’s efforts to woo Trump, Cutz added: “If Bolsonaro is smart, he has already put a call into the White House. He said he has an open line. Now is the time to use it.”