Most LLM apps we review have one of two test setups. Either there are no tests around the model calls, or the tests hit a live API, take minutes, cost money, and fail for reasons nobody can reproduce.
Both are avoidable. The fix is to separate two questions that get mixed together:
- Does the code around the model behave correctly? Parsing, routing, retries, tool dispatch, state transitions. This is ordinary software testing and should be deterministic.
- Is the model's output good? This is an eval question. It needs a dataset and a score, not an assertion.
This post covers the first. Record-replay tests answer it cheaply. They do not answer the second, and we say where the line is below.
What record-replay buys you
You run the real chain or graph once against a live model. The HTTP exchange (or the model-call boundary) is saved to a file. In CI, the same test reads from that file instead of the network.
What you get:
- Tests that run in milliseconds and cost nothing.
- Failures that point at your code, since the model's answer is fixed.
- A concrete artifact to review in a pull request when behavior changes.
What you do not get: any information about whether a new prompt is better. A replayed response is always the old response.
Pick the recording boundary
You have two reasonable places to cut.
HTTP level. Record requests and responses with an HTTP recording library for your client (for Python, VCR-style tools such as vcrpy or pytest-recording work with httpx and requests). Advantage: the whole SDK and LangChain integration code runs for real, so serialization bugs are caught. Disadvantage: cassettes are verbose, and any SDK upgrade that changes request shape invalidates them.
Model level. Substitute the chat model with a fake that returns scripted messages. LangChain ships fake chat models for tests, and for LangGraph you can pass any chat model object to the graph. Advantage: stable and readable. Disadvantage: you skip the provider integration, so you need at least a few HTTP-level or live smoke tests.
Our default: model-level fakes for the bulk of logic tests, plus a small HTTP-level set for the integration path.
Key cassettes on what matters
The common failure of HTTP replay is a cassette that never matches because the request contains something volatile. Strip or normalize before matching:
- Authorization headers (also required for safety; see below).
- Timestamps, request IDs, and random IDs injected into prompts.
- Ordering of tool definitions or retrieved chunks, if your code does not guarantee a stable order.
Match on method, URL, and the request body after normalization. Do not match on headers.
If the prompt includes the current date, inject a clock into your code and freeze it in tests. This is a code change worth making anyway.
Scrub secrets and customer data
A cassette is a file in your repository. Treat it like one.
- Filter API keys and auth headers at record time, not afterward.
- Record against synthetic or already-public inputs where possible. If you record from real traffic, run the same redaction you use for trace export.
- Add a CI check that greps cassettes for key prefixes and obvious PII patterns.
What to assert
Assert on structure and behavior, not on prose.
def test_refund_request_routes_to_approval(replay):
result = run_graph("I want a refund for order 1182", model=replay("refund_route"))
assert result.route == "needs_approval"
assert result.tool_calls[0].name == "lookup_order"
assert result.tool_calls[0].args == {"order_id": "1182"}
Useful targets:
- Which tool was called, with which arguments.
- Which branch the router took.
- That malformed model output triggers your repair or retry path. Script a bad response on purpose; this is the case live tests almost never hit.
- That a timeout or rate-limit error leads to your fallback chain, not a stack trace. A fake that raises is simpler than a cassette here.
- That state after an interrupt and resume matches state before it.
Avoid asserting on exact generated text. That test will pass on replay forever and tell you nothing.
Re-recording is a deliberate act
Cassettes go stale. Set the rule that they are re-recorded by a human, on purpose, with a command like make rerecord, and that the diff is reviewed.
Trigger a re-record when you change a prompt, a tool schema, or the model version. Then read the diff. If the tool-call arguments changed, you found a behavior change before users did.
Do not set the recorder to "record new episodes" in CI. That silently turns a missing cassette into a live call.
Where replay ends and evals begin
Replay tests are a regression net for your code. They stay green when the model gets worse, because the model is not in the loop.
So keep a second layer:
- An offline eval set, run against live models, scored with task metrics. We describe how to start one in our post on bootstrapping an eval suite from production traces.
- Run it on prompt, retriever, and model changes, and gate merges on the eval delta, not on a replayed assertion.
- Run a small live smoke test on a schedule to catch provider-side drift: a changed default, a deprecated parameter, a new rate limit.
A reasonable split for a mid-size app: hundreds of replay tests in every pull request, a few live smoke calls nightly, and an eval run on any change that touches prompts or retrieval.
Trade-offs
- Cassettes add files and maintenance. If your app is a single prompt and a parser, a fake model and three tests are enough.
- HTTP-level recording couples tests to SDK internals. Pin versions, or prefer model-level fakes.
- Replay can create false confidence. If your team starts reading green CI as "the prompts are fine," the net is doing harm. Name the two layers in your test directory so the difference stays visible.
Checklist
- Decide the boundary: fakes for logic, HTTP cassettes for integration.
- Normalize volatile fields; freeze the clock.
- Scrub secrets and customer data at record time; add a CI scan.
- Assert on tool calls, routes, and state, not prose.
- Script failure cases: malformed output, timeouts, rate limits.
- Re-record only by hand, and review the diff.
- Keep evals as the quality gate.