WASHINGTON — The country's intelligence chief was on the verge of resigning at the end of last year over his frustrations with President Donald Trump but was talked out of it by his closest ally in the administration, Vice President Mike Pence, according to current and former senior administration officials.

Among the tensions the officials said have marred the relationship between the president and Director of National Intelligence Dan Coats: Trump pushed Coats to find evidence that former President Barack Obama wiretapped him; he demanded Coats publicly criticize the U.S. intelligence community as biased; and he accused Coats of being behind leaks of classified information. More recently Trump also fumed to aides after Coats publicly defended the importance of the North Atlantic Treaty Organization in countering Russia's aggression, officials said.

But the tipping point for Coats came in December with Trump's abrupt decision to withdraw all U.S. troops from Syria, and the contentious departure of former Defense Secretary James Mattis after protesting the policy, according to the current and former officials. The vice president, who has repeatedly played the role of envoy between Trump and Coats, convinced his longtime Indiana friend to stay until at least this summer, the officials said.

Similarly, whenever Trump is souring on the DNI he privately calls "Mister Rogers" — because he won't implement a directive or has left the impression he thinks the president is irrational — Pence has encouraged Trump to stick with Coats, according to the current and former officials.

Coats — who served as a Republican congressman and senator from Indiana and later as ambassador to Germany — was confirmed as the DNI two years ago with bipartisan support after being recruited to the job by Pence. But the dynamic between Coats and Trump quickly came to mirror the president's relationship with the U.S. intelligence community as a whole, which had been fraught from the start.

Coats has tried to navigate the national security approach of a president officials say sees intelligence professionals as simply extra advisers whose advice he's free to ignore. At times that has meant public pushback from Coats, such as when he issued a pointed statement defending U.S. intelligence officials after Trump stood next to Russian President Vladimir Putin last July and said he believed Putin's denial of election interference instead of the U.S. intelligence community's unanimous assessment.

Other times Coats has taken steps that appeared designed to appease Trump. Early last year, for instance, he carried out Trump's directive to develop new procedures for the so-called "unmasking" of the names of U.S. citizens who indirectly turn up in intelligence gathering. Trump and some of his leading Republican allies in Congress had attacked the Obama administration's use of unmasking, which is common in any administration, as improper and evidence of bias against Trump officials.

Officials said the dynamic between Trump and Coats has stabilized for now. Yet they note that Trump could decide the date of Coats's departure at any time. And while Pence ultimately convinced Coats to stay until the summer, no final decision has been made, according to the current and former senior administration officials.

In a statement to NBC News, Coats said, "I am focused on doing my job, and it is frustrating to repeatedly be asked to respond to anonymous sources and unsubstantiated, often false rumors that undercut the critical work of the Intelligence Community and its relationship with the President. I am proud to lead an IC singularly focused on the vital mission of providing timely and unbiased intelligence to President Trump, Vice President Pence and the national security team in support of our nation’s security."

A spokesperson for Pence declined to comment.

Director of National Intelligence Dan Coats testifies on worldwide threats during a Senate Intelligence Committee hearing on Capitol Hill on Feb. 13, 2018. Saul Loeb / AFP - Getty Images file

Part of Pence's argument to Coats in December was that the resignation of another top national security official on the heels of Mattis's contentious exit would be seen as a major upheaval in the Trump team, current and former senior administration officials said.

It's akin to the role White House officials say Pence played in giving former Secretary of State Rex Tillerson a "pep talk" to dissuade him from resigning just six months into the job. And it was one of many times that Pence has smoothed over rough patches in the relationship between Trump and Coats, according to the current and former officials, who said that effort began almost immediately after Coats was confirmed.

Pence has talked Coats through his frustrations with Trump by "encouraging him to continue the work he's doing and reiterating that he has the support of the White House," according to one White House official. The vice president's message to Coats has been to "hold the line, and to keep doing the work he's doing," the official said.

The vice president's interference sheds light on how the mild-mannered Coats, who had no relationship with Trump before entering his administration, has survived the president's repeated ire, while some of his colleagues — including Mattis, Tillerson and former National Security Adviser H.R. McMaster — did not.

Trump and the Obama 'wire tap'

The relationship between Trump and Coats showed strains early on, according to current and former officials.

Twelve days before Coats was sworn in by Pence as DNI in March 2017, Trump accused Obama of wiretapping him during the 2016 campaign. "Just found out that Obama had my 'wires tapped' in Trump Tower just before the victory," he wrote on Twitter on March 4. Coats would find it was a subject Trump raised repeatedly.

