It should come as no great surprise that the largest one-year spending cut in American history came down to the wire. Leaders on both sides were under enormous pressure to hold out for everything they could.

The overriding question for months had been whether Speaker John Boehner could thread the needle between tea party demands that he defund health care and slash spending to the bone and the reality of having to win approval of a Democratic White House and Senate.

By waiting until the last minute to assent, the Ohio Republican squeezed the Democrats for every last penny and conceivable concession, while a host of Republicans took to the Senate floor and the airwaves in the final hours to give him cover to cut the final deal.

Boehner succeeded in getting President Barack Obama and Senate Democrats to accept $38 billion in spending cuts — several billion more than Boehner’s leadership team initially proposed. And he did it without shutting down the government, a move that polls showed was politically radioactive with the majority of the public and that Boehner himself had warned would hurt Republicans.

The deal was sealed in a dramatic 10:30 p.m. handshake between three top staffers who had negotiated around the clock in a fourth-floor Capitol office.

David Krone, chief of staff for Senate Majority Leader Harry Reid (D-Nev.), White House liaison Rob Nabors and Barry Jackson, Boehner’s chief of staff, reached agreement just as Obama and Reid were anxiously calling for an update, according to a senior Democratic leadership aide.

They were racing against the clock. The federal government would start shutting down at 11 p.m. unless at least one chamber had passed a bill.

They didn’t even have time to work out the fine print. Appropriations staffers would spend the weekend writing a bill and working out the last details after the House and Senate sped a short-term funding extension to the president’s desk.

The framework for the deal had largely been reached a few hours before the handshake, after a daylong flurry of offers, counter-offers and phone calls between Boehner and Obama, and nearly nonstop press conferences by Senate Democrats accusing Boehner of threatening to shut down the government to defund Planned Parenthood.

Boehner in the end signaled he could give up the language barring federal funding of Planned Parenthood in return for a separate vote on the Senate floor, and Democrats agreed to an extra $500 million in spending cuts from an unspent transportation account as well as several other spending changes.

After the handshake, Boehner, who was speaking to his Conference members in the basement, told them a deal had been struck, which was met with cheers.

Obama went to the cameras and hailed the deal, talking like a born-again deficit hawk and co-opting Republican talking points. On Sunday, Obama adviser David Plouffe said the president would propose a new deficit-cutting plan this week that would tackle Medicare and Medicaid costs, in what amounts to a do-over for his 2012 budget and a sign of just how much the agenda has shifted in Washington.

The final deal cut spending $78.5 billion below Obama’s original budget request for the year, about $38 billion below current spending levels.

Democrats had opened the bidding months ago with a plan to have the government operate at current levels but then began offering progressively larger cuts, first $4 billion, then $10 billion, $21 billion and finally $33 billion. While the Democrats kept trying to smoke him out, Boehner refused to commit to a number, linking any final spending deal to the numerous riders sought by the House. He figured Democrats would have to keep upping the ante, with public concern over massive deficits on the rise.

“Democrats kept moving in our direction without Boehner being forced to reveal his hand because they were negotiating out of a position of weakness,” a senior GOP aide said.

But the Speaker had troubles of his own. Fifty-four Republicans had abandoned the last short-term bill he negotiated, and more threatened to jump if he compromised too much on a final deal. Boehner needed to have something significant to bring back to his base or he would have a revolt on his hands.

Boehner’s push late last week for a one-week CR that cut $12 billion from domestic spending and included full-year funding for the Defense Department knocked Democrats off-balance, as they struggled to find a way to avoid looking like they were blocking troop paychecks.

An Obama veto threat followed, with the president making it clear that it was now or never for a full-year deal.

At the same time, Boehner was trying to prepare his Conference for a final deal, warning that a shutdown would help Democrats, and cost money to boot. While Boehner didn’t explicitly draw a connection, his allies made the case that the real battle was the upcoming debate over House Budget Chairman Paul Ryan’s (R-Wis.) plan for $6.2 trillion in spending cuts over the next decade.

As the final deal was unveiled, Senate Democrats and the White House touted their victories, but they were largely celebrating what they didn’t lose.

They succeeded in preserving a significant chunk of domestic discretionary spending by agreeing to cuts in mandatory spending accounts like transportation funding. Democrats said the final deal cut $17.8 billion from mandatory spending while protecting funding for Head Start, the health care reform law and energy research, although by Sunday evening the details had not been released.

A senior Democratic aide said the prospects for a deal improved significantly Thursday when Boehner gave up most of the House policy riders, from defunding parts of the health care and Wall Street reform laws to prohibiting the Environmental Protection Agency from regulating greenhouse gases. Democrats agreed to a proposal by Senate Majority Whip Dick Durbin (D-Ill.) to allow votes on the Senate floor on some riders, votes that are expected to fail but may be politically difficult for the president’s party.

In other cases, they agreed to requirements for studies or audits, such as a detailed accounting of health care waivers the administration has granted.

Boehner also gave up the most contentious rider — the provision to bar federal funding of Planned Parenthood — which threatened to scuttle the whole deal.

Jackson, Boehner’s chief of staff, had told Democrats the Planned Parenthood provision was non-negotiable. The president and Reid, during negotiations in the Oval Office on Thursday night, were equally inflexible.

Republicans at one point proposed requiring Democrats to strip the Planned Parenthood rider from the bill on the Senate floor and send the whole bill back to the House. The GOP wanted a guarantee that Obama would sign the overall bill if the Senate was unable to strip the provision. Democrats refused.

Boehner ultimately relented on the issue, but he won several significant concessions from Democrats — including barring the D.C. government from paying for abortions, restarting the D.C. school voucher program that Boehner has long championed and keeping the Guant&aacute;namo Bay, Cuba, prison open.

As the dust settled over the weekend, what remained was for all sides to declare victory and for staff to hash out what had actually been agreed to. The bill still has to be drafted and both chambers still have to vote this week to finally implement the spending plan for the rest of this year.

But this is just the first in a trilogy of shutdown showdowns this year. A debt limit hike is due next month, and they get to do this all again come September, when next year’s budget is due.