方寸 Portal

AI Engineer Portal

Your personal operating system for career transition.

Private mode

Exercise

Build an LLM call tracer with span export

Build an LLM Call Tracer with Span Export

Observability for LLM applications goes beyond logging. When a user sees a bad response, you need to reconstruct the entire request lifecycle: what was retrieved, what prompt was built, which model was called, what the tokens and latency were, and where time was spent.

Distributed tracing — using traces and spans — is the right abstraction. A trace represents one user request end-to-end. Spans within the trace represent individual steps (retrieval, prompt construction, LLM call, post-processing).

What to build

Implement a RequestTracer that:

  1. Creates a root trace with a trace_id, request_id, and start timestamp.
  2. span(name: str, **attrs) -> SpanContext — returns a context manager that creates a child span capturing start_ms, end_ms, latency_ms, and all keyword attributes.
  3. Spans auto-complete on context manager exit, even if the body raises (record the error).
  4. export() -> TraceExport — returns the complete trace as a structured object.
  5. TraceExport has: trace_id, request_id, total_latency_ms, spans: list[SpanData], llm_calls: list[LLMCallSummary].
  6. SpanContext.add_llm_call(model, input_tokens, output_tokens, latency_ms) — attach an LLM call summary to the active span.

Constraints

  • Standard library only.
  • Spans form a flat list (no tree — single level for simplicity).
  • export() should be callable multiple times without side effects.

Evaluation / medium / Step 15 of 36

Practice stage

Evaluation and review loops

Hint

Separate the scoring logic from the interpretation logic. Your goal is not just a number; it is a useful next action.

Success criteria
  • - Produces a useful signal, not decorative output
  • - Makes regression review easier
  • - Would support a benchmark or observability loop
Review checklist
  • - Would this output help decide what to fix next?
  • - Are important failure modes visible?
  • - Does the score hide any ambiguity I should record?

Practice

Generate a variation

Generate a new exercise variation to deepen understanding or practice a related concept.

Attempt history

Recent submissions

Before you submit, decide what a strong answer should make obvious to the reviewer.

No attempts yet.