# Cost waterfall

A run's measured spend, step by step: where the money went, a credit that steps it back, and the step that took the most.

Live variants: https://design.arcadia.is/components/cost-waterfall

```tsx
import { CostWaterfall } from "@arcadia/ui/charts";
```

A cached read credits the run. Scroll the steps to see its details.

```tsx
import { CostWaterfall } from "@arcadia/ui/charts";

<CostWaterfall steps={MEASURED} task="Report on model API prices" maxHeight={336} columns={["in", "out", "time"]} defaultOpenId="cache" />
```

## Accessibility

The rows are a tree: every step is a treeitem with its level, its place among its siblings, its state when it has children, and one label that names it, its kind, its model, its outcome where it states one, the figure the bar draws, its time and each optional column's reading. A sub-agent says its figure is with its steps. The bar, the rules and the tick values are drawn for the eye and are hidden from readers, who have the same numbers in the row labels and in the details.

One row is in the Tab order at a time. The arrow keys walk the visible rows, Right opens a folded sub-agent or moves to its first step, Left folds it or moves to its parent, Enter or Space opens the details, and Esc closes them and puts focus back on the row. A jump unfolds the branch in its way and puts focus on the step it found.

A changed run state is announced politely as Live, Done or Failed, and a run whose outcome was not measured announces nothing. A step's details are a named group inside its row, and their box becomes a Tab stop only where the text overflows, so a short prompt adds no stop.

Bars share one cumulative scale. A connector joins adjacent steps that meet at the same total, but does not cross an open detail or lead from an agent total into its own steps. On narrow screens the scale and bars span both columns. Switching units by pointer uses Swap. Under reduced motion it is one frame.

By default, costs are in dollars and share one decimal precision across the run. Credits have a leading minus and an outlined bar that steps back. Pass `formatCost` to use another currency or precision.

## API reference

### CostWaterfall

- `steps` (`readonly CostStep[]`, required): The run's steps in the order they ran; a sub-agent holds its own under children.
- `task` (`string`): What the run set out to do, in one quiet line beside the title.
- `status` (`CostStepStatus | null`): The run's state: running, done or failed. Absent reads the steps' own states; null says the outcome was not measured, and the head draws no state.
- `mode` (`"cost" | "tokens"`): The unit the bars measure, controlled: cost is measured spend, tokens is input and output. A controlled unit stands even where nothing measured it: every figure then says Not measured and draws no bar, and no spend is invented.
- `defaultMode` (`"cost" | "tokens"`): The unit the run opens on, where it measured both, when the caller does not own it: cost where any step measured one, else tokens.
- `onModeChange` (`(mode: CostWaterfallMode) => void`): Told the unit a person picked in the head.
- `collapsed` (`readonly string[]`): The sub-agents folded to one row, controlled: an id here hides its children.
- `defaultCollapsed` (`readonly string[]`): The sub-agents folded from the first frame, when the caller does not own that.
- `onCollapsedChange` (`(ids: string[]) => void`): Told the sub-agents folded after a person folds or unfolds one.
- `openId` (`string | null`): The step whose details are open, controlled.
- `defaultOpenId` (`string | null`): The step whose details are open from the first frame, when the caller does not own that.
- `onOpenChange` (`(id: string | null, step?: CostStep) => void`): Told the step whose details a person opened, with that step.
- `maxHeight` (`number`): The rows' bound in pixels: past it they scroll under the head and its axis.
- `formatCost` (`(amount: number) => string`): How an amount reads on a figure, in the cost column and in the details. Default: dollars with aligned decimals across rows and a leading minus for credits.
- `columns` (`boolean | readonly CostWaterfallColumn[]`): The numeric columns at each row's end, read from the step's own `tokensIn`, `tokensOut`, `cost` and `duration`, with a sub-agent's children added where they measured the same field: `in`, `out`, `cost` and `time`, in that order. `true` shows all four; absent shows none. A field nothing measured reads a quiet hyphen, never a zero.
- `title` (`string`, default `"Run cost"`): The head's title.
- `rootStyle` (`RootStyle<CostWaterfallKnobs>`)

Rest props go to `HTMLAttributes<HTMLDivElement>`.
