The potential of AI has captured the imagination of everyone, and it's already leading to massive investments from big business. The IDC forecasts annual spending on AI software to reach $279 billion by 2027, or an increase of 31% per year.

If your portfolio needs an AI upgrade, Microsoft (MSFT 1.82%) and Nvidia (NVDA 6.18%) have to be at the top of your buy list. These stocks have outperformed the Nasdaq Composite in 2023, and are up 56% and 220%, respectively. Here's why they will remain rewarding investments for years to come.

1. Microsoft

Shares of Microsoft are hitting new highs as the company rolls out AI tools across its software portfolio. It is offering a subscription to the AI Copilot assistant in Microsoft 365, while AI services are starting to pad the growth of the Microsoft Azure cloud computing business.

While Microsoft's Bing AI is free to use, these new tools come at a cost. It requires tremendous investment in hardware to train AI models, and that's exactly why Microsoft is a great AI stock to buy. Despite these investments, Microsoft reported year-over-year growth in earnings per share of 27% in the most recent quarter. It's one of the few companies that has the resources to release cutting-edge AI technologies and profit from them.

The AI tools it is releasing for Windows, Office, and Azure could win new customers (subscribers) for years. Microsoft already has over 1 million paid Copilot users, with Copilot for business subscriptions up 40% quarter-over-quarter.

Azure also continues to gain market share in cloud computing for enterprise, with revenue beating Wall Street's expectations last quarter driven by higher consumption of AI services.

The stock has more than tripled over the last five years, but it's just getting started on monetizing the growing demand for AI.

2. Nvidia

Nvidia is probably the safest high-growth AI stock to own for the long haul. It has been a phenomenal performer for investors over the last decade, and that's a credit to Nvidia's dominance in supplying graphics processing units (GPUs), the key hardware required for AI training.

With AI adoption at an inflection point, demand for Nvidia's GPUs is exploding, with revenue up 206% year over year last quarter. It is delivering mission-critical AI hardware while earning huge profits. The company's trailing-12-month profit margin is now 42% -- higher than Microsoft's 35%.

Nvidia is also investing in software that takes advantage of its GPU capabilities. It recently announced an AI foundry service that runs on Microsoft Azure and will help companies of all sizes develop generative AI applications. "Our partnership with Nvidia spans every layer of the Copilot stack -- from silicon to software -- as we innovate together for this new age of AI," Microsoft CEO Satya Nadella said.

Nvidia just announced its new H200 GPU, which delivers 18 times the performance of the previous H100 chip for running AI models. Amazon, Google, Microsoft, and Oracle are early adopters, and will implement the H200 in their cloud servers next year.

The stock trades at just 24 times next year's earnings estimate. Nvidia's valuation is low enough to offer great returns in 2024 and beyond.