The deal paving the way for the $17.5 billion merger between NStar and Northeast Utilities was set in motion more than a year ago, when executives from the two companies began meeting privately with officials from the Patrick administration and Attorney General Martha Coakley’s office.

The private talks, which began in January 2011, ran parallel to the very public review of the merger being conducted by the state Department of Public Utilities. The DPU review included public hearings and mounds of legal briefs, but sources say the real deal was being pieced together in the private meetings, much as attorneys often try to settle cases before they go to trial.

Utility officials ironed out rate-relief questions with the attorney general, while separately hammering out agreements on Cape Wind and other renewable energy issues with officials from the Patrick administration. Those deals now go to the DPU for final approval, probably next month.

Sources say the talks with Coakley’s aides were fairly straightforward, reflecting ideas outlined publicly in the DPU proceedings. The talks with the Patrick administration were more politically sensitive, revolving around contentious issues only hinted at in the public hearings.

Officials from the Patrick administration and the two utilities declined to say who initiated their talks or to outline their opening positions. One source said the Cape Wind negotiations were resolved fairly early in 2011, but added that Cape Wind was part of a broader package of concessions unveiled by Gov. Patrick at a press conference last month.

NStar agreed to sign a 15-year contract with Cape Wind to purchase 129 megawatts of power, or about 27.5 percent of the project’s output. The price will be roughly the same as National Grid agreed to pay in late 2010 for 50 percent of the output. That price, initially 18.7 cents a kilowatt hour, or double current prices at the time, will rise 3.5 percent a year. Officials estimated the above-market cost of the deal would be between $500 million and $800 million, but that figure may be higher now because electricity prices have fallen. Like National Grid, ratepayers will pay NStar 4 percent of the contract’s value just for doing the deal.

A source said NStar wanted to buy less of Cape Wind’s output and Patrick aides wanted the utility to buy more. The two sides ultimately settled somewhere in the middle, the source said.

NStar also agreed to increase its target for energy efficiency savings, launch two new solar developments, develop a system for charging electric cars, and pledge not to make any effort to use large-scale hydropower to meet its renewable energy obligations under the state’s Green Communities Act.

The hydropower language was apparently inserted into the agreement out of fear that NStar might lobby the Legislature to change the law so it could use cheap Canadian hydropower to meet its renewable energy goals. Tom May, the CEO of NStar, sits on the board of a business group pushing for changes in the law. The group claims the changes would allow the state to reach its clean energy goals more quickly and at much less cost.

But Mark Sylvia, commissioner of the Department of Energy Resources, said allowing hydropower to be used to meet renewable energy obligations would end up hurting efforts to bring additional wind and solar power online. “It would have a huge negative impact on other renewables,” Sylvia said.

The private meetings with the Patrick administration were held once a month at either NStar’s headquarters in Boston or at the state’s Executive Office of Energy and Environmental Affairs on Beacon Hill, a source said.

NStar’s May never participated in the meetings, but Rick Sullivan, the governor’s secretary of energy and environmental affairs, did sit in occasionally. Sullivan’s general counsel, Gary Davis, and his undersecretary, Barbara Kates-Garnick, played major roles. Another key player for a time was Phil Giudice, who was head of DOER and then undersecretary of energy before leaving last year to work at Rhumb Line Energy, a consulting firm that also employs Ian Bowles, the former secretary of energy and environmental affairs.