Skip to main content

Testing the LLM Step without Calling the LLM

Why This Matters

Calling a real LLM in your tests makes them slow, costly and unreliable: the same prompt can give a slightly different answer tomorrow. But you still need to test everything around the LLM: the prompt, the parsing, and what happens when the answer is bad.

Replace the LLM with a recording

Because the LLM is a parameter (lesson 5, Calling an LLM and Getting Structured Data Back), tests pass in a RecordedLLM:

def test_extracts_all_fields_from_a_recorded_answer():
    path = ROOT / "samples" / "order-1.eml"
    order = extract_order(read_message(path), RecordedLLM.for_sample(path))

    assert order.supplier == "Outdoor Supply BV"
    assert order.order_number == "PO-20417"
    assert order.delivery_date == date(2026, 12, 14)
    assert order.lines == (OrderLine("TENT-2P", 10, 189.00), OrderLine("STOVE-G", 24, 39.95))
    assert order.total == 2848.80

This tests your code, not the model: given this answer, do we build the right order?

Test the prompt too

RecordedLLM remembers the prompts it received, so you can check what you send:

def test_the_prompt_contains_the_message_and_forbids_guessing():
    llm = RecordedLLM('{"supplier": "x", "order_number": "PO-00001", "delivery_date": "2026-12-14", "lines": []}')
    extract_order("Please deliver 3 tents", llm)

    assert "Please deliver 3 tents" in llm.prompts[0]
    assert "Do not guess" in llm.prompts[0]

If someone “simplifies” the prompt and removes the instruction not to guess, this test fails.

Test the bad answers

Models sometimes answer with chatty text, leave out fields, or return null. The parametrised test from lesson 7, Your First pytest Tests checks that each of those becomes an ExtractionError, and tests/test_pipeline.py checks the result: status failed, nothing booked.

def test_a_vague_message_fails_safely(erp, catalog, today):
    result = run("order-3.eml", erp, catalog, today)

    assert result.status == "failed"
    assert erp.booked == []

And the model itself?

Recordings test your code. Whether a model is good enough is a different question, answered with an evaluation set: a few dozen real emails with the answers you expect, run against the real model now and then (for example weekly, or when you change the model or the prompt), with a score you track. Start with the samples in this project and grow the set with every email that went wrong in production.

Exercise

  1. Make the RecordedLLM in the first test return a price of "189,00" (with a comma). What happens, and is that what you want?
  2. Add a recorded answer where the model returns the right fields but wrapped in a Markdown code block (```json ... ```). Does the bot cope? Decide whether it should, and write the test.
  3. Optional, with an API key: write a small script that runs all samples with AnthropicLLM and compares the result with the recordings. That is the start of your evaluation set.

Key Takeaways

  • Never call a real LLM in unit tests; replay recorded answers.
  • Test the prompt you send, the parsing, and every kind of bad answer.
  • Bad answers must end in failed, never in a booked order.
  • Evaluate the model separately, with a growing set of real examples.