WASHINGTON (AP) -- Moody's Investors Service upgraded the outlook for U.S. government debt to "stable" from "negative" and affirmed the United States' blue chip Aaa rating.

The rating agency cited a surprising drop in the federal deficit -- the difference between what the government collects in taxes and what it spends. The U.S. government is on track to report its lowest annual deficit in five years.

Through the first eight months of the budget year, the deficit has totaled $509.8 billion, according to the Treasury Department. That's nearly $400 billion lower than the same period last year.

The Congressional Budget Office forecasts the annual deficit will be $670 billion when the budget year ends on Sept. 30. That would be well below last year's deficit of $1.09 trillion and the lowest since President Barack Obama took office. It would still be the fifth-largest deficit in U.S. history.

The deficit hit a peak 10.1 percent of gross domestic product -- the broadest measure of the U.S. economy -- in the depths of the Great Recession in 2009. CBO expects the deficit to fall to 3.4 percent of GDP in 2014 and 2.1 percent in 2015.

Moody's had lowered the outlook to "negative" two years ago. But it never went as far as rival Standard & Poor's, which stripped the U.S. of its top credit rating in 2011.

S&P last month upgraded its outlook for long-term U.S. government debt but kept its rating at AA+, a notch below its top grade.

A stronger credit outlook and rating should allow governments to borrow at lower interest rates by signaling that their bonds are less risky. Weaker credit ratings should force them to pay higher rates.

But investors largely ignored S&P's downgrade in 2011. Stocks fell briefly and then rebounded. Yields on Treasurys later fell to record lows.

An improving economy and tax hikes and spending cuts that took effect this year have narrowed the government's budget gap.

Still, Moody's warned that the government needed to control longer-term deficits as Baby Boomers age and begin to collect Social Security and Medicare. Failure to do so "could put the rating under pressure again."