Republicans and Democrats have reached a deal on the US budget, an hour before a deadline that would have forced the government to close many services.
The parties have agreed to slash about $38bn (£23bn) from spending for the year until 30 September.
President Barack Obama said the cuts would be difficult but necessary.
“Some of the cuts we agreed to will be painful,” he said.
“Programmes people rely on will be cut back. Needed infrastructure projects will be delayed. And I would not have made these cuts in better circumstances.”
He said it was the “the biggest annual spending cut in history”, but said America needed to start living within its means.
The BBC’s Mark Mardell in Washington says Mr Obama has put a gloss on the measures, but they are a victory for the Republicans.
Announcing the deal, House Speaker John Boehner, a leading Republican, said it had been a “long fight”.
“We fought to keep government spending down because it really will create a better situation for job creators,” he said. Without an agreement by midnight on Friday, the government would have been forced to shut down, barring some 800,000 government employees from working.
The last US government shutdown came in 1995 amid a dispute between the Republican Congress and Democratic President Bill Clinton’s White House.
The shutdown lasted for 20 days and was estimated to have shaved one percentage point off US economic growth for one quarter of the year.
During the current stand-off, talks were stalled for days as Republicans – urged on by the fiscally conservative Tea Party movement – pushed for larger budget cuts than Democrats were willing to concede.