Updated on December 20 at 7:35 p.m. ET

Secretary of Defense James Mattis is resigning over conflicts with President Donald Trump concerning American policy overseas—the highest-profile official to quit the administration over disagreements of principle with the president.

In a resignation letter, Mattis laid out a series of differences with Trump, who he said deserved to have a secretary of defense who was aligned with him. Mattis cited the importance of international alliances and partnerships, constraining Russia, and maintaining an American military presence overseas. By the standard of resignation letters, which typically flatter a president and avoid direct conflict, the missive is a scathing rebuke.

“I believe we must be resolute and unambiguous in our approach to those countries whose strategic interests are increasingly in tension with ours,” Mattis wrote. “It is clear that China and Russia, for example, want to shape a world consistent with their authoritarian model—gaining veto authority over other nations’ economic, diplomatic, and security decisions—to promote their own interests at the expense of their neighbors, America and our allies.”

The news comes a day after Trump said that U.S. troops would leave Syria, a move the Pentagon opposed. The president announced Mattis’s exit in a tweet. “General Jim Mattis will be retiring, with distinction, at the end of February, after having served my Administration as Secretary of Defense for the past two years,” Trump wrote. “A new Secretary of Defense will be named shortly. I greatly thank Jim for his service!”

The question now facing both U.S. and foreign leaders is plain: Who will constrain Trump? Mattis was one of the few remaining officials who seemed able to twist the president’s arm out of his most rash and unwise decisions and impulses.

“Mattis is the last brake on a president that makes major life-and-death decisions by whim without reading, deliberation, or any thought as to consequences and risks,” said a senior U.S. national-security official on Thursday, who spoke on the condition of anonymity in order to talk freely. “The saving grace is that this president has not been tested by a major national-security crisis. But it will come, and when it does, we are fucked.”

While the administration has seen heavy turnover, there have been few resignations on principle, despite Trump’s frequent departures from norms. It’s also exceedingly rare for senior officials in any administration to resign in protest. There had been frequent speculation that Mattis might leave the Pentagon after some of Trump’s previous actions. The economic adviser Gary Cohn resigned over his disagreements with Trump about tariffs.

Mattis’s resignation brings to an end the single strangest stint in Trump’s inner orbit. Mattis was always a misfit: a westerner in a team of East Coast types, a veteran among mostly civilians, an ascetic among the profligate, an institutionalist among nihilists, a lifelong public servant among political climbers and businessmen, an aphorist serving a logorrheic president.

Yet Mattis put together one of the steadiest and most successful tenures of any Trump official, and managed to stay largely untainted by the dysfunction that has marked the administration and tarnished many aides. Mattis might be the only top official to emerge with his reputation enhanced. Nonetheless, the chaos of the Trump administration and his own differences of opinion with the president mean that Mattis’s accomplishments largely consisted of protecting the status quo—he was a defense secretary who spent most of his term on the defensive.

Mattis retired from a decorated career in the Marines—capped by a stint leading Central Command—in 2013, but he was pulled out of retirement to join the Trump administration in late 2016. The president-elect, spurned by many traditional Cabinet candidates, sought current and ex-military leaders, including Mattis, John Kelly, and Michael Flynn, to join his team. In Mattis’s case, he required a congressional waiver to serve as the head of the Pentagon, because he hadn’t been retired the minimum seven years.

Trump was particularly drawn to Mattis, who had an archive of colorful quotes—“I come in peace. I didn’t bring artillery. But I’m pleading with you, with tears in my eyes: If you fuck with me, I’ll kill you all”—and a colorful nickname, “Mad Dog.” It took months of Trump using the nickname before Mattis made clear he detested it.

Another of Mattis’s nicknames was “The Warrior Monk,” and in the Trump administration, it was the monastic, rather than militaristic, Mattis who predominated. Inevitably, this brought the Pentagon into conflict with the White House. Mattis reportedly stuck with the job out of a sense of duty, hoping that his presence could avoid catastrophe and war.

Read: James Mattis has somehow stayed on Trump’s good side

A prime example of the tension was NATO. Mattis, despite his long military career, is a believer in diplomacy, telling Congress in 2013, “If you don’t fund the State Department fully, then I need to buy more ammunition ultimately.” But Trump had little understanding of or use for the alliance. According to Bob Woodward’s Fear, Mattis joined with several other advisers in February 2017 to convince the president of its importance.

“If you didn’t have NATO, you would have to invent it,” Mattis said, according to Woodward. “There’s no way Russia could win a war if they took on NATO.” Trump seemed to be persuaded, Woodward reported. “You can have your NATO,” the president told Mattis. The administration would support the alliance, “but you become the rent collector.”

Mattis became a sort of ambassador at large to Europe, repeatedly placed in the role of reassuring allies about America’s steadiness and fidelity to its commitments, and cleaning up after Trump’s lovefests with Russian President Vladimir Putin.

