Instrument a multi-operation AI API (chat, RAG Q&A, summarization) with full observability: distributed tracing, structured cost logs, async quality sampling, drift detection, and three SLOs with automated alerting.
Every request └─ traced_llm_call() → span with model, tokens, latency └─ make_request_log() → structured JSON to stdout / log aggregator └─ maybe_score_response() → async 5% sample → quality_scores table └─ SLO checker (runs every 5 min) → alert if breach detected └─ Nightly drift job → KS test baseline vs current → Slack alert if drifting
@observe() decorator or manual Span objects. Each span must capture: model, input_tokens, output_tokens, latency_ms, operation.CostRecord: model, input_tokens, output_tokens, operation, cost_usd. Write to a SQLite table (local dev) or PostgreSQL (production).cost_budget_exceeded event and print a warning. Simulate triggering it by sending 1,000 test requests.maybe_score_response() with 5% sampling rate, running asynchronously after the response has been sent to the user (background task or asyncio).detect_quality_drift(baseline, current) and verify it flags the drift as statistically significant.regression_gate(threshold=0.90). Confirm it passes on the good prompt and fails on the degraded prompt.check_all_slos() that reads from your cost and quality DB tables and returns compliance ratios. Schedule it to run every 5 minutes (use a simple while loop with time.sleep(300) for local dev).# Final integration test — run all observability checks
import asyncio
async def observability_health_check() -> dict:
slo_status = check_all_slos(PRODUCTION_SLOS)
quality_status = detect_quality_drift(
get_quality_scores(days_ago=14, days=7),
get_quality_scores(days_ago=7, days=7),
)
cost_status = {
"daily_cost": get_daily_cost(),
"budget_remaining": DAILY_BUDGET - get_daily_cost(),
}
return {
"slos": slo_status,
"quality_drift": quality_status["is_drifting"],
"cost": cost_status,
"healthy": (
all(v["compliant"] for v in slo_status.values())
and not quality_status["is_drifting"]
and cost_status["budget_remaining"] > 0
),
}
status = asyncio.run(observability_health_check())
print(f"System healthy: {status['healthy']}")
for slo_name, slo_data in status['slos'].items():
marker = "✓" if slo_data["compliant"] else "✗"
print(f" {marker} {slo_name}: {slo_data['current']:.1%} (target: {slo_data['target']:.1%})")Graduation criteria:All three SLOs tracked and alertable, quality drift detector successfully flags the degraded prompt, regression gate blocks the degraded configuration, and cost tracking correctly attributes spend to each operation with <5% error in cost calculation.