affectUI › Components › Agents

Agent State Machine

One agent as a state diagram – every state it can be in is a card, every transition a line with a count of how often the run took it: forward to the right, back to an earlier state around underneath, a retry as a loop on top. The current state glows in the agent's blue with its pixel mark twinkling, a waiting state is amber, the final one carries a bullseye; when the agent moves on, a light runs along that transition and its count rolls up. Point at a state and its transitions come forward; click a state or a count and its card says how long the agent spent there and what made it leave.

Part of affectUI Pro. Install: npx shadcn@latest add https://www.affectui.com/r/agent-state-machine.json · Import from @/components/ui/agent-state-machine

When to use

  • Showing what an agent is doing right now – and every state it can be in – as a diagram people know from UML
  • Seeing how a run went: which paths it took, how often it looped back, where it spent its time
  • Runs that pause for a person, where someone approves right where the agent waits

When not to use

  • Every message and tool call in order – use Agent Sequence
  • Several agents handing work to each other – use Handoff Swimlanes
  • How long each step took on a time axis – use Trace Waterfall

Usage

import { AgentStateMachine, type MachineState, type MachineTransition } from "@/components/ui/agent-state-machine";

const states: MachineState[] = [
  { id: "plan", label: "Planning", color: "#8b5cf6", tools: [{ name: "Linear", logo: <LinearLogo /> }], visits: 3, time: 6000 },
  { id: "tool", label: "Calling tool", color: "#0ea5e9", tools: [{ name: "GitHub", logo: <GitHubLogo /> }], visits: 2, time: 9000 },
  { id: "ask", label: "Waiting for you", kind: "waiting", col: 1, row: 1 },
  { id: "done", label: "Done", kind: "final" },
];

const transitions: MachineTransition[] = [
  { from: "plan", to: "tool", label: "needs data", count: 2 },
  { from: "tool", to: "tool", label: "timeout, retry", count: 1 },
  { from: "tool", to: "plan", label: "not enough", count: 1 },
  { from: "plan", to: "ask", label: "needs approval" },
  { from: "ask", to: "tool", label: "approved" },
  { from: "tool", to: "done", label: "answered" },
];

export function Run() {
  return (
    <AgentStateMachine
      agent={{ id: "atlas", name: "Atlas", model: "Claude Opus 5.5", logo: <ClaudeLogo /> }}
      states={states}
      transitions={transitions}
      current="tool"
      onApprove={() => api.approve()}
    />
  );
}

Props

NameTypeDefaultDescription
statesMachineState[]Every state – { id, label, kind?, description?, color?, tools?, time?, visits?, col?, row? }; the first one is where the run starts
kind"normal" | "waiting" | "error" | "final"Waiting turns amber and can be approved in its card, error turns red, final carries a bullseye and turns green once reached
tools{ name, logo? }[]The tools the agent uses in that state – small overlapping tiles with their logos on the card, by name in its card
colorstringThe color of the state's icon
time / visitsnumberTime spent in the state so far (ms) and how often the run entered it – shown on the card
transitionsMachineTransition[]{ from, to, label, count?, detail?, id? } – forward lines go right, back to an earlier state over the top or underneath, a retry in the same state is a loop
currentstringThe state the agent is in now – it glows; when it changes along a transition, a light runs along it
agent{ id?, name, model?, logo? }The header – the agent's pixel mark, its name, and the model with its maker's logo
onApprove / onReject(state) => voidShow Approve and Reject in the card of a waiting state the agent is in – the card closes after either
onSelect({ state } | { transition } | null) => voidA state's or a count's card opened or closed
formatTime(ms) => stringFormats the time in a state – 12s, 2m 05s by default
title / classNamestringThe heading instead of the agent's name, and an extra class on the root

Accessibility

  • Every state and every count is a button with aria-expanded – Enter or Space opens its card
  • A state reads as its name, whether it's the current one, how often it was entered and how long the agent spent there
  • A count reads as its trigger, from where to where and how often – "needs data: Planning to Calling tool, 2 times"
  • Where the agent is now is announced as it changes; the header never repeats the same words
  • A card opened with the keyboard takes focus – on a waiting state straight to Approve; Esc closes it from anywhere and focus goes back
  • The lines, dots and the light are decoration and hidden from screen readers – each card lists where its state leads
  • A focus ring shows only for the keyboard
  • The classic style has a rounder card with a hairline border, rounder states, pill tags and buttons and firmer type – the same behavior as the clean one

Responsive

  • Fills its container's width – spare room widens the gaps between the columns
  • Short of room the gaps narrow first; still too wide, the whole diagram scales down to fit – it never scrolls sideways
  • Long state names end in …; the full name is in the card – Arabic and other scripts read in their own direction
  • Counts get thousands separators – 1,284× – and from 10,000 on a card they're short – 12K×; times that aren't numbers are left out
  • With more than three tools a card shows two tiles and +n; short of room the tiles give way, never the time or the count
  • Transitions to or from states that don't exist and repeated ids are left out; states nothing leads to go after the rest
  • A long chain in one row scales down a lot – give such states rows so the diagram stays readable
  • Lines back to an earlier state go over the top where nothing sits above their ends, otherwise underneath; pin states with col and row where they'd cross a card
  • Cards open below what you clicked, above it near the bottom of the screen
  • Dark mode built in: switches when an ancestor (usually <html>) has the class dark or data-theme="dark"

Requirements

  • React 19

Related

  • Agent Sequence – One agent run as a sequence diagram – a column for you, every agent and every tool, and each message an arrow from one lifeline to the next: calls solid, replies dashed, every label a tag in its line's color, a bar on the lifeline while a participant works, parallel and retried messages in labelled frames; new messages draw themselves across with a light at the tip, and a click on a participant or a message opens its card
  • Handoff Swimlanes – Who does what in an agent run, and where the work changes hands – a process diagram with a lane for you, every agent and every tool. Each task is a box in the lane of whoever does it, a handoff an arrow to the next one, an approval a diamond; done boxes are quiet, the running one shimmers in its agent's color, waiting is amber, failed is red, and a light runs along every handoff that's happening now. Click a box, a diamond or a lane and its card opens – an approval waiting for you can be approved right there
  • Agent Tree – Who started whom – an orchestrator and its sub-agents as a live tree, with status, cost and time on every card, costs rolling up each branch, and branches that fold to one line
  • Trace Waterfall – Where the time and money of an agent run go – every LLM call, tool call and sub-agent as a bar on a live timeline, with tokens and cost per step, the slowest call marked and branches that fold into their parent