President Biden and Speaker Kevin McCarthy on Saturday reached an agreement in principle to lift the debt limit for two years while cutting and capping some government spending over the same period, a breakthrough after a marathon set of crisis talks that has brought the nation within days of its first default in history.

Congressional passage of the plan before June 5, when the Treasury is projected to exhaust its ability to pay its obligations, is not assured, particularly in the House, which plans to consider it on Wednesday. Republicans hold a narrow majority in the chamber, and right-wing lawmakers who had demanded significantly larger budget cuts in exchange for lifting the borrowing limit were already in revolt.

But the compromise, which would effectively freeze federal spending that had been on track to grow, had the blessing of both the Democratic president and the Republican speaker, raising hopes that it could break the fiscal stalemate that has gripped Washington and the nation for weeks, threatening an economic crisis.

Mr. Biden urged the House and Senate to pass the agreement in a late-night statement issued by the White House, saying it would prevent a catastrophic default.

“It is an important step forward that reduces spending while protecting critical programs for working people and growing the economy for everyone,” Mr. Biden said. “And the agreement protects my and congressional Democrats’ key priorities and legislative accomplishments. The agreement represents a compromise, which means not everyone gets what they want.”

The president and Mr. McCarthy spoke by phone on Saturday evening to resolve final sticking points.

In a nighttime news conference outside his Capitol office that lasted just one minute, Mr. McCarthy said the deal contained “historic reductions in spending, consequential reforms that will lift people out of poverty into the work force, rein in government overreach” and would add no new taxes. He declined to answer questions or provide specifics, but said he planned to release legislative text on Sunday.

“We still have more work to do tonight to finish all the writing of it,” he said.

The plan was structured with the aim of enticing votes from both parties, though it has drawn the ire not only of conservative Republicans but also Democrats furious at being asked to vote for cuts they oppose with the threat of default looming.

Still, it gives Republicans the ability to say that they succeeded in reducing some federal spending — even as funding for the military and veterans’ programs would continue to grow — while allowing Democrats to say they spared most domestic programs from significant cuts.

The deal would suspend the borrowing limit, which is currently $31.4 trillion, for two years — enough to get past the next presidential election.

According to a person familiar with the agreement, it also would impose new work requirements for some recipients of government aid, including food stamps and the Temporary Assistance for Needy Families program. It would place new limits on how long certain recipients of food stamps — people under the age of 54, who do not have children — could benefit from the program. But it also would expand food stamp access for veterans and the homeless, said the person, who spoke on condition of anonymity because they were not authorized to discuss details of the package.

The tentative deal also claws back some unspent money from a previous pandemic relief bill, and reduces by $10 billion — to $70 billion from $80 billion — new enforcement funding for the I.R.S. to crack down on tax cheats. It includes measures meant to speed environmental reviews of certain energy projects and a provision meant to force the president to find budget savings to offset the costs of a unilateral action, like forgiving student loans — though administration officials could circumvent that requirement. It also includes an enforcement measure that is meant to avert a government shutdown later this year.

The work requirements and the environmental review reforms were among the last details the two sides worked out on Saturday.

White House and congressional negotiators — working around the clock at the Capitol, in the White House and virtually — pushed the resolution nearly to the last minute, increasing pressure on lawmakers to accept a solution unpopular with activists on both the right and left. Economists and Wall Street analysts warned that a default would be devastating and potentially lead to a global economic meltdown.

To avert a default, the House and the Senate must pass the deal and send it to Mr. Biden for his signature. That promises to be a heavy lift for both Mr. McCarthy and Representative Hakeem Jeffries of New York, the Democratic leader, who must now cobble together a coalition of House Republicans and Democrats to push it through.

Mr. McCarthy has repeatedly said he believes a majority of his conference would vote for the deal, but it is not clear yet how many Republicans will back the compromise — and how many Democrats might be needed to vote for it to make up for G.O.P. defections.

The path also is likely to be rocky in the Senate, where quick action requires bipartisan support and conservatives have signaled they are unwilling to go along.

In a sign of their displeasure, House Freedom Caucus members were huddling to identify procedural tools to delay passage of the agreement or make the bill more conservative.

Republicans have refused for months to raise the debt limit unless Mr. Biden agreed to spending cuts and reduce future debt — risking a default to wield their leverage. The final agreement accomplishes their goal, but only modestly. A New York Times analysis of the spending caps at the center of the agreement suggests they will reduce federal spending by about $650 billion over a decade, if spending grows at the expected rate of inflation after the caps lift in two years.

The cuts in the package are almost certainly both too modest to win the votes of hard-line conservatives and too stringent to win the votes of progressives in the House. Lawmakers in the House Freedom Caucus were privately pillorying the deal on Saturday night, and the Congressional Progressive Caucus had already begun to fume about it even before negotiators finalized the agreement.

But budget hawks urged passage. “The process was tense, risky and ugly, but in the end, we have a plan to enact savings and lift the debt ceiling, and that is what is needed,” said Maya MacGuineas, president of the Committee for a Responsible Federal Budget in Washington.

The deal would impose caps on discretionary spending for two years, though those caps would apply differently to spending on the military than to the rest of the federal budget. Spending on the military would grow next year, as would spending on some veterans’ care. Spending on other domestic programs would fall slightly — or stay roughly flat — compared with this year’s levels.

The announcement came after months of political brinkmanship. Mr. Biden and congressional Democrats initially insisted that House Republicans raise the debt ceiling without conditions, but relented after Mr. McCarthy marshaled his conference to pass a bill to increase the nation’s borrowing limit in exchange for cutting government programs by an average of 18 percent over a decade. Republicans purposefully avoided laying out exactly which programs they planned to cut, but the bill’s passage forced Mr. Biden to do what he had said he never would: negotiate over raising the debt ceiling.

The deal was ultimately struck by a group led by Mr. Biden’s counselor, Steve Ricchetti; his budget director, Shalanda Young; and two of Mr. McCarthy’s closest confidants, Representatives Patrick T. McHenry of North Carolina and Garret Graves of Louisiana. They agreed to use some creative accounting maneuvers in the deal to help provide both sides political cover.

But Mr. McCarthy was still likely to face a revolt from the hard-right lawmakers in his conference whom he empowered as part of the concessions he made to become speaker in January, after a bruising 15-round election.

On a private call to brief members of his conference on the emerging deal, Mr. McCarthy sold the agreement as a victory, saying there was little in the package that Democrats supported. But hard-right lawmakers in the Freedom Caucus, who for days had been venting frustration with the emerging contours of the deal, made their displeasure known.

Everything “they fought for” in the House bill were omitted from the agreement, Representative Bob Good of Virginia said, according to a person familiar with the remarks who spoke on the condition of anonymity to describe a private call. Mr. McCarthy and his deputies defended the deal, citing several wins, including rolling back money for the I.R.S.

Progressives, too, had vented their unhappiness before the deal was even announced.

Lindsay Owens, the executive director of the liberal Groundwork Collaborative in Washington, criticized the deal for forcing budget cuts in domestic programs — and in particular, for reducing enforcement money for the I.R.S.

“Conceding to Republican demands to hamstring the I.R.S.’s ability to go after wealthy tax evaders is a losing proposition for Democrats,” she said. “It undermines an important policy initiative, drains a good source of revenue and requires the caucus to vote down a policy that is incredibly popular with the public.”

Peter Baker contributed reporting.