Recipe: a fake-first model client
The feature is tested against cut-off and empty replies before any real model is involved.
Two recipes to start every LLM feature: a fake model behind the same interface as the real one, so the feature is testable from day one, and a meter that counts tokens, estimates cost and refuses calls over budget. Plus the checklist to run before you ship.
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 5 hands-on exercises with automatic checks.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Extend FakeModel so a scripted reply can be an exception (for example a RateLimitError instance) that complete raises instead of returning, and test that summarise lets it propagate.
Add a report() method to the meter that returns "calls=N tokens_in=N tokens_out=N cost=$X" with the cost to 6 decimals.
Explain it without notes
Why build an LLM feature against a fake model first?
Why does the budget guard check the worst case before the call instead of the actual cost after it?
Expected output
normal reply -> Rain is expected all week in Pune.
cut-off reply -> ERROR: summary was cut off (raise max_tokens or ask for less)
empty reply -> ERROR: empty summary
requests recorded: 3, system prompt: Summarise the user's text in one sentence.