AI Engineer Portal
Your personal operating system for career transition.
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:
- Creates a root trace with a
trace_id,request_id, and start timestamp. span(name: str, **attrs) -> SpanContext— returns a context manager that creates a child span capturingstart_ms,end_ms,latency_ms, and all keyword attributes.- Spans auto-complete on context manager exit, even if the body raises (record the error).
export() -> TraceExport— returns the complete trace as a structured object.TraceExporthas:trace_id,request_id,total_latency_ms,spans: list[SpanData],llm_calls: list[LLMCallSummary].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
Evaluation and review loops
Separate the scoring logic from the interpretation logic. Your goal is not just a number; it is a useful next action.
- - Produces a useful signal, not decorative output
- - Makes regression review easier
- - Would support a benchmark or observability loop
- - 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.