By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
| Piece | Job | Where it lives |
|---|---|---|
| Prometheus client | Records counters and histograms in your app | Inside the backend |
| /metrics endpoint | Exposes numbers in a scrapeable format | Inside the backend |
| Prometheus server | Scrapes and stores time series | Separate container |
| Grafana | Queries Prometheus and draws dashboards | Separate container |
| Alertmanager (optional) | Routes alerts to Slack/PagerDuty | Separate container |
from prometheus_client import Counter, Histogram, Gauge
REQUESTS = Counter("nova_requests_total", "AI requests", ["intent", "status"])
TTFT = Histogram("nova_ttft_seconds", "Time to first token",
buckets=(0.2, 0.5, 1.0, 2.0, 4.0, 8.0))
LATENCY = Histogram("nova_latency_seconds", "Total latency",
buckets=(0.5, 1, 2, 4, 8, 15, 30))
TOKENS = Counter("nova_tokens_total", "Tokens used", ["direction"])
REFUSALS = Counter("nova_refusals_total", "Answers that refused")
JUDGE = Gauge("nova_faithfulness_last", "Last sampled faithfulness score")
def observe(intent, status, ttft, total, tokens_in, tokens_out, refused):
REQUESTS.labels(intent=intent, status=status).inc()
TTFT.observe(ttft)
LATENCY.observe(total)
TOKENS.labels(direction="in").inc(tokens_in)
TOKENS.labels(direction="out").inc(tokens_out)
if refused:
REFUSALS.inc()# FastAPI: mount the metrics endpoint
from fastapi import FastAPI, Response
from prometheus_client import generate_latest, CONTENT_TYPE_LATEST
app = FastAPI()
@app.get("/metrics")
def metrics():
return Response(generate_latest(), media_type=CONTENT_TYPE_LATEST)# prometheus.yml
scrape_configs:
- job_name: nova
scrape_interval: 15s
static_configs:
- targets: ["backend:8000"]# error rate over 5 minutes
sum(rate(nova_requests_total{status=~"5.."}[5m]))
/ sum(rate(nova_requests_total[5m]))
# p95 time to first token
histogram_quantile(0.95, sum by (le) (rate(nova_ttft_seconds_bucket[5m])))
# refusal rate vs a 7-day baseline ratio
sum(rate(nova_refusals_total[1h])) / sum(rate(nova_requests_total[1h]))
/
(sum(rate(nova_refusals_total[1h] offset 7d)) / sum(rate(nova_requests_total[1h] offset 7d)))
# cost per hour from tokens (adjust prices)
sum(rate(nova_tokens_total{direction="in"}[1h])) / 1e6 * 3
+ sum(rate(nova_tokens_total{direction="out"}[1h])) / 1e6 * 15# docker-compose.yml (excerpt)
services:
prometheus:
image: prom/prometheus:latest
volumes: ["./prometheus.yml:/etc/prometheus/prometheus.yml"]
ports: ["9090:9090"]
grafana:
image: grafana/grafana:latest
environment: ["GF_AUTH_ANONYMOUS_ENABLED=true"]
ports: ["3001:3000"]Open Grafana on port 3001, add Prometheus at http://prometheus:9090 as a data source, and build four panels: error rate, p95 TTFT, refusal ratio, sampled faithfulness. That is a real AI quality dashboard in an afternoon.
Key takeaways
Instrument requests, latency, tokens, refusals and a sampled quality score.
Expose /metrics and let Prometheus scrape it; Grafana draws the panels.
Learn four PromQL patterns: rate, histogram_quantile, ratio and offset for baselines.
Annotate deploys on quality graphs — most changes are explained by a release.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.