The service skeleton: auth, quota, history, events, log
A fake model streams the reply. Each call returns a status and the SSE lines a client would receive.
A small service skeleton you can copy: authenticate, enforce a tenant quota, build a budgeted context, call the model, stream events, store the exchange and log the outcome, plus a pre-launch review that checks the production basics.
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.
Write sse(event) that turns a Python dict into one line data: <compact json> (use json.dumps with separators=(",", ":")) followed by a blank line, and print the encoding of {"delta": "Hi "} with repr.
Write join_events(lines) that takes the SSE lines of the skeleton (strings starting with data: ) and returns the text made from all delta events joined together, ignoring other events.
Explain it without notes
Walk through the steps of a request in the skeleton and say why they are in that order.
What belongs in the final event of a stream?
What would you add before taking this skeleton to production?
Expected output
200 9 events, first: data: {"delta":"Sure. "}, last: data: [DONE]
200 9 events, first: data: {"delta":"Sure. "}, last: data: [DONE]
429 1 events, first: data: {"error":"quota"}, last: data: {"error":"quota"}
401 1 events, first: data: {"error":"unauthorized"}, last: data: {"error":"unauthorized"}
tenant=acme status=200 context=1
tenant=acme status=200 context=2
tenant=acme status=429