To settle charges related to the packaging, marketing, sale, and issuance of residential mortgage-backed securities (RMBS), Citigroup will pay a total of $7 billion, including a record $4 billion fine to the Department of Justice (DOJ).

The deal was reached after months of negotiations – Citi’s first offer to settle was reportedly $363 million, a fraction of the final total – between the bank and the RMBS Working Group, a division of the Financial Fraud Enforcement Task Force consisting of state and federal authorities and financial regulators, and reported preparation by the DOJ to file a civil suit if a satisfactory settlement was not reached.

According to the settlement’s statement of facts, Citigroup securitized and sold RMBS with underlying mortgage loans that it knew had material defects between 2003 and 2008. Citi employees had knowledge that “significant percentages” of the loans reviewed in due diligence had material defects and yet the company securitized the loan pools with the defective loans and sold the resulting RMBS to investors for billions of dollars.

The DOJ highlighted internal company e-mails that documented both awareness of the problem and a failure to heed warnings. In one message, a trader wrote that he “went thru Diligence Reports and think that we should start praying … I would not be surprised if half of these loans went down,” adding, “It’s amazing that some of these loans were closed at all.”

The consent decree with the DOJ includes a record-setting $4 billion penalty based on the agency’s allegations that Citi violated the Financial Institutions Reform, Recovery and Enforcement Act (FIRREA) as well as $208.25 million to the Federal Deposit Insurance Corporation (FDIC) for securities claims. Five states will also receive funds: Citi agreed to pay $102.7 million to California, $92 million to New York, $45.7 million to Massachusetts, $44 million to Illinois, and $7.35 to Delaware.

The remaining $2.5 billion was promised to consumers. Citi will modify loan terms for struggling borrowers, provide donations to legal aid groups, and offer assistance with down payments. Because the bank does not service enough troubled loans to meet the settlement’s terms, it also agreed to pay $180 million to finance affordable rental housing in “high cost of living areas,” a move the DOJ said was intended to fill a gap in locations where cities and states cut funding for affordable housing in the wake of the financial crisis.

Citigroup’s required consumer relief must be completed by 2018.

To read the Statement of Facts, click here.

To read the settlement agreement, click here.

Why it matters: The settlement arguably is the product of a hard-line approach taken by the DOJ, providing the largest civil penalty ever. U.S. Attorney General Eric Holder characterized Citi’s conduct as “egregious,” adding that “the bank’s activities contributed mightily to the financial crisis that devastated our economy in 2008.” Holder also stated that the deal does not absolve Citi or its employees of possible criminal charges.