Washington (CNN) -- Democrats and Republicans narrowly averted a partial shutdown of the federal government Friday night, agreeing on a budget deal and a short-term funding extension little more than an hour before the clock struck midnight and time ran out.
The new funding extension, which cuts spending by $2 billion, will last through next Thursday.
"The government will be open for business," President Barack Obama said.
"In the final hours before our government would have been forced to shut down, leaders in both parties reached an agreement that will allow our small businesses to get the loans they need, our families to get the mortgages they applied for, and hundreds of thousands of Americans to show up at work and take home their paychecks on time."
Negotiators capped days of frantic closed-door talks and public recriminations by agreeing on a framework for a package of $38.5 billion in spending cuts covering the rest of the fiscal year, which expires September 30.
Republicans, bolstered by their capture of the House of Representatives in last November's midterm elections, had initially pushed for $61 billion in cuts.
A GOP push to strip $317 million in federal funding from Planned Parenthood failed. Democrats also turned back Republican attempts to get federal dollars currently set aside for family planning and women's health turned into block grants for states.
Such a move would have given governors and state legislatures more ability to cut funding for services opposed by conservatives.
Sources told CNN, however, that leaders of the Democratic-controlled Senate agreed to hold separate votes on both measures, as well as on an initiative to repeal President Barack Obama's health care overhaul.
Obama praised the agreement, calling the cuts "painful" but necessary to secure the country's economic future.
This "is what the American people expect us to do," the president said at the White House. "That's why they sent us here."
"This has been a lot of discussion and a long fight," said House Speaker John Boehner, R-Ohio. Republicans fought to "create a better environment for job creators in our country."
Senate Majority Leader Harry Reid, D-Nevada, called the deal "difficult but important for the country."
Nevertheless, the furious down-to-the-wire talks portend even tougher rounds of negotiations when Congress takes up an increase in the nation's debt ceiling and the fiscal year 2012 budget in the months ahead.
If the negotiations had failed, approximately 800,000 government workers would have been furloughed. A range of government services would have come to halt.
Obama noted earlier in the week that the mechanism of shutting down government operations had already started. An inability to reach a deal would have hurt federal workers, people who rely on government services and the nation's broader economic recovery, he warned.
"For us to go backwards because Washington couldn't get its act together is unacceptable," the president said.
Top aides on both sides of the aisle had seemed increasingly resigned to the prospect of a shutdown. Congressional staffers began receiving their furlough notices Thursday afternoon.
Friday's agreement to slash $38.5 billion in spending comes on top of two previous funding extensions that included $10 billion in cuts.
Republicans, under pressure from the conservative Tea Party movement to reduce the size of government, blame Democrats for failing to pass a fiscal year 2011 budget last year when they controlled both the Senate and the House. They also say Obama and his party are ignoring the peril of rising federal deficits and the national debt.
Democrats call the $61 billion in cuts initially pushed by the Republicans extreme, and argue reductions of that scale would have harmed the economic recovery while damaging education and innovation programs essential for continued growth.
The budget brinkmanship showed the political stakes of the situation, with both parties trying to depict the other as unwilling to do what's right for the country.
It also demonstrated the cavernous gap between the two parties on social issues.
Democrats said the Republican drive to defund Planned Parenthood proves the GOP is fixated on abortion and other issues related to women's health. Republicans repeatedly insisted that the size of spending reductions was the main cause of the dispute in recent days.
CNN's Dana Bash, Deirdre Walsh, Ted Barrett, John King, Kate Bolduan, Brianna Keilar, Terry Frieden, Ed Henry and Dan Lothian contributed to this report.