In the midst of an artificial intelligence spending spree, Meta posted stronger-than-expected results for the second quarter, buoyed by growing ad revenue even as its expenses increased.

Shares of Meta Platforms Inc. surged more than 9% after-hours as a result.

The Menlo Park, California-based company earned $18.34 billion, or $7.14 per share, in the April-June period. That’s up 36% from $13.47 billion, or $5.16 per share, in the same period a year earlier.

Revenue jumped 22% to $47.52 billion from $39.07 billion.

Analysts expected Meta to earn $5.88 per share on revenue of $44.81 billion, according to a poll by FactSet.

Meta said it expects costs to increase as it spends billions on infrastructure and luring highly compensated employees as it works on its AI ambitions. It’s forecasting 2025 expenses to be in the range of $114 billion to $118 billion, up 20% to 24% year-over-year.