WASHINGTON — President Trump was already revved up when he emerged from his limousine to visit NATO’s new headquarters in Brussels last May. He had just met France’s recently elected president, Emmanuel Macron, whom he greeted with a white-knuckle handshake and a complaint that Europeans do not pay their fair share of the alliance’s costs.

On the long walk through the NATO building’s cathedral-like atrium, the president’s anger grew. He looked at the polished floors and shimmering glass walls with a property developer’s eye. (“It’s all glass,” he said later. “One bomb could take it out.”) By the time he reached an outdoor plaza where he was to speak to the other NATO leaders, Mr. Trump was fuming, according to two aides who were with him that day.

He was there to dedicate the building, but instead he took a shot at it.

“I never asked once what the new NATO headquarters cost,” Mr. Trump told the leaders, his voice thick with sarcasm. “I refuse to do that. But it is beautiful.” His visceral reaction to the $1.2 billion building, more than anything else, colored his first encounter with the alliance, aides said.

Nearly a year into his presidency, Mr. Trump remains an erratic, idiosyncratic leader on the global stage, an insurgent who attacks allies the United States has nurtured since World War II and who can seem more at home with America’s adversaries. His Twitter posts, delivered without warning or consultation, often make a mockery of his administration’s policies and subvert the messages his emissaries are trying to deliver abroad.

Mr. Trump has pulled out of trade and climate change agreements and denounced the 2015 nuclear deal with Iran. He has broken with decades of American policy in the Middle East by recognizing Jerusalem as the capital of Israel. And he has taunted Kim Jong-un of North Korea as “short and fat,” fanning fears of war on the peninsula.

He has assiduously cultivated President Xi Jinping of China and avoided criticizing President Vladimir V. Putin of Russia — leaders of the two countries that his own national security strategy calls the greatest geopolitical threats to America.

Above all, Mr. Trump has transformed the world’s view of the United States from a reliable anchor of the liberal, rules-based international order into something more inward-looking and unpredictable. That is a seminal change from the role the country has played for 70 years, under presidents from both parties, and it has lasting implications for how other countries chart their futures.

Mr. Trump’s unorthodox approach “has moved a lot of us out of our comfort zone, me included,” the national security adviser, Lt. Gen. H. R. McMaster, said in an interview. A three-star Army general who served in Iraq and Afghanistan and wrote a well-regarded book about the White House’s strategic failure in Vietnam, General McMaster defined Trump foreign policy as “pragmatic realism” rather than isolationism.

“The consensus view has been that engagement overseas is an unmitigated good, regardless of the circumstances,” General McMaster said. “But there are problems that are maybe both intractable and of marginal interest to the American people, that do not justify investments of blood and treasure.”

Mr. Trump’s advisers argue that he has blown the cobwebs off decades of foreign policy doctrine and, as he approaches his first anniversary, that he has learned the realities of the world in which the United States must operate.

They point to gains in the Middle East, where Crown Prince Mohammed bin Salman is transforming Saudi Arabia; in Asia, where China is doing more to pressure a nuclear-armed North Korea; and even in Europe, where Mr. Trump’s criticism has prodded NATO members to ante up more for their defense.

While Mr. Trump has held more than 130 meetings and phone calls with foreign leaders since taking office, he has left the rest of the world still puzzling over how to handle an American president unlike any other. Foreign leaders have tested a variety of techniques to deal with him, from shameless pandering to keeping a studied distance.

“Most foreign leaders are still trying to get a handle on him,” said Richard N. Haass, a top State Department official in the George W. Bush administration who is now the president of the Council on Foreign Relations. “Everywhere I go, I’m still getting asked, ‘Help us understand this president, help us navigate this situation.’

“We’re beginning to see countries take matters into their own hands. They’re hedging against America’s unreliability.”

Mr. Trump met with Chancellor Angela Merkel of Germany, second from left, and other members of the Group of 7 in Italy in May.Credit
Stephen Crowley/The New York Times

Difficulties With Merkel

Few countries have struggled more to adapt to Mr. Trump than Germany, and few leaders seem less personally in sync with him than its leader, Chancellor Angela Merkel, the physicist turned politician. After she won a fourth term, their relationship took on weighty symbolism: the great disrupter versus the last defender of the liberal world order.

In one of their first phone calls, the chancellor explained to the president why Ukraine was a vital part of the trans-Atlantic relationship. Mr. Trump, officials recalled, had little idea of Ukraine’s importance, its history of being bullied by Russia or what the United States and its allies had done to try to push back Mr. Putin.

