Your profile

One profile for every MSDEVBUILD site. Only you can see it, and the site owner if you write in.

Lesson 23 of 39
❮Prev Next❯

Tracing, Token Analytics and Latency

Module 2: GenAI & Agents ⏱ 12 min read Updated

Your note

My notes for this lesson

Select any text in the lesson to highlight it or add a note. Saved in this browser.

    No highlights yet. Select a sentence in the lesson, then pick a colour or add a note.

    All my bookmarks and notes

    Reading settings

    For every lesson, saved in this browser.

    Text size
    Line spacing
    Font

    With IaaS you rent the virtual machine. Microsoft looks after the building, power and hardware; you patch the operating system.

    30-second answer

    Tracing records each agent run, model call and tool call as a span with a duration and token counts. Connect an Application Insights resource to the project, opt in to GenAI tracing, configure Azure Monitor and instrument the SDK, then read the spans to see latency by step and tokens by model. Recording message content is a separate switch and should stay off in production, because prompts and answers can contain personal data.

    Simulate it » No Azure account needed

    Exam objective: 2.3.3 Set up observability by implementing tracing, token analytics, safety signals and latency breakdowns. (Skills measured as of April 16, 2026)

    Simulations

    Feel it before you read it. No account needed, nothing leaves this page.

    Azure portal: Read a trace with tokens and latency Connect Application Insights and read a trace with token counts.
    </> Embed

    Embed this simulation

    Free for trainers, teachers and bloggers. Paste this into your page:

    Loading the portal…

    Tracing needs an <strong>Application Insights</strong> connection on the project. That’s where the spans are stored.

    👆 Click the glowing item in the portal to continue

    Read the steps as text
    1. Tracing needs an Application Insights connection on the project. That’s where the spans are stored.
    2. The Tracing page lists runs with duration and tokens. Open the slow one.
    3. Each span is one step: the agent run, a model call with its tokens, and a tool call. The tool call is the slowest, so that’s where to look first.
    4. Tracing in code is opt-in and content capture is a separate switch. Monitoring then gives token and latency charts from the same data.

    🧪 Simulation for learning. Screens are simplified versions of the Azure portal; names, prices and IDs are examples. Nothing here touches real Azure.

    😟 THE PROBLEM

    Replies take four seconds, the bill keeps growing, and nobody knows which step is to blame.

    😊 THE SOLUTION

    Trace every run and read tokens and latency by span.

    • Slow runs✓ Latency by span
    • Token spend✓ Tokens by model and operation
    • Prompts holding personal data✓ Content recording off in production
    🛠️ Solved with: OpenTelemetry tracing with Application Insights

    Start with a question

    Why does a delivery app show you each stop and how long it took?

    When a parcel arrives late, a total time doesn’t help. You want to see the steps: packed, left the depot, waited at the hub, out for delivery. The long wait at the hub tells you where to fix things.

    A trace is that tracking page for an AI run. Each step is a span with its time and, for model calls, its tokens. You see where the time and the money went.

    Explain it like I’m 10

    The technical explanation

    What a trace looks like

    SpanDurationTokens
    invoke_agent order-agent2140 msn/a (the whole run)
    chat support-chat (plan)610 ms412 in
    execute_tool get_order_status940 msn/a
    chat support-chat (answer)590 ms96 out

    Read it top to bottom: the agent run contains model calls and tool calls. The slowest child is the first place to look.

    Set it up

    1. Connect Application Insights to the project (Foundry portal, Tracing, or a connection).
    2. Opt in to the GenAI instrumentation, which is a preview as of 25 November 2026.
    3. Configure Azure Monitor with the project’s connection string and instrument the SDK.
    # pip install azure-ai-projects opentelemetry-sdk azure-core-tracing-opentelemetry azure-monitor-opentelemetry
    # export AZURE_EXPERIMENTAL_ENABLE_GENAI_TRACING=true                  (opt in)
    # export OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT=true       (dev only: records prompts and answers)
    from azure.ai.projects.telemetry import AIProjectInstrumentor
    from azure.monitor.opentelemetry import configure_azure_monitor
    
    configure_azure_monitor(connection_string=project.telemetry.get_application_insights_connection_string())
    AIProjectInstrumentor().instrument()
    
    openai = project.get_openai_client(agent_name="order-agent")
    conversation = openai.conversations.create()
    print(openai.responses.create(conversation=conversation.id, input="Where is order ORD-1042?").output_text)

    Token analytics and latency in KQL

    Spans carry attributes such as gen_ai.request.model, gen_ai.usage.input_tokens and gen_ai.usage.output_tokens. Summarise them:

    dependencies
    | where timestamp > ago(1d) and isnotempty(customDimensions["gen_ai.operation.name"])
    | extend model = tostring(customDimensions["gen_ai.request.model"]),
             tokens_in = toint(customDimensions["gen_ai.usage.input_tokens"]),
             tokens_out = toint(customDimensions["gen_ai.usage.output_tokens"])
    | summarize calls=count(), tokens_in=sum(tokens_in), tokens_out=sum(tokens_out),
                p50=percentile(duration, 50), p95=percentile(duration, 95) by name, model

    Use it to answer: which model costs most, which operation is slow, and is the p95 much worse than the p50? Safety signals work the same way: filter results and blocked requests show up as annotations you can count and alert on.

    Privacy

    Message content recording is off by default and separate from tracing. Turn it on in development to debug prompts. In production keep it off unless your policy allows it, restrict who can read the Application Insights data, and set a retention period.

    Architect level Client-side and service-side views

    Client-side tracing, shown here, instruments your code with OpenTelemetry, so you see the app’s own spans next to the SDK calls. Foundry also shows agent runs and monitoring dashboards in the portal. Use the client-side traces to follow one request across your own services, and the portal views to watch quality, tokens and latency over time. Both read from Application Insights.

    Real-world business scenario

    Retail

    Contoso Retail (fictional)

    The problem
    The order agent took about four seconds per reply, the monthly bill rose, and nobody knew which step caused either.
    What they used
    Application Insights connected to the Foundry project, GenAI tracing opted in, content recording off in production, and a KQL query for tokens and p50 and p95 latency by model and operation.
    Why that choice
    The trace showed the get_order_status tool took 940 ms and the planning call used 412 input tokens that a shorter prompt could cut.
    The result
    The team added a cache for the order lookup and trimmed the prompt, and the p95 latency dropped without touching the model.

    When to use it and when not to

    ✓ Use it when…

    • You need to find slow or costly steps in a model or agent run.
    • You must audit which tools an agent called.
    • You want token and latency numbers by model and operation.

    ✕ Think twice when…

    • Recording message content in production without a privacy decision.
    • Looking only at average latency.
    • Tracing instead of evaluating quality. Traces don't say if answers are right.

    ↔ Alternatives to consider

    • Evaluators for answer quality.
    • Azure Monitor metrics for simple request counts and errors.
    • Foundry monitoring dashboards for trends over time.
    Ready for more?The full lesson adds the technical explanation, when to use it, interview questions and revision notes.

    Test yourself with exercises

    5 original practice questions in the AI-103 style. Pick your answers, then check them. You will see why each option is right or wrong. Score 75% or more and this topic is marked complete.

    Question 1

    Question 1 of 5

    Agent runs take about four seconds and the team must find the slowest step.

    What should they examine?

    Question 2

    Question 2 of 5

    A finance team needs token usage per model and operation.

    Where do these numbers come from?

    Question 3

    Question 3 of 5

    Compliance does not allow prompts and answers containing personal data to be stored in telemetry.

    What should you do?

    Question 4

    Question 4 of 5

    A developer wants traces from an agent to appear in the Foundry portal.

    What must be connected to the project?

    Question 5

    Question 5 of 5

    The GenAI tracing instrumentation for the Foundry SDK is a preview that must be explicitly enabled.

    Which action enables it?

    Interview preparation

    Say your answer out loud first, then open the model answer. Interviewers care more about your reasoning than the exact words.

    Beginner What is a trace?

    Model answer

    It's the story of one run, step by step. Each step, like a model call or a tool call, is a span with how long it took and, for models, how many tokens it used.

    Intermediate How do you find the slowest step of an agent run?

    Model answer

    I open the trace and compare span durations. In the example the tool call took 940 ms and the model calls 610 ms and 590 ms, so the tool is the first thing I would look at.

    Experienced developer How do you get token usage per model?

    Model answer

    Model spans record input and output token counts and the model name. I query those attributes in Application Insights with KQL, summarising by model and operation, and compare p50 and p95 latency too.

    Scenario Compliance says no personal data may be stored in telemetry. What do you check?

    Model answer

    That message content recording is off, so spans keep timings, token counts and names but not prompts or answers. I also check retention and who can read the Application Insights data.

    Quick revision

    Key points

    • Spans show duration and tokens for each step.
    • Application Insights stores the traces.
    • Tracing is opt-in; content recording is a separate opt-in.
    • Query token attributes to analyse cost by model.
    • Look at p95 latency, not just the average.

    Important terminology

    Trace
    The record of one request or agent run, made of spans in order.
    Span
    One timed step inside a trace, such as a model call or a tool call.
    OpenTelemetry
    The open standard for collecting traces, metrics and logs that Foundry tracing builds on.
    Application Insights
    The Azure Monitor service that stores the traces and lets you query them.
    Token analytics
    Looking at input and output tokens by model and operation to understand cost.
    Content recording
    An opt-in setting that stores prompts and answers in spans. Useful for debugging, risky for privacy.

    Common mistakes

    • Forgetting to opt in, then seeing no spans.
    • Leaving content recording on in production.
    • Judging latency only by the average.

    Exam tips

    • "Find the slowest step" = span durations in the trace.
    • "No prompts in telemetry" = content recording off.
    • "Where traces are stored" = Application Insights connected to the project.

    Five quick revision questions

    1. 1.What is one timed step called?Show

      A span.

    2. 2.Where are traces stored?Show

      Application Insights.

    3. 3.Is content recording on by default?Show

      No, it is a separate opt-in.

    4. 4.What does a model span record?Show

      Model name and token counts.

    5. 5.Why check p95 latency?Show

      It shows the slow tail that averages hide.

    Official references

    Finished this lesson? Tick it off. Your progress is saved in this browser.

    Next up: Orchestrate Multiple Models and Rules Engines

    Studying with a friend? Send them this lesson

    It's free and needs no sign-up. Their progress saves in their own browser.

    Lesson done? Make it stick with a quick gameSort It, Match the Pairs, Lightning Round and Who Am I? Earn XP and badges. Play ❯

    Install MSDEVBUILD Azure

    Free · no account · works offline

    Save the whole course for offline ›

    Hi, I’m your Nimbus

    Your study buddy. Pick what we do next.

    MSDEVBUILD · Multi-Stack Dev Build

    Sign in to MSDEVBUILD

    One account for Azure certifications and the blog: sign in here and you are signed in on both. Your XP, streak, practice deck and certificate stay in sync on every device. Optional: everything works without an account.