Interview's MateAI Engineering
Interview's Mate

AI Engineering

LLMs, RAG, agents & evals

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
AI Engineering topics
Lesson 77/ 160
04. Evals

OpenTelemetry for LLMs

Standard OTel instrumentation patterns for LLM spans, attributes, and exporters.

AdvancedOTel Spans · Attributes3 quiz questions
Practice Generative AI Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidHands-onQuiz (3)Practice

In plain words

OpenTelemetry is a common language for apps to report what they did, so any monitoring tool can read the AI parts the same way as the rest of the system.

Think of it like…

A universal power plug and wiring standard: any appliance (instrumented library) works with any socket (backend), and you don't rewire the house when you change the supplier.

Where you’ve seen it

A request enters an API gateway (trace context in the W3C traceparent header), calls a Python RAG service that is auto-instrumented, which calls a vector database and then a model provider. The LLM span has name chat {model} and attributes such as gen_ai.operation.name = chat, gen_ai.request.model, gen_ai.usage.input_tokens = 3100 and gen_ai.usage.output_tokens = 420. The app exports OTLP to a Collector with a batch processor, a redaction processor that drops prompt content from attributes and a tail sampling policy that keeps 100% of error traces and traces slower than 5 seconds plus 5% of the rest. The Collector fans out to two backends: an LLM platform for prompt and eval workflows and the company's existing tracing system for latency and error alerts.

Words you’ll hear

OTLP
The OpenTelemetry protocol for sending traces, metrics and logs to a Collector or backend.
GenAI semantic conventions
Standard attribute and metric names for model calls and agent steps.
Collector
A pipeline service that receives, processes (batch, redact, sample) and exports telemetry.
Tail sampling
Deciding which traces to keep after they complete, based on errors or latency.

Visual diagram

Loading interactive diagram

Theory & deep dive

What the GenAI conventions standardise

A model call is a span (conventionally named like chat gpt-model or {operation} {model}) with attributes under the gen_ai. namespace: the operation (chat, text completion, embeddings), the provider, the requested model and response model, request parameters (temperature, max tokens), token usage (input and output tokens), and finish reasons. Tool calls and agent steps have their own span conventions. Companion metrics include token usage histograms and operation duration (and time to first token for streaming), which feed latency and cost dashboards without parsing traces.

Prompts and completions can be recorded as span attributes or events, but that capture is opt-in because it may contain personal or confidential data. Libraries such as OpenLLMetry (Traceloop), OpenInference and provider-specific instrumentation packages implement the conventions for popular SDKs and frameworks, and many LLM platforms (Langfuse among them) accept OTLP, so the same spans can feed more than one tool. Because parts of the specification were still marked as development or experimental, check the current version and pin your instrumentation libraries.

An LLM span

name: "chat model-x"; gen_ai.operation.name: "chat"; gen_ai.request.model: "model-x"; gen_ai.usage.input_tokens: 3100; gen_ai.usage.output_tokens: 420; duration: 3.4 s The same attributes can be summed into cost per request or per feature.

Key takeaway: Standard gen_ai attributes and metrics make model calls portable; content capture is opt-in.

Context propagation, the Collector and sampling

Context propagation carries the trace id across process boundaries using the W3C traceparent header (HTTP) or message headers (queues), so a span in the retrieval service, a background worker and the LLM call all join the same trace. Make sure frameworks, thread pools and async tasks propagate context, since lost context produces orphan spans. The Collector sits between apps and backends to batch exports, add or remove attributes, redact sensitive content, apply tail sampling (it needs the complete trace to decide, so it holds spans in memory briefly), and route to several destinations.

Design for cost and safety: keep full prompt and completion payloads only for sampled or flagged traces, limit attribute sizes, and set retention. Metrics are cheap and always on (token counts, duration, error rate, TTFT by model and feature); traces are rich and sampled. Compared with a vendor SDK, OTel adds some setup, but it avoids lock-in and unifies AI telemetry with the rest of the platform. Many teams combine both: OTel instrumentation feeding an LLM platform for prompt management, datasets and evaluation workflows that general tracing backends lack.

