As a child, Colin Sullivan (Matt Damon) had been introduced to organized crime by Irish-American mobster Frank Costello (Jack Nicholson) in the Irish neighborhood of South Boston. Over the years, Costello grooms him to become a mole inside the Massachusetts State Police, until Sullivan is accepted into the Special Investigations Unit, which focuses on organized crime.
Before graduating from the police academy, Billy Costigan (Leonardo DiCaprio) is recruited by Captain Queenan (Martin Sheen) and Staff Sergeant Dignam (Mark Wahlberg) to go undercover, as his family ties to organized crime make him a perfect infiltrator. He drops out of the academy and does time in prison on a fake assault charge to increase his credibility.
Each man infiltrates his respective organization, and Sullivan begins a romance with police psychiatrist Madolyn Madden (Vera Farmiga). Costigan is seeing her under the terms of his probation, and they begin a relationship, too. After Costello escapes a sting operation, each mole becomes aware of the other's existence. Sullivan is told to find the "rat" and asks Costello for information to identify the informer.
Costigan follows Costello into a porn theater, where Costello gives Sullivan an envelope containing personal information on his crew members. Costigan chases Sullivan through Chinatown. When it is over, neither man knows the other's identity. Sullivan has Queenan tailed to a meeting with Costigan on the roof of a building. Queenan orders Costigan to flee while he confronts Costello's men alone. The men then throw Queenan off the building to his death. When they exit, Costigan pretends he has come to join them. Television news reports that crew member Delahunt (Mark Rolston) has been a Boston Police Department undercover cop, but Costello believes it to be a lie probably designed to lull him into a sense of security. Dignam resigns rather than work with Sullivan, who he suspects is the mole after he is asked why he had Queenan followed.
Using Queenan's phone, Sullivan reaches Costigan, who refuses to abort his mission. Sullivan learns from Queenan's diary of Costello's role as an informant for the FBI, causing him to worry about his own identity being revealed. With Costigan's help, Costello is traced to a cocaine drop-off, where a gunfight erupts between Costello's crew and the police, which results in most of the crew being killed. Costello, confronted by Sullivan, admits he is an FBI informant. Costello tries to shoot Sullivan, but Sullivan shoots him multiple times. With Costello dead, Sullivan is applauded the next day by everyone on the force. In good faith, Costigan comes to Sullivan for restoration of his true identity and to be paid for his work, but notices the envelope from Costello on Sullivan's desk and flees, finally realizing Sullivan is the enemy. Fearing retaliation, Sullivan erases Costigan's records from the police computer system.
Sullivan is unaware that Madolyn had an affair with Costigan when she tells Sullivan that she is pregnant. Later, Sullivan finds her listening to a CD from Costigan containing incriminating recorded conversations between Costello and Sullivan. Sullivan unsuccessfully attempts to assuage her suspicions. He then contacts Costigan, who reveals that Costello recorded every one of their conversations, that Costello's attorney left Costigan in possession of the recordings, and that Costigan intends to implicate Sullivan. The two agree to meet at the building where Queenan died.
On the roof, Costigan catches Sullivan off-guard and handcuffs him. As Costigan had secretly arranged, Trooper Brown (Anthony Anderson) appears on the roof as well. Shocked, Brown draws his gun on Costigan, who attempts to justify his actions by exposing Sullivan as Costello's mole. Costigan asks Brown why Dignam did not accompany him as Costigan had requested, but Brown does not answer. Costigan leads Sullivan, his hostage, to the elevator. When it reaches the ground floor, Trooper Barrigan (James Badge Dale) shoots Costigan in the head, then shoots Brown, and afterward reveals to Sullivan that Costello had more than one mole in the police. Sullivan then shoots and kills Barrigan. At state police headquarters, Sullivan identifies Barrigan as the mole and recommends Costigan for the Medal of Merit.
At Costigan's funeral, Sullivan notices that Madolyn is tearful. As they leave the gravesite Sullivan attempts to talk to her but she ignores him. When Sullivan returns to his apartment, he is ambushed by Dignam, who shoots and kills him as he enters.
Output: What does the envelope that Costello gives Sullivan contain?