# Trace View

> Where a run spent its time: each step in a tree beside a bar on one time axis, with the chosen step's tokens, attributes, input and output.

Page: https://uicontext.com/docs/components/trace-view

Group: Agents.

## When to use

Where a run spent its time: each step in a tree beside a bar on one time axis, with the chosen step's tokens, attributes, input and output. Use [Run Summary](https://uicontext.com/docs/components/run-summary.md) instead when people need the run's outcome and cost, not where its time went.

## Usage

```ts
import { type Span, TraceView } from "./ui/uicontext/components/TraceView.ts"

export const RunTrace = <M>(h: Html.H<M>, spans: ReadonlyArray<Span>) =>
	TraceView(h, { props: { label: "Run trace", spans } })
```

## Examples

Each example is a complete file. Its imports point where the CLI installs the source.

### An agent's run

Choose a step to see its details. The failed query is red, and so is every step above it.

```ts title="src/demo.ts" source="www/src/examples/trace-view/demo.ts"
import type { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { sx } from "./ui/uicontext/components/Sx.ts"
import { type Span, TraceView } from "./ui/uicontext/components/TraceView.ts"

const styles = stylex.create({ wrap: { inlineSize: "100%" } })

// a support agent answering "Why did my export fail?": it plans, searches the docs, looks up the
// customer's jobs, retries a slow call, and writes the reply
const spans: ReadonlyArray<Span> = [
	{ id: "run", parent: null, name: "answer_ticket #4821", kind: "agent", start: 0, duration: 7420, attributes: { ticket: "4821", customer: "Harborview Supply" } },
	{ id: "plan", parent: "run", name: "plan", kind: "llm", start: 40, duration: 1180, tokens: { input: 1840, output: 212 }, attributes: { model: "large-2", temperature: "0.2" }, input: "Ticket: Every CSV export since Monday comes back empty.\nTools: search_docs, get_jobs, draft_reply", output: "1. Look up the customer's recent export jobs.\n2. Search the docs for empty exports.\n3. Draft a reply." },
	{ id: "jobs", parent: "run", name: "get_jobs", kind: "tool", start: 1260, duration: 2310, status: "error", error: "Timed out after 2000 ms; retried once", attributes: { customer_id: "cus_N8x", limit: "20" } },
	{ id: "jobs-q1", parent: "jobs", name: "SELECT export_jobs", kind: "db", start: 1290, duration: 2010, status: "error", error: "statement timeout", attributes: { rows: "0", index: "none" } },
	{ id: "jobs-q2", parent: "jobs", name: "SELECT export_jobs (retry)", kind: "db", start: 3330, duration: 220, attributes: { rows: "14", index: "export_jobs_customer_idx" } },
	{ id: "docs", parent: "run", name: "search_docs", kind: "retrieval", start: 1270, duration: 640, attributes: { query: "csv export empty", hits: "6" }, output: "Exports skip rows the viewer can't see.\nA filter saved on the view applies to exports too." },
	{ id: "docs-http", parent: "docs", name: "POST /v1/search", kind: "http", start: 1300, duration: 560, attributes: { status: "200" } },
	{ id: "reply", parent: "run", name: "draft_reply", kind: "llm", start: 3600, duration: 3720, tokens: { input: 4120, output: 486 }, attributes: { model: "large-2", stream: "true" }, output: "Hi Ada, your exports are empty because the Invoices view has a saved filter (Status is Draft). Clear it under Filters, or export from All invoices. Sorry for the trouble." },
	{ id: "send", parent: "run", name: "post_internal_note", kind: "tool", start: 7340, duration: 60, attributes: { visibility: "team" } },
]

export const AgentRunTrace = <M>(h: Html.H<M>) =>
	h.div.attrs(sx(styles.wrap)).children(TraceView(h, { key: "run", props: { label: "Run for ticket 4821", spans } }))
```

### A slow request, opened one level

`depth` sets how many levels start open. Open load board to see twelve queries, one after another.