Tail-sampling policy

Keep a trace if it has an error status, or total duration above 5 s, or a negative-feedback score; otherwise keep 5%. At 50,000 requests a day with 4% problem traces, you store about 2,000 problem traces and 2,400 sampled normal ones, about 4,400 instead of 50,000.

Key takeaway: Propagate context, process in the Collector, redact and tail-sample, and send metrics always and traces selectively.

How it works, step by step

Step 1 of 4
Engineering

1. Instrument with OTel

Use auto-instrumentation for model SDKs and frameworks, and add manual spans for retrieval, tools and business steps.

gen_ai.* attributes
Key numbers at this step
Pin
library versions

When to use it & trade-offs

What it is

OpenTelemetry (OTel) is the vendor-neutral standard for emitting traces, metrics and logs. For LLM applications it adds the GenAI semantic conventions: agreed attribute names for model calls (operation name, provider, requested and response model, input and output token counts, finish reasons) and metrics (token usage, operation duration, time to first token). Instrumenting with OTel means your LLM spans use a common schema, travel through the same OTLP pipeline and Collector as the rest of your services, and can be sent to any backend (an LLM platform such as Langfuse, Grafana Tempo, Datadog, Jaeger) without rewriting the instrumentation.

Why it matters: LLM calls are rarely isolated: a request hits an API gateway, a retrieval service, a vector database, a model provider and a tool backend. Using OTel puts those spans into one trace with correct parent-child links across services, avoids lock-in to a single LLM tool and lets existing SRE tooling and alerting cover AI features. The caveat is that the GenAI conventions are still evolving, so you pin versions and check the current specification.

When to reach for it

  • LLM features inside a multi-service architecture
  • Reusing existing alerting and dashboards for AI traffic
  • Keeping the option to switch observability vendors

When to avoid or be careful

  • Capturing prompts and completions by default without a privacy review
  • Exporting every trace with no sampling
  • Mixing custom attribute names that break dashboards

OpenTelemetry vs a vendor SDK for LLM telemetry

OpenTelemetry instrumentation
Best for: Multi-service systems and avoiding lock-in

Strengths: Standard schema; unified with existing observability; many backends.

Watch out: More setup; conventions still evolving.

Vendor or platform SDK
Best for: Fast start with rich LLM-specific features

Strengths: Simple integration; prompt, dataset and eval tools.

Watch out: Coupling to one tool.

Both together
Best for: Portable telemetry plus LLM workflows

Strengths: Best of each; export OTLP into the platform.

Watch out: More moving parts to run.

Interview signals & numbers to know

When this topic comes up

  • “One trace across the gateway, retrieval and model call”
  • “Avoid vendor lock-in for observability”
  • “GenAI semantic conventions”
  • OTLP and Collector
  • Tail sampling

Benchmarks & rules of thumb

gen_ai.usage.* -
token attributes used to compute cost
~4,400 of 50,000 -
traces stored under the example sampling policy
5 s -
latency threshold in the example tail-sampling rule

Common mistakes & follow-ups

Common interview mistakes

  • Losing trace context across a queue or thread pool, creating orphan spans.
  • Recording full prompts in span attributes for all traffic.
  • Assuming the conventions are frozen and not pinning versions.

Likely interviewer follow-ups

  • What are the GenAI semantic conventions?
  • How do you sample LLM traces without losing the interesting ones?
  • When would you choose OTel over an LLM-specific SDK?
Interview tip: Say "metrics always, traces sampled, content opt-in". It's a compact answer that covers cost, privacy and usefulness.
Loading hands-on exercise

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply OpenTelemetry for LLMs with generative AI interview Q&A and hands-on practice.

Open Generative AI Q&A →Tech Learn Q&A
Previous topic
Langfuse
Next topic
P95 Latency