Topic 2 of 4 · 4 min read
Recording and replaying real API responses
Goal: Record real responses as fixtures with keys and personal data scrubbed and replay them deterministically in CI.
Record one real exchange with the provider, scrub it, save it in the repo, and replay it in tests: you get the real response shape without the real call. A hand-written mock shows what you think the API returns. A recording shows what it returned.
Check yourself · 1 of 3
What does a recorded fixture give you that a hand-written mock does not?
Why it matters
Mocks drift. The provider adds a field, changes how a stream ends or wraps an error differently, and your fake stays the same. A recorded fixture is a real sample, so tests parse real data. Replay is also deterministic: the same bytes every run, with no key in CI.
How to do it
- Pick a tool. VCR.py in Python records HTTP exchanges into "cassette" files. In JavaScript, Mock Service Worker, Polly.js or a small
fetchwrapper that writes files does the same job. The idea is identical: first run records, later runs replay. - Record on purpose. Set the mode so that CI can never record. In VCR.py use
record_mode="none"in CI, so a missing cassette fails instead of hitting the network. - Scrub before saving. Remove the
Authorizationorx-api-keyheader, organisation ids and any personal data in prompts or replies. Use the tool's filter hooks (filter_headersin VCR.py) so scrubbing is automatic, not remembered. Then read the file once before committing. - Match requests sensibly. Match on method, URL and a stable part of the body. If you match on the full body, any wording change to your prompt breaks every cassette. If you match on nothing, a wrong request still gets a reply.
- Refresh on a schedule. Re-record when you change the model, the prompt or the SDK version, and every few months. Review the diff of the new file: a changed field is exactly what you wanted to learn.
Before and after
# Before: key and customer email end up in git
@vcr.use_cassette("summary.yaml")
def test_summary(): ...
# After: scrubbed, and CI can never record
my_vcr = vcr.VCR(
filter_headers=["authorization", "x-api-key"],
record_mode="none" if os.getenv("CI") else "once",
)
@my_vcr.use_cassette("summary.yaml")
def test_summary(): ...
What goes wrong
- Committing secrets. An unfiltered header is a leaked key in git history.
- Stale fixtures. The model was retired months ago and the tests still pass on old data.
- Brittle matching. Every prompt tweak invalidates all cassettes, so people stop re-recording and start deleting tests.
- Treating a replay as proof. It shows your parser handles that response. It does not show the provider still sends it.
Examples
Scrubbing a recorded response
A cassette holds a request with x-api-key: sk_live_...redacted and a reply that quotes a customer's email address. The filter removes the header. A review step replaces the email with user@example.com, and a check script fails the commit if a string matching a key pattern or an email is found in the fixtures folder.
Refreshing after a model change
You move from one model version to the next. You delete the cassette, run the test locally with recording on, and compare the new file with git diff. The stop reason field is now end_turn where it used to be stop. The diff showed you a parser bug before users did.
Key points
- A recording captures what the provider really returned, so tests parse real data instead of a guess.
- CI should replay only: a missing fixture must fail, never trigger a live call.
- Scrub keys, organisation ids and personal data automatically with filters, then read the file before committing.
- Match on method, URL and a stable part of the body, so small prompt edits do not break every fixture.
- Re-record when the model, prompt or SDK changes and review the diff of the new file.
Common mistakes
- Recording without header filters, which commits an API key to the repo.
- Letting CI record when a cassette is missing, so a broken test quietly calls the provider.
- Matching on the full request body, so every prompt edit breaks all fixtures.
- Never re-recording, so tests pass against responses from a model that no longer exists.
Check yourself · 2 of 3
A cassette recorded the request header x-api-key. What is the right fix before committing?
Check yourself · 3 of 3
CI runs a test whose cassette file is missing. Which behavior is best?
Practice items, not counted towards your mastery.
Start here
- AI SDK Core: testing (opens in a new tab)DocsFree
Mock language models and test helpers that return fixed text, streams and errors without network calls.
- Mock Service Worker documentation (opens in a new tab)ToolFree
Intercepting HTTP requests at the network layer, so the real SDK runs against a fake server.
- VCR.py documentation (opens in a new tab)ToolFree
Record modes, header filtering and replay for HTTP cassettes in Python tests.