Sub topic 1
Why live LLM calls make bad unit tests
Prerequisites
This chapter assumes you can:
- write and run unit tests with pytest or a similar framework
- read basic Python classes, functions, and exceptions
- call an LLM API and parse a JSON reply
- pass a dependency as an argument (dependency injection)
The problem in one test
Suppose your code asks a model to pull a total out of an invoice. The obvious test sends a real request and checks the answer:
def test_extract_total_live(live_client):
assert extract_total(live_client, "Total: $42.50") == 42.5This test passes most days. It is still a poor unit test, for four separate reasons.
Four ways a live call breaks a test
Output varies between runs
A model samples its output. Even with temperature set to 0, providers do not promise identical text on every call. Model versions also change behind a name. The same prompt can return {"total": 42.5} today and Total: 42.50 tomorrow. When the test fails, you cannot tell whether your code broke or the model varied.
Tests become slow
A real request takes from a few hundred milliseconds to several seconds. A unit test suite should finish in seconds so you run it after every edit. Hundreds of live calls turn it into something you run once a day.
Tests cost money
Every call is billed by tokens. A suite that runs on every commit, on every branch, in CI, adds up quickly. Calls can also hit rate limits, which makes tests fail for reasons unrelated to your code.
You cannot reach the failure paths
The code you most need to test is the code that handles bad news: malformed JSON, a timeout, a rate-limit error. A live model rarely produces these on demand. You cannot write a reliable test for a retry loop if you can't make the failure happen.
How flakiness compounds
Small per-call failure rates multiply across a suite. This script computes the chance that every test passes when each one is independently reliable 98% of the time:
per_test_pass_rate = 0.98
number_of_tests = 200
all_pass = per_test_pass_rate ** number_of_tests
print(f"Chance the whole suite is green: {all_pass:.1%}")It prints about 1.8%. A 2% flake rate per call sounds minor. Across 200 live tests, the suite is almost never fully green.
Where the nondeterminism enters
Your own code is the only part you control. The rest of the diagram is the network, the provider, and the model. A unit test should exercise only the part you wrote.
Common mistakes
- Loosening assertions until they pass. Replacing
== 42.5withis not Nonehides real bugs. - Retrying the whole test on failure. This masks flakiness and still costs money.
- Setting temperature to 0 and calling it deterministic. It reduces variation but does not remove it.
- Mocking at the wrong level. Patching deep inside an SDK ties your tests to its internals, so an SDK upgrade breaks them.
What comes next
The fix is to stop calling the model in unit tests. You put a small interface between your code and the model client. In tests, you replace the client with something predictable. The next chapters cover what to call each kind of replacement, how to build a scripted fake, and how to replay real responses recorded earlier.
Live calls still have a place. They belong in a small number of separate checks, covered at the end of this tutorial, not in your unit tests.
Exercise
A suite has 50 live LLM tests, and each one passes 99% of the time independently. Roughly what is the chance the whole suite is green on a given run, and name two other problems besides flakiness that live calls cause.
Show answer
0.99 ** 50 is about 0.605, so the suite is green only about 60% of the time. Other problems: tests are slow (each call takes hundreds of milliseconds to seconds), tests cost money per token, and failure paths such as malformed JSON, timeouts, and rate-limit errors cannot be triggered on demand.