Washington CNN  — 

President Donald Trump and bipartisan congressional leaders clinched a sweeping two-year budget agreement that would produce hundreds of billions in new spending and take the threat of a fiscal crisis off of Washington’s plate for more than two years.

The deal, which came together in a burst of urgency during a period of two weeks amid a steady stream of phone calls Speaker Nancy Pelosi and Treasury Secretary Steve Mnuchin, would eliminate – for good – the budget caps put into place in 2011 and suspend the debt ceiling until July 31, 2021.

“I am pleased to announce that a deal has been struck with Senate Majority Leader Mitch McConnell, Senate Minority Leader Chuck Schumer, Speaker of the House Nancy Pelosi, and House Minority Leader Kevin McCarthy - on a two-year Budget and Debt Ceiling, with no poison pills,” Trump tweeted shortly after congressional leaders signed off on the final agreement in a Monday evening conference call. “This was a real compromise in order to give another big victory to our Great Military and Vets!”

The agreement, should it pass both chambers of Congress and be signed into law, would remove twin crises staring down the White House and lawmakers in the looming spending cuts and debt ceiling deadline, clearing the decks on both until after the 2020 election. While leaders in both chambers will still need to muster the votes for passage, the fact that all four signed off on the deal and a Trump endorsement followed bodes well for its future, people involved say.

In a brief interview Monday evening, Pelosi said two factors above all played a critical role in her successful behind-the-scenes negotiations with Mnuchin: avoiding a stock market collapse and the fiscal fallout from failing to raise the debt ceiling.

“The debt ceiling and the stock market were always two major motivators for him to try and clear the way,” Pelosi told CNN. “We avoided sequestration and for the moment avoided a shutdown. For federal workers, that was very important.”

Pelosi, in a joint statement with Schumer touting the boosts in spending for Democratic priorities, said the House will move to pass the legislation before the chamber departs on Friday for a six-week recess.

“The House will now move swiftly to bring the budget caps and debt ceiling agreement legislation to the Floor, so that it can be sent to the President’s desk as soon as possible,” Pelosi and Schumer said in a statement. “With this agreement, we can avoid the damage of sequestration and continue to advance progress for the people.”

Asked whether she ever worried if Trump would sign off on the compromise, Pelosi told CNN: “I didn’t have any anxiety about that.” She added that Trump, too, was concerned about shaking the stock market.

The agreement also marks the end of the Budget Control Act, put in place in 2011 on the grounds of threatening cuts so steep that agreements defined by fiscal restraint would be the only option – something that has consistently been undone by subsequent deadline driven budget agreements that sought to delay the spending caps and infuse new funds for both defense programs and domestic spending.

It’s a move that has already drawn fierce backlash from conservatives, who have for years railed against the increased spending, yet is likely to thread a needle to passage between Republicans who support increased defense spending and Democrats who have backed boosts to domestic priorities. The House Republican Study Committee, a large caucus of GOP conservatives, already warned negotiators against the agreement when the outlines were starting to form last week. Several vocal outside GOP groups have done the same.

“We know everybody is not gonna vote for this either way. Democrats or Republicans,” Senate Appropriations Committee Chairman Richard Shelby, an Alabama Republican, told reporters. “But I believe with the principals involved, now the leadership is involved, I think there’s a good chance to move forward on an appropriation bill and put to rest a potential default on our international credit.”

In total, the agreement would lock in a $1.3 trillion budget deal, which includes $320 billion in additional spending over the course of two years. The number falls short of what Democrats entered the negotiations requesting, but is still a marked increase with equal increases for both domestic and defense spending. The off-sets in the agreement – a key sticking point for days – ended up totaling $77 billion, which was half of what the White House had requested, and didn’t come from Democratic priorities, but instead through accounting shifts.

“While the reality of divided government means this is not exactly the deal Republicans would have written on our own, it is what we need to keep building on that progress,” McConnell, who touted the increased defense spending, said in a statement. That defense spending increase, along with the stability provided by the agreement for future Pentagon budgets, is a top deal point for Republicans.

