Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 11.8 — Playbook: An Instrumented Model Client
LLM Engineering · Example 1 of 2

Recipe: the observed model wrapper

Four scripted calls, one of them a 503. Each call leaves a JSON log line (prompt redacted), a span line and a record. Prices are example numbers.

One wrapper around any model that leaves a redacted JSON log line, a trace span and a metrics record for every call, plus a small summary that checks the metrics against limits. Copy it and plug in your real client.

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

Write a Recorder class with a method record(status, latency_ms) and a method summary() that returns "calls=N errors=N". Use it with three calls: ok, error, ok.

02

Write check_limits(summary, limits) where both are dicts; return a sorted list of the names whose summary value is greater than the limit.

Explain it without notes

01

Why wrap the model in one place instead of logging at each call site?

02

Why should the wrapper record a failed call and then re-raise the error?

03

How do you make sure instrumentation does not leak private data?

Recipe: the observed model wrapper
Sign in to run this example in your browser.

Expected output

{"ts":0,"event":"llm_call","request_id":"r1","model":"small","status":"ok","tokens_in":1200,"tokens_out":300,"latency_ms":700,"prompt":"Summarise my invoice"}
{"ts":700,"event":"llm_call","request_id":"r2","model":"small","status":"error","tokens_in":8,"tokens_out":0,"latency_ms":2000,"prompt":"Email [EMAIL] the report"}
{"ts":2700,"event":"llm_call","request_id":"r3","model":"small","status":"ok","tokens_in":40,"tokens_out":10,"latency_ms":250,"prompt":"Translate hello"}
{"ts":2950,"event":"llm_call","request_id":"r4","model":"small","status":"ok","tokens_in":1000,"tokens_out":200,"latency_ms":650,"prompt":"Summarise my receipt"}
span-1 model_call r1 700 ms ok
span-2 model_call r2 2000 ms error
span-3 model_call r3 250 ms ok
span-4 model_call r4 650 ms ok
calls=4 errors=1 tokens_in=2248 tokens_out=510 cost=0.001889 dollars