Workflow orchestration
A workflow owns the decisions
AI automation uses a model as one bounded component inside a predictable process. An agent may select its next step; a workflow defines transitions in application code. For a support queue, a worker can extract a category and urgency, but the host validates the output, checks authorization and decides whether to prepare a routing preview or escalate. Start with the structured-output and human-approval lessons. Here billing and technical are the only routine categories; urgent and unknown tickets need human review. The input is a fabricated extraction, not a real model result.
def route(extracted, authorized): # Model output is untrusted; authorization comes from the host. if not authorized: return "denied" if not isinstance(extracted, dict) or set(extracted) != {"category", "urgent"}: return "review" category, urgent = extracted["category"], extracted["urgent"] if type(category) is not str or type(urgent) is not bool: return "review" if urgent or category not in {"billing", "technical"}: return "review" return category + "_preview"
print(route({"category":"billing", "urgent":False}, True))print(route({"category":"technical", "urgent":True}, True))print(route({"category":"billing", "urgent":False}, False))Separate ingestion, extraction and routing
A producer accepts a ticket, assigns a stable tenant-scoped ID and version, and stores a minimal reference in a queue. A worker retrieves only permitted data, extracts a strict category and boolean urgency, and checks allowed values. The state machine is received → queued → validated → preview or review; denied requests terminate. Queueing buffers bursts but does not increase downstream capacity. Use worker concurrency limits, backpressure and an age limit. Acknowledge a message only after durable disposition, not merely after a model response. In production, authenticate at ingress and enforce permissions again at the worker.
Handle retries and duplicate delivery
A transient failure may recover; a permanent validation failure needs review rather than repeated calls. Bound attempts and deadlines, respect service retry guidance, add exponential backoff with jitter and use a dead-letter queue for exhausted work. A standard at-least-once queue can redeliver a message. Use a stable key scoped by tenant, ticket and operation version; a repeated identical payload returns the existing outcome, while a changed payload under the same key is a conflict. This toy ledger is in memory and sequential, with no network or sleeping. It records only preview success, not a real effect. Empty outcomes mean no attempt was observed; the fixture routes to dead_letter. Do not copy this as a distributed exactly-once implementation.
def consume(ledger, key, payload, outcomes, max_attempts): # Local, sequential simulation. Only success records a preview. if type(max_attempts) is not int or max_attempts < 1: raise ValueError("positive attempt limit required") if key in ledger: return ("duplicate" if ledger[key] == payload else "conflict", 0) for attempts, outcome in enumerate(outcomes[:max_attempts], 1): if outcome == "ok": ledger[key] = payload return "preview", attempts if outcome != "transient": return "review", attempts return "dead_letter", min(len(outcomes), max_attempts)
ledger = {}key = ("tenant-a", "ticket-1", 1)print(consume(ledger, key, "billing", ["transient","ok"], 3))print(consume(ledger, key, "billing", ["ok"], 3))print(consume(ledger, key, "technical", ["ok"], 3))print(consume(ledger, ("tenant-a","ticket-2",1), "billing", ["transient","transient","ok"], 2))Connect to backend reliability patterns
Real consumers need durable uniqueness, transactions or an outbox plus a downstream idempotency mechanism. A timeout may mean an effect happened but its response was lost: reconcile before retrying an ambiguous write. Keep authorization and approval independent of model output. Dead-letter replay requires a reason, corrected configuration and authorization; replay with the same logical key. Log ticket ID, transition, attempt number, latency and error class without message bodies or secrets. Monitor queue depth, oldest age, retry rates, dead-letter count, duplicate conflicts and escalation rates. The official queue and retry references in source-evidence.json connect these concepts to backend systems; repository-specific lesson links cannot be verified here.
Practice the state boundaries
Try a missing urgent field, a string instead of a boolean, a denied ticket and an unknown category. Then simulate a permanent failure, exhausted retries and a duplicate with a different payload. Explain why none authorizes an external write. Sketch a persistent state machine with a unique operation key and a separate human review queue. Reference: ref-ai-workflow-orchestration. Next, compare the rule-only and mock extraction routes on the same cases.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Workflow orchestration quiz
Assess deterministic routing, reliability, evaluation and human review.
Who owns the final routing transition?
What happens to an authorized urgent ticket?
Why use a queue?
What does repeating the same successful key and payload return?
What should happen after an ambiguous write timeout?