The U.S. economy contracted in the first three months of the year, as supply constraints at home, demand shortfalls abroad and rapid inflation worldwide weighed on an otherwise resilient recovery.

Gross domestic product, adjusted for inflation, fell 0.4 percent in the first quarter, the Commerce Department said Thursday. It was the first decline since the early days of the pandemic, and a sharp reversal from the rapid 1.7 percent growth in the final three months of 2021.

But the negative number masked evidence of a recovery that economists said remained fundamentally strong. The decline — 1.4 percent on an annualized basis — mostly resulted from the way inventories and trade figure in the calculation, as well as reduced government spending as Covid-19 relief efforts wind down. Measures of underlying demand showed solid growth.

Most important, consumer spending, the engine of the U.S. economy, grew 0.7 percent in the first quarter despite soaring gas prices and the Omicron wave of the coronavirus, which restrained spending on restaurants, travel and similar services in January.

“Consumer spending is the aircraft carrier in the middle of the ocean — it just keeps plowing ahead,” said Jay Bryson, chief economist for Wells Fargo.

Still, that resilience could be tested in coming months as the fastest inflation in four decades continues to take a toll. Consumer prices rose at a 7 percent annual rate in the first quarter, and Americans’ after-tax incomes, adjusted for inflation, fell for the fourth quarter in a row.

The share of Americans listing inflation as the most significant household financial problem reached a record high in a Gallup survey released Thursday. A total of 46 percent rated their personal finances positively, down from 57 percent a year ago, when a majority of households were freshly benefiting from rounds of direct federal aid.

Despite that gloomy outlook, higher prices haven’t yet dampened consumers’ willingness to spend. But that will change if inflation keeps outpacing income gains, said Beth Ann Bovino, chief U.S. economist for S&P Global. The saving rate in the first quarter fell below its prepandemic level for the first time, as consumers saved less in order to keep spending.

“There’s a tipping point,” she said. Sometime this year, she added, “I’m expecting to see households starting to respond either by trading down, looking for deals, being less willing to pay higher prices.”

At the Melting Pot, a national chain of nearly 100 fondue restaurants, revenue dipped in early January as soaring coronavirus cases kept both diners and employees at home. But reservations bounced back quickly, and Valentine’s Day — “our Super Bowl,” said the chief executive, Bob Johnston — was the strongest on record. Sales this spring have been up 40 percent or more from 2019, and growth would be even stronger if franchisees could hire enough people.

“We’re not able to meet demand,” Mr. Johnston said. “We need more team members, and we’re struggling with keeping the bench full.”

The Melting Pot is raising wages to attract workers, and is paying more for many ingredients. So far, it has been able to raise prices to compensate for higher costs without losing business, but Mr. Johnston said he didn’t know how long that would continue.

“We try to be very careful with that and not overly confident that we can continue to raise prices with no impact,” he said. “There could be a line we can’t see, and we don’t want to cross that line.”

Republicans have seized on rising prices to blast President Biden’s economic policies. The report on Thursday gave them room to ramp up that criticism.

“Accelerating inflation, a worker crisis and the growing risk of a significant recession are the signature economic failures of the Biden administration,” Representative Kevin Brady of Texas, the top Republican on the House Ways and Means Committee, said in a news release.

The White House sought to dismiss the decline in gross domestic product as a result of quirks in the data that didn’t reflect the economy’s overall strength.

“While last quarter’s growth estimate was affected by technical factors, the United States confronts the challenges of Covid-19 around the world, Putin’s unprovoked invasion of Ukraine and global inflation from a position of strength,” Mr. Biden said in a statement after the release, referring to President Vladimir V. Putin of Russia.

Indeed, the first-quarter weakness was partly related to the robustness of the U.S. recovery compared with the rest of the world. American retailers have responded to consumer demand by importing more. At the same time, U.S. exports have lagged because of weaker economic growth abroad. As a result, the trade deficit has ballooned, taking more than three percentage points away from the change in gross domestic product in the first quarter.

“The moral of the story is that the Omicron wave, the war in Ukraine and new lockdowns in China were more costly for growth abroad than they were at home,” said Diane Swonk, chief economist for the accounting firm Grant Thornton. “Domestic spending was remarkably resilient. It actually accelerated.”

Slower inventory expansion shaved nearly another percentage point off growth. Companies raced to build up inventories in late 2021 to make sure supply-chain disruptions didn’t leave them with bare shelves during the holiday season. That meant they didn’t have to do as much restocking as usual in the new year.

A measure of underlying growth that strips out the effects of inventories and trade rose 0.6 percent in the first quarter, adjusted for inflation. That was a modest acceleration from the end of last year.

Still, economists warned not to dismiss inventory and trade effects entirely. Both reflect the challenges that domestic producers are having meeting sky-high consumer demand.

“If we are importing things rather than making them here, that reflects that we are demanding more than we can produce,” said Wendy Edelberg, director of the Hamilton Project, an economic policy arm of the Brookings Institution. “It suggests that our economy just does not have the capacity to meet demand.”

The Federal Reserve is trying to tamp down demand by raising interest rates, which policymakers hope will tame inflation. But Russia’s invasion of Ukraine and a new round of Covid lockdowns in China have complicated its job by prolonging supply disruptions, which the central bank can do little about.

Matt Younger, who owns a small construction firm in Annapolis, Md., is dealing with long delays and higher prices for just about everything that goes into building a house: two-by-fours, plywood, windows, garage doors.

“It’s like playing a game of chess — I’ve got to be a couple moves ahead on everything in case I can’t get something,” he said.

Now, rising interest rates are threatening to cool off the red-hot real estate market. Mortgage applications have fallen sharply, sales of new and existing homes have also dipped, and anecdotal evidence from across the country suggests that the madcap bidding wars that characterized the residential real estate market for much of the past two years may be starting to fade.

So far, however, none of that has led to a slowdown in the construction business. Residential construction grew 0.5 percent in the first quarter, only slightly slower than in the final quarter of 2021, and applications for building permits rose in March. Mr. Younger’s business is still booming.

Mr. Younger said he had stopped offering fixed-price contracts because he couldn’t be sure in advance how much his materials would cost. Eventually, he suspects, some renovation customers will scale back projects to fit their budgets. But in terms of new homes, there is such a housing shortage in the Washington area that he doubts demand will dry up — and even if sales slow, he can always rent out the homes he is building.

Farther up the supply chain, however, some businesses are getting squeezed.

Marilyn Santiago runs Creative Architectural Resin Products, a Florida-based manufacturer of faux wood beams, shutters and other decorative features for homes. Ms. Santiago, like Mr. Younger, has seen the costs of her materials skyrocket in recent months. But she has struggled to pass those increases on to her homebuilder clients because they are looking for ways to keep their own costs down.

Construction delays are also wreaking havoc on Ms. Santiago’s business. Her products generally can’t be installed until after a house’s windows, and with windows on back order across the country her warehouse is overflowing with finished pieces that should have been delivered months ago.

“If you got to my house, you’re going to see a bunch of brackets, and my truck is a warehouse, too,” she said.

Now Ms. Santiago is considering getting a new warehouse — but storage prices have soared as well.

“It’s like everyone is taking advantage of the situation — and we as small-business owners, we are basically the punching bag of the supply-and-demand world,” she said.

Jeanna Smialek, Talmon Joseph Smith and Ana Swanson contributed reporting.