Skip to content
AgenticAIWiki
  1. Learn
  2. Part 13
  3. 67. Observability and tracing
  4. 67.2

Lesson 67.2 · 2 of 9 · 4 min read · Reviewed 24 Nov 2026

Token, cost and latency dashboards

By the end, you can

  • Build dashboards for tokens, cost and latency.
  • Pick percentiles and slices that reveal problems.

A traceTrace: A record of everything that happened during one request or agent run, as a tree of timed steps. explains one run. Dashboards show the shape of thousands.

The core panels

Panel Notes
Requests per minute, errors per minute Split errors by type: rate limit, overload, timeout, tool failure
Input, output and cached tokens Rising input tokens often means context is growing unchecked
Cost per day, per feature, per request Computed from tokens and the price sheet, not guessed
Time to first token What users feel in a streaming UI
Total run time What matters for agents and batch jobs
Steps per agent run A jump often means a loop or a confused agent

Percentiles, not averages

If 95 runs take 2 seconds and 5 take 60, the average is about 5 seconds, which looks fine and describes nobody. Chart p50, p95 and p99.

Slicing

Tag every request with feature, model, prompt version and tenant. When cost doubles, you want to see at once that one feature on one prompt version caused it.

Alerts

Alert on things a person should act on: error rate above a threshold, p95 latencyLatency: The time from sending a request until you get the response, or until the first token arrives when streaming. far above normal, cost per hour well above forecast. Lesson 71.3 covers budgets.

Key points

  • Watch three families of numbers: volume (requests, tokens), cost, and latency (time to first token and total).
  • Use percentiles (p50, p95, p99), not averages: a few very slow agent runs hide inside a good average.
  • Slice by feature, model, prompt version and tenant so a change shows up where it happened.
Hands-on labRuns in the Playground

Find the slow tail with percentiles

Show why p95 and p99 reveal slow runs that an average hides.

You will have: A per-feature latency summary with average, p50, p95, p99 and an alert flag.

Steps

  1. Run the starter and note that every percentile prints 0.0.
  2. Implement nearest-rank percentile().
  3. Set the ALERT flag when p95 is above 10 seconds.
  4. Compare the chat average with its p50 and p95.

Done when

  • chat shows p95 of 45.0s and is flagged ALERT.
  • search stays ok with p95 under 2 seconds.
  • The chat average (about 5 s) matches neither the fast runs nor the slow ones.

Open in the Playground No API key needed.

Starter code (Python)
# Percentiles, not averages: summarise run times per feature and alert on the tail.
import math

FAST = [1.8, 2.1, 1.9, 2.4, 2.0, 2.2, 1.7, 2.3, 2.0, 1.9]
RUNS = {
    "chat": FAST * 9 + [2.1, 2.0, 1.9, 2.2] + [45, 48, 50, 52, 55, 60],  # 100 runs, 6 very slow
    "search": [0.9, 1.1, 1.0, 1.2, 0.8] * 20,                           # 100 runs, all quick
}
P95_ALERT_SECONDS = 10.0


def percentile(values, q):
    """Nearest-rank percentile: the smallest value with at least q of the data at or below it."""
    # TODO: sort the values and return the one at index ceil(q * n) - 1
    return 0.0


def summary(values):
    return {"avg": sum(values) / len(values), "p50": percentile(values, 0.50),
            "p95": percentile(values, 0.95), "p99": percentile(values, 0.99)}


for feature, runs in RUNS.items():
    s = summary(runs)
    # TODO: set flag to "ALERT" when p95 is above P95_ALERT_SECONDS
    flag = "ok"
    print(f"{feature:<7} n={len(runs)} avg={s['avg']:.1f}s p50={s['p50']:.1f}s p95={s['p95']:.1f}s p99={s['p99']:.1f}s {flag}")
Show a solution
# Percentiles, not averages: summarise run times per feature and alert on the tail.
import math

FAST = [1.8, 2.1, 1.9, 2.4, 2.0, 2.2, 1.7, 2.3, 2.0, 1.9]
RUNS = {
    "chat": FAST * 9 + [2.1, 2.0, 1.9, 2.2] + [45, 48, 50, 52, 55, 60],  # 100 runs, 6 very slow
    "search": [0.9, 1.1, 1.0, 1.2, 0.8] * 20,                           # 100 runs, all quick
}
P95_ALERT_SECONDS = 10.0


def percentile(values, q):
    """Nearest-rank percentile: the smallest value with at least q of the data at or below it."""
    s = sorted(values)
    return s[max(0, math.ceil(q * len(s)) - 1)]


def summary(values):
    return {"avg": sum(values) / len(values), "p50": percentile(values, 0.50),
            "p95": percentile(values, 0.95), "p99": percentile(values, 0.99)}


for feature, runs in RUNS.items():
    s = summary(runs)
    flag = "ALERT" if s["p95"] > P95_ALERT_SECONDS else "ok"
    print(f"{feature:<7} n={len(runs)} avg={s['avg']:.1f}s p50={s['p50']:.1f}s p95={s['p95']:.1f}s p99={s['p99']:.1f}s {flag}")
Quick checkAverage latency looks fine, but users complain the assistant is sometimes very slow. What should you chart?
How sure are you?
Pick an answer and how sure you are