AI Observability

Unlocking AI Visibility: A Guide to OpenTelemetry for Machine Learning Systems

As artificial intelligence shifts from experimental prototypes to mission-critical production systems, the complexity of observability has grown exponentially. Traditional monitoring tools that focus on server uptime and request latency are no longer sufficient. When dealing with Large Language Models (LLMs) and complex neural networks, developers need to understand not just if a request succeeded, but why it took three seconds and how it compared to previous inferences. This is where OpenTelemetry (OTel) becomes indispensable.

Why Standard Monitoring Fails for AI

In a standard web application, a request is a simple linear transaction. In an AI pipeline, a single user query might trigger a chain of events: a retrieval-augmented generation (RAG) search, a context window preparation, a vector database lookup, and finally, an LLM inference. Each step introduces latency and potential points of failure. OpenTelemetry provides a vendor-neutral, language-agnostic framework to collect telemetry data—traces, metrics, and logs—and send them to your preferred backend. For AI engineers, this means correlating the cost of a specific model call with its performance and accuracy metrics.

The Core Triad: Traces, Metrics, and Logs

To effectively observe an AI system, you must implement all three pillars of OpenTelemetry: 1. Traces: A trace represents a single request as it flows through your system. In AI, a trace might start when a user types a prompt and end when the LLM generates the final token. By adding custom attributes, you can track the model version, token count, and latency for each step. 2. Metrics: These provide aggregated data. You should track the distribution of token generation times, the success rate of vector lookups, and the overall GPU utilization. 3. Logs: Detailed contextual information, such as the specific error returned by the model API or the raw input data for debugging hallucinations.

Practical Implementation with Python

Implementing OpenTelemetry in a Python-based AI stack is straightforward. Below is an example of how to instrument a simple function that calls an LLM. We use the opentelemetry-api and opentelemetry-sdk libraries to create a span that wraps our inference logic.
import time
from opentelemetry import trace

# Initialize the tracer provider (configuration omitted for brevity)
trace.set_tracer_provider(...)
tracer = trace.get_tracer(__name__)

def generate_response(prompt: str) -> str:
    # Create a new span to track this specific inference
    with tracer.start_as_current_span("llm_inference") as span:
        # Add custom attributes relevant to AI observability
        span.set_attribute("gen_ai.request.model", "gpt-4")
        span.set_attribute("gen_ai.request.max_tokens", 500)
        
        start_time = time.time()
        try:
            # Simulate API call to LLM
            response = call_external_llm_api(prompt)
            
            # Record success and duration
            span.set_status(trace.StatusCode.OK)
            return response
        except Exception as e:
            # Record error details
            span.set_status(trace.StatusCode.ERROR, str(e))
            span.record_exception(e)
            raise

def call_external_llm_api(prompt):
    # Placeholder for actual API call
    return "This is a simulated response."
In this example, the gen_ai.request.model attribute is particularly valuable. When analyzing data in a backend like Jaeger or Datadog, you can filter traces specifically by the model used, allowing you to compare performance differences between "gpt-4" and "gpt-3.5" directly within the trace view.

Best Practices for Production

When scaling OpenTelemetry for AI, keep sampling rates in mind. AI inference can be expensive, and sending every single trace to your backend may incur high storage costs. Use sampling strategies to only trace a percentage of requests or to always trace requests that exceed a certain latency threshold. Additionally, ensure you are using standard semantic conventions for AI. The OpenTelemetry community has defined specific conventions under the gen_ai prefix (e.g., gen_ai.system, gen_ai.request.messages). Adhering to these ensures that your data is readable and compatible with a wide range of observability tools.

Conclusion

OpenTelemetry is more than just a tool for debugging; it is a foundational component of modern MLOps. By instrumenting your AI pipelines with traces, metrics, and logs, you gain the visibility needed to optimize costs, improve response times, and maintain high-quality outputs. As AI systems become more complex, the ability to observably navigate that complexity will be the difference between a prototype and a production-ready product. Start instrumenting your models today to build the next generation of reliable AI applications.
Share: