How event-sourced sagas transformed multi-step business processes from error-prone to bulletproof
This article is part of the “Building with AI” series documenting my journey using multi-agent AI workflows to build production systems.All examples are from personal projects and do not represent employer technologies.
January 2026. My SaaS platform needed to implement product bundle/unbundle workflows for subscription management. The requirement: customers should be able to remove items from a bundle, get appropriate price adjustments, and trigger approvals if changes are significant.The Problem: What happens if Step 3 fails after Steps 1-2 succeed? You’re left with a half-completed operation and corrupted state.AI Focus: Can AI design a saga pattern that handles compensation logic automatically?System Example: A state machine workflow with automatic rollback using the SagaStep macro for multi-step business processes.
Builder agent generated the SagaStep macro to eliminate boilerplate:
Auto-implement compensation methods
Track step execution order
Handle partial rollbacks
I defined the business rules (what triggers approval, how to calculate price adjustments) and reviewed the compensation logic for edge cases.AI doesn’t know your business constraints—it needs you to define “approval required if >50% components removed.”
2,073 lines of workflow code, 457 lines of integration tests, 11 test scenarios covering happy path + compensation. Zero partial failures in production.
Mistake: The initial saga implementation didn’t handle idempotency. If a compensation step failed and we retried, it would try to “cancel approval” twice.Why it failed: AI designed pure compensation logic but didn’t consider retry scenarios. DynamoDB would throw ConditionalCheckFailedException on the second cancel attempt.How we fixed it: Added idempotency checks to compensation methods:
Copy
async fn compensate_create_approval(&mut self) -> Result<()> { if let Some(approval_id) = self.approval_id { // Check if already canceled match get_approval_status(approval_id).await? { ApprovalStatus::Canceled => return Ok(()), // Already done ApprovalStatus::Pending => cancel_approval(approval_id).await?, _ => {} // Other states don't need cancellation } } Ok(())}
Lesson: AI designs correct forward and backward flows but misses production edge cases like retry idempotency. Humans need to add defensive checks.
AI excels at state machine generation. Give it business rules + compensation requirements, and it generates perfect forward/backward flows with all the boilerplate.
AI Weakness
AI doesn’t think about retry scenarios or idempotency. It assumes happy path execution and single-pass compensation.
Human Role
Humans define business rules (approval thresholds, pricing formulas) and add production hardening (idempotency, error recovery).
Process Insight
The best workflow design separates business logic (pure functions) from orchestration (saga). AI is perfect for generating orchestration boilerplate.
Separate logic from orchestration - Pure business functions (validate, calculate) + saga for orchestration. AI generates the saga, you write the business logic.
Design compensation first - Don’t start coding until you know how each step rolls back. AI can help design this if you give it the requirements.
Add idempotency everywhere - Compensation steps will retry. Make sure they can run multiple times safely.
Pro tip: Use the SagaStep macro pattern even for non-saga workflows. The compensation boilerplate alone saves 150 lines per workflow, and it forces you to think about rollback logic upfront.
How do you handle multi-step workflows? Manual rollback or automated compensation?Connect on LinkedIn or comment on the YouTube Short
Disclaimer: This content represents my personal learning journey using AI for a personal project. It does not represent my employer’s views, technologies, or approaches.All code examples are generic patterns or pseudocode for educational purposes.