Automate before adding an agent
Start with deterministic rules and steps. Introduce a model only where understanding, classification, or exception handling requires judgment. Keeping the model inside a bounded node makes the workflow easier to test, pause, and resume.
Workflow skeleton
trigger -> validate -> load context -> AI decision -> human approval (optional)
-> execute -> verify result -> notify and audit
Every node needs an input, output, timeout, retry policy, and failure destination. External APIs and queues can deliver duplicates, so action nodes need idempotency keys. Long runs should save checkpoints and resume from the latest successful step.
Isolate non-determinism
The model node should return schema-constrained fields or candidate actions, never write to a database directly. A rules node then checks permissions, amount, target, and business state. Low-confidence or high-risk results go to a human queue, and the final decision is recorded.
const proposal = await classify(input);
const action = ActionSchema.parse(proposal);
if (!policy.allows(action, context)) return pause("approval_required");
await executeOnce(action, { idempotencyKey: run.id });
Observability and replay
Record the run, step, input summary, model and prompt versions, latency, tokens, retries, and final state. Store only necessary summaries or hashes for sensitive data. Use fixed samples for regression and compare classification accuracy, human-escalation rate, success rate, and unit cost. “The model sounded right” is not an acceptance criterion.
When not to use AI
Use code for fixed field mappings, thresholds, permission checks, and financial calculations. When the error cost is high and the rule is expressible, deterministic logic is cheaper and easier to audit.
Sources
See Temporal workflow concepts, n8n error handling, and the OpenAI structured outputs guide.
