- Accordion
- Attachment
- Avatar
- Badge
- Breadcrumb
- Button
- Button Group
- Card
- Checkbox
- Code Block
- Collapsible
- Dialog
- Dropdown Menu
- Empty
- Field
- Hover Card
- Input
- Input Group
- Input OTP
- Item
- Label
- Marker
- Message
- Number Flow
- Pagination
- Popover
- Progress
- Radio Group
- Select
- Slider
- Sonner
- Spinner
- Switch
- Tooltip
- Separator
- Sheet
- Tabs
- Textarea
- Toggle
- Toggle Group
- Trace
Chain of Thought
Displays a summary that expands into the steps an agent took.
install 41s
compile 38s
images 3m 52sImage optimization takes almost four minutes because its cache key includes the commit hash. Key it on the image directory instead.
Chain of Thought is a summary line that expands into the steps an agent took. The summary says what happened, like "Researched in 3 steps". The steps sit inside it, each one a line of its own.
Installation
$ bunx --bun frakment add trace code-block badge$ npx frakment add trace code-block badge$ pnpm dlx frakment add trace code-block badge$ yarn dlx frakment add trace code-block badge
Install the following dependencies:
$ bun add lucide-react$ npm install lucide-react$ pnpm add lucide-react$ yarn add lucide-react
Install the required registry component:
$ bunx --bun frakment add trace$ npx frakment add trace$ pnpm dlx frakment add trace$ yarn dlx frakment add trace
Optional — install these primitives to replicate the full demo:
$ bunx --bun frakment add badge bubble code-block message npm install @ai-sdk/react ai$ npx frakment add badge bubble code-block message npm install @ai-sdk/react ai$ pnpm dlx frakment add badge bubble code-block message npm install @ai-sdk/react ai$ yarn dlx frakment add badge bubble code-block message npm install @ai-sdk/react ai
The demo uses useChat from @ai-sdk/react to simulate streaming agent output. If you only need the ChainOfThought component itself, you can skip this step — trace is the only required dependency.
Copy and paste the following code into your project.
"use client";
import { CircleCheckIcon } from "lucide-react";
import * as React from "react";
import {
Trace,
TraceCollapsible,
TraceCollapsibleContent,
TraceCollapsibleTrigger,
TraceIcon,
TraceLine,
TraceTitle,
} from "@/components/ui/trace";
import { cn } from "@/lib/utils";
interface ChainOfThoughtProps extends React.ComponentProps<"div"> {
defaultOpen?: boolean;
done?: boolean;
}
function ChainOfThought({
className,
defaultOpen = true,
done = false,
children,
...props
}: ChainOfThoughtProps) {
const [open, setOpen] = React.useState(defaultOpen || !done);
React.useEffect(() => {
if (done) {
setOpen(false);
}
}, [done]);
return (
<div
className={cn("w-full", className)}
data-slot="chain-of-thought"
{...props}
>
<Trace>
<TraceCollapsible onOpenChange={setOpen} open={open}>
{children}
</TraceCollapsible>
</Trace>
</div>
);
}
function ChainOfThoughtHeader({
className,
children = "Researched in 3 steps",
...props
}: React.ComponentProps<"div">) {
return (
<TraceCollapsibleTrigger>
<TraceLine>
<TraceIcon>
<CircleCheckIcon />
</TraceIcon>
<TraceTitle className={cn(className)} {...props}>
{children}
</TraceTitle>
</TraceLine>
</TraceCollapsibleTrigger>
);
}
function ChainOfThoughtContent({
className,
children,
...props
}: React.ComponentProps<"div">) {
return (
<TraceCollapsibleContent className={className} {...props}>
<Trace className="pt-2">{children}</Trace>
</TraceCollapsibleContent>
);
}
function ChainOfThoughtStep({
className,
children,
...props
}: React.ComponentProps<"div">) {
return (
<TraceLine className={className} {...props}>
<TraceIcon>
<CircleCheckIcon />
</TraceIcon>
<TraceTitle>{children}</TraceTitle>
</TraceLine>
);
}
export {
ChainOfThought,
ChainOfThoughtContent,
ChainOfThoughtHeader,
ChainOfThoughtStep,
};Update the import paths to match your project setup.
Usage
import {
ChainOfThought,
ChainOfThoughtHeader,
ChainOfThoughtContent,
ChainOfThoughtStep,
} from "@/components/ui/chain-of-thought";<ChainOfThought defaultOpen>
<ChainOfThoughtHeader>Researched in 3 steps</ChainOfThoughtHeader>
<ChainOfThoughtContent>
<ChainOfThoughtStep>Read the build log for dpl_8Kx2</ChainOfThoughtStep>
<ChainOfThoughtStep>
Checked bundle size for dashboard.js
</ChainOfThoughtStep>
<ChainOfThoughtStep>Profiled 3 API endpoints</ChainOfThoughtStep>
</ChainOfThoughtContent>
</ChainOfThought>Full demo
The preview on this page shows the chain-of-thought primitive composing with useChat, createChat, and transport from @frakment/ai. It streams three tool calls with a line-by-line build log and uses nested TraceCollapsible for auto-expand and manual toggle.
The composition pulls @ai-sdk/react, ai, badge, bubble, button, code-block, message, and trace. Follow the Chat tutorial for the wiring pattern, or replace createChat with your own backend that streams data-buildLog and tool parts.
In Context
Research summary
install 41s
compile 38s
images 3m 52sImage optimization takes almost four minutes because its cache key includes the commit hash. Key it on the image directory instead.
API Reference
| Prop | Type | Default | Description |
|---|---|---|---|
status | runningerrorpending | — | Set on TraceLine. Controls color, and shimmer when running. |
open | boolean | — | Controlled open state on TraceCollapsible. Use open={open ?? running} to open while steps stream. |
defaultOpen | boolean | — | Start expanded. Useful for static content. |
Accessibility
- TraceIcon is
aria-hidden. TraceTitle is the accessible name. Write it as a sentence: "Searched for minimumReleaseAge", not just "minimumReleaseAge". - Status is conveyed by icon shape and color, not color alone.
prefers-reduced-motion: shimmer stops automatically.- Use
open={open ?? running}so a reader's manual toggle wins over the auto state.
Anatomy
<ChainOfThought>
<ChainOfThoughtHeader />
<ChainOfThoughtContent>
<ChainOfThoughtStep />
</ChainOfThoughtContent>
</ChainOfThought>- ChainOfThought — collapsible root
- ChainOfThoughtHeader — summary line ("Researched in 3 steps")
- ChainOfThoughtContent — step list
- ChainOfThoughtStep — one step with a Trace line
Best Practices
- One summary per turn. The header is a single line, not a tree.
- Keep steps atomic. One action per step.
- Collapse by default. Steps expand on demand.
- Keep nesting shallow. Two levels is enough.