Logs, traces, cost and speed: knowing what your app is doing
Shipping AI Apps · 10 questions
Short notes
Key idea
Once real users arrive, you need to see what the app is doing: which requests are slow, which fail, what each answer cost, and the whole chain of steps behind one reply. That means structured logs, traces and a few key metrics.
Syntax
- Structured logs: one JSON line per request with request ID, user ID (hashed), prompt version, model, tokens in/out, cost, latency, status.
- Traces: one trace per request, with a span for each step (retrieve, LLM call, tool call) and its timing.
- Metrics: requests/min, error rate, p50/p95 latency, cost per day, cost per user.
- Tools: OpenTelemetry for traces; LLM observability tools; or plain logs into a database or cloud logging.
log.info("llm_call", extra={"req": rid, "model": MODEL, "in": n_in, "out": n_out, "ms": ms, "cost_inr": cost})
Example
p95 latency jumps to 12 seconds. The traces show retrieval taking 9 of them after the index grew. Adding a filter brings it back to 2 seconds. Without spans you'd have blamed the model.
Traps
- Logging full prompts with personal data; redact or sample.
- Averages hide slow requests; watch p95.
- No request ID makes it impossible to connect logs across steps.
- Unknown cost per feature means surprises on the bill.
Want the full lesson with step-by-step examples, a project and a certificate? Shipping AI Apps course, ₹180 →
- Question 1easy
Why log each request as one JSON object with fixed fields?
- Question 2medium
Average latency is 1.9 s, p95 is 7 s. What does that tell you?
- Question 3medium
What does this print?
import pandas as pd logs = pd.DataFrame({"request_id": ["a", "b", "c", "d"], "ms": [900, 3800, 1200, 3100], "output_tokens": [40, 260, 70, 230]}) print(logs.sort_values("ms", ascending=False).head(2)["request_id"].tolist()) - Question 4medium
Your slowest requests all have long answers. What's the cheapest fix?
- Question 5medium
Why log requests in FastAPI middleware instead of inside each endpoint?
Logs, traces, cost and speed: knowing what your app is doing
Logs, traces, cost and speed: knowing what your app is doing
Logs, traces, cost and speed: knowing what your app is doing
Logs, traces, cost and speed: knowing what your app is doing
Logs, traces, cost and speed: knowing what your app is doing
5 more questions on this topic, with hints, and tests
Sign up free for 21 days of every question and test.