The largest tech companies are spending tens of billions of dollars on data centers to meet growing demand for artificial intelligence (AI). These are massive expenditures, but they all make sense considering how transformative AI technology is expected to be for the economy. For example, Morgan Stanley recently estimated that the long-term efficiencies from AI could be worth $40 trillion.

If that estimate is anywhere close to accurate, investors are not bullish enough on AI. There could be an unprecedented amount of economic growth about to happen over the next few decades. Buying and holding the stocks of top AI companies could help you realize significant gains.

Here are two top AI stocks to buy and hold for the long term.

The letters "AI" shown on top of a digital background.

Image source: Getty Images.

1. Palantir Technologies

The growing adoption of AI is playing to the strengths of Palantir Technologies' (PLTR 1.44%) differentiated technology. Its AI platform deeply integrates with a company's operations, which can intelligently turn data into actionable insights.

The company started as a defense contractor, but it has unleashed its technology to the commercial market with tremendous success. For example, Citibank has reduced the time to onboard customers from days to just seconds. And Fannie Mae is using Palantir to reduce the time it takes to detect mortgage fraud from two months to almost instantly.

These are just a few examples of how companies are experiencing tremendous gains in efficiency by using Palantir. The company also continues to do a lot of business with the U.S. government. It recently signed a 10-year agreement with the U.S. Army worth up to $10 billion. That's nearly triple Palantir's trailing-12-month revenue of $3.44 billion.

In the second quarter, Palantir posted a year-over-year increase in revenue of 48%. This is up from a 39% growth rate in the previous quarter. Management expects revenue to accelerate again to 50% in Q3.

Palantir's differentiated offering and accelerating growth are a big part of why the stock is trading at a high valuation. While no software company has ever sustained a price-to-sales multiple of 136, Palantir's financial results are impressive. For example, while small, fast-growing software companies usually report losses in their early growth phase while investing to maximize revenue growth, Palantir is already earning a stellar profit margin of 33% in Q2, which is on par with Microsoft.

Its margin should continue to increase as the business scales. CEO Alex Karp believes the business can increase revenue by 10x with fewer employees. At the rate it is growing, it can probably achieve that within seven years. Wedbush Securities analyst Dan Ives believes Palantir will reach a $1 trillion market cap by 2028, more than double its current value.

Nvidia headquarters.

Image source: Nvidia.

2. Nvidia

Nvidia (NVDA -0.26%) has been the poster child of the AI boom over the last few years, and it's likely not going to lose that mantle anytime soon. Every data center must have Nvidia chips to be competitive. The company offers a complete stack of solutions across processors, networking, and software, which gives Nvidia a competitive advantage.

All the leading cloud service providers and AI model builders, including ChatGPT maker OpenAI, are using Nvidia's Blackwell chips for training AI in large data centers. Selling these sophisticated computing products is Nvidia's main growth driver. In the last quarter, its data center revenue grew 56% year over year.

Nvidia is building a strong competitive advantage around its data center chips. This is evident in the robust growth of its networking solutions, which made up 15% of its revenue last quarter but grew an impressive 98% year over year.

Earlier this year, Nvidia launched NVLink Fusion, which can integrate custom chips from other semiconductor companies into its computing systems and software. This further stretches its addressable market and protects its competitive lead.

Tying all this together is Nvidia's CUDA software, which has seen the number of developers double over the last three years to 5.9 million. CUDA allows customers to optimize Nvidia's GPUs for a wide variety of applications. The network of developers using CUDA has been fundamental to Nvidia, leading the graphics processing unit (GPU) market for the last 20 years.

Despite the stock's amazing run, it is still a solid investment that could hit new highs over the next year. This is clear by looking at the stock's forward price-to-earnings multiple on next year's earnings estimates, which is currently 30. This is a very conservative valuation for almost any high-growth business and suggests that Nvidia is still undervalued.