Several retiring senators were eager to use the bill to pass favored measures and cement their legacies. Among departing senior Republicans were Richard Burr of North Carolina, who was the ranking member of the Senate Health, Education, Labor and Pensions Committee; Roy Blunt of Missouri, a Republican who was ranking member on the Senate Appropriations health subcommittee; and Richard Shelby of Alabama, vice chairman of the Senate Appropriations Committee. Legacies were also meaningful for the retiring Democrat Patrick Leahy, who was the chairman of Appropriations, as well as Nancy Pelosi, who was giving up her position as the top House Democrat.

Mr. Burr had been working all year with his Democratic counterpart to develop a pandemic preparedness package known as the Prevent Pandemics Act. That legislation passed as part of the spending bill.

Mitch McConnell, the Senate minority leader, had signaled earlier in the year that he hoped for a relatively modest spending bill. But he did not stand in the way of the giant bill in the end.

“Probably a lot of the driver was, ‘Let’s resolve it and accept the reality of a lot of stagnation we’ll see in the next Congress,’” said Drew Keyes, a senior policy analyst at the Paragon Health Institute and a former staffer on the Republican Study Committee. He was critical of the size and scope of the bill, especially given the limited debate on many of its provisions. But he said he understood why it came together: “We saw a lot of pieces that felt like this is the last opportunity.”

Some convoluted budget math made it possible for lawmakers to pass expansions of Medicaid without appearing to cost much money, an opportunity that was likely to disappear over time. By scheduling an end date for an expensive pandemic policy, Congress could then use the projected savings to pay for expanded Medicaid benefits for children, postpartum mothers and residents of U.S. territories.

The bill requires states to keep children signed up for at least a year at a time, and extends funding for the Children’s Health Insurance Program. It also sets up a series of policies meant to discourage states from automatically dumping large numbers of adult enrollees after the end of an emergency policy that protected enrollments during the pandemic. The provisions reflected a longstanding interest by Ms. Pelosi in broadening health coverage through the Affordable Care Act and other means.

In addition to the expiring funding sources, there was a “time-limited coalition behind some of those policies,” said Matthew Fiedler, a senior fellow at the Brookings Institution, who was tracking the Medicaid provisions.