Sub topic 14
Monitoring and maintaining the router
This chapter assumes you have the classify, route, and evaluate functions from earlier chapters, and that you know accuracy and a confusion matrix. A router that scored well on your labeled set can still get worse after launch. Monitoring is how you find out.
Why routers degrade
The model and prompt may not change, but the input does. Three things usually cause trouble:
- Input drift. Customers start writing about a new product or a new kind of problem.
- Label drift. Your team adds a queue or changes what "account" means, and the label definitions go stale.
- Model changes. A provider updates the model behind an alias, and the same prompt behaves differently.
You rarely see these in an error message. You see them as shifts in the numbers.
What to log
Log one record for every routing decision. Include a timestamp, a message ID, the predicted label, the confidence, the destination queue, the number of attempts, and the prompt version. Store the message text only if your privacy rules allow it. Otherwise store a reference to it.
The prompt version matters. Without it you cannot tell whether a change in behavior came from the data or from your edit.
Signals to watch
| Signal | What a change suggests |
|---|---|
| Share of items sent to human triage | Drift, or a threshold that no longer fits |
| Share of items labeled "other" | New topics with no label yet |
| Label distribution compared to a baseline | Input drift or a model change |
| Mean confidence | Calibration shift |
| Retry and fallback rate | Malformed output, often after a model change |
| Latency and cost per item | Longer inputs, more retries |
Here is a small tracker for the first four signals.
from collections import Counter
class RouterStats:
def __init__(self):
self.total = 0
self.labels = Counter()
self.triaged = 0
self.confidence_sum = 0.0
def record(self, label, confidence, queue):
self.total += 1
self.labels[label] += 1
self.confidence_sum += confidence
if queue == "human-triage":
self.triaged += 1
def report(self):
if self.total == 0:
return {}
return {
"triage_rate": self.triaged / self.total,
"other_rate": self.labels["other"] / self.total,
"mean_confidence": self.confidence_sum / self.total,
"distribution": {k: v / self.total for k, v in self.labels.items()},
}
def distribution_shift(baseline, current):
"""Total variation distance between two label distributions (0 to 1)."""
keys = set(baseline) | set(current)
return 0.5 * sum(abs(baseline.get(k, 0.0) - current.get(k, 0.0)) for k in keys)Compute a report for each day or week. Compare its distribution to a baseline taken from a healthy period. Set an alert when distribution_shift or triage_rate crosses a limit you choose from your own history. A fixed number copied from a tutorial will not fit your data.
Keep measuring accuracy
Rates tell you something changed. They do not tell you whether the router is right. Each week, sample a small set of recent decisions, including some low-confidence ones, and have a person label them. Add these to your labeled set.
Then run evaluate on the grown set. Fresh examples show weaknesses the original set missed.
Changing things safely
Treat the prompt like code. Give it a version, and run evaluate on the full labeled set before and after each edit. Ship the edit only if accuracy holds and the confusion matrix shows no new problem pair. Pin the model to a specific version where your provider allows it, and re-run the evaluation before moving to a new one.
When "other" keeps collecting the same kind of message, read a sample. If a pattern is clear, add a label with a definition and a few examples, then re-evaluate. Do not add labels for rare patterns.
Exercise
Over two weeks, the share of items labeled "other" rises from 4% to 15%, while accuracy on your fixed labeled set stays the same. What is the most likely cause, and what do you do first?
Show answer
The labeled set is old, so it cannot show new kinds of input. The likely cause is input drift: new topics that fit no existing label. First, read a sample of recent "other" items. If they share a pattern, add a label with a definition, add examples, and re-run evaluate on a labeled set that now includes those items.