Sub topic 10
Monitoring a Routed System Over Time
This chapter assumes you know how rule-based routers and cascades work, how to compute expected cost per request, and how to pick a threshold on a labeled sample. Those choices were made on data from one moment. Production traffic changes, models are updated, and prices move. Monitoring tells you when your routing policy has stopped matching reality.
Why a tuned router drifts
A threshold of 0.8 was good for the sample you labeled. It can stop being good for several reasons:
- Traffic mix shifts. A new feature sends longer or harder prompts, so the rule-based markers no longer match the work.
- Model changes. A provider updates the small model. Its confidence scores may shift even if its accuracy does not.
- Price or latency changes. The cost ratio between tiers moves, so the best escalation rate moves too.
- Seasonal or topical changes. Users ask about new subjects the sample never covered.
None of these raises an error. The system keeps returning answers, so you only notice by measuring.
What to log for every request
Log one structured record per request. Without it you cannot compute anything below.
import json
import time
def log_route(request_id, route, confidence, escalated,
latency_ms, tokens_in, tokens_out, fell_back):
record = {
"ts": time.time(),
"request_id": request_id,
"route": route, # "small" or "large"
"confidence": confidence, # None for rule-based routes
"escalated": escalated,
"latency_ms": latency_ms,
"tokens_in": tokens_in,
"tokens_out": tokens_out,
"fell_back": fell_back, # True if the fallback path ran
}
print(json.dumps(record))Avoid logging raw prompts unless your privacy policy allows it. Logging the prompt length and the route is often enough.
Metrics worth tracking
Compute these over a rolling window, such as the last hour and the last day.
| Metric | What it tells you | Warning sign |
|---|---|---|
| Share of requests routed to the large model | Whether traffic or rules have shifted | A sudden rise or fall |
| Escalation rate (cascade) | How often the small model is not confident | Drift away from the rate you measured in the sweep |
| Mean confidence of the small model | Calibration shift after a model update | A step change at a deploy date |
| Cost per request | Real spend against your expected-cost formula | Gap between measured and expected cost |
| p50 and p95 latency per route | Whether cascades are adding delay | p95 growing faster than p50 |
| Fallback rate | Reliability of each model | Any sustained increase |
Compare measured cost per request with the value from expected_cost_cascade. If the escalation rate rises from 20% to 40%, the formula predicts the cost increase, and monitoring shows it happening.
diff-not-usedWatching quality, not just cost
Cost and latency are easy to log. Quality is harder, because no label arrives with each response. Two practical approaches:
- Audit a small random sample. Each day, pull a few dozen requests from each route. Have a person or a trusted grader score them. Include requests the small model answered with high confidence, since overconfident wrong answers are the costly failure.
- Shadow the large model. For a tiny fraction of small-model answers, also call the large model and compare. Disagreement is a rough signal, not a proof of error.
Both methods let you recompute accuracy at your current threshold, using the same idea as the sweep function from the evaluation chapter, on fresh data.
def window_stats(records):
# Each record: {"route": str, "escalated": bool, "cost": float}
n = len(records)
if n == 0:
return None
escalated = sum(1 for r in records if r["escalated"])
large = sum(1 for r in records if r["route"] == "large")
total_cost = sum(r["cost"] for r in records)
return {
"escalation_rate": escalated / n,
"large_share": large / n,
"cost_per_request": total_cost / n,
}A simple alert rule
Alert on deviation from a baseline, not on a fixed number. Record the escalation rate from your evaluation, then flag windows that differ by more than a tolerance.
def check_escalation(stats, baseline, tolerance=0.10):
if stats is None:
return False
return abs(stats["escalation_rate"] - baseline) > toleranceUse a window large enough that noise does not trigger alerts. With only 20 requests in a window, a rate of 0.30 versus 0.20 is a difference of two requests.
The monitoring loop
When an alert fires, resist changing the threshold immediately. First find the cause. A rise in escalation after a small-model update may call for a new threshold. A rise after a new product feature may call for new routing rules instead.
Practical habits
- Pin model versions where the provider allows it, and treat any version change as a reason to re-run the sweep.
- Dashboard cost per route as well as the total. A stable total can hide a shift between routes.
- Review the alert tolerances after the first few weeks of real data.
Exercise
Over a week, your cascade's escalation rate rises from 20% to 35%, and mean small-model confidence drops on the same day the provider updated the small model. Cost per request rises. Name two checks you would run before changing the threshold, and one possible fix.
Show answer
First, check whether the traffic mix changed by comparing prompt lengths and topics before and after that day. If it did not, the cause is likely the model update. Second, audit a sample of recent small-model answers and re-run the sweep on fresh labeled data to see whether accuracy at the current threshold changed. A possible fix is to lower the threshold if the new model's confidence scores are lower but its accuracy is unchanged, which brings the escalation rate and cost back down.
Exercise
Why is it better to alert on a deviation from a baseline escalation rate than on a fixed value such as 0.5?
Show answer
A good escalation rate depends on your traffic and threshold. One system may normally escalate 10% and another 40%. A deviation from your own measured baseline signals change in your system, while a fixed value may never fire or may fire constantly.