This photo shows the exterior view of JPMorgan Chase offices in San Francisco. JPMorgan Chase, the biggest U.S. bank by assets, reported a third-quarter loss Friday after a big charge for legal expenses.

NEW YORK — Mounting legal costs pushed JPMorgan Chase to a rare loss in the third quarter, the first under the leadership of Jamie Dimon.

The largest U.S. bank by assets set aside $9.2 billion in the quarter to cover a string of litigation stemming from the housing crisis and the bank’s “London Whale” trading debacle.

JPMorgan said it has placed a total of $23 billion in reserve to cover potential legal costs, including the $9.2 billion.

In a conference call with reporters, CEO Dimon called the costs “painful,” but said they reflected “the reality we have to deal with.”

While JPMorgan emerged from the financial crisis and the Great Recession as one of the strongest U.S. banks, it has been dogged by legal problems in recent years.

Last month, it agreed to pay $920 million and admitted that it failed to oversee trading that led to a huge $6 billion loss last year. That episode came to be known as the “London Whale,” referring to the location of the trader who made those oversized bets.

The bank said in August that it is facing a federal criminal probe focused on mortgage-backed securities sold before the financial crisis. Federal energy regulators, meanwhile, are investigating some of the company’s bidding practices in energy markets.

The bank is also said to be discussing an $11 billion national settlement with the Department of Justice over mortgage-backed securities. The securities lost value after a bubble in the housing market burst, helping to bring on the financial crisis.

JP Morgan lost $380 million in the third quarter, compared with net income of $5.7 billion a year earlier. On a per-share basis, it lost 17 cents, versus earnings of $1.40 a year earlier. Excluding charges, however, its adjusted earnings beat Wall Street expectations.

The bank was helped by credit card sales and more deposits at its consumer banking unit. Net income at JPMorgan’s investment banking division rose as expenses fell, and it reduced its reserves for losses on loans.

The overall quarterly loss was the bank’s first since the second quarter of 2004, before the global financial crisis and the start of Dimon’s tenure as CEO in December 2005.

Dimon declined to say when the bank would finally put its legal worries behind it, but said he wasn’t expecting further losses of this magnitude.

Excluding the litigation expense, it earned $5.8 billion, or $1.42 a share, in the quarter. That beat analysts’ forecasts for earnings of $1.19 a share, according to FactSet.