After the Task Fails: State Management, Bounded Retries, and Outcome Verification
"You said it was fixed—why is my package still heading to my hometown?" The user complained first thing Saturday morning. The logs revealed: the reschedule instruction was indeed sent, but the reschedule service happened to restart in those few seconds; the agent got a timeout error, auto-retried three times, all failed, and then replied "your reschedule is complete". At no point did any link answer the most basic question: was this order actually rescheduled or not? Failure is not scary; what is scary is a system that cannot even tell "which step failed", yet dares to promise the user an outcome.
This post is part of the "AI Customer Service Product and Engineering Practices" experience-sharing series, drawn from a practice review of an AI customer service product at a leading logistics company. In this post I want to share how to design state management, automatic retries, rollback and compensation, and final outcome verification for the tool call failures, task interruptions, and outcomes inconsistent with the user's request that an AI Agent hits during logistics exception handling. This is the last line of defense that keeps a fully managed service from collapsing after something goes wrong.

Five Key Design Points in Failure Recovery
- State management: task and step states must be persisted and resumable from checkpoints.
- Retry strategy: distinguishing retryable from non-retryable errors, controlling retry counts and intervals.
- Compensation and rollback: reverse-compensating reversible operations, intercepting irreversible ones upfront.
- Outcome verification: trusting the business system's real state, not interface return codes.
- Human fallback: seamless handoff after failure without making the user re-explain.
The Overall Design
In this effort, I would invest the effort before failure happens—persist state first, bound the retries, never skip verification, and keep a summary ready for handoff. The overall approach in one sentence: persist the state machine first, retry by error type with bounds, compensate the reversible and intercept the irreversible, verify against real business state, and transfer to humans with a service summary.
Step 1: Persist the state machine, resume from checkpoints after failure
Every task and every step has a state machine: pending, running, succeeded, failed, compensated, transferred-to-human—all persisted, along with parameter versions, operation identifiers, and business acknowledgments. After failure the system can locate the checkpoint and resume, instead of making the user start over; an unknown outcome must be recorded as a distinct state—never mark it failed and blindly re-submit the write.
Step 2: Retry by error type, exponential backoff with jitter
- Retryable: timeouts, rate limiting, system 5xx—use exponential backoff + random jitter, with maximum counts and total duration.
- Non-retryable: parameter errors, insufficient permissions, explicit business rejection—retrying only amplifies the problem; escalate or transfer to humans directly.
Retries must be idempotent: reuse the idempotency key of the same logical operation, and check state before executing, so a retry never becomes a duplicate submission.
Step 3: Compensate the reversible, intercept the irreversible, handle partial success individually
Reversible operations use Saga reverse compensation—for example, a failed reschedule restores the original delivery plan; irreversible operations such as compensation already paid out must be intercepted before execution and escalated for approval—never discuss "system rollback" after the money is gone. Partial success is handled individually, without a wholesale rollback—undoing the two steps already completed because the third failed is a second injury to the user.
Step 4: Never skip outcome verification—trust real business state
An interface success does not mean business success. After execution, query the business system again, confirm the state truly changed, then compare against the user's request: is the rescheduled date right, is the address the one the user wanted. On mismatch, transfer to humans—never reply to the user with an interface return code as the business result.
Step 5: Human fallback with a service summary
On failure or verification mismatch, compile the dialogue, queried information, completed operations, and failure reasons into a service summary so the human agent takes over directly without the user re-explaining—especially critical in fully managed service. Acceptance tests should cover duplicate execution, interruption recovery, compensation failure, changed user intent, and outcome mismatch, measuring recovery success rate and duplicate writes—not who "retried the most".
Practice in Bytedesk
In the open-source Bytedesk customer service system (GitHub, website https://www.weiyuai.cn ), tool invocation logs help locate the failed call step, and tickets provide states like handling, suspended, resumed, and escalated with clear owners to follow up the recovery flow; transfers to humans carry full conversation context, and with a service summary, agents need not re-collect information. Task step checkpoints, unknown-outcome states, and compensation acknowledgments must be persisted on the business side—a ticket resuming does not mean a business write can be safely replayed; idempotency and authoritative state checks remain necessary before replay.
Key Takeaways
- Persist task and step states; after failure, resume from the checkpoint; an unknown outcome is a distinct state—do not mark it failed and re-submit.
- Retry by error type: timeouts, rate limiting, and 5xx retry with bounded exponential backoff and jitter; parameter, permission, and business rejections are non-retryable.
- Idempotency before every retry: reuse the idempotency key of the same logical operation and check state first.
- Reversible operations use Saga compensation; irreversible ones are intercepted and escalated for approval; partial success is handled individually, never wholesale rolled back.
- Verification trusts the business system's real state; on mismatch, transfer to humans with a service summary—never making the user re-describe is the baseline of fully managed service.
