Unit 08.02: Handling the exception that breaks the flow
On real data, the exceptions are not the edge. They are frequently most of the volume on a first pass.
Every exception needs a named destination
Triage sorts inputs into the normal path or a named exception route. An exception with nowhere to go becomes an agent improvising.
The code triages four invoices.
INVOICES = [
{"id": "INV-1", "amount": 240.0, "currency": "USD", "po": "PO-88"},
{"id": "INV-2", "amount": 240.0, "currency": "USD", "po": None},
{"id": "INV-3", "amount": -50.0, "currency": "USD", "po": "PO-91"},
{"id": "INV-4", "amount": 240.0, "currency": "GBP", "po": "PO-92"},
]
def triage(inv):
if inv["po"] is None:
return "exception: no purchase order -- route to procurement"
if inv["amount"] < 0:
return "exception: credit note, not an invoice -- route to finance"
if inv["currency"] not in {"USD", "EUR"}:
return "exception: unsupported currency -- route to a human"
return "normal path"
for inv in INVOICES:
print(f"{inv['id']} {triage(inv)}")
normal = sum(1 for i in INVOICES if triage(i) == "normal path")
print(f"\n{normal}/{len(INVOICES)} on the normal path")
# Three of four are exceptions, which is realistic for a first pass over real
# data. Each has a named destination -- an exception with nowhere to go becomes
# an agent improvising, which is the failure this whole module is about.
Three of four are exceptions - a missing purchase order, a credit note, an unsupported currency - and each has a specific destination. That proportion is realistic and it is why the triage step exists at all.
The credit note is the instructive one. A negative amount is not a malformed invoice; it is a different document type that arrived in the same queue. Routing it to finance is correct, and asking an agent to "handle" it would produce a confident attempt to approve a payment of minus fifty.
The mistake this prevents
The mistake is discovering the exception rate after launch. Run the triage rules over a month of real historical data before building anything else - if three quarters of items are exceptions, the automation's value is much smaller than the proposal assumed, and better to know now.
Takeaway
Triage before automating, on real historical data, and give every exception a named destination. An exception with nowhere to go is where an agent starts improvising.