German officials were alarmed by Mr. Trump’s lack of knowledge, but they got even more rattled when White House aides called to complain afterward that Ms. Merkel had been condescending toward the new president. The Germans were determined not to repeat that diplomatic gaffe when Ms. Merkel met Mr. Trump at the White House in March.

At first, things again went badly. Mr. Trump did not shake Ms. Merkel’s hand in the Oval Office, despite the requests of the assembled photographers. (The president said he did not hear them.)

Later, he told Ms. Merkel that he wanted to negotiate a new bilateral trade agreement with Germany. The problem with this idea was that Germany, as a member of the European Union, could not negotiate its own agreement with the United States.

Rather than exposing Mr. Trump’s ignorance, Ms. Merkel said the United States could, of course, negotiate a bilateral agreement, but that it would have to be with Germany and the other 27 members of the union because Brussels conducted such negotiations on behalf of its members.

“So it could be bilateral?” Mr. Trump asked Ms. Merkel, according to several people in the room. The chancellor nodded.

Afterward, German officials expressed relief among themselves that Ms. Merkel had managed to get through the exchange without embarrassing the president or appearing to lecture him. Some White House officials, however, said they found the episode humiliating.

For Ms. Merkel and many other Germans, something elemental has changed across the Atlantic. “We Europeans must really take our destiny into our own hands,” she said in May. “The times in which we can fully count on others — they are somewhat over.”

President Xi Jinping of China reopened a long-dormant theater inside the Forbidden City to present Mr. Trump and his wife, Melania, an evening of Chinese opera last month.Credit
Doug Mills/The New York Times

Concerns on Statecraft

Mr. Trump gets along better with Mr. Macron, a 40-year-old former investment banker and fellow political insurgent who ran for the French presidency as the anti-Trump. Despite disagreeing with him on trade, immigration and climate change, Mr. Macron figured out early how to appeal to the president: He invited him to a military parade.

But Mr. Macron has discovered that being buddies with Mr. Trump can also be complicated. During the Bastille Day visit, officials recalled, Mr. Trump told Mr. Macron he was rethinking his decision to pull out of the Paris climate accord.

That prompted French diplomats to make a flurry of excited calls to the White House for clarification the following week, only to find out that American policy had not changed. White House officials say that Mr. Trump was merely reiterating that the United States would be open to rejoining the pact on more advantageous terms.

There have been fewer misunderstandings with autocrats. Mr. Xi of China and King Salman of Saudi Arabia both won over Mr. Trump by giving him a lavish welcome when he visited. The Saudi monarch projected his image on the side of a hotel; Mr. Xi reopened a long-dormant theater inside the Forbidden City to present Mr. Trump and his wife, Melania, an evening of Chinese opera.

“Did you see the show?” Mr. Trump asked reporters on Air Force One after he left Beijing in November. “They say in the history of people coming to China, there’s been nothing like that. And I believe it.”

Later, chatting with his aides, Mr. Trump continued to marvel at the respect Mr. Xi had shown him. It was a show of respect for the American people, not just for the president, one adviser replied gently.

Then, of course, there is the strange case of Mr. Putin. The president spoke of his warm telephone calls with the Russian president, even as he introduced a national security strategy that acknowledged Russia’s efforts to weaken democracies by meddling in their elections.

“Statecraft has been singularly absent from the treatment of some of his allies, particularly the U.K.,” said Peter Westmacott, a former British ambassador to the United States.

Mr. Trump’s feuds with Ms. May and other British officials have left him in a strange position: feted in Beijing and Riyadh but barely welcome in London, which Mr. Trump is expected to visit early next year, despite warnings that he will face angry protesters.

Aides to Mr. Trump argue that his outreach to autocrats has been vindicated. When Crown Prince Mohammed bin Salman visited the White House in March, the president lavished attention on him. Since then, they say, Saudi Arabia has reopened cinemas and allowed women to drive.

But critics say Mr. Trump gives more than he gets. By backing the 32-year-old crown prince so wholeheartedly, the president cemented his status as heir to the House of Saud. The crown prince has since jailed his rivals as Saudi Arabia pursued a deadly intervention in Yemen’s civil war.

Mr. Trump granted an enormous concession to Prime Minister Benjamin Netanyahu when he announced this month that the United States would formally recognize Jerusalem as the capital of Israel. But he did not ask anything of Mr. Netanyahu in return.