But along with the significant boost to defense spending, the President and administration officials touted an agreement to limit policy riders in future spending bills and didn’t include a commitment that the administration would halt efforts to fund its border wall through re-programming of appropriated funds.

The former sets the stage for spending bills that have a cleaner line to agreement. Lawmakers will now have the month of September to fund the next fiscal year through the Appropriations process - an effort that had been stalled by the lack of a topline budget agreement.

The latter issue, however, may become a problem for progressives in the House and was enough to already bring out the concerns from one key Democrat – Sen. Patrick Leahy of Vermont, the top Democrat on the Senate Appropriations panel.

“I’m just concerned there’s too much flexibility for the President to move money around without congressional approval,” Leahy told reporters before the deal was announced. “I want very specific things in there.”

But Leahy, after the deal was announced, said in a statement he would support the agreement.

Trump’s positive announcement of the agreement, particularly given his past reluctance to sign off on deals loathed by conservatives, is a crucial development – one explicitly sought by GOP leaders – in ensuring the agreement receives the requisite number of GOP votes to ensure passage.

For leaders in both parties, the process of reaching the agreement had been filled with fits and starts. For months, McConnell – and at various stages, McCarthy alongside him – worked behind the scenes to get Trump behind an agreement, even as elements inside the White House were cool to increased spending on the domestic side.

The effort included, several people involved said, making clear that Mnuchin, not acting White House chief of staff Mick Mulvaney or acting Office of Management and Budget Director Russell Vought, serves as the lead negotiator for the White House.

The talks all but fell apart in June, with both sides claiming the other wasn’t operating in good faith, and congressional Republicans voicing concern that there was no unified position inside the administration that would lead to an outcome.

That shifted shortly after lawmakers returned to the Capitol after the July Fourth holiday. Mnuchin brought new urgency to the table with a message to lawmakers: the Treasury Department’s projections showed at least one scenario where the US would run out of money by early September – shortly after lawmakers returned from the August recess and nearly a month earlier than originally projected.

That message, which was followed by a letter to Capitol Hill saying the same, helped launch what would become a regular – and fruitful – negotiation between Pelosi and Mnuchin, who spoke more than a dozen times over the course of two weeks and three times on Monday, according to a Pelosi aide.

Still, at various points the negotiations appeared to bog down, in part due to administration officials who sources say were unhappy with the direction of the agreement, and in part given the scope and scale of the talks that were occurring in an exceedingly tight time window. Even after topline numbers were agreed to, the offsets became a significant issue, as did other technical pieces regarding future Democratic spending bills.

But as this past weekend progressed – and the deadline approached – it became clear that an agreement was in reach. What wasn’t clear, according to multiple people involved, was where Trump stood. He’d been briefed on the outlines Sunday night, but didn’t render a verdict on where he stood, two sources told CNN. While he said positive things in an appearance Monday, he didn’t explicitly back the deal.

“Nothing matters ‘til he’s in,” one person directly involved in the negotiations warned midway through Monday, as the negotiators closed out the technical details on a final agreement.

As those technical details were locked in on the staff level, congressional leaders all joined a 5 p.m. conference call with administration officials to finalize the agreement. Pelosi was stuck on a tarmac in Detroit, where she’d been to speak at the NAACP national convention, as she awaited the departure of a flight that had been delayed for more than two hours.

With an iPhone to her ear, Pelosi stood up to allow other passengers into her row and as the rest of the passengers on Delta Flight 1144 had to deplane, Pelosi and her detail stayed on board. A flight attendant said the speaker was on the phone for most of the time.

Shortly before the call ended, the word started traveling around Capitol Hill that a tweet from Trump was imminent. All four leaders signed off. So did the administration officials.

The President tweeted a short time after, announcing the agreement and effectively putting an end to a looming crisis that threatened to send Washington – and Wall Street – into a fiscal tailspin.

A few minutes after Trump sent out his tweet about the budget deal, Pelosi got off the phone as the plane entered an active taxiway.

She moved onto a crossword puzzle in the Sunday Times Magazine as her plane – now delayed more than three hours – awaited takeoff.

CNN’s Jeff Zeleny and Betsy Klein contributed to this report.