Start free

Zero to FDE / Blog / LLM observability explained: traces, cost and what to log

Blog

LLM observability explained: traces, cost and what to log

LLM observability means recording what your system did on each request, so you can explain any answer, any failure and any cost. For a single model call that is a log line. For an agent it is a trace: one record per step, linked by an id.

Why it is different from ordinary monitoring

A normal service fails with an error code. An LLM system can return a confident, well-formatted, wrong answer with status 200. Uptime and latency graphs will not show it. You need the inputs and outputs of every step.

What to record for every step

FieldWhy
Trace id and step nameTo rebuild the whole request in order
Model and prompt versionTo know which change caused a regression
Input and output tokensCost is tokens multiplied by price
LatencyUsers feel the slowest step
Tool name, arguments, result or errorMost agent failures are tool failures
Retrieved document idsTo tell a retrieval problem from a generation problem
User or tenant idTo answer “what happened to this customer”

Be careful with content. Prompts and outputs often contain personal data. Decide what you store, for how long and who can read it before you turn logging on.

Traces and spans

A trace is one request from start to finish. Each step inside it, such as a retrieval or a model call, is a span with a start time, a duration and attributes. This is the standard model from OpenTelemetry, which also publishes naming conventions for generative AI calls so that tools agree on field names.1

The idea in fifteen lines

import json, time, uuid

def traced(step, fn, trace_id, **attrs):
    start = time.perf_counter()
    out = fn()
    print(json.dumps({"trace_id": trace_id, "step": step, "ms": round((time.perf_counter() - start) * 1000), **attrs}))
    return out

trace_id = uuid.uuid4().hex[:8]
docs = traced("retrieve", lambda: ["refund-policy.md"], trace_id, k=1)
text = traced("generate", lambda: "Refunds within 7 days [refund-policy.md]", trace_id, input_tokens=412, output_tokens=18)

It prints one JSON line per step with a shared trace_id. A tracing library does the same thing with nesting, sampling and a viewer.

Four numbers worth a dashboard

  • Cost per request, and per customer.
  • Latency at the 50th and 95th percentile.
  • Tool error rate and steps per task.
  • Eval score on sampled live traffic, so quality has a graph too.

Observability and evals work together

Traces show you what failed in production. Those failures become new cases in your eval set, and the eval set stops them returning. Tracing, cost control and guardrails are Week 9 of the Zero to FDE roadmap.

Zero to FDE is a self-paced 16-week course, ₹2,999 once, with a 7-day full refund. Week 1 is free, and one Week 4 lesson is free if you already write backend code. It has no live classes, no mentors and no accredited certificate.

Sources

  1. OpenTelemetry, semantic conventions for generative AI systems

Zero to FDE is a 16-week path, for working engineers and AIML graduates, to the work Forward Deployed Engineers do: backend basics, LLMs, RAG, agents, evaluation and deployment, with a shipped deliverable every week. Week 1 is free and complete, no card needed.

Start Week 1 free

More from the blog