BP will pay $18.7 billion to settle all civil claims stemming from the 2010 Gulf of Mexico spill under an agreement with the Justice Department and state and local governments that were affected by the largest oil spill in United States history.

"[A]fter productive discussions with BP over the previous several weeks, we have reached an agreement in principle that would justly and comprehensively address outstanding federal and state claims, including Clean Water Act civil penalties and natural resource damages," Attorney General Loretta Lynch said Thursday in a statement. "BP is also resolving significant economic claims with the impacted state and local governments."

The agreement will be worked into a consent decree that must be approved by a federal court. The British oil giant already has spent more than $40 billion in legal fees and civil and criminal penalties from the Deepwater Horizon incident, in which 11 workers were killed and 4 million barrels of oil spilled into the Gulf.

For BP, the settlement reflects a desire to move on from a lengthy litigation process.

"We have made significant progress, and with this agreement we provide a path to closure for BP and the Gulf. It resolves the company's largest remaining legal exposures, provides clarity on costs and creates certainty of payment for all parties involved," said Carl-Henric Svanberg, BP's chairman. "In deciding to follow this path, the board has balanced the risks, timing and consequences associated with many years of litigation against its wish for the company to be able to set a clear course for the future."

Gulf state lawmakers said they were happy that most of the legal proceedings from the disaster are now resolved. Sen. David Vitter, R-La., noted that the settlement, if approved, would send $6.8 billion to Louisiana, bringing total payments from the spill to $10 billion.

"Five years ago, the BP explosion and spill took the lives of 11 men and devastated our coast, our communities and our economy," Vitter said in a statement. "Properly holding BP accountable and making sure that these monies go toward restoring our coast have been top priorities, and moving forward I'll make sure we put shovels in the ground soon using this money wisely according to the detailed plan I've laid out."

BP will pay the federal government $5.5 billion in Clean Water Act penalties, paid out over 15 years. It also will pay $7.1 billion over 15 years to five Gulf states — Louisiana, Alabama, Mississippi, Texas and Florida — for natural resources damages. BP will pay $4.9 billion to those states over 18 years for other claims and $1 billion to 400 local governments.

Environmental and conservation groups praised the announcement, saying it would bring dollars to help restore a region that's still recovering.

"From the BP oil disaster comes an opportunity to chart a new future for the Gulf. While a settlement is a vital step in the process to restore the Gulf, there is still much work to be done. Now our state and federal decision-makers must commit themselves to a transparent and science-based approach to ecosystem restoration," Bethany Carl Kraft, director of Ocean Conservancy's Gulf restoration program, said in a statement.