Course contentsShow
← AI Engineering
Lesson 881 of 1,88622. Testing and CI/CD for AI SystemsFree lesson

Testing LLM API Calls with Mocks

Using mock responses to test API integration logic without making real LLM calls.

Testing LLM API Calls with Mocks

What you'll learn: How to test your LLM integration code without making expensive or slow API calls by simulating responses.

The Core Problem

When you build AI features, your code makes API calls to LLM providers. But running tests that hit real APIs is problematic: they're slow, cost money with each call, require network connectivity, and can produce non-deterministic results. You need a way to verify your integration logic works correctly without actually calling the LLM.

What Are Mocks?

Think of mocks as stand-ins or test doubles. Just like a stunt double in a movie stands in for the actor during dangerous scenes, a mock stands in for the real LLM API during tests. You configure the mock to return predetermined responses, allowing you to test how your code handles different scenarios.

How It Works

Instead of your test making a real API call to OpenAI, Anthropic, or another provider, you intercept that call and return a fake response you've crafted. This lets you:

  • Test error handling: Simulate rate limits, timeouts, or API errors
  • Test edge cases: Return unusual but valid responses
  • Test quickly: No network delays, tests run in milliseconds
  • Test reliably: Same input always produces same output
  • Test cheaply: Zero API costs

You're not testing whether the LLM itself works (that's their job), you're testing whether your code correctly handles the API's responses, parses results, manages errors, and implements retries.

Key Takeaway: Mocking LLM API calls lets you test integration logic thoroughly, quickly, and cheaply by simulating responses rather than making real API calls. This is essential for fast, reliable test suites in AI systems.