Gen. David H. Petraeus warned at a pomp-filled retirement ceremony on Wednesday that the nation’s leaders, faced with tough budget decisions, should be careful not to cut the military’s budget too deeply in the years ahead.

The vast majority of the hour-long ceremony at Fort Myer in Arlington celebrated Petraeus’s 37 years of military service and his six years of leading troops in Iraq and Afghanistan since 2003. Military bands played, medals were awarded, and Petraeus issued a long list of thanks to his mentors, his peers and the troops who fought under his command.

Petraeus is retiring as the wars that have defined his career as a general and dominated U.S. foreign policy are winding down. He also leaves amid an economic crisis and looming defense reductions that probably will cut $400 billion to $1 trillion from military budgets over the next decade. The near certainty of deep reductions is clearly on Petraeus’s mind as he takes off his uniform and prepares to lead the Central Intelligence Agency.

“I do believe we have relearned since 9/11 the timeless lesson that we don’t always get to fight the wars for which we are most prepared or most inclined,” Petraeus told the crowd. “Given that reality, we will need to maintain the full-spectrum capability that we have developed over this last decade of conflict in Iraq, Afghanistan and elsewhere.”

The majority of the ceremony and speeches focused on Petraeus’s past — something of a departure for a general who throughout his career has focused tirelessly, and at times obsessively, on the future.

“Dave, you have run the race well, swifter and surer than the rest,” Adm. Mike Mullen, the chairman of the Joint Chiefs of Staff, said as Petraeus sat in the hot sun behind him. “You now stand among the giants — not just in our time, but of all time, joining the likes of Grant, Pershing, Marshall and Eisenhower as one of the great battle captains of American history.”

No soldier has had a greater impact on the way the military has fought over the past five years than Petraeus, who was a driving force behind the military’s embrace of a counterinsurgency doctrine that elevated the importance of protecting terrified locals from insurgent attacks and building local governance and infrastructure. Under Petraeus’s command, both tasks received as much energy and attention as killing the enemy.

Petraeus also pressed his troops to experiment and take risks in working with former enemies and building indigenous security forces whose loyalty initially seemed questionable. These gambles paid especially high dividends in Iraq, which was convulsed by sectarian violence and in the grip of a bloody insurgency when Petraeus assumed command in 2007.

“It was a time of doubt, of chaos, of death,” Mullen recalled. Within a year of Petraeus’s taking command, violence levels had fallen precipitously throughout the country.

President Obama dispatched Petraeus to Afghanistan in 2010, after Gen. Stanley A. McChrystal was forced to resign under pressure. Over the past year, Petraeus oversaw a strategy that has been credited with reversing the Taliban’s momentum in the country. The outcome of the war, however, remains in doubt.

Petraeus and his wife, Holly, whose father was a general, leave the Army after making 23 moves over the course of his career. The general’s son recently completed a tour as a platoon leader in Afghanistan and is a first lieutenant serving in Italy.

Petraeus will not be taking much of a break. In a few weeks, he will take over the top job at the CIA. “We wish [Petraeus] happiness and prosperity in his well-earned retirement,” the announcer at Wednesday’s ceremony intoned. The remark drew guffaws from the crowd.