That showed another hallmark of Mr. Trump’s foreign policy: how much it is driven by domestic politics. In this case, he was fulfilling a campaign promise to move the American Embassy to Jerusalem from Tel Aviv. While evangelicals and some hard-line, pro-Israel American Jews exulted, the Palestinians seethed — leaving Mr. Trump’s dreams of brokering a peace accord between them and the Israelis in tatters.

With China, Mr. Trump’s cultivation of Mr. Xi probably persuaded him to put more economic pressure on its neighbor North Korea over its provocative behavior. But even the president has acknowledged, as recently as Thursday, that it is not enough. And in return for Mr. Xi’s efforts, Mr. Trump has largely shelved his trade agenda vis-à-vis Beijing.

“It was a big mistake to draw that linkage,” said Robert B. Zoellick, who served as United States trade representative under Mr. Bush. “The Chinese are playing him, and it’s not just the Chinese. The world sees his narcissism and strokes his ego, diverting him from applying disciplined pressure.”

Mr. Trump’s protectionist instincts could prove the most damaging in the long term, Mr. Zoellick said. Trade, unlike security, springs from deeply rooted convictions. Mr. Trump believes that multilateral accords — like the Trans-Pacific Partnership, from which he pulled out in his first week in office — are stacked against America.

Globalist and nationalist voices are often competing for influence over Mr. Trump’s foreign policy decisions.Credit
Stephen Crowley/The New York Times

Globalist vs. Nationalist

For some of Mr. Trump’s advisers, the key to understanding his statecraft is not how he deals with Mr. Xi or Ms. Merkel, but the ideological contest over America’s role that plays out daily between the West Wing and agencies like the State Department and the Pentagon.

“There’s a chasm that can’t be bridged between the globalists and the nationalists,” said Stephen K. Bannon, the president’s former chief strategist and the leader of the nationalist wing, who has kept Mr. Trump’s ear since leaving the White House last summer.

On the globalist side of the debate stand General McMaster; Secretary of Defense Jim Mattis; Secretary of State Rex W. Tillerson; and Mr. Trump’s chief economic adviser, Gary D. Cohn. On the nationalist side, in addition to Mr. Bannon, stand Stephen Miller, the president’s top domestic adviser, and Robert Lighthizer, the chief trade negotiator. On many days, the nationalist group includes the commander in chief himself.

The globalists have curbed some of Mr. Trump’s most radical impulses. He has yet to rip up the Iran nuclear deal, though he has refused to recertify it. He has reaffirmed the United States’ support for NATO, despite his objections about those members he believes are freeloading. And he has ordered thousands of additional American troops into Afghanistan, even after promising during the campaign to stay away from nation-building.

This has prompted a few Europeans to hope that “his bark is worse than his bite,” in the words of Mr. Westmacott.

Mr. Trump acknowledges that being in office has changed him. “My original instinct was to pull out,” he said of Afghanistan, “and, historically, I like following my instincts. But all my life I’ve heard that decisions are much different when you sit behind the desk in the Oval Office.”

Yet some things have not changed. Mr. Trump’s advisers have utterly failed to curb his Twitter posts, for example. Some gamely suggest that they create diplomatic openings. Others say they roll with the punches when he labels Mr. Kim of North Korea “Little Rocket Man.” For Mr. Tillerson, however, the tweets have severely tarnished his credibility in foreign capitals.

“All of them know they still can’t control the thunderbolt from on high,” said John D. Negroponte, who served as the director of national intelligence for Mr. Bush.

The tweets highlight that Mr. Trump still holds a radically different view of the United States’ role in the world than most of his predecessors. His advisers point to a revealing meeting at the Pentagon on July 20, when Mr. Mattis, Mr. Tillerson and Mr. Cohn walked the president through the country’s trade and security obligations around the world.

The group convened in the secure conference room of the Joint Chiefs of Staff, a storied inner sanctum known as the tank. Mr. Mattis led off the session by declaring that “the greatest thing the ‘greatest generation’ left us was the rules-based postwar international order,” according to a person who was in the room.

After listening for about 50 minutes, this person said, Mr. Trump had heard enough. He began peppering Mr. Mattis and Mr. Tillerson with questions about who pays for NATO and the terms of the free trade agreements with South Korea and other countries.

The postwar international order, the president of the United States declared, is “not working at all.”

A version of this article appears in print on December 29, 2017, on Page A1 of the New York edition with the headline: Insurgent President Is Changing, Unpredictably, U.S. Global Role. Order Reprints|Today's Paper|Subscribe