Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 9.6 — Fakes and Recorded Responses
LLM Engineering · Example 2 of 2

Record once, replay by a hash of the request

The live model is itself a fake here so the page can run it. tiny_hash is h = (h * 31 + code) % 1000000007, identical in all three languages.

Most tests of an LLM feature should not call a real model. A fake model with scripted replies tests your code (parsing, retries, fallbacks, prompt assembly), and recorded real responses replayed by a request hash test it against realistic data, for free and in milliseconds.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 6 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Write ScriptedModel(replies) with a complete(text) method that returns the replies in order and raises IndexError with the message script exhausted when asked for more. Call it three times with two replies and print what happens.

02

Write make_key(system, user, temperature) that returns the tiny_hash of system + "|" + user + "|" + str(temperature) (hash: h = 7, then h = (h * 31 + ord(ch)) % 1000000007 for each character). Print the key for ("s", "u", 0) and show that changing the temperature to 1 changes it.

Explain it without notes

01

What bugs does a fake model catch, and what does it not catch?

02

Describe record and replay for model calls, and what goes into the key.

03

Why should a changed prompt make a replay fail?

04

Which kinds of test would you run on every commit, and which on a schedule?

Record once, replay by a hash of the request
Sign in to run this example in your browser.

Expected output

recording: 3 live calls, 3 replies saved
  key 283656824: {"category": "billing", "priority": "high"}
  key 472295308: {"category": "bug", "priority": "high"}
  key 761913016: {"category": "other", "priority": "low"}
replay: 0 live calls, same answers: yes
after editing the prompt: no recording for key 40295764