```ts title="src/request.ts" source="www/src/examples/trace-view/request.ts"
import type { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { sx } from "./ui/uicontext/components/Sx.ts"
import { type Span, TraceView } from "./ui/uicontext/components/TraceView.ts"

const styles = stylex.create({ wrap: { inlineSize: "100%" } })

// one slow page load on the server, opened only to the first level: the N+1 queries hide inside
const spans: ReadonlyArray<Span> = [
	{ id: "req", parent: null, name: "GET /projects/ENG/board", kind: "http", start: 0, duration: 1840, attributes: { status: "200", user: "u_291" } },
	{ id: "auth", parent: "req", name: "check session", kind: "db", start: 4, duration: 12 },
	{ id: "board", parent: "req", name: "load board", kind: "tool", start: 20, duration: 1610 },
	...Array.from({ length: 12 }, (_, i): Span => ({ id: `q${i}`, parent: "board", name: `SELECT issues WHERE column = ${i + 1}`, kind: "db", start: 30 + i * 132, duration: 118 + (i % 3) * 9, attributes: { rows: String(8 + i) } })),
	{ id: "render", parent: "req", name: "render", kind: "tool", start: 1640, duration: 190 },
]

export const SlowRequest = <M>(h: Html.H<M>) =>
	h.div.attrs(sx(styles.wrap)).children(TraceView(h, { key: "req", props: { label: "Board request", spans, depth: 1 } }))
```

## API reference

**Props**

| Name | Type | Description |
| --- | --- | --- |
| `label` (required) | `string` | What the trace is, for screen readers: "Run 4821". |
| `spans` (required) | `Span[]` | — |
| `depth` | `number` | How many levels start open; all by default. |
| `locale` | `string` | For the numbers; the browser's by default. |

## Installation

```sh
pnpm dlx shadcn@latest add checkfu/uicontext/trace-view
```

With npm: `npx shadcn@latest add checkfu/uicontext/trace-view`. With yarn: `yarn dlx shadcn@latest add checkfu/uicontext/trace-view`. With bun: `bunx --bun shadcn@latest add checkfu/uicontext/trace-view`.

It writes these files:

- `src/ui/uicontext/components/Direction.ts`
- `src/ui/uicontext/components/Glyph.ts`
- `src/ui/uicontext/components/IconStroke.ts`
- `src/ui/uicontext/components/Sx.ts`
- `src/ui/uicontext/components/TraceView.ts`
- `src/ui/uicontext/components/TreeView.ts`
- `src/ui/uicontext/components/domain/Format.ts`
- `src/ui/uicontext/components/domain/Glyph.ts`
- `src/ui/uicontext/components/tokens.stylex.ts`

And installs the npm packages `@stylexjs/stylex`, `effect`.

## Source

- [src/TraceView.ts](https://github.com/checkfu/uicontext/blob/main/src/TraceView.ts)

## Related

- [Agent Tools](https://uicontext.com/docs/components/agent-tools.md) (`agent-tools`): Lets agents in the browser read the page and act through the same controls people use.
- [Tool Confirmation](https://uicontext.com/docs/components/tool-confirmation.md) (`tool-confirmation`): Asks a person to confirm before an agent's action changes anything.
- [Prompt Template](https://uicontext.com/docs/components/prompt-template.md) (`prompt-template`): Write a prompt with {{variables}} the app fills in: they stand out, unknown ones are flagged, and a preview shows it with samples.
- [Agent Settings](https://uicontext.com/docs/components/agent-settings.md) (`agent-settings`): Edit an agent's name, instructions, tools and model.
- [Computer](https://uicontext.com/docs/components/computer.md) (`computer`): The machine an agent works on: a live view of its screen, full size or as a thumbnail with Take over, and its browser with tabs and an address bar.
- [Persona Avatar](https://uicontext.com/docs/components/persona-avatar.md) (`persona-avatar`): An agent's face: a shape and a color with eyes that move with what it's doing.
