Reasoning
Displays the agent's thinking as it streams.
import { For, createSignal, untrack, createMemo } from "solid-js"
import {
useDemoChat,
createChat,
getMessageText
} from "~/lib/ai-demo-chat"
import { Bubble, BubbleContent } from "~/components/ui/bubble"
import { Button } from "~/components/ui/button"
import { Message, MessageContent } from "~/components/ui/message"
import {
Trace,
TraceCollapsible,
TraceCollapsibleContent,
TraceCollapsibleTrigger,
TraceLine,
TraceTitle
} from "~/components/ui/trace"
const chat = createChat()
.user("Why is the checkout deploy so slow?")
.assistant(({ writer }) => {
writer.reasoning(
"The deploy log has three phases. Install takes 38s and next build takes 52s, both normal for this repo.\n\nImage optimization takes 3m 50s. It reruns on every deploy because its cache key includes the commit hash, so the cache never hits.",
{ delayMs: 40 }
)
writer.sleep(400)
writer.text(
"Image optimization is the bottleneck. Key its cache on the lockfile and the image directory instead of the commit hash, and the deploy should take under two minutes."
)
})
const transcript = chat.get()
const [question] = chat.get(1)
const transport = chat.transport({ delayMs: 20 })
function ReasoningRow(componentProps: {
text: string
running: boolean
defaultOpen?: boolean | undefined
}) {
const [open, setOpen] = createSignal(
untrack(() => componentProps.defaultOpen)
)
const content = createMemo(() => {
const { text, running } = componentProps
const line = (
<TraceLine status={running ? "running" : undefined}>
<TraceTitle>{running ? "Thinking" : "Thought for 5s"}</TraceTitle>
</TraceLine>
)
if (text.length === 0) {
return line
}
return (
<TraceCollapsible open={open() ?? running} onOpenChange={setOpen}>
<TraceCollapsibleTrigger>{line}</TraceCollapsibleTrigger>
<TraceCollapsibleContent class="whitespace-pre-wrap">
{text}
</TraceCollapsibleContent>
</TraceCollapsible>
)
})
return <>{content()}</>
}
export default function ReasoningDemo() {
const { messages, sendMessage, setMessages, status } = useDemoChat({
messages: transcript,
transport
})
const busy = () => status() === "submitted" || status() === "streaming"
return (
<div class="flex min-h-72 w-full max-w-sm flex-col gap-4 py-6 text-sm">
{
<For each={messages()}>
{(message) => {
const content = createMemo(() => {
if (message.role === "user") {
return (
<Message align="end">
<MessageContent>
<Bubble align="end" variant="muted">
<BubbleContent>{getMessageText(message)}</BubbleContent>
</Bubble>
</MessageContent>
</Message>
)
}
const streaming = busy() && message === messages().at(-1)
const reasoning = message.parts.filter(
(part) => part.type === "reasoning"
)
const text = getMessageText(message)
return (
<Message>
<MessageContent class="gap-4">
{reasoning.length > 0 && (
<Trace>
{
<For each={reasoning}>
{(part) => (
<ReasoningRow
text={part.text}
running={
streaming && part.state === "streaming"
}
defaultOpen={busy() ? undefined : true}
/>
)}
</For>
}
</Trace>
)}
{text && <p>{text}</p>}
</MessageContent>
</Message>
)
})
return <>{content()}</>
}}
</For>
}
{!busy() && (
<Button
variant="outline"
size="sm"
class="mt-auto self-start"
onClick={() => {
setMessages([])
sendMessage(question)
}}
>
Replay
</Button>
)}
</div>
)
}Reasoning is a line for the agentโs thinking. It reads โThinkingโ and opens while the model thinks, then reads โThoughtโ and closes when it is done. The thinking itself sits inside, and the line can show how long it took.
Reasoning is built with Trace with a TraceCollapsible for the thinking.
Installation
Reasoning is built with the Trace component.
Install the required Solid UI dependencies:
Copy and paste the following code into your project.
// Inline SVG icons from Lucide (https://lucide.dev), ISC License, Copyright (c) Lucide Icons and Contributors.
import type { ComponentProps, JSX } from "@solidjs/web"
import { merge, omit } from "solid-js"
import { cn } from "~/lib/utils"
import {
Collapsible,
CollapsibleContent,
CollapsibleTrigger
} from "~/components/ui/collapsible"
export function Trace(
props: Omit<ComponentProps<"div">, "class"> & { class?: string }
) {
return (
<div
{...props}
data-slot="trace"
class={cn(
"gap-2 *:has-[>[data-slot=trace-line]]:rounded-md *:has-[>[data-slot=trace-line]]:focus-visible:ring-3 *:has-[>[data-slot=trace-line]]:focus-visible:ring-ring/50 flex w-full min-w-0 flex-col *:has-[>[data-slot=trace-line]]:flex *:has-[>[data-slot=trace-line]]:w-fit *:has-[>[data-slot=trace-line]]:max-w-full *:has-[>[data-slot=trace-line]]:min-w-0 *:has-[>[data-slot=trace-line]]:text-left *:has-[>[data-slot=trace-line]]:outline-none",
props.class
)}
/>
)
}
export function TraceLine(
props: Omit<ComponentProps<"span">, "class"> & {
class?: string
status?: "running" | "error" | "pending" | undefined
}
) {
const rest = omit(props, "class", "status")
return (
<span
{...rest}
data-slot="trace-line"
data-status={props.status ?? undefined}
class={cn(
"gap-2 text-sm flex w-fit max-w-full min-w-0 items-center text-muted-foreground transition-colors in-[a,button]:cursor-pointer in-[a:hover,button:hover]:text-foreground data-[status=error]:text-destructive data-[status=error]:in-[a:hover,button:hover]:text-destructive data-[status=pending]:opacity-60 data-[status=running]:*:data-[slot=trace-title]:shimmer",
props.class
)}
/>
)
}
export function TraceIcon(
props: Omit<ComponentProps<"span">, "class"> & { class?: string }
) {
return (
<span
{...props}
data-slot="trace-icon"
class={cn(
"size-4 [&_svg:not([class*='size-'])]:size-4 [[data-slot=trace-title]~&]:-ml-1 flex shrink-0 items-center justify-center [&_svg]:pointer-events-none",
props.class
)}
/>
)
}
export function TraceTitle(
props: Omit<ComponentProps<"span">, "class"> & { class?: string }
) {
return (
<span
{...props}
data-slot="trace-title"
class={cn(
"min-w-0 truncate motion-reduce:shimmer-none",
props.class
)}
/>
)
}
export function TraceDescription(
props: Omit<ComponentProps<"span">, "class"> & { class?: string }
) {
return (
<span
{...props}
data-slot="trace-description"
class={cn(
"shrink-0 text-muted-foreground/70 tabular-nums",
props.class
)}
/>
)
}
export function TraceCollapsible(props: ComponentProps<typeof Collapsible>) {
const rest = omit(props, "class", "render")
return (
<Collapsible
{...rest}
class={cn(
"flex w-full min-w-0 flex-col",
props.class
)}
render={(part, state) => {
const forwarded = merge(part, { "data-slot": "trace-collapsible" })
return props.render ? (
props.render(forwarded, state)
) : (
<div {...forwarded} />
)
}}
/>
)
}
export function TraceCollapsibleTrigger(
props: ComponentProps<typeof CollapsibleTrigger>
) {
const rest = omit(props, "class", "children", "render")
return (
<CollapsibleTrigger
{...rest}
class={cn(
"gap-1 rounded-md focus-visible:ring-3 focus-visible:ring-ring/50 group/trace-collapsible-trigger flex w-fit max-w-full min-w-0 cursor-pointer items-center text-left text-muted-foreground transition-colors outline-none hover:text-foreground has-[[data-slot=trace-line][data-status=error]]:text-destructive",
props.class
)}
render={(part, state) => {
const forwarded = merge(part, {
"data-slot": "trace-collapsible-trigger"
})
return props.render ? (
props.render(forwarded, state)
) : (
<button {...forwarded} />
)
}}
>
{props.children}
<TraceIcon class="transition-transform group-aria-expanded/trace-collapsible-trigger:rotate-90">
<svg
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
class="size-4"
>
<path d="m9 18 6-6-6-6" />
</svg>
</TraceIcon>
</CollapsibleTrigger>
)
}
export function TraceCollapsibleContent(
props: Omit<ComponentProps<typeof CollapsibleContent>, "children"> & {
children?: JSX.Element
}
) {
const rest = omit(props, "class", "children", "render")
return (
<CollapsibleContent
{...rest}
class="duration-200 h-(--collapsible-panel-height) min-w-0 overflow-hidden transition-[height] data-ending-style:h-0 data-starting-style:h-0"
render={(part, state) => {
const forwarded = merge(part, {
"data-slot": "trace-collapsible-content"
})
return props.render ? (
props.render(forwarded, state)
) : (
<div {...forwarded} />
)
}}
>
<div
data-slot="trace-collapsible-content-inner"
class={cn(
"mt-2 mb-1 ml-[calc(--spacing(2)-1px)] border-l pr-1 pl-4 leading-relaxed min-w-0 wrap-break-word text-muted-foreground",
props.class
)}
>
{props.children}
</div>
</CollapsibleContent>
)
}Update the import paths to match your project setup.
Usage
import {
Trace,
TraceCollapsible,
TraceCollapsibleContent,
TraceCollapsibleTrigger,
TraceIcon,
TraceLine,
TraceTitle
} from "~/components/ui/trace"<Trace>
<TraceCollapsible>
<TraceCollapsibleTrigger>
<TraceLine>
<TraceIcon>
<CircleCheckIcon />
</TraceIcon>
<TraceTitle>Thought</TraceTitle>
</TraceLine>
</TraceCollapsibleTrigger>
<TraceCollapsibleContent class="whitespace-pre-wrap">
{text}
</TraceCollapsibleContent>
</TraceCollapsible>
</Trace>Duration
The AI SDK does not time reasoning. Measure it on the server, send it as message metadata, and show it in a TraceDescription after the title.
// Inline SVG icons from Lucide (https://lucide.dev), ISC License, Copyright (c) Lucide Icons and Contributors.
import {
Trace,
TraceCollapsible,
TraceCollapsibleContent,
TraceCollapsibleTrigger,
TraceDescription,
TraceIcon,
TraceLine,
TraceTitle
} from "~/components/ui/trace"
export default function ReasoningDuration() {
return (
<Trace class="max-w-sm">
<TraceCollapsible>
<TraceCollapsibleTrigger>
<TraceLine>
<TraceIcon>
<svg
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
>
<circle cx="12" cy="12" r="10" />
<path d="m16 9-5.5 5.5L8 12" />
</svg>
</TraceIcon>
<TraceTitle>Thought</TraceTitle>
<TraceDescription>12s</TraceDescription>
</TraceLine>
</TraceCollapsibleTrigger>
<TraceCollapsibleContent class="whitespace-pre-wrap">
The build log shows install and compile finish in under a minute.
Image optimization takes almost four, and it reruns on every deploy
because the cache key includes the commit hash.
</TraceCollapsibleContent>
</TraceCollapsible>
</Trace>
)
}<TraceLine>
<TraceIcon>
<CircleCheckIcon />
</TraceIcon>
<TraceTitle>Thought</TraceTitle>
<TraceDescription>{seconds}s</TraceDescription>
</TraceLine>AI SDK
Render each reasoning part as a line. It starts as a plain TraceLine and becomes a TraceCollapsible once text arrives, so there is nothing to open before the model has written anything. It opens while the model thinks and closes when it finishes. If the person opens or closes it, their choice wins. See Open State.
import { createSignal } from "solid-js"
function ReasoningLine(componentProps: { text: string; running: boolean }) {
const [open, setOpen] = createSignal<boolean>()
return (
<>
{(() => {
const { text, running } = componentProps
const line = (
<TraceLine status={running ? "running" : undefined}>
<TraceIcon>
{running ? (
<LoaderCircleIcon class="animate-spin" />
) : (
<CircleCheckIcon />
)}
</TraceIcon>
<TraceTitle>{running ? "Thinking" : "Thought"}</TraceTitle>
</TraceLine>
)
if (text.length === 0) {
return line
}
return (
<TraceCollapsible open={open() ?? running} onOpenChange={setOpen}>
<TraceCollapsibleTrigger>{line}</TraceCollapsibleTrigger>
<TraceCollapsibleContent class="whitespace-pre-wrap">
{text}
</TraceCollapsibleContent>
</TraceCollapsible>
)
})()}
</>
)
}import { For } from "solid-js"
;<Trace>
{
<For each={message.parts.filter((part) => part.type === "reasoning")}>
{(part, index) => (
<ReasoningLine
text={part.text}
running={streaming && part.state === "streaming"}
/>
)}
</For>
}
</Trace>Some models send several reasoning parts in one message. Each one is a line in the same Trace.
Migrating from AI SDK Elements
If you added Reasoning from AI SDK Elements, your project has a components/ai-elements/reasoning.tsx wrapper. Paste this prompt into your coding agent to rebuild it on Trace. Every export keeps its name and props, so the code that uses it keeps working.
Migrate components/ai-elements/reasoning.tsx from AI SDK Elements to the Solid UI Trace component.
Keep every export, with the same name and props, so the code that imports it keeps working. Only change how each part renders.
1. Install Trace: npx solidui-cli@latest add trace
2. Read https://www.solid-ui.com/docs/ai-elements/reasoning.md and build Reasoning the same way.
3. Rebuild each part:
- Reasoning renders a Trace with a TraceCollapsible. Keep isStreaming, open, defaultOpen, onOpenChange and duration, and the useReasoning context.
- ReasoningTrigger renders a TraceCollapsibleTrigger with a TraceLine, a TraceIcon and a TraceTitle. Set status="running" while isStreaming is true, instead of the Shimmer. The title reads "Thinking" while it streams, then the duration, like "Thought for 5s".
- ReasoningContent renders a TraceCollapsibleContent. Use your Markdown renderer for the text.
- Keep the open behavior: open while it streams, close when it finishes, unless the person opened or closed it.
4. Remove the imports the file no longer uses.
5. Run the type checker and fix any errors.API Reference
See Trace.