Terrible! Just found out that Obama had my "wires tapped" in Trump Tower just before the victory. Nothing found. This is McCarthyism! — Donald J. Trump (@realDonaldTrump) March 4, 2017

Before Coats's confirmation Trump had also already publicly disagreed with U.S. intelligence officials' assessment on Russia's interference in the 2016 election, accused them of being politically motivated against him and even suggested their tactics were reminiscent of Nazi Germany.

In his new role, Coats was responsible for walking a president he hardly knew through his daily intelligence briefing. He quickly found his boss had a short attention span for the information he was providing, current and former administration officials said. Coats struggled with how to respond when Trump veered off on unrelated tangents or bluntly disagreed with the intelligence he presented — as he often did, the current and former senior administration officials said.

Coats found it particularly hard to hide his exasperation with Trump's insistence in the weeks after taking office that Obama had wiretapped him during the 2016 campaign, according to the officials. Over and over again Trump raised the issue, and over and over Coats told him he wasn't wiretapped, officials said, but the president didn't want to hear it.

"It was a recurring thing and began early on," a senior administration official who observed the exchanges said. "You could tell that Coats thought the president was crazy."

The president remained convinced he'd been a target of FBI surveillance on former Trump campaign aide Carter Page, and that there had been even more surveillance ordered against him, officials said.

Trump believed it was Coats's responsibility to come up with conclusive proof he had been wiretapped, make it public and criticize the alleged eavesdropping, officials said. He repeatedly demanded that Coats get the word out that the U.S. intelligence community was biased against him, officials said.

In addition to Coats, Obama administration officials, including former DNI James Clapper, have denied Trump's surveillance claim.

Yet even now the president still appears unconvinced. Earlier this month he wrote on Twitter, citing Fox News commentary: "New evidence that the Obama era team of the FBI, DOJ & CIA were working together to Spy on (and take out) President Trump, all the way back to 2015."

Complicating Coats's relationship with Trump, said officials, was that his opinions of his boss were easy for the president to read in his facial expressions.

Laughing at Trump?

Trump has opted not to receive the standard presidential intelligence briefing every day, instead taking it more intermittently. So Coats came to spend less time with Trump than his predecessors did with George W. Bush and Obama. (The DNI position was created following the 9/11 terrorist attacks.)

Tensions between Trump and Coats were manageable as long as the dynamic played out behind closed doors, officials said.

Coats’s motto as DNI has been "seek the truth, speak the truth" — a phrase he regularly repeats to underscore "his commitment to provide unbiased timely intelligence to policymakers," according to a senior intelligence official.

However, Coats didn't speak publicly as often as other members of Trump's national security team who clashed with the president, such as Tillerson.

That changed last July when, in a live, televised interview with Andrea Mitchell of NBC News at the Aspen Security Forum, Coats appeared to laugh at Trump when he was told in real time the president had invited Putin to Washington, D.C. Trump believed Coats had mocked him and was angry, say officials.

Afterwards, Pence stepped in to calm down Trump and counsel Coats, who then personally apologized to Trump, according to officials, and issued a public statement saying, "My admittedly awkward response was in no way meant to be disrespectful or criticize the actions of the president."

Coats's advocates inside the administration have continued to defend him to the president, officials said, including when Trump insists his top intelligence officials must be behind leaks out of the Pentagon and intelligence agencies.

One former official described the fraying of the relationship between Coats and Trump as "a slow burn" that picked up speed after that July interview. Another official said Coats at that time also privately considered whether he should remain in the administration, with Pence assuring him he should.

The dynamic between the president and Coats took another hit after Trump's Dec. 19 announcement that the U.S. would withdraw all of its troops from Syria.

Mattis's resignation mirrored how Coats felt about the decision, officials said, and Coats had come to see the defense secretary as an ally. "He and Mattis bonded in many ways," one former senior administration official said of Coats. After Mattis's resignation, Coats "struggled with what the right thing to do was," a second official said.

A month later, Trump was upset with Coats for publicly touting the value of NATO, particularly in keeping a check on Russia, officials said. He also fumed over his assessments before Congress that Iran is complying with the nuclear deal Trump withdrew from, and that North Korea is unlikely to give up its nuclear weapons program. The president viewed Coats's comments as undermining his foreign policy, including his diplomatic talks with North Korean leader Kim Jong Un.

"Perhaps Intelligence should go back to school!" he wrote on Twitter.