Eric O'Neill is a young FBI employee assigned to work undercover as a clerk to Robert Hanssen, a senior agent he is told is suspected of being a sexual deviant. Hanssen has been recalled to FBI headquarters ostensibly to head up a new division specializing in Information Assurance.
Initially, Hanssen insists on a strict formality between the two men. He frequently rails against the bureaucracy of the FBI and complains that only those who regularly "shoot guns" are considered for senior positions instead of those, like himself, who are involved in vital national security matters. He calls the bureau's information technology systems antiquated and laments the lack of coordination and information exchange with other intelligence agencies.
Eventually, Hanssen becomes a friend and mentor to O'Neill and takes a personal interest in him and his wife Juliana, who is suspicious of Hanssen and resents his intrusions. A devout Catholic who is also a member of Opus Dei, Hanssen urges O'Neill, a lapsed Catholic, and his secular East German-born wife to become active churchgoers.
O'Neill finds no evidence of Hanssen leading a secret double life and develops a growing respect for his boss, so he confronts his handler in the undercover assignment, Kate Burroughs, and she admits that the sexual deviance allegations are only a secondary consideration. Hanssen is suspected of spying for the Soviet Union and Russia for years and being responsible for the deaths of agents working for the United States. He learns that FBI Director Louis Freeh is personally leading the investigation.
While the FBI could arrest Hanssen under lesser charges, they want to catch him in an act of espionage, so they can threaten him with the death penalty for treason and possibly induce him to divulge the information he has compromised. O'Neill is ordered to obtain data from Hanssen's Palm Pilot and keep him occupied while FBI agents search his car and plant covert listening devices in it.
The tracking devices in Hanssen's car cause interference with the radio, which makes Hanssen suspicious. He also wonders why he was placed in an isolated position in the FBI only a few months before he's scheduled to retire. He tells O'Neill he believes he is being surveilled by Russian agents. The FBI intercepts a message he sends to his Russian handlers saying he will not provide any more information. O'Neill persuades Hanssen that he is not being trailed by the Russians or by him on behalf of the FBI. With his confidence restored, Hanssen makes one last dead drop of stolen information, and the FBI catches him in the act.
Although he is assured promotion, O'Neill is discouraged with the toll the case has taken on his marriage and opts to leave the agency to work in the private sector. After he tells Burroughs of his intentions, she wishes him well, then O'Neill leaves his office with his belongings. He manages to catch an elevator before it descends, but is taken aback to see Hanssen inside, in handcuffs flanked by Plesac and an officer. Their eyes meet, and Hanssen tells O'Neill "Pray for me." The doors close and the scene fades to black.