See more of the story

WASHINGTON – The day before he upended the 2016 election, James Comey, the director of the Federal Bureau of Investigation, summoned agents and lawyers to his conference room. They had been debating all day, and it was time for a decision.

Comey's plan was to tell Congress that the FBI had received new evidence and was reopening its investigation into Hillary Clinton, the presidential front-runner. The move would violate the policies of an agency that does not reveal its investigations or do anything that may influence an election. But Comey had declared the case closed, and he believed he was obligated to tell Congress that had changed.

"Should you consider what you're about to do may help elect Donald Trump?" an adviser asked, Comey recalled recently at a closed meeting.

He could not let politics affect his decision, he replied. "If we ever start considering who might be affected, and in what way, by what we do, we're done," he told the agents.

Fearing the backlash that would come if it were revealed after the election that the FBI had been investigating the next president and had kept it a secret, Comey sent a letter informing Congress that the case was reopened.

What he did not say was that the FBI was also investigating the Trump campaign. Just weeks before, Comey had declined to answer a question from Congress about whether there was such an investigation. Only in March, long after the election, did Comey confirm that there was one.

For Comey, keeping the FBI out of politics is such a preoccupation that he once said he would never play basketball with President Barack Obama because of the appearance of being chummy with the man who appointed him. But the leader of the nation's pre-eminent law enforcement agency shaped the contours, if not the outcome, of the race by his handling of the Clinton and Trump-related investigations.

An examination based on interviews with more than 30 current and former law enforcement, congressional and other government officials found that while partisanship was not a factor in Comey's approach to the two investigations, he handled them in starkly different ways.

In the case of Clinton, he rewrote the script, partly based on the FBI's expectation that she would win and fearing the bureau would be accused of helping her. In the case of Trump, he conducted the investigation by the book, with the FBI's traditional secrecy.

This go-it-alone strategy was shaped by his distrust of senior officials at the Justice Department, who he and other FBI officials felt had provided Clinton with political cover. The distrust extended to his boss, Loretta Lynch, the attorney general, who Comey believed had subtly helped play down the Clinton inquiry.

The examination also showed that at one point, Obama himself was reluctant to disclose the suspected Russian influence in the election last summer, for fear that his administration would be accused of meddling.

Comey has not squarely addressed his decisions. He has asserted that the FBI is blind to partisan considerations. "We just don't care. We can't care," he said at a public event recently.

But circumstances and choices landed him in uncharted and perhaps unwanted territory.

"This was unique in the history of the FBI," said Michael Steinbach, a former senior national security official at the FBI. "People say, 'This has never been done before.' Well, there never was a before."

The FBI's involvement with Clinton's e-mails began in July 2015 when it received a letter from the inspector general for the intelligence community.

The letter said that classified information had been found on Clinton's home e-mail server, which she had used as secretary of state. Comey's deputies quickly concluded that there was reasonable evidence that a crime may have occurred in the way classified materials were handled.

On July 10, 2015, the FBI opened a criminal investigation, code-named "Midyear," into Clinton's handling of classified information. The Midyear team included two dozen investigators led by a senior analyst and by an experienced FBI supervisor, Peter Strzok.

The FBI investigation into Clinton's e-mail server was the biggest political story in the fall of 2015. But something much bigger was happening. And nobody recognized it.

While agents were investigating Clinton, the Democratic National Committee's computer system was compromised. It appeared to have been the work of Russian hackers. The significance of this moment is obvious now, but it did not immediately cause alarm at the FBI.

Months passed before the DNC and the FBI met to address the hacks. And it would take more than a year for the government to conclude that the Russian president, Vladimir Putin, had a plan to steer the outcome of an American election.

Last spring, Strzok reported to Comey that Clinton had clearly been careless, but agents and prosecutors agreed that they had no proof of intent. It became clear to Comey that Clinton was almost certainly not going to face charges.

He quietly began work on talking points, toying with the notion that, in the midst of a bitter campaign, a Justice Department led by Democrats may not have the credibility to close the case and that he alone should explain that decision.

As the Clinton investigation headed into its final months, there were two very different ideas about how the case would end. Lynch and her advisers thought a short statement would suffice. Comey was making his own plans.

In late June, Lynch's plane touched down at Phoenix Sky Harbor International Airport. Bill Clinton was also in Phoenix that day. When the former president learned who was on the plane, his aides say, he asked to say hello.

The meeting was soon the talk of Washington. Lynch said they had only exchanged pleasantries, but Republicans called for her to recuse herself and appoint a special prosecutor. Lynch said she would accept whatever career prosecutors and the FBI recommended on the Clinton case — something she had planned to do all along.

Agents interviewed Hillary Clinton the next day, and the interview did not change the unanimous conclusion that she should not be charged.

Two days later, on the morning of July 5, Comey called Lynch to say that he was about to hold a news conference.

"Any reasonable person in Secretary Clinton's position" should have known better, Comey said. He called her "extremely careless."

Only in the final minutes did he say that "no charges are appropriate in this case."

Days later, Carter Page, an American businessman, gave a speech in Moscow criticizing U.S. foreign policy. Page had been under FBI scrutiny years earlier, as he was believed to have been marked for recruitment by Russian spies. And he was now a foreign policy adviser to Trump.

Page later traveled to Moscow again, raising new concerns among counterintelligence agents. A former senior U.S. intelligence official said that Page met with a suspected intelligence officer and there was information that the Russians wanted to recruit him.

In late July, the FBI opened an investigation into possible collusion between members of Trump's campaign and Russian operatives.

In late August, Comey and his deputies were briefed on a provocative set of documents, from a former British intelligence agent named Christopher Steele, about purported dealings between Russian figures and Trump's campaign. It was increasingly clear at the FBI that Russia was trying to interfere with the election.

Comey became convinced that the public needed to understand the scope of the foreign interference. He proposed writing an op-ed piece that laid out how Russia was trying to undermine the vote.

The president replied that going public would play right into Russia's hands by sowing doubts about the election's legitimacy. Trump was already saying the system was "rigged," and if the Obama administration accused Russia of interference, Republicans could accuse the White House of stoking national security fears to help Clinton.

Comey argued that he had unique credibility to call out the Russians and avoid that criticism, but the White House decided it would be odd for Comey to make such an accusation on his own. The op-ed idea was quashed.

By fall, the gravity of the Russian effort to affect the election had become clear.

Comey and other senior administration officials met twice in early October to again discuss a public statement about Russian meddling. Comey said that it would look too political for the FBI to comment so close to the election.

Then the Daily Mail, a British tabloid, reported that Anthony Weiner, the former New York congressman, had exchanged sexually charged messages with a 15-year-old girl. Prosecutors in Manhattan sought a search warrant for Weiner's computer. Weiner's wife, Huma Abedin, was one of Clinton's closest confidantes, and had used an e-mail account on her server.

FBI agents in New York seized Weiner's laptop in October and agents found messages linked to Clinton.

Comey learned of the Clinton e-mails on Oct. 26 and gathered his team the next day.

Back in July, he had told Congress that the Clinton investigation was closed. What was his obligation, he asked, to acknowledge that this was no longer true? The next morning, Oct. 28, Comey wrote to Congress that the investigation had been reopened.

On Nov. 6, two days before Election Day, Comey sent a second letter to Congress. "Based on our review," he wrote, "we have not changed our conclusions."

Comey did not vote, records show. Clinton blamed him for her loss, saying that his inquiry "stopped our momentum."

History is likely to see Russian influence as the more significant story of the 2016 election. Last month, Comey acknowledged for the first time that the FBI was investigating members of the Trump campaign for possible collusion with Russia.