Edit messages or regenerate AI responses, then switch between alternative replies. Branching navigation built into assistant-ui's React chat UI.
Branching lets users navigate between alternative versions of a message. A new branch is created when:
- A user message is edited
- An assistant message is reloaded (reload creates a new branch on the same message)
Where the branches live depends on who holds the messages. LocalRuntime keeps the whole tree itself. With ExternalStoreRuntime your store holds one path, the messages array: when an edit or a regenerate replaces the end of that array, the runtime keeps the replaced messages as the other branch, and calls your setMessages with the chosen path when the user switches. To hand the runtime a tree you already have, such as one loaded from your database, pass it as messageRepository (see External store).
A thread is a tree, not a list. Every message records its parent, and messages that share a parent are branches of each other. The thread shows one path through the tree, from the first message to the head, and the branch picker on a message moves that path to a sibling. Nothing is deleted when you edit or regenerate.
The panel reads the tree with thread.export(). It returns an ExportedMessageRepository: every message with its parentId, plus the headId. That is also the shape a ThreadHistoryAdapter loads, which is how saved threads keep their branches.
Shortest Working Pattern
Place a branch picker inside your message component:
import { BranchPickerPrimitive } from "@assistant-ui/react";
const BranchPicker = () => (
<BranchPickerPrimitive.Root hideWhenSingleBranch>
<BranchPickerPrimitive.Previous />
<BranchPickerPrimitive.Number /> / <BranchPickerPrimitive.Count />
<BranchPickerPrimitive.Next />
</BranchPickerPrimitive.Root>
);BranchPickerPrimitive.Previous and .Next automatically disable at branch boundaries and while a run is in flight (unless the runtime supports switchBranchDuringRun). For the full primitive API, see BranchPickerPrimitive.
Triggering Reload
ActionBarPrimitive.Reload creates a new branch on an assistant message and re-runs from there:
import { ActionBarPrimitive, MessagePrimitive } from "@assistant-ui/react";
const AssistantMessage = () => (
<MessagePrimitive.Root>
<MessagePrimitive.Parts />
<ActionBarPrimitive.Root>
<ActionBarPrimitive.Reload />
</ActionBarPrimitive.Root>
</MessagePrimitive.Root>
);Reload is disabled while thread.isRunning or thread.isDisabled is true. See ActionBarPrimitive for the full reference.
Programmatic Branch Navigation
For headless or keyboard-shortcut flows, navigate directly to a branch by id via aui.message.switchToBranch:
import { useAui } from "@assistant-ui/react";
const SwitchToBranch = ({ branchId }: { branchId: string }) => {
const aui = useAui();
return (
<button onClick={() => aui.message.switchToBranch({ branchId })}>
Go to branch
</button>
);
};This must be called inside a message context (e.g. nested within MessagePrimitive.Root).
Grouped Parts After Branching
Each branch is a distinct message version with its own content parts. MessagePrimitive.GroupedParts provides hierarchical adjacent grouping of those parts, useful when a message mixes tool calls and text across branches. See the MessagePrimitive reference for GroupedParts usage.