Skip to content
Streak:0Loading your progress…Sign in

Module 14 · Lesson 1 of 2

Observability & cost

Know what your system is doing and what it costs, per request. ~25 min

Lesson notes

For every LLM request, log (as structured JSON): request/trace id, user id (pseudonymous), model, prompt version, input/output/cached tokens, latency, stop reason, tool calls, and outcome. Redact secrets and personal data at the logger. Use OpenTelemetry traces so one user action shows retrieval, each model call and each tool call as spans.

Cost levers, cheapest first: prompt caching for stable prefixes, trimming context, smaller/cheaper models for easy sub-tasks, lower reasoning effort where quality holds (measure with evals!), batch APIs for offline work, and per-user quotas so one runaway loop can't burn the budget.

Practice

  1. 1. Redacting logger

    medium25 XP

    Write redact(obj) returning a deep copy of nested dicts/lists where (a) any dict key that (case-insensitively) contains password, secret, api_key or authorization, or ends with token, has its value replaced by "[REDACTED]", and (b) email addresses inside any string are replaced with "[EMAIL]". Non-container values pass through. Then log_event(event, **fields) -> str returns a JSON line {"event": ..., **redacted fields} with sorted keys.

    Ctrl/⌘ + Enter runs the tests · Esc then Tab leaves the editor

  2. 2. LLM cost calculator

    easy10 XP

    Write cost_usd(usage, prices) where usage has input_tokens, output_tokens, cache_read_tokens (default 0) and prices has per-million-token USD rates input, output, cache_read. Return the cost rounded to 6 decimals. Then monthly_cost(requests_per_day, avg_usage, prices, days=30) rounded to 2 decimals.

    Ctrl/⌘ + Enter runs the tests · Esc then Tab leaves the editor