# Reasoning trail

The agent's thinking chain in a chat: steps on one hairline with their thoughts and sources, streamed with a soft edge, folding into one line when the answer starts, any retry or failure kept.

Tier: pro. Type: component. Category: ai. Tags: agent, streaming, sources, motion, trace.

## Install

```bash
npx shadcn@latest add @pixilui-pro/reasoning-trail
```

One-time setup: add the registry to `components.json`:

```json
{
  "registries": {
    "@pixilui": "https://pixilui.com/r/{name}.json",
    "@pixilui-pro": {
      "url": "https://pixilui.com/r/pro/{name}.json",
      "headers": { "Authorization": "Bearer ${PIXILUI_TOKEN}" }
    }
  }
}
```

Pro items need a license. Create a token on https://pixilui.com/account and set `PIXILUI_TOKEN` in `.env.local`; the CLI sends it as a bearer header.

Files: `components/pixilui/reasoning-trail.tsx`.

## Preview

Live preview: https://pixilui.com/components/reasoning-trail

Knobs:

- `head`: `action`, `working`

Signature: The answer starts and the steps fade, rise toward the head and fold away, leaving one line: Worked for 14s · 1 retry.

Styles: `lively` (default), `quiet`, set with `data-style` on <html>. The item reads the style through the foundation; it needs no prop.

## Code

Pro source is available to license holders.

## Usage

When to use:

- Above an agent's answer: what it read, looked up and concluded, as it happens, then one click away.
- An answer with citations: ReasoningTrailStream streams it with the soft edge and ReasoningTrailCitation opens the dark source card that keeps the claim dark.
- A past message: pass the finished steps with status done and it arrives drawn (settled).

When not to use:

- Only the current action and a timer: use thinking state (free), the same line without steps.
- A run's tool calls with inputs and outputs: use the tool call row.
- A run's timeline across many runs: use the run timeline.

```tsx
import { IconFile } from "@/components/pixilui/icons/file";
import { IconQuery } from "@/components/pixilui/icons/query";
import { ReasoningTrail, ReasoningTrailAnswer, ReasoningTrailStream, useReasoningTrailSimulation, type ReasoningTrailSource } from "@/components/pixilui/reasoning-trail";

// The first option of each knob is the demo's default; the docs preview starts there.
export const knobs = {
  head: ["action", "working"],
} as const;

/** No animated icon fits a calendar: the static Hugeicons calendar-03 (MIT), drawn inline. */
function IconCalendar({ className }: { className?: string }) {
  return (
    <svg className={className} viewBox="0 0 24 24" aria-hidden="true">
      <g fill="none" stroke="currentColor" strokeLinecap="round" strokeLinejoin="round" strokeWidth="1.5">
        <path d="M16 2v4M8 2v4m5-2h-2C7.229 4 5.343 4 4.172 5.172S3 8.229 3 12v2c0 3.771 0 5.657 1.172 6.828S7.229 22 11 22h2c3.771 0 5.657 0 6.828-1.172S21 17.771 21 14v-2c0-3.771 0-5.657-1.172-6.828S16.771 4 13 4M3 10h18" />
        <path d="M12.126 14H12m.125 4H12m-4.376-4H7.5m.125 4H7.5m9.125-4H16.5m-4.25 0a.25.25 0 1 1-.5 0a.25.25 0 0 1 .5 0m0 4a.25.25 0 1 1-.5 0a.25.25 0 0 1 .5 0m-4.5-4a.25.25 0 1 1-.5 0a.25.25 0 0 1 .5 0m0 4a.25.25 0 1 1-.5 0a.25.25 0 0 1 .5 0m9-4a.25.25 0 1 1-.5 0a.25.25 0 0 1 .5 0" />
      </g>
    </svg>
  );
}

const SNOWFLAKE: ReasoningTrailSource = {
  id: "query",
  name: "Snowflake",
  icon: <IconQuery />,
  ink: "var(--pixilui-h-tool)",
  domain: "Snowflake · retention by source",
  quote: "“Week-one retention: 38% before Nov 4. Since then, 41% for all signups, 46% without Product Hunt, and 29% for Product Hunt alone.”",
};
const CALENDAR: ReasoningTrailSource = {
  id: "calendar",
  name: "Launch calendar",
  icon: <IconCalendar />,
  ink: "var(--pixilui-h-edit)",
  domain: "Launch calendar · Growth",
  quote: "“Product Hunt launch, Nov 6. 2,140 signups in the first three days, about four times a normal week.”",
};
const SOURCES: Record<string, ReasoningTrailSource> = {
  v2: { id: "v2", name: "Onboarding v2", icon: <IconFile />, ink: "var(--pixilui-h-read)" },
  cal: { id: "cal", name: "Launch calendar", icon: <IconCalendar />, ink: "var(--pixilui-h-edit)" },
};

/** A chat turn: the trail plays the reference run, folds into one line with its retry, and the answer streams in with citations. */
export default function ReasoningTrailDemo({ head = "action" }: KnobValues<typeof knobs>) {
  const run = useReasoningTrailSimulation();
  const steps = run.steps.map((s) => ({ ...s, sources: s.sources?.map((x) => SOURCES[x.id] ?? x) }));
  return (
    // a centered column, as the lock sets the question and the line
    <div className="mx-auto flex w-full max-w-xl flex-col gap-4 rounded-panel bg-surface p-6 elevation-raised">
      <p className="self-end rounded-control bg-surface-muted px-3 py-2 type-label text-foreground">Did the new onboarding help retention?</p>
      <ReasoningTrail key={head} status={run.status} head={head} defaultOpen={head === "working"} words={run.words} mark={run.mark} flag={run.flag} steps={steps} />
      {run.answering ? (
        <ReasoningTrailAnswer className="type-body text-foreground">
          <ReasoningTrailStream
            content={[
              { claim: ["Yes, more than the dashboard shows. Week-one retention went from 38% to ", { strong: "46%" }, " for people who didn't come from the Product Hunt launch.", { cite: [SNOWFLAKE] }] },
              " ",
              { claim: ["The overall number reads 41% because launch signups leave faster, and they arrived the same week.", { cite: [CALENDAR] }] },
            ]}
          />
        </ReasoningTrailAnswer>
      ) : null}
    </div>
  );
}
```

## Examples

None.

## API

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `steps` (required) | `ReasoningTrailStep[]` |  | The agent's work so far, in order. Add steps as they happen; a step keeps its element by id. |
| `thoughtCps` | `number` |  | How fast thoughts stream, letters a second (--pixilui-trail-thought-cps, 70, by default). |

## Keyboard

- `Enter` + `Space`: On the head: folds or opens the steps.
- `Tab`: Reaches each citation; focus opens its card.
- `Escape`: Closes the open citation card.

## Accessibility

- The head is a button with aria-expanded; its words are role status, so each phase is announced politely.
- Streaming text is aria-busy until it lands. Citations are buttons named "Source: Snowflake"; the card is role tooltip, tied by aria-describedby.
- Step icons and the mark are aria-hidden. Retries and failures are words, never only a color.

## Motion

- A step fades in place and the hairline draws down to it (250ms); its label changes with the quiet swap; the live label carries the text shimmer.
- Thoughts stream at 70 letters a second and the answer at 120 with the soft edge: each letter fades in, so the front is a gradient about 18 letters long, with no cursor; a citation arrives with its text, taking a word's time.
- Source chips gather one at a time, 420ms apart.
- The fold: the time stops, Working becomes Worked for, the flag joins, the steps fade and rise, then the space closes. Opening reverses it, and either turns around halfway.
- A citation's card grows from its pointer below the chip after a short wait (200ms in, 150ms out); the claim stays dark while the rest of the answer dims.
- Reduced motion: text whole at once, chips together, no band, the fold and the card instant.

## Responsive

- Fills its column; thoughts wrap at the reading measure. The card is 320 wide, kept inside the window, and opens above when there is no room below.

## Performance

- CSS in the foundation (section 8) for the look and the fold. A stream renders the letters still fading as spans and collapses to plain text once it lands.

## Notes for AI

- <ReasoningTrail status="live" head="action" words="Reading 2 of 2 sources" mark="reading" steps={steps} />; set status="done" with flag="1 retry" when the answer starts, then stream the answer.
- A step is { id, kind: think | query | read, title, text?, sources?, live? }. Add steps and grow text as the model works; keep ids stable so each step keeps its element and motion.
- reasoningTrailMarkFor(kind) gives a step kind's activity mark (think: thinking, query: fetching, read: reading).
- The answer: <ReasoningTrailAnswer><ReasoningTrailStream content={[{ claim: ["Retention went from 38% to ", { strong: "46%" }, ".", { cite: [source] }] }]} /></ReasoningTrailAnswer>.
- From the Vercel AI SDK: map reasoning parts to steps and the chat status with thinkingStateFromChat (thinking-state). Nothing imports from ai.
- useReasoningTrailSimulation() plays the reference run at human speed; pass beats for your own.
- Every visible word is data or a prop: titles, thoughts, source names, the head's words and labels, the citation's labelPrefix and labelJoin.
- Per-style rule: none; the trail is the same in lively and quiet.

## Related

- [Thinking state](https://pixilui.com/components/thinking-state/markdown): The working line: what the agent is doing in words, led by the activity mark, with a timer; when the answer starts it reads Worked for 14s, keeps anything that went wrong on the line, and folds.
- [Reasoning summary](https://pixilui.com/components/reasoning-summary/markdown): The reasoning trail folded to its working line: the activity mark and what the agent is doing in words with a timer, then Worked for 14s with any retry kept on the line, opening to the full trail.
- [Activity mark](https://pixilui.com/components/thinking-mark/markdown): What an agent is doing right now, acted out on a 5 by 5 grid at 16px or larger: a radar sweep while it searches, a reading head through text, a question mark when it needs you. 36 activities.
- [Chip](https://pixilui.com/components/chip/markdown): The small labelled pill of an AI product: context in a composer, a filter, a fact behind a decision and the citation after a claim, in one shape and one light grey.