Elsewhere around the globe, Mattis exerted a similarly constraining influence on Trump. He worked with other aides to try to convince Trump not to end a free-trade agreement with South Korea, worked to convince Trump that a military strike against North Korea was unwise, and argued forcefully against American withdrawal from the Korean peninsula.

Mattis pursued a similar strategy in Syria: neither pull back nor escalate. According to Woodward, Trump called Mattis in April 2017, following a chemical attack by the Syrian regime, and demanded that the U.S. military kill Bashar al-Assad. Woodward reports that Mattis told an aide, “We’re not going to do any of that. We’re going to be much more measured.” In the end, Trump launched only limited air strikes. A year later, when the president announced that he wanted a full withdrawal of American troops from Syria, military advisers talked him down from that, too.

In Afghanistan, Mattis was actually able to convince Trump to send more troops. But the secretary was not always able to prevail. Though he was an outspoken critic of the Iranian regime, he came to a grudging belief that the nuclear deal negotiated under former President Barack Obama was worth preserving. Along with then–Secretary of State Rex Tillerson and others, Mattis helped convince Trump not to trash the Iran deal, but ran out of luck in spring 2018, when the president tore it up.

Mattis and Trump viewed matters differently stateside, too. A notable flare-up came over allowing transgender members of the armed services. Mattis sided with an Obama-era decision to open the branches up to transgender members. Trump was opposed, and while Mattis was on vacation, Trump tweeted that he was going to bar them from serving. Thanks to Trump’s failure to consult with the Joint Chiefs of Staff and the total lack of specificity in his announcements, the Pentagon was able to postpone the order for months, ultimately landing on a result that curtailed transgender Americans’ service but granted Mattis leeway to implement rules as he saw fit.

Following a visit to France for Bastille Day in 2017, Trump became entranced by the idea of a massive military parade through Washington. Mattis’s Pentagon, skeptical of the idea, managed to slow-walk it; in summer 2018, someone leaked a $92 million estimate for the cost of the display, though Mattis publicly disputed that. Trump eventually kicked the can down the road, postponing the event until at least 2019.

Read: Trump gives an order too vague for his generals to obey

How was Mattis able to survive so long despite his differences with Trump? Woodward offered this formula: “Avoid the confrontation, demonstrate respect and deference, proceed smartly with business, travel as much as possible, get and stay out of town.” Mattis benefited from Trump’s tendency to view the military as a solution to all problems, which gave the Pentagon easy chances to shine. Mattis was also highly popular—with military leaders, members of Congress, and the pundit class—which gave him some protection, and fairly press-averse, minimizing the risk that he’d steal the spotlight from the president and thus alienate him.

For a long time, this worked. And then it didn’t. By summer 2018, there were reports that Mattis was more and more sidelined from White House policy discussions. Tillerson, Mattis’s ally, had finally been fired in March, and Trump had a much better rapport with his replacement, Mike Pompeo. The State Department began to eclipse the Pentagon.

Then came the Woodward book, which depicted Mattis as not only slow-walking the president’s directives but also being privately dismissive and sneering about him, saying he had the understanding of a fifth or sixth grader. Mattis denied the quotes attributed to him in the book, but Woodward has a reputation for accuracy, and his account echoed the evident tension between the defense secretary and the president.

In mid-October, Trump appeared on 60 Minutes, where he told Lesley Stahl that though “I like General Mattis,” he believed he knew more about NATO than his defense secretary. Trump hinted at Mattis’s impending exit as well.

“I think he’s sort of a Democrat, if you wanna know the truth,” Trump said. “But General Mattis is a good guy. We get along very well. He may leave. I mean, at some point, everybody leaves. Everybody. People leave. That’s Washington.”

Nonetheless, Mattis had managed to outlast a secretary of state; two national-security advisers; two White House chiefs of staff; secretaries of health and human services, the interior, and veterans’ affairs; and heads of the CIA and the Environmental Protection Agency, among others. In part because Mattis seemed to spend so much of his time working to constrain Trump, it’s not yet clear what legacy he will leave at the Pentagon.

Mattis’s departure will send an immediate shudder through both Washington and foreign capitals. The president will be hard-pressed to find a replacement who will instill confidence in Congress and the ranks of the military while still maintaining an effective relationship with the White House. Meanwhile, Mattis’s exit could even further strain relations with American allies, who have seen him as a calming influence and for whom he has often served as a direct conduit. In the end, Mattis proved to be the Trump administration’s most effective diplomat, whether negotiating the fraught internal battles of the administration or speaking to foreign leaders.

The friction between Mattis and Trump no doubt wore on both the president and the secretary. According to Woodward, Mattis once told friends, “Secretaries of defense don’t always get to choose the president they work for.” As Mattis’s resignation demonstrates, however, he eventually realized he could choose not to work for Trump.