---
title: "Chain of Thought"
description: "An expandable summary of observable agent steps."
---

# Chain of Thought

An expandable summary of observable agent steps.

## Usage

Expand the summary to inspect search steps and linked sources. Replay reveals the scripted steps progressively.

## Example

```contract
use uiFontFamily, BubbleFrame from "@rightui/ui"
use ButtonAI as ButtonContent from "@rightui/ui"
use TraceFrame, TraceLine, TraceCollapsible, TraceDisclosure, TraceSource, CodeBlockContent from "@rightui/ui"

component Demo
  props
    width: number
    reduced: bool
  inject
    uiFont: string
    rightuiDemoTime: number
    rightuiRunDemo: action
  state started = -1
  derive elapsed = started < 0 ? 12700 : max(0, rightuiDemoTime - started)
  derive busy = elapsed < 2700
  state researchOpen = false
  action toggleResearch
    researchOpen = not researchOpen
  action replay
    started = rightuiDemoTime
    rightuiRunDemo(6700)
  view
    column gap="ui.space-16" min-width=0 min-height=288 max-width="24rem" width="100%" padding="ui.space-24 ui.space-0"
      BubbleFrame(id="ai-chain-of-thought-question", align="end", variant="muted")
        text "Why is the checkout deploy so slow?" font-family=uiFontFamily(uiFont) data-rightui-font="body"
      TraceFrame()
        TraceDisclosure(id="ai-chain-of-thought-0", title=(busy ? "Researching" : "Researched in 3 steps"), icon="check", status=(busy ? "running" : ""), open=(researchOpen or busy), reduced=reduced, toggle=toggleResearch)
          TraceCollapsible(id="ai-chain-log", title=(elapsed < 900 ? "Reading the build log for dpl_8Kx2" : "Read the build log for dpl_8Kx2"), icon="check", status=(elapsed < 900 ? "running" : ""), initiallyOpen=true, reduced=reduced)
            if elapsed >= 900
              CodeBlockContent(id="ai-chain-log-output", code="install    41s\ncompile    38s\nimages   3m 52s")
          if elapsed >= 900
            TraceCollapsible(id="ai-chain-web", title=(elapsed < 1800 ? "Searching the web for next.js image cache key" : "Searched the web for next.js image cache key"), icon="check", status=(elapsed < 1800 ? "running" : ""), initiallyOpen=false, reduced=reduced)
              TraceSource(title="Image component | nextjs.org", href="https://nextjs.org/docs/app/api-reference/components/image", reduced=reduced)
              TraceSource(title="Build cache | vercel.com", href="https://vercel.com/docs/deployments/troubleshoot-a-build", reduced=reduced)
          if elapsed >= 1800
            TraceCollapsible(id="ai-chain-code", title=(elapsed < 2700 ? "Searching the code for cacheKey" : "Searched the code for cacheKey"), icon="check", status=(elapsed < 2700 ? "running" : ""), initiallyOpen=false, reduced=reduced)
              row gap="ui.space-6" flex-wrap="wrap"
                text "next.config.ts" padding="ui.space-2 ui.space-8" border-radius="ui.radius-inline" background-color="ui.muted" font-family="ui.font-code" font-size="ui.text-xs"
                text "scripts/optimize-images.ts" padding="ui.space-2 ui.space-8" border-radius="ui.radius-inline" background-color="ui.muted" font-family="ui.font-code" font-size="ui.text-xs"
      if elapsed >= 2700
        text "Image optimization takes almost four minutes because its cache key includes the commit hash. Key it on the image directory instead.".slice(0, started < 0 or reduced ? 131 : floor((elapsed - 2700) / 20)) font-size="ui.text-sm" line-height="ui.leading-relaxed" font-family=uiFontFamily(uiFont) data-rightui-font="body"
      if not busy
        row margin-top="auto" align-items="flex-start"
          ButtonContent(id="ai-chain-of-thought-replay", label="Replay", variant="outline", size="sm", disabled=false, press=replay)
            text "Replay" font-family=uiFontFamily(uiFont) data-rightui-font="body"

```

## Keyboard

Tab to controls; Enter or Space to activate buttons.

## Demo runtime

Timed examples use an injected clock. Add these members to your root component. Replace the scripted state with events from your TypeScript or Rust data source when connecting a model.

