Implementing Observability with Tracing, Token Analytics and Latency Breakdowns
This lesson explains how to implement observability, the ability to explain a running system's behaviour from its telemetry, with OpenTelemetry and Application Insights: traces of every model and tool call, token usage, safety signals and latency per step.
Learning outcomes
After completing this lesson, you will be able to:
- Set up tracing that follows the OpenTelemetry GenAI semantic conventions.
- Aggregate token usage and safety signals.
- Break down the latency of a slow request by span.
Setting up tracing
Aggregate metrics, such as average latency or total tokens per hour, show that something has changed but not why. A single request to a RAG application or an agent may involve a query rewrite, a search, two model calls and three tool calls, and any of them can be the slow, expensive or failing step. A trace records one request as a tree of spans, one per operation, each with its duration, its inputs and outputs and attributes such as token counts. Reading a trace answers questions that metrics cannot: which step was slow, which tool returned an error, and what the model actually received.
Tracing in Foundry follows the OpenTelemetry GenAI semantic conventions, a standard set of span names and attributes for generative AI operations, so traces from different libraries and frameworks look the same in Application Insights. The application connects the project to an Application Insights resource, retrieves its connection string through the project client, configures the Azure Monitor exporter and enables instrumentation for the OpenAI client. Each model call then produces a span with attributes such as the operation, model, input and output token counts and finish reason.
from azure.monitor.opentelemetry import configure_azure_monitor
from opentelemetry.instrumentation.openai_v2 import OpenAIInstrumentor
from opentelemetry import trace
configure_azure_monitor(connection_string=project.telemetry.get_application_insights_connection_string())
OpenAIInstrumentor().instrument() # spans for every OpenAI client call
tracer = trace.get_tracer(__name__)
with tracer.start_as_current_span("answer_question"): # application span around retrieval and generation
...Prompt and completion content are not recorded by default; setting OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT=true records them, which helps debugging but must comply with privacy policy.
Token analytics and safety signals
Token counts from spans and Azure Monitor metrics are aggregated by application, feature, user segment, deployment and model, often with custom span attributes for the feature name. Analysis reveals expensive prompts, unexpectedly long outputs, low cache hit rates and features whose cost does not match their value. Safety signals come from guardrail annotations and blocked responses, safety evaluations and user reports, and are monitored alongside quality so that changes that improve one do not degrade the other.
Latency breakdowns
A trace shows the duration of each span, so the time of a slow request can be split into retrieval, each model call, each tool call and application code. For streamed responses, time to first token determines perceived responsiveness, while total time determines throughput. The following list shows typical findings and responses.
- Retrieval dominates: reduce semantic ranking candidates, add replicas, cache frequent queries.
- One model call dominates: use a smaller or faster model, shorten the prompt, lower reasoning effort.
- Tool calls dominate: optimize the back end, run independent calls in parallel.
- Long time to first token in chat: stream the response and reduce prompt size.
The following chart shows trace spans for one request before and after an optimization.

The following screenshot shows a trace in the Foundry portal. The timeline splits the 3.24-second run into two model calls of 1.12 seconds and 432 milliseconds and a tool call of 716 milliseconds, so the slowest step is visible at once.

Fix the slow span
A slow agent
An agent's median response time rose from 4 to 11 seconds after a new tool was added. Azure Monitor shows the model's time to response is unchanged.
Open traces for slow runs and compare span durations. If the new tool's spans take 6 seconds, the fix is in the tool's back end or in calling it only when needed; if the agent now makes several extra model calls to decide whether to use the tool, its description and instructions need work. Aggregate deployment metrics alone could not show this.
Are prompts and responses recorded in traces by default?
Show answer
No. Message content is captured only when it is explicitly enabled.
Exam Tips
Remember for the exam
- Azure Monitor exporter plus OpenAI instrumentation produces GenAI spans in Application Insights.
- Message content is captured only when explicitly enabled.
- Use span durations to find which step causes latency.