Designing States, Errors and Failure Flows
A UI for long-running AI tasks has to answer four questions: how far has it got (progress), what is happening now (state), why did it go wrong (errors), and when does it need me (intervention). This article covers how to translate system state into the UI completely and accurately.
Core concepts
| Concept | Meaning | How it shows up in the UI |
|---|---|---|
| State machine (finite state machine) | At any moment an object is in exactly one of a finite set of states and can only move between them on defined events | Status label, available actions and layout all follow from the current state |
| Layered state | Model task, step, executor (agent) and connection state separately | The summary shows task state, the timeline shows step state, a corner shows connection state |
| State × UI matrix | Rows are states, columns are UI elements (summary, primary action, timeline, notification); each cell says exactly what to show | Design, code and tests line up against one table |
| "Impossible states" | Contradictory combinations that appear when you model with several booleans, e.g. isLoading && isError | A spinner and an error message on screen at the same time |
| Partial success | Some sub-tasks in a batch succeed and some fail | "7/10 done, 3 failed" + actions for the failed items |
| Intervention point (human-in-the-loop) | The system needs a person to confirm, provide information or authorize before it can continue | A clear alert + one click to where action is needed + automatic resume afterwards |
Typical states of a long-running agent task
Task: queued → running ⇄ waiting_human
│ ↘ retrying ↗
↓
succeeded | partially_succeeded | failed | cancelled
Step: pending / running / retrying(n/N) / blocked / needs_human / succeeded / skipped / failed
Connection: live / reconnecting / offlineHow it works
1. Why a state machine instead of a pile of booleans
Four booleans (isLoading, isError, isPaused, needsHuman) give 16 combinations; most are meaningless, yet any of them can occur. A state machine lists the legal states, each state maps to exactly one UI, and transitions only happen on events. Benefits:
- In design you can enumerate every state and not miss a screen
- In code a TypeScript discriminated union makes the compiler check every branch
- In tests you write one case per state
type TaskState =
| { status: 'queued'; position: number }
| { status: 'running'; step: string; progress?: number }
| { status: 'retrying'; step: string; attempt: number; maxAttempts: number; reason: string }
| { status: 'waiting_human'; request: HumanRequest; since: string }
| { status: 'partially_succeeded'; done: number; failed: FailedItem[] }
| { status: 'failed'; error: TaskError }
| { status: 'succeeded'; resultUrl: string }
| { status: 'cancelled'; by: string }2. Classify failures and give each class different actions
| Class | Example | How to show it | User action |
|---|---|---|---|
| System can recover on its own | Timeout or rate limit, retrying | Yellow, "Retrying automatically 2/3" | None needed; can expand to see why |
| Needs information from the user | Missing parameter, needs authorization, needs a choice | Prominent banner + waiting time | Fill in / authorize / choose |
| Needs a user decision | Partial failure: continue or rerun? | List of failed items | Skip and continue / retry failed items |
| Unrecoverable | Invalid input, permission denied | Red failure card | Edit and rerun / contact support |
3. Three-part error copy
NN/g's error-message guidelines ask that errors be visible, explain the problem in plain language, and offer constructive advice on how to fix it. For AI tasks this can be fixed into three parts:
- What happened: which step, what problem (not just an error code)
- What the system already did: how many retries, what fallback it tried
- What you can do: 1–3 buttons, ordered by recommendation
Example: "The paper search service timed out. We retried automatically 3 times and all failed. You can: retry this step later / switch to the backup search source / skip search and use the papers you uploaded."
4. Elements of an intervention point
| Element | How |
|---|---|
| Seen | In-page banner, unread count in the page title, optional browser notification (needs the user's permission) |
| Reachable | A "Handle it" button opens the right form and focuses its first field |
| In context | Explain why the system needs you and what it has already tried |
| Time-bound | Show how long it has been waiting; state the timeout policy ("this step will be skipped after 30 minutes without a response") |
| Resumable | Confirm immediately after submit ("Got it, the task is continuing") and record it on the timeline |
In practice
State × UI matrix template
| State | Summary copy | Color / icon | Primary action | Secondary action | Notify? | Screen reader announcement |
|---|---|---|---|---|---|---|
| running | Step 3/5: reading papers | Blue / spinner | Pause | Cancel | No | No |
| retrying | Retrying automatically 2/3: search timed out | Yellow / circular arrows | — | View reason | No | No |
| waiting_human | Needs you: add search keywords | Orange / hand | Handle it | Skip | Yes | Yes (polite) |
| partially_succeeded | 7/10 papers read, 3 couldn't be fetched | Orange / half circle | Upload PDF | Skip and continue | Yes | Yes |
| failed | Search failed: service unavailable | Red / cross | Retry from this step | Edit and rerun | Yes | Yes |
| succeeded | Report is ready | Green / check | View report | Export | Yes | Yes |
"While you were away" summary
Users leave long tasks running. Record when the user last looked; when they come back, the first screen shows which steps finished, which errors occurred, and what is waiting for them.
Honest progress
- When the number of steps is known use "step n/N"; when it isn't, just say what it is doing now. Don't fake a percentage
- If the time-remaining estimate is unreliable, hide it, or show "about" and say what it's based on
- If no new events arrive for a while, show "last updated 12:03" so it doesn't look frozen
Common pitfalls
- Designs only for the happy path: error, empty and partial-success states turn out to have no design once coding starts. Check every cell of the matrix.
- Every error says "Something went wrong, please retry": users can't tell whether to wait, change something or give up.
- Showing retrying as a red failure: red while the system is recovering on its own invites the wrong action.
- State shown by color alone: users with color-vision deficiencies can't tell them apart; add icons and text (WCAG 1.4.1).
- Intervention requests buried in the timeline: anything that needs a person should also be collected in an "inbox".
- Optimistically showing "done": the front end assumes success while the back end actually failed. State must follow server events.
Common interview questions
- For an agent task that runs for 30 minutes, which states does the UI need?
- Why model UI state as a state machine? How do you express it in TypeScript?
- What classes of failure are there, and what actions does each give the user?
- How do you design for partial success?
- When the system needs a human, how do you make sure the user sees it and can act quickly?
- What makes a good error message?
Related projects
- Agent Run Console: its section 3 "State model" is the full application of this article — the state × UI matrix, three-part error cards and the human-action inbox are all built there.
- For state modeling on the back end see Task state machine and resumable runs; for when to ask for human help see Human-in-the-loop.
Sources
- NN/g: Error-Message Guidelines — visibility, readability and constructive advice in error messages
- NN/g: 10 Usability Heuristics — "visibility of system status", "help users recognize, diagnose, and recover from errors"
- W3C: Understanding SC 1.4.1 Use of Color — don't convey information by color alone
- TypeScript Handbook: Narrowing / Discriminated unions
- Stately: XState docs — a front-end state machine library (whether to adopt one depends on complexity)
- MDN: Notifications API — browser notifications require user permission