```contract
use defaultFontWeights from "@rightui/ui"
  state demoTime = 0
  state demoUntil = 0
  provide
    uiFont = "theme"
    uiWeights = defaultFontWeights()
    rightuiDemoTime = demoTime
    rightuiRunDemo = runDemo
  action runDemo(duration: number)
    demoUntil = max(demoUntil, demoTime + duration)
  action tickDemo
    demoTime = min(demoUntil, demoTime + 32)
  task demoClock when demoTime < demoUntil
    every(32, tickDemo)
```

## Source

File: patterns/ai-elements/chain-of-thought.contract

```contract
use uiFontFamily, BubbleFrame from "@rightui/ui"
use ButtonAI as ButtonContent from "@rightui/ui"
use TraceFrame, TraceLine, TraceCollapsible, TraceDisclosure, TraceSource, CodeBlockContent from "@rightui/ui"

component Demo
  props
    width: number
    reduced: bool
  inject
    uiFont: string
    rightuiDemoTime: number
    rightuiRunDemo: action
  state started = -1
  derive elapsed = started < 0 ? 12700 : max(0, rightuiDemoTime - started)
  derive busy = elapsed < 2700
  state researchOpen = false
  action toggleResearch
    researchOpen = not researchOpen
  action replay
    started = rightuiDemoTime
    rightuiRunDemo(6700)
  view
    column gap="ui.space-16" min-width=0 min-height=288 max-width="24rem" width="100%" padding="ui.space-24 ui.space-0"
      BubbleFrame(id="ai-chain-of-thought-question", align="end", variant="muted")
        text "Why is the checkout deploy so slow?" font-family=uiFontFamily(uiFont) data-rightui-font="body"
      TraceFrame()
        TraceDisclosure(id="ai-chain-of-thought-0", title=(busy ? "Researching" : "Researched in 3 steps"), icon="check", status=(busy ? "running" : ""), open=(researchOpen or busy), reduced=reduced, toggle=toggleResearch)
          TraceCollapsible(id="ai-chain-log", title=(elapsed < 900 ? "Reading the build log for dpl_8Kx2" : "Read the build log for dpl_8Kx2"), icon="check", status=(elapsed < 900 ? "running" : ""), initiallyOpen=true, reduced=reduced)
            if elapsed >= 900
              CodeBlockContent(id="ai-chain-log-output", code="install    41s\ncompile    38s\nimages   3m 52s")
          if elapsed >= 900
            TraceCollapsible(id="ai-chain-web", title=(elapsed < 1800 ? "Searching the web for next.js image cache key" : "Searched the web for next.js image cache key"), icon="check", status=(elapsed < 1800 ? "running" : ""), initiallyOpen=false, reduced=reduced)
              TraceSource(title="Image component | nextjs.org", href="https://nextjs.org/docs/app/api-reference/components/image", reduced=reduced)
              TraceSource(title="Build cache | vercel.com", href="https://vercel.com/docs/deployments/troubleshoot-a-build", reduced=reduced)
          if elapsed >= 1800
            TraceCollapsible(id="ai-chain-code", title=(elapsed < 2700 ? "Searching the code for cacheKey" : "Searched the code for cacheKey"), icon="check", status=(elapsed < 2700 ? "running" : ""), initiallyOpen=false, reduced=reduced)
              row gap="ui.space-6" flex-wrap="wrap"
                text "next.config.ts" padding="ui.space-2 ui.space-8" border-radius="ui.radius-inline" background-color="ui.muted" font-family="ui.font-code" font-size="ui.text-xs"
                text "scripts/optimize-images.ts" padding="ui.space-2 ui.space-8" border-radius="ui.radius-inline" background-color="ui.muted" font-family="ui.font-code" font-size="ui.text-xs"
      if elapsed >= 2700
        text "Image optimization takes almost four minutes because its cache key includes the commit hash. Key it on the image directory instead.".slice(0, started < 0 or reduced ? 131 : floor((elapsed - 2700) / 20)) font-size="ui.text-sm" line-height="ui.leading-relaxed" font-family=uiFontFamily(uiFont) data-rightui-font="body"
      if not busy
        row margin-top="auto" align-items="flex-start"
          ButtonContent(id="ai-chain-of-thought-replay", label="Replay", variant="outline", size="sm", disabled=false, press=replay)
            text "Replay" font-family=uiFontFamily(uiFont) data-rightui-font="body"

```
