Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 0.7 — Playbook: Your First LLM Feature
LLM Engineering · Example 2 of 2

Recipe: a usage and cost meter with a budget

Prices are example numbers. Read your provider's price page and keep prices in configuration.

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.

01

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.

02

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

01

Why build an LLM feature against a fake model first?

02

Why does the budget guard check the worst case before the call instead of the actual cost after it?

Recipe: a usage and cost meter with a budget
Sign in to run this example in your browser.

Expected output

call 1: ok, total so far 0.000160 dollars
call 2: ok, total so far 0.000320 dollars
call 3: ok, total so far 0.000480 dollars
call 4: ok, total so far 0.000640 dollars
call 5: ok, total so far 0.000800 dollars
call 6: refused (budget 0.001000 would be passed)
calls=5 tokens_in=5000 tokens_out=750
cost per 1,000 such calls: 0.1600 dollars