But Mr. Trump upended the proceedings anyway. One meeting, on July 12, was ostensibly supposed to be about Ukraine and Georgia — two non-NATO members with aspirations to join the alliance.

Accepted protocol dictates that alliance members do not discuss internal business in front of nonmembers. But as is frequently the case, Mr. Trump did not adhere to the established norms, according to several American and European officials who were in the room.

He complained that European governments were not spending enough on the shared costs of defense, leaving the United States to carry an outsize burden. He expressed frustration that European leaders would not, on the spot, pledge to spend more. And he appeared not to grasp the details when several tried to explain to him that spending levels were set by parliaments in individual countries, the American and European officials said.

Then, at another leaders gathering at the same summit meeting, Mr. Trump appeared to be taken by surprise by Jens Stoltenberg, the NATO secretary general.

Backing Mr. Trump’s position, Mr. Stoltenberg pushed allies to increase their spending and praised the United States for leading by example — including by increasing its military spending in Europe. At that, according to one official who was in the room, Mr. Trump whipped his head around and glared at American officials behind him, surprised by Mr. Stoltenberg’s remarks and betraying ignorance of his administration’s own spending plans.

Mr. Trump appeared especially annoyed, officials in the meeting said, with Chancellor Angela Merkel of Germany and her country’s military spending of 1 percent of its gross domestic product.

By comparison, the United States’ military spending is about 4 percent of G.D.P., and Mr. Trump has railed against allies for not meeting the NATO spending goal of 2 percent of economic output. At the summit meeting, he surprised the leaders by demanding 4 percent — a move that would essentially put the goal out of reach for many alliance members. He also threatened that the United States would “go its own way” in 2019 if military spending from other NATO countries did not